summaryrefslogtreecommitdiffstats
path: root/config/src/tests/subscription
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /config/src/tests/subscription
Publish
Diffstat (limited to 'config/src/tests/subscription')
-rw-r--r--config/src/tests/subscription/.gitignore3
-rw-r--r--config/src/tests/subscription/CMakeLists.txt9
-rw-r--r--config/src/tests/subscription/subscription.cpp119
3 files changed, 131 insertions, 0 deletions
diff --git a/config/src/tests/subscription/.gitignore b/config/src/tests/subscription/.gitignore
new file mode 100644
index 00000000000..65556950180
--- /dev/null
+++ b/config/src/tests/subscription/.gitignore
@@ -0,0 +1,3 @@
+/config-my.cpp
+/config-my.h
+config_subscription_test_app
diff --git a/config/src/tests/subscription/CMakeLists.txt b/config/src/tests/subscription/CMakeLists.txt
new file mode 100644
index 00000000000..bbc3546c1da
--- /dev/null
+++ b/config/src/tests/subscription/CMakeLists.txt
@@ -0,0 +1,9 @@
+# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(config_subscription_test_app
+ SOURCES
+ subscription.cpp
+ DEPENDS
+ config_cloudconfig
+)
+vespa_add_test(NAME config_subscription_test_app COMMAND config_subscription_test_app)
+vespa_generate_config(config_subscription_test_app ../../test/resources/configdefinitions/my.def)
diff --git a/config/src/tests/subscription/subscription.cpp b/config/src/tests/subscription/subscription.cpp
new file mode 100644
index 00000000000..69cba9707fe
--- /dev/null
+++ b/config/src/tests/subscription/subscription.cpp
@@ -0,0 +1,119 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#include <vespa/fastos/fastos.h>
+#include <vespa/vespalib/testkit/test_kit.h>
+#include <vespa/config/common/misc.h>
+#include <vespa/config/common/configholder.h>
+#include <vespa/config/subscription/configsubscription.h>
+#include <config-my.h>
+
+using namespace config;
+
+namespace {
+
+ struct SourceFixture
+ {
+ int numClose;
+ int numGetConfig;
+ int numReload;
+ SourceFixture()
+ : numClose(0),
+ numGetConfig(0),
+ numReload(0)
+ { }
+ };
+
+ struct MySource : public Source
+ {
+ MySource(SourceFixture * src)
+ : source(src)
+ {}
+
+ void getConfig() { source->numGetConfig++; }
+ void close() { source->numClose++; }
+ void reload(int64_t gen) { (void) gen; source->numReload++; }
+
+ SourceFixture * source;
+ };
+
+ struct SubscriptionFixture
+ {
+ IConfigHolder::SP holder;
+ ConfigSubscription sub;
+ SourceFixture src;
+ SubscriptionFixture(const ConfigKey & key)
+ : holder(new ConfigHolder()),
+ sub(0, key, holder, Source::UP(new MySource(&src)))
+ {
+ }
+ };
+}
+
+TEST_FF("requireThatKeyIsReturned", ConfigKey("foo", "bar", "bim", "boo"), SubscriptionFixture(f1))
+{
+ ASSERT_TRUE(f1 == f2.sub.getKey());
+}
+
+TEST_F("requireThatUpdateReturns", SubscriptionFixture(ConfigKey::create<MyConfig>("myid")))
+{
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), 1, 1)));
+ ASSERT_TRUE(f1.sub.nextUpdate(0, 0));
+ ASSERT_TRUE(f1.sub.hasChanged());
+ ASSERT_EQUAL(1, f1.sub.getGeneration());
+}
+
+TEST_F("requireThatNextUpdateBlocks", SubscriptionFixture(ConfigKey::create<MyConfig>("myid")))
+{
+ ASSERT_FALSE(f1.sub.nextUpdate(0, 0));
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), 1, 1)));
+ FastOS_Time timer;
+ timer.SetNow();
+ ASSERT_FALSE(f1.sub.nextUpdate(1, 500));
+ ASSERT_TRUE(timer.MilliSecsToNow() > 400.0);
+}
+
+TEST_MT_F("requireThatNextUpdateReturnsWhenNotified", 2, SubscriptionFixture(ConfigKey::create<MyConfig>("myid")))
+{
+ if (thread_id == 0) {
+ FastOS_Time timer;
+ timer.SetNow();
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), 1, 1)));
+ ASSERT_TRUE(f1.sub.nextUpdate(2, 5000));
+ ASSERT_TRUE(timer.MilliSecsToNow() > 200.0);
+ } else {
+ FastOS_Thread::Sleep(500);
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), 1, 1)));
+ }
+}
+
+
+TEST_MT_F("requireThatNextUpdateReturnsInterrupted", 2, SubscriptionFixture(ConfigKey::create<MyConfig>("myid")))
+{
+ if (thread_id == 0) {
+ FastOS_Time timer;
+ timer.SetNow();
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), 1, 1)));
+ ASSERT_TRUE(f1.sub.nextUpdate(1, 5000));
+ ASSERT_TRUE(timer.MilliSecsToNow() > 300.0);
+ } else {
+ FastOS_Thread::Sleep(500);
+ f1.sub.close();
+ }
+}
+
+TEST_F("Require that isChanged takes generation into account", SubscriptionFixture(ConfigKey::create<MyConfig>("myid")))
+{
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), true, 1)));
+ ASSERT_TRUE(f1.sub.nextUpdate(0, 0));
+ f1.sub.flip();
+ ASSERT_EQUAL(1, f1.sub.getLastGenerationChanged());
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), true, 2)));
+ ASSERT_TRUE(f1.sub.nextUpdate(1, 0));
+ f1.sub.flip();
+ ASSERT_EQUAL(2, f1.sub.getLastGenerationChanged());
+ f1.holder->handle(ConfigUpdate::UP(new ConfigUpdate(ConfigValue(), false, 3)));
+ ASSERT_TRUE(f1.sub.nextUpdate(2, 0));
+ f1.sub.flip();
+ ASSERT_EQUAL(2, f1.sub.getLastGenerationChanged());
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }