class Subscription(unittest.TestCase): def tearDown(self): os.remove('time.context') def testChangingPlugin(self): os.environ["CONTEXT_PROVIDERS"] = "." if os.path.isdir("../testplugins"): # for your local machine os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] = "../testplugins" else: # if libcontextsubscriber-tests is installed os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] = "/usr/lib/contextkit/subscriber-test-plugins" if not os.path.exists(os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] + "/" + "contextsubscribertime1.so") \ or not os.path.exists(os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] + "/" + "contextsubscribertime2.so"): self.assert_(False, "Couldn't find the test time plugins") self.context_client = CLTool("context-listen", "Test.Time") self.context_client.expect("Available commands") # wait for it # Copy the declaration file, declaring libcontextsubscribertime1 plugin. os.system('cp time1.context.temp time.context.temp') os.system('mv time.context.temp time.context') #print "now reading" # Expect value coming from plugin libcontextsubscribertime1 self.assert_(self.context_client.expect("Test.Time = QString:\"Time1:")) # Modify the registry so that the key is now provided by libcontextsubscribertime2 os.system('cp time2.context.temp time.context.temp') os.system('mv time.context.temp time.context') # Expect value coming from plugin libcontextsubscribertime2 self.assert_(self.context_client.expect("Test.Time = QString:\"Time2:")) self.context_client.wait()
def testTypes(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth", "test.fake") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it # test querying the type of all properties listen.send("type test.int") self.assert_( listen.expect("^type: INT$")) listen.send("type test.truth") self.assert_( listen.expect("^type: TRUTH$")) listen.send("type test.string") self.assert_( listen.expect("^type: STRING$")) listen.send("type test.fake") self.assert_( listen.expect("^type:$")) listen.wait() provider.wait()
def testProviders(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth", "test.fake") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it # test querying the provider(s) listen.send("providers test.int") self.assert_( listen.expect("^providers: session:com.nokia.test@contextkit-dbus$")) listen.send("providers test.fake") self.assert_( listen.expect("^providers:$")) listen.wait() provider.wait()
def testReturnValue(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "-5", "string", "test.string", "something", "double", "test.double", "4.231", "truth", "test.truth", "False") provider.send("dump") self.assert_(provider.expect("Wrote ./context-provide.context")) # wait for it info_client = CLTool("context-ls","test.*") returnValue = info_client.wait() self.assertEqual(returnValue, 0, "context-ls exited with return value != 0") info_client.wait() provider.wait()
def testInfos(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Assert the returned information for key, description and type Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it listen.send("ikey test.int") self.assert_( listen.expect("^ikey: test.int$"), "ikey didn't work") listen.send("man test.truth") self.assert_( listen.expect("^man: A phony but very flexible property.$"), "man didn't work") listen.send("type test.truth") self.assert_( listen.expect("^type: TRUTH$"), "type didn't work") listen.wait() provider.wait()
def testRapidChanges(self): """ Description This test verifies that the subscriber library doesn't emit unnecessary valueChanged signals (in spite of receiving multiple D-Bus messages in the same main loop iteration). Steps 1. starts up a provider with one property 2. starts a client, verifies that the value arrives 3. suspends the client (so it stops processing D-Bus messages) 4. sets new value for the property twice in the provider 5. resumes the client 6. verifies that the client receives only one valueChanged signal """ provider_fast = CLTool("context-provide", "--v2", "com.nokia.fast", "int","test.fast","44") provider_fast.expect("Setting key") context_client = CLTool("context-listen", "test.fast") self.assert_(context_client.expect(wanted("test.fast", "int", "44")), "Bad value for the fast property, wanted 44") context_client.suspend() provider_fast.send("test.fast = 34") provider_fast.expect("Setting key") provider_fast.send("test.fast = 54") provider_fast.expect("Setting key") context_client.resume() context_client.expect(wanted("test.fast", "int", "54")) # the two value changes can happen very close in time, so we # have to check that beyond the good value there are no other # value(s) if len(re.findall("test.fast =", context_client.last_output)) != 1: context_client.printio() self.assert_(False, "expected a single valueChanged") # not even after waiting one second self.assertFalse(context_client.expect("test.fast =", wantdump = False, timeout=1), "expected a single valueChanged") context_client.wait() provider_fast.wait()
def testTruthTypePermutations(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change property of type bool to False, None and True Property value of type bool is updated to False, None and True Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "truth", "test.truth", "False") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it provider.send("test.truth = False") provider.expect("Setting") # wait for it listen = CLTool("context-listen", "test.truth") self.assert_( listen.expect(wanted("test.truth", "bool", "false")), "setting to false didn't work") provider.send("unset test.truth") self.assert_( listen.expect(wantedUnknown("test.truth")), "setting to unknown didn't work") provider.send("test.truth = True") self.assert_( listen.expect(wanted("test.truth", "bool", "true")), "setting to true didn't work") listen.wait() provider.wait()
def testStringTypePermutations(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change property of type string to empty string and None Property value of type bool is updated to empty string and None Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "string", "test.string", "something") provider.expect("Setting key: test.string") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.string") self.assert_( listen.expect("^test.string = QString:\"something\"$"), "setting to 'something' didn't work") provider.send('test.string = ""') self.assert_( listen.expect("^test.string = QString:\"\"$"), "setting to empty string didn't work") provider.send('unset test.string') self.assert_( listen.expect(wantedUnknown("test.string")), "setting to null didn't work") listen.wait() provider.wait()
def testProperties(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "-5", "string", "test.string", "something", "double", "test.double", "4.231", "truth", "test.truth", "False") provider.send("dump") self.assert_(provider.expect("Wrote ./context-provide.context"), "context-provide.context couldn't been written by context-provide") info_client = CLTool("context-ls","-l","-d","test.*") self.assert_(info_client.expect(["^test.int\tinteger\tcontextkit-dbus\tsession:com.nokia.test$", "^test.double\tnumber\tcontextkit-dbus\tsession:com.nokia.test$", "^test.truth\tbool\tcontextkit-dbus\tsession:com.nokia.test$", "^test.string\tstring\tcontextkit-dbus\tsession:com.nokia.test$", "^Documentation: A phony but very flexible property.$"]), "Bad introspection result from context-ls") info_client.wait() provider.wait()
def testWaitForSubscribeFail(self): """ Description Subscriber blocks until WaitForSubsciption returns, even if provider is not on DBus (specific to ContextKitPlugin) Pre-conditions Provider does exist for the property in the registry but it is not running Steps Subscribe and wait for the property Post-conditions The wait returned in 3 seconds (enough for testing that provider is not on D-Bus) """ context_client = CLTool("context-listen") context_client.send("n test.nonexistent") context_client.send("w test.nonexistent") self.assert_(context_client.expect("wait finished for test.nonexistent"), "Wait for subscription is probably in a dead state") context_client.wait()
def testCommanderFunctionality(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "42") provider.send("dump") self.assert_(provider.expect("Wrote")) # wait for it listen = CLTool("context-listen", "test.int", "test.string") listen.expect("Available commands") # wait for starting commander = CLTool("context-provide", "--v2") # check type-check commander.send("add string test.int foobar") commander.expect("Added key: test.int") self.assert_(listen.expect("Type check failed for \"test.int\""), "Type check didn't work") # check the non-existent property commander.send("add string test.string barfoo") commander.expect("Added key: test.string") self.assert_(listen.expect(wanted("test.string", "QString", "\"barfoo\"")), "Non-existent property couldn't be commanded") # change the type of the non-existent property commander.send("add int test.string 42") self.assert_(listen.expect(wanted("test.string", "int", "42")), "Non-existent property's type couldn't be overwritten") commander.wait() listen.wait() provider.wait()
def testTwoProviders(self): provider1 = CLTool("context-provide", "--v2","com.nokia.test", "truth","test.truth","True") provider1.expect("Setting key: test.truth") # wait for it provider1.send("dump context-provide1.context") provider1.expect("Wrote") # wait for it provider2 = CLTool("context-provide", "--v2","com.nokia.test2", "int","test.int","24") provider2.expect("Setting key: test.int") # wait for it provider2.send("dump context-provide2.context") provider2.expect("Wrote") # wait for it listen = CLTool("context-listen","test.int","test.truth") listen.expect("Available commands") # wait for it provider2.send("test.int = -68") self.assert_( listen.expect(wanted("test.int", "int", "-68"))) provider1.send("test.truth = False") self.assert_( listen.expect(wanted("test.truth", "bool", "false"))) listen.wait() provider2.wait() provider1.wait()
def testCommanderFunctionality(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "42") provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.int") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Bad value initially from the real provider, wanted 42") commander = CLTool("context-provide", "--v2") commander.send("add int test.int 4242") commander.send("start") commander.expect("Added") # wait for it self.assert_(listen.expect(wanted("test.int", "int", "4242")), "Value after commander has been started is wrong, wanted 4242") commander.send("unset test.int") listen.comment("commander commanded test.int to unknown") self.assert_(listen.expect(wantedUnknown("test.int")), "Value after commander has changed it to unknown is wrong") commander.send("test.int = 1235") self.assert_(listen.expect(wanted("test.int", "int", "1235")), "Value after commander has changed it is wrong, wanted 1235") commander.wait() listen.comment("Commander killed") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Value after killing the commander is wrong, wanted 42") listen.wait() provider.wait()
def testMultipleProviders2(self): """ Description This test verifies correct client behavior in the presence of multiple providers. In this test we always start the client after the providers have already been started and values have been set. Steps 1. starts two providers (X and Y) providing the same P property 2. X sets P to V1 3. Y sets P to V2 4. starts a client 5. commands X to sleep, to make sure the client gets the value from Y first checks that value for P is V2 even though it got first V2 and then V1 """ provider_x = CLTool("context-provide", "--v2", "test.x", "int", "test.prop", "44") provider_x.send("dump x.context") provider_x.expect("Wrote") provider_y = CLTool("context-provide", "--v2", "test.y", "int", "test.prop", "22") provider_y.send("dump y.context") provider_y.expect("Wrote") client = CLTool("context-listen") client.expect("Available commands") provider_x.send("sleep 2") provider_x.expect("Sleeping") client.send("n test.prop") time.sleep(4) client.send("value test.prop") self.assert_(client.expect("^value: qulonglong:22$")) client.wait() provider_y.wait() provider_y.wait()
def testAllDataTypes(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Properties values Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") self.assert_( listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]), "Actual key values pairs do not match expected") listen.wait() provider.wait()
def testMultipleProviders(self): """ Description This test verifies correct client behavior in the presence of multiple providers. Steps 1. starts up a client 2. starts two providers (X and Y) providing the same P property 3. X sets P to V1 and verifies that the client got it 4. Y sets P to V2 and likewise verifies in the client 5. Y sets P to NULL, the client verifies that P goes back to V1 6. Y sets P to V3, the client verifies P == V3 7. Y is removed from the registry, client verifies that P == V1 8. X is removed from the registry, client verifies that P == NULL """ client = CLTool("context-listen", "test.prop") client.expect("Available commands") provider_x = CLTool("context-provide", "--v2", "test.X", "int", "test.prop", "44") provider_x.send("dump x.context") provider_x.expect("Wrote") provider_y = CLTool("context-provide", "--v2", "test.Y", "int", "test.prop", "22") provider_y.send("dump y.context") provider_y.expect("Wrote") provider_x.send("test.prop = 55"); provider_x.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_y.send("test.prop = 77"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "77"))) provider_y.send("unset test.prop"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_y.send("test.prop = 99"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "99"))) provider_y.wait() os.unlink("y.context") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_x.wait() os.unlink("x.context") self.assert_(client.expect(wantedUnknown("test.prop"))) client.wait()
def testNoCommanding(self): # CONTEXT_COMMANDING is not set del os.environ['CONTEXT_COMMANDING'] provider = CLTool("context-provide", "--v2", "contextkit.test", "int", "test.int", "42") provider.send("dump") provider.expect("Wrote") # wait for it commander = CLTool("context-provide", "--v2") commander.send("add int test.int 4242") commander.send("start") commander.expect("Added") # wait for it os.environ["CONTEXT_CLI_IGNORE_COMMANDER"] = "" listen = CLTool("context-listen", "test.int") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Provider provided value is wrong") listen.wait() commander.wait() provider.wait()
def testValue(self): """ Description Subscribe to 4 properties covering basic data types Query the value of the property (when it's known or unknown, with and without a default value) Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change value of type int from the provider to a known value Query the value (both with and without a default value) and assert the result Change value of type int from the provider to unknown Query the value (both with and without a default value) and assert the result Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") self.assert_( listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]), "Actual key values pairs do not match expected") provider.send("test.int = 100") listen.expect(wanted("test.int", "int", "100")) # wait for it listen.send("value test.int") self.assert_( listen.expect("^value: qulonglong:100$"), "Value command returned wrong value") listen.send("value test.int \"defaultValue\"") self.assert_( listen.expect("^value: qulonglong:100$"), "Value command returned wrong value") provider.send("unset test.int") listen.expect("Unknown") # wait for it listen.send("value test.int") self.assert_( listen.expect("^value: Unknown$"), "Value command returned wrong value") listen.send("value test.int \"defaultValue\"") self.assert_( listen.expect("^value: QString:\"defaultValue\"$"), "Value command returned wrong value") listen.send("key test.int") self.assert_( listen.expect("^key: test.int$"), "Key command returned wrong value") # Because of a python threading / process / CLTool # issue, processes need to be waited for in the # opposite order as they are started. listen.wait() provider.wait()
class MultipleSubscribers(unittest.TestCase): def setUp(self): self.flexiprovider = CLTool("context-provide", "--v2","com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") self.flexiprovider.expect("Setting key: test.truth") # wait for it self.flexiprovider.send("dump") self.flexiprovider.expect("Wrote") # wait for it self.context_client1 = CLTool("context-listen","test.int","test.double","test.string","test.truth") self.context_client2 = CLTool("context-listen","test.int","test.double") self.context_client3 = CLTool("context-listen","test.int","test.string","test.truth") self.context_client4 = CLTool("context-listen","test.int","test.double","test.string") def tearDown(self): self.context_client4.wait() self.context_client3.wait() self.context_client2.wait() self.context_client1.wait() self.flexiprovider.wait() os.unlink('./context-provide.context') def testInitialSubscription(self): """ Description 4 subscribers instantiate multiple properties of 4 basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Subscribers receive initial values from provider Post-conditions Kill provider References None """ client1_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client2_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1")] client3_expected = [wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client4_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\"")] self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") def testValueChanged(self): """ Description 4 subscribers instantiate multiple properties of 4 basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Values are changed successively from the provider Subscribers receive updated values Post-conditions Kill provider References None """ client1_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client2_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1")] client3_expected = [wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client4_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\"")] self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") self.flexiprovider.send("test.double = -5.3") client1_expected = wanted("test.double", "double", "-5.3") client2_expected = wanted("test.double", "double", "-5.3") client4_expected = wanted("test.double", "double", "-5.3") self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") self.context_client3.send("type test.truth") client3_expected = "^type: TRUTH$" self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.flexiprovider.send("unset test.truth") client1_expected = wantedUnknown("test.truth") client3_expected = wantedUnknown("test.truth") self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected")
def testAsynchronicity(self): """ Description This test verifies that the asynchronicity of the subscriber library Pre-conditions 2 Providers started with context-provide tool. Each provider offers one property. 1 provider is delayed (3s). Steps Subscribe to both properties Verify that return values are correct Measure the time elapsed between the reception of the 2 values. Post-conditions Kill providers References None """ # start the client provider_slow = CLTool("context-provide", "--v2", "com.nokia.slow", "int","test.slow","42") provider_slow.expect("Setting key") # wait for it provider_fast = CLTool("context-provide", "--v2", "com.nokia.fast", "int","test.fast","44") provider_fast.expect("Setting key") # wait for it context_client = CLTool("context-listen") context_client.expect("Available commands") # wait for it provider_slow.comment("provider_slow sleep time started at" + str(time.time())) provider_slow.send("sleep 3") provider_slow.expect("Sleeping") # wait for it context_client.send("n test.slow ; n test.fast") # check the fast property self.assert_(context_client.expect(wanted("test.fast", "int", "44")), # timeout == 3 seconds "Bad value for the fast property, wanted 44") fast_time = time.time() context_client.comment("Fast property arrived with good value at: " + str(fast_time)) # check the slow property self.assert_(context_client.expect(wanted("test.slow", "int", "42")), # timeout == 10 seconds max, but 5 is enough usually "Bad value for the slow property, wanted 42") slow_time = time.time() context_client.comment("Slow property arrived with good value at: " + str(slow_time)) if slow_time - fast_time < 1.5: provider_slow.printio() context_client.printio() self.assert_(False, "The arrival time of the fast and slow property is not far enough from each other") # context_client.printio() context_client.wait() provider_fast.wait() provider_slow.wait()