diff options
Diffstat (limited to 'slobrok/src/tests')
-rw-r--r-- | slobrok/src/tests/union_service_map/CMakeLists.txt | 9 | ||||
-rw-r--r-- | slobrok/src/tests/union_service_map/union_service_map_test.cpp | 180 |
2 files changed, 189 insertions, 0 deletions
diff --git a/slobrok/src/tests/union_service_map/CMakeLists.txt b/slobrok/src/tests/union_service_map/CMakeLists.txt new file mode 100644 index 00000000000..523294742f2 --- /dev/null +++ b/slobrok/src/tests/union_service_map/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(slobrok_union_service_map_test_app TEST + SOURCES + union_service_map_test.cpp + DEPENDS + slobrok_slobrokserver + GTest::GTest +) +vespa_add_test(NAME slobrok_union_service_map_test_app COMMAND slobrok_union_service_map_test_app) diff --git a/slobrok/src/tests/union_service_map/union_service_map_test.cpp b/slobrok/src/tests/union_service_map/union_service_map_test.cpp new file mode 100644 index 00000000000..5f1f70fb9fb --- /dev/null +++ b/slobrok/src/tests/union_service_map/union_service_map_test.cpp @@ -0,0 +1,180 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/slobrok/server/union_service_map.h> +#include <vespa/vespalib/gtest/gtest.h> +#include <vespa/vespalib/util/stringfmt.h> + +using namespace vespalib; +using namespace slobrok; +using vespalib::make_string_short::fmt; + +enum class Event { NONE, ADD, REMOVE, UPDATE }; + +struct MapObserver : public MapListener { + MapObserver(); + virtual ~MapObserver(); + void add(const ServiceMapping &mapping) override; + void remove(const ServiceMapping &mapping) override; + void update(const ServiceMapping &old_mapping, + const ServiceMapping &new_mapping) override; + + Event last_event = Event::NONE; + ServiceMapping last_add = {{}, {}}; + ServiceMapping last_remove = {{}, {}}; + + void clear() { last_event = Event::NONE; } +}; + +MapObserver::MapObserver() = default; +MapObserver::~MapObserver() = default; + +void MapObserver::add(const ServiceMapping &mapping) { + last_event = Event::ADD; + last_add = mapping; +} + +void MapObserver::remove(const ServiceMapping &mapping) { + last_event = Event::REMOVE; + last_remove = mapping; +} + +void MapObserver::update(const ServiceMapping &old_mapping, + const ServiceMapping &new_mapping) +{ + last_event = Event::UPDATE; + last_remove = old_mapping; + last_add = new_mapping; +} + +TEST(UnionServiceMapTest, forwards_simple_requests) { + ProxyMapSource source; + UnionServiceMap unionizer; + MapObserver observer; + unionizer.registerListener(observer); + source.registerListener(unionizer); + + EXPECT_EQ(observer.last_event, Event::NONE); + + ServiceMapping one{"foo/1", "bar/1"}; + source.add(one); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, one); + ServiceMapping two{"foo/2", "bar/2"}; + source.add(two); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, two); + + source.remove(one); + EXPECT_EQ(observer.last_event, Event::REMOVE); + EXPECT_EQ(observer.last_remove, one); + + ServiceMapping two_q{"foo/2", "qux/2"}; + source.update(two, two_q); + // update implemented ass remove+add: + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_remove, two); + EXPECT_EQ(observer.last_add, two_q); +} + +TEST(UnionServiceMapTest, handles_refcount) { + ProxyMapSource source1; + ProxyMapSource source2; + ProxyMapSource source3; + UnionServiceMap unionizer; + MapObserver observer; + unionizer.registerListener(observer); + source1.registerListener(unionizer); + source2.registerListener(unionizer); + source3.registerListener(unionizer); + + EXPECT_EQ(observer.last_event, Event::NONE); + ServiceMapping one{"foo/1", "bar/1"}; + source1.add(one); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, one); + observer.clear(); + EXPECT_EQ(observer.last_event, Event::NONE); + source2.add(one); + EXPECT_EQ(observer.last_event, Event::NONE); + source3.add(one); + EXPECT_EQ(observer.last_event, Event::NONE); + ServiceMapping two{"foo/2", "bar/2"}; + source1.add(two); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, two); + observer.clear(); + EXPECT_EQ(observer.last_event, Event::NONE); + source2.add(two); + EXPECT_EQ(observer.last_event, Event::NONE); + + source1.remove(one); + EXPECT_EQ(observer.last_event, Event::NONE); + source2.remove(one); + EXPECT_EQ(observer.last_event, Event::NONE); + + source1.remove(two); + EXPECT_EQ(observer.last_event, Event::NONE); + source2.remove(two); + EXPECT_EQ(observer.last_event, Event::REMOVE); + EXPECT_EQ(observer.last_remove, two); + + observer.clear(); + EXPECT_EQ(observer.last_event, Event::NONE); + source3.remove(one); + EXPECT_EQ(observer.last_event, Event::REMOVE); + EXPECT_EQ(observer.last_remove, one); +} + +TEST(UnionServiceMapTest, handles_conflicts) { + ProxyMapSource source1; + ProxyMapSource source2; + ProxyMapSource source3; + UnionServiceMap unionizer; + MapObserver observer; + unionizer.registerListener(observer); + source1.registerListener(unionizer); + source2.registerListener(unionizer); + source3.registerListener(unionizer); + + EXPECT_EQ(observer.last_event, Event::NONE); + ServiceMapping one{"foo/1", "bar/1"}; + source1.add(one); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, one); + observer.clear(); + source2.add(one); + EXPECT_EQ(observer.last_event, Event::NONE); + + ServiceMapping two{"foo/2", "bar/2"}; + source1.add(two); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, two); + observer.clear(); + source2.add(two); + EXPECT_EQ(observer.last_event, Event::NONE); + + ServiceMapping one_q{"foo/1", "qux/1"}; + source3.add(one_q); + EXPECT_EQ(observer.last_event, Event::REMOVE); + EXPECT_EQ(observer.last_remove, one); + + ServiceMapping two_q{"foo/2", "qux/2"}; + source3.add(two_q); + EXPECT_EQ(observer.last_event, Event::REMOVE); + EXPECT_EQ(observer.last_remove, two); + + source3.remove(one_q); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, one); + + observer.clear(); + source1.remove(two); + EXPECT_EQ(observer.last_event, Event::NONE); + source2.remove(two); + EXPECT_EQ(observer.last_event, Event::ADD); + EXPECT_EQ(observer.last_add, two_q); +} + + +GTEST_MAIN_RUN_ALL_TESTS() + |