Repository: vespa-engine/vespa
File: searchcore/src/tests/index/disk_indexes/disk_indexes_test.cpp (C++)

Code before:
    std::filesystem::create_directories(std::filesystem::path(dir));
    /*
     * Fusion started, but no files written yet.
     */
    EXPECT_EQ(0u, transient_size());
    constexpr uint32_t seek_pos = 999999;
    {
        std::string name = dir + "/foo";
        std::ofstream ostr(name, std::ios::binary);
        ostr.seekp(seek_pos);
        ostr.write(" ", 1);
        ostr.flush();
        ostr.close();
    }
    /*
     * Fusion started, one file written.
     */
    EXPECT_EQ((seek_pos + block_size) / block_size * block_size, transient_size());
    EXPECT_TRUE(remove(fusion2)); // stop tracking disk space for fusion output
    /*
     * Fusion aborted.
     */
    EXPECT_EQ(0, transient_size());
}

TEST_F(DiskIndexesTest, get_size_on_disk)
{
    EXPECT_EQ(0, get_size_on_disk(true));
    setActive("index.fusion.1", 1000000);
    EXPECT_EQ(1000000, get_size_on_disk(false));
    setActive("index.flush.2", 500000);
    EXPECT_EQ(1500000, get_size_on_disk(false));
    setActive("index.flush.3", 200000);
    EXPECT_EQ(1700000, get_size_on_disk(false));
    setActive("index.fusion.3", 1600000);
    EXPECT_EQ(1600000, get_size_on_disk(false));
    EXPECT_EQ(3300000, get_size_on_disk(true));
    notActive("index.fusion.1");
    notActive("index.flush.2");
    notActive("index.flush.3");
    EXPECT_TRUE(remove(get_index_disk_dir("index.fusion.1")));
    EXPECT_TRUE(remove(get_index_disk_dir("index.flush.2")));
    EXPECT_TRUE(remove(get_index_disk_dir("index.flush.3")));
    EXPECT_EQ(1600000, get_size_on_disk(true));
}

}

GTEST_MAIN_RUN_ALL_TESTS()


Review comment (suggestion):
Consider making the test name slightly more explicit in its intentions, e.g. `get_size_on_disk_considers_index_staleness` (or something more apt).

Code after:
    std::filesystem::create_directories(std::filesystem::path(dir));
    /*
     * Fusion started, but no files written yet.
     */
    EXPECT_EQ(0u, transient_size());
    constexpr uint32_t seek_pos = 999999;
    {
        std::string name = dir + "/foo";
        std::ofstream ostr(name, std::ios::binary);
        ostr.seekp(seek_pos);
        ostr.write(" ", 1);
        ostr.flush();
        ostr.close();
    }
    /*
     * Fusion started, one file written.
     */
    EXPECT_EQ((seek_pos + block_size) / block_size * block_size, transient_size());
    EXPECT_TRUE(remove(fusion2)); // stop tracking disk space for fusion output
    /*
     * Fusion aborted.
     */
    EXPECT_EQ(0, transient_size());
}

TEST_F(DiskIndexesTest, get_size_on_disk_considers_index_staleness)
{
    EXPECT_EQ(0, get_size_on_disk(true));
    setActive("index.fusion.1", 1000000);
    EXPECT_EQ(1000000, get_size_on_disk(false));
    setActive("index.flush.2", 500000);
    EXPECT_EQ(1500000, get_size_on_disk(false));
    setActive("index.flush.3", 200000);
    EXPECT_EQ(1700000, get_size_on_disk(false));
    setActive("index.fusion.3", 1600000);
    // index.fusion.1, index.flush.2 and index.flush.3 are marked stale due to index.fusion.3 being marked active
    EXPECT_EQ(1600000, get_size_on_disk(false));
    EXPECT_EQ(3300000, get_size_on_disk(true));
    notActive("index.fusion.1");
    notActive("index.flush.2");
    notActive("index.flush.3");
    EXPECT_TRUE(remove(get_index_disk_dir("index.fusion.1")));
    EXPECT_TRUE(remove(get_index_disk_dir("index.flush.2")));
    EXPECT_TRUE(remove(get_index_disk_dir("index.flush.3")));
    EXPECT_EQ(1600000, get_size_on_disk(true));
}

}

GTEST_MAIN_RUN_ALL_TESTS()