// File Sanity check TEST(FTDCFileTest, TestFileBasicMetadata) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path p(tempdir.path()); p /= kTestFile; deleteFileIfNeeded(p); BSONObj doc1 = BSON("name" << "joe" << "key1" << 34 << "key2" << 45); BSONObj doc2 = BSON("name" << "joe" << "key3" << 34 << "key5" << 45); FTDCConfig config; FTDCFileWriter writer(&config); ASSERT_OK(writer.open(p)); ASSERT_OK(writer.writeMetadata(doc1, Date_t())); ASSERT_OK(writer.writeMetadata(doc2, Date_t())); writer.close(); FTDCFileReader reader; ASSERT_OK(reader.open(p)); ASSERT_OK(reader.hasNext()); BSONObj doc1a = std::get<1>(reader.next()); ASSERT_BSONOBJ_EQ(doc1, doc1a); ASSERT_OK(reader.hasNext()); BSONObj doc2a = std::get<1>(reader.next()); ASSERT_BSONOBJ_EQ(doc2, doc2a); auto sw = reader.hasNext(); ASSERT_OK(sw); ASSERT_EQUALS(sw.getValue(), false); }
std::vector<std::tuple<FTDCBSONUtil::FTDCType, BSONObj, Date_t>> FTDCFileManager::recoverInterimFile() { decltype(recoverInterimFile()) docs; auto interimFile = FTDCUtil::getInterimFile(_path); // Nothing to do if it does not exist if (!boost::filesystem::exists(interimFile)) { return docs; } size_t size = boost::filesystem::file_size(interimFile); if (size == 0) { return docs; } FTDCFileReader read; auto s = read.open(interimFile); if (!s.isOK()) { log() << "Unclean full-time diagnostic data capture shutdown detected, found interim file, " "but failed " "to open it, some " "metrics may have been lost. " << s; // Note: We ignore any actual errors as reading from the interim files is a best-effort return docs; } StatusWith<bool> m = read.hasNext(); for (; m.isOK() && m.getValue(); m = read.hasNext()) { auto triplet = read.next(); docs.emplace_back(std::tuple<FTDCBSONUtil::FTDCType, BSONObj, Date_t>( std::get<0>(triplet), std::get<1>(triplet).getOwned(), std::get<2>(triplet))); } // Warn if the interim file was corrupt or we had an unclean shutdown if (!m.isOK() || !docs.empty()) { log() << "Unclean full-time diagnostic data capture shutdown detected, found interim file, " "some " "metrics may have been lost. " << m.getStatus(); } // Note: We ignore any actual errors as reading from the interim files is a best-effort return docs; }
// Test a bad file TEST(FTDCFileTest, TestBadFile) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path p(tempdir.path()); p /= kTestFile; std::ofstream stream(p.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(); FTDCFileReader reader; ASSERT_OK(reader.open(p)); auto sw = reader.hasNext(); ASSERT_NOT_OK(sw); }