Skip to content

Commit

Permalink
Merge pull request #32251 from vespa-engine/toregge/rewrite-searchcor…
Browse files Browse the repository at this point in the history
…e-session-manager-unit-test-to-gtest

Rewrite searchcore session manager unit test to gtest.
  • Loading branch information
baldersheim authored Aug 25, 2024
2 parents 90bba89 + bf7ecd0 commit d02b132
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
1 change: 1 addition & 0 deletions searchcore/src/tests/proton/matching/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ vespa_add_executable(searchcore_sessionmanager_test_app TEST
DEPENDS
searchcore_matching
searchcore_grouping
GTest::gtest
)
vespa_add_test(NAME searchcore_sessionmanager_test_app COMMAND searchcore_sessionmanager_test_app)
vespa_add_executable(searchcore_matching_stats_test_app TEST
Expand Down
50 changes: 24 additions & 26 deletions searchcore/src/tests/proton/matching/sessionmanager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
#include <vespa/searchcore/proton/matching/session_manager_explorer.h>
#include <vespa/searchcore/proton/matching/search_session.h>
#include <vespa/searchcore/proton/matching/match_tools.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/testkit/test_master.hpp>
#include <vespa/vespalib/gtest/gtest.h>
#include <string>

#include <vespa/log/log.h>
Expand All @@ -21,20 +19,19 @@ using namespace proton::matching;
using vespalib::StateExplorer;
using vespalib::steady_time;

namespace {
namespace proton::matching {

void PrintTo(const SessionManager::Stats& stats, std::ostream* os) {
*os << "{" << stats.numInsert << "," << stats.numPick << "," << stats.numDropped << "," <<
stats.numCached << "," << stats.numTimedout << "}";
}

void checkStats(SessionManager::Stats stats, uint32_t numInsert,
uint32_t numPick, uint32_t numDropped, uint32_t numCached,
uint32_t numTimedout) {
EXPECT_EQUAL(numInsert, stats.numInsert);
EXPECT_EQUAL(numPick, stats.numPick);
EXPECT_EQUAL(numDropped, stats.numDropped);
EXPECT_EQUAL(numCached, stats.numCached);
EXPECT_EQUAL(numTimedout, stats.numTimedout);
}

namespace {

TEST("require that SessionManager handles SearchSessions.") {
TEST(SessionManagerTest, require_that_SessionManager_handles_SearchSessions)
{
string session_id("foo");
steady_time start(100ns);
steady_time doom(1000ns);
Expand All @@ -43,24 +40,25 @@ TEST("require that SessionManager handles SearchSessions.") {
auto session = std::make_shared<SearchSession>(session_id, start, doom, std::move(mtf), std::move(owned_objects));

SessionManager session_manager(10);
TEST_DO(checkStats(session_manager.getSearchStats(), 0, 0, 0, 0, 0));
EXPECT_EQ(SessionManager::Stats(0, 0, 0, 0, 0), session_manager.getSearchStats());
session_manager.insert(std::move(session));
TEST_DO(checkStats(session_manager.getSearchStats(), 1, 0, 0, 1, 0));
EXPECT_EQ(SessionManager::Stats(1, 0, 0, 1, 0), session_manager.getSearchStats());
session = session_manager.pickSearch(session_id);
EXPECT_TRUE(session.get());
TEST_DO(checkStats(session_manager.getSearchStats(), 0, 1, 0, 1, 0));
EXPECT_EQ(SessionManager::Stats(0, 1, 0, 1, 0), session_manager.getSearchStats());
session_manager.insert(std::move(session));
TEST_DO(checkStats(session_manager.getSearchStats(), 1, 0, 0, 1, 0));
EXPECT_EQ(SessionManager::Stats(1, 0, 0, 1, 0), session_manager.getSearchStats());
session_manager.pruneTimedOutSessions(steady_time(500ns));
TEST_DO(checkStats(session_manager.getSearchStats(), 0, 0, 0, 1, 0));
EXPECT_EQ(SessionManager::Stats(0, 0, 0, 1, 0), session_manager.getSearchStats());
session_manager.pruneTimedOutSessions(steady_time(2000ns));
TEST_DO(checkStats(session_manager.getSearchStats(), 0, 0, 0, 0, 1));
EXPECT_EQ(SessionManager::Stats(0, 0, 0, 0, 1), session_manager.getSearchStats());

session = session_manager.pickSearch(session_id);
EXPECT_FALSE(session.get());
}

TEST("require that SessionManager can be explored") {
TEST(SessionManagerTest, require_that_SessionManager_can_be_explored)
{
steady_time start(100ns);
steady_time doom(1000ns);
SessionManager session_manager(10);
Expand All @@ -71,20 +69,20 @@ TEST("require that SessionManager can be explored") {
session_manager.insert(std::make_shared<SearchSession>("baz", start, doom,
MatchToolsFactory::UP(), SearchSession::OwnershipBundle()));
SessionManagerExplorer explorer(session_manager);
EXPECT_EQUAL(std::vector<std::string>({"search"}),
EXPECT_EQ(std::vector<std::string>({"search"}),
explorer.get_children_names());
std::unique_ptr<StateExplorer> search = explorer.get_child("search");
ASSERT_TRUE(search.get() != nullptr);
vespalib::Slime state;
vespalib::Slime full_state;
search->get_state(vespalib::slime::SlimeInserter(state), false);
search->get_state(vespalib::slime::SlimeInserter(full_state), true);
EXPECT_EQUAL(3, state.get()["numSessions"].asLong());
EXPECT_EQUAL(3, full_state.get()["numSessions"].asLong());
EXPECT_EQUAL(0u, state.get()["sessions"].entries());
EXPECT_EQUAL(3u, full_state.get()["sessions"].entries());
EXPECT_EQ(3, state.get()["numSessions"].asLong());
EXPECT_EQ(3, full_state.get()["numSessions"].asLong());
EXPECT_EQ(0u, state.get()["sessions"].entries());
EXPECT_EQ(3u, full_state.get()["sessions"].entries());
}

} // namespace

TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()
29 changes: 23 additions & 6 deletions searchcore/src/vespa/searchcore/proton/matching/sessionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,35 @@ struct SearchSessionCache;
class SessionManager {
public:
struct Stats {
Stats()
uint32_t numInsert;
uint32_t numPick;
uint32_t numDropped;
uint32_t numCached;
uint32_t numTimedout;

Stats() noexcept
: numInsert(0),
numPick(0),
numDropped(0),
numCached(0),
numTimedout(0)
{}
uint32_t numInsert;
uint32_t numPick;
uint32_t numDropped;
uint32_t numCached;
uint32_t numTimedout;
Stats(uint32_t numInsert_in, uint32_t numPick_in, uint32_t numDropped_in, uint32_t numCached_in,
uint32_t numTimedout_in) noexcept
: numInsert(numInsert_in),
numPick(numPick_in),
numDropped(numDropped_in),
numCached(numCached_in),
numTimedout(numTimedout_in)
{
}
bool operator==(const Stats& rhs) const noexcept {
return numInsert == rhs.numInsert &&
numPick == rhs.numPick &&
numDropped == rhs.numDropped &&
numCached == rhs.numCached &&
numTimedout == rhs.numTimedout;
}
};

struct SearchSessionInfo {
Expand Down

0 comments on commit d02b132

Please sign in to comment.