示例#1
0
// Test a full buffer
TEST(FTDCFileManagerTest, TestFull) {
    Client* client = &cc();
    FTDCConfig c;
    c.maxFileSizeBytes = 300;
    c.maxDirectorySizeBytes = 1000;
    c.maxSamplesPerInterimMetricChunk = 1;

    unittest::TempDir tempdir("metrics_testpath");
    boost::filesystem::path dir(tempdir.path());
    createDirectoryClean(dir);

    FTDCCollectorCollection rotate;
    auto swMgr = FTDCFileManager::create(&c, dir, &rotate, client);
    ASSERT_OK(swMgr.getStatus());
    auto mgr = std::move(swMgr.getValue());

    // Test a large numbers of zeros, and incremental numbers in a full buffer
    for (int j = 0; j < 10; j++) {
        ASSERT_OK(
            mgr->writeSampleAndRotateIfNeeded(client,
                                              BSON("name"
                                                   << "joe"
                                                   << "key1" << 3230792343LL << "key2" << 235135)));

        for (size_t i = 0; i <= FTDCConfig::kMaxSamplesPerArchiveMetricChunkDefault - 2; i++) {
            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(
                client,
                BSON("name"
                     << "joe"
                     << "key1" << static_cast<long long int>(i * j * 37) << "key2"
                     << static_cast<long long int>(i * (645 << j)))));
        }

        ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                    BSON("name"
                                                         << "joe"
                                                         << "key1" << 34 << "key2" << 45)));

        // Add Value
        ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                    BSON("name"
                                                         << "joe"
                                                         << "key1" << 34 << "key2" << 45)));
    }

    mgr->close();

    auto files = scanDirectory(dir);

    int sum = 0;
    for (auto& file : files) {
        int fs = boost::filesystem::file_size(file);
        ASSERT_TRUE(fs < c.maxFileSizeBytes * 1.10);
        if (file.generic_string().find("interim") == std::string::npos) {
            sum += fs;
        }
    }

    ASSERT_TRUE(sum < c.maxDirectorySizeBytes * 1.10 && sum > c.maxDirectorySizeBytes * 0.90);
}
// Test a normal restart
TEST(FTDCFileManagerTest, TestNormalRestart) {
    Client* client = &cc();
    FTDCConfig c;
    c.maxFileSizeBytes = 1000;
    c.maxDirectorySizeBytes = 3000;

    unittest::TempDir tempdir("metrics_testpath");
    boost::filesystem::path dir(tempdir.path());

    createDirectoryClean(dir);

    for (int i = 0; i < 3; i++) {
        // Do a few cases of stop and start to ensure it works as expected
        FTDCCollectorCollection rotate;
        auto swMgr = FTDCFileManager::create(&c, dir, &rotate, client);
        ASSERT_OK(swMgr.getStatus());
        auto mgr = std::move(swMgr.getValue());

        // Test a large numbers of zeros, and incremental numbers in a full buffer
        for (int j = 0; j < 4; j++) {
            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1" << 3230792343LL << "key2"
                                                             << 235135),
                                                        Date_t()));

            for (size_t i = 0; i <= FTDCConfig::kMaxSamplesPerArchiveMetricChunkDefault - 2; i++) {
                ASSERT_OK(
                    mgr->writeSampleAndRotateIfNeeded(
                        client,
                        BSON("name"
                             << "joe"
                             << "key1" << static_cast<long long int>(i * j * 37) << "key2"
                             << static_cast<long long int>(i * (645 << j))),
                        Date_t()));
            }

            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1" << 34 << "key2" << 45),
                                                        Date_t()));

            // Add Value
            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1" << 34 << "key2" << 45),
                                                        Date_t()));
        }

        mgr->close();

        // Validate the interim file does not have data
        ValidateInterimFileHasData(dir, false);
    }
}
示例#3
0
// Test a restart after a crash with a corrupt archive file
TEST(FTDCFileManagerTest, TestCorruptCrashRestart) {
    Client* client = &cc();
    FTDCConfig c;
    c.maxFileSizeBytes = 1000;
    c.maxDirectorySizeBytes = 3000;

    unittest::TempDir tempdir("metrics_testpath");
    boost::filesystem::path dir(tempdir.path());

    createDirectoryClean(dir);

    for (int i = 0; i < 2; i++) {
        // Do a few cases of stop and start to ensure it works as expected
        FTDCCollectorCollection rotate;
        auto swMgr = FTDCFileManager::create(&c, dir, &rotate, client);
        ASSERT_OK(swMgr.getStatus());
        auto mgr = std::move(swMgr.getValue());

        // Test a large numbers of zeros, and incremental numbers in a full buffer
        for (int j = 0; j < 4; j++) {
            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1"
                                                             << 3230792343LL
                                                             << "key2"
                                                             << 235135),
                                                        Date_t()));

            for (size_t i = 0; i <= FTDCConfig::kMaxSamplesPerArchiveMetricChunkDefault - 2; i++) {
                ASSERT_OK(
                    mgr->writeSampleAndRotateIfNeeded(
                        client,
                        BSON("name"
                             << "joe"
                             << "key1"
                             << static_cast<long long int>(i * j * 37)
                             << "key2"
                             << static_cast<long long int>(i * (645 << j))),
                        Date_t()));
            }

            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1"
                                                             << 34
                                                             << "key2"
                                                             << 45),
                                                        Date_t()));

            // Add Value
            ASSERT_OK(mgr->writeSampleAndRotateIfNeeded(client,
                                                        BSON("name"
                                                             << "joe"
                                                             << "key1"
                                                             << 34
                                                             << "key2"
                                                             << 45),
                                                        Date_t()));
        }

        mgr->close();

        auto swFile = mgr->generateArchiveFileName(dir, "0test-crash");
        ASSERT_OK(swFile);

        std::ofstream stream(swFile.getValue().c_str());
        // This test case caused us to allocate more memory then the size of the file the first time
        // I tried it
        stream << "Hello World";

        stream.close();
    }
}