TEST_F(FailureFixture, ExistingDiskMessageOnDestructTest) {
    std::random_device generator;
    std::uniform_int_distribution<unsigned long long> distribution(1, DEFAULT_MAX_MEMORY_SIZE);
    auto number_to_create = distribution(generator);

    {
        PriorityBuffer<PriorityMessage> buffer{get_priority};
        
        // Push DEFAULT_MAX_MEMORY_SIZE messages into the buffer with 0 priority
        for (int i = 0; i < DEFAULT_MAX_MEMORY_SIZE; ++i) {
            auto message = std::unique_ptr<PriorityMessage>{ new PriorityMessage{} };
            message->set_priority(0);
            ASSERT_TRUE(message->IsInitialized());
            buffer.Push(std::move(message));
        }

        std::stringstream stream;
        stream << "SELECT hash FROM "
               << table_name_
               << " ORDER BY priority LIMIT "
               << number_to_create
               << ";";
        auto response = execute_(stream.str());
        ASSERT_EQ(number_to_create, response.size());

        for (auto& record : response) {
            auto file_path = buffer_path_ / fs::path{record["hash"]};
            std::ofstream file_out{file_path.native()};
            file_out << "hello world";
        }
    }

    // Let the buffer drop and try to push memory messages to disk
    EXPECT_EQ(DEFAULT_MAX_MEMORY_SIZE - number_to_create, number_of_files_());
}
示例#2
0
 virtual void execute()
 {
     if (now() > nextExecutionTime_)
     {
         if (execute_())
         {
             nextExecutionTime_ = now() + period_;
         }
         else
         {
             finished_ = true;
         }
     }
 }
TEST_F(FailureFixture, ExistingDiskMessageTest) {
    PriorityBuffer<PriorityMessage> buffer{get_priority};
    
    // Push DEFAULT_MAX_MEMORY_SIZE messages into he buffer with 0 priority
    for (int i = 0; i < DEFAULT_MAX_MEMORY_SIZE; ++i) {
        auto message = std::unique_ptr<PriorityMessage>{ new PriorityMessage{} };
        message->set_priority(0);
        ASSERT_TRUE(message->IsInitialized());
        buffer.Push(std::move(message));
    }

    std::random_device generator;
    std::uniform_int_distribution<unsigned long long> distribution(1, DEFAULT_MAX_MEMORY_SIZE);
    auto number_to_create = distribution(generator);

    std::stringstream stream;
    stream << "SELECT hash FROM "
           << table_name_
           << " ORDER BY priority LIMIT "
           << number_to_create
           << ";";
    auto response = execute_(stream.str());
    ASSERT_EQ(number_to_create, response.size());

    for (auto& record : response) {
        auto file_path = buffer_path_ / fs::path{record["hash"]};
        std::ofstream file_out{file_path.native()};
        file_out << "hello world";
    }

    // Push DEFAULT_MAX_MEMORY_SIZE messages into he buffer with 1 priority, pushing all the
    // previous messages out
    for (int i = 0; i < DEFAULT_MAX_MEMORY_SIZE; ++i) {
        auto message = std::unique_ptr<PriorityMessage>{ new PriorityMessage{} };
        message->set_priority(1);
        ASSERT_TRUE(message->IsInitialized());
        buffer.Push(std::move(message));
    }

    for (int i = 0; i < 100 - number_to_create; ++i) {
        auto message = buffer.Pop();
        ASSERT_TRUE(message->IsInitialized());
        ASSERT_GE(1, message->priority());
    }

    EXPECT_EQ(nullptr, buffer.Pop());
}
 void executeUntil(const boost::posix_time::ptime& time)
 {
    while (!finished_ && (now() < time))
       finished_ = !execute_();
 }