def test_match_observations_match_wildcard_single(self): """ When a observer observes to a uri (wildcard prefix), the observer is returned for all uris upon lookup where the observed uri matches the wildcard pattern. """ obs_map = UriObservationMap() obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, u"com.example..create", match=Subscribe.MATCH_WILDCARD) # test matches for uri in [u"com.example.foobar.create", u"com.example.1.create" ]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [observation1]) self.assertEqual(observations[0].observers, set([obs1])) # test non-matches for uri in [u"com.example.foobar.delete", u"com.example.foobar.create2", u"com.example.foobar.create.barbaz" u"com.example.foobar", u"com.example.create", u"com.example" ]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [])
def test_match_observations_match_wildcard_multi(self): """ Test with multiple wildcards in wildcard-matching observation. """ obs_map = UriObservationMap() obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, u"com...create", match=Subscribe.MATCH_WILDCARD) # test matches for uri in [u"com.example.foobar.create", u"com.example.1.create", u"com.myapp.foobar.create", u"com.myapp.1.create", ]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [observation1]) self.assertEqual(observations[0].observers, set([obs1])) # test non-matches for uri in [u"com.example.foobar.delete", u"com.example.foobar.create2", u"com.example.foobar.create.barbaz" u"com.example.foobar", u"org.example.foobar.create", u"org.example.1.create", u"org.myapp.foobar.create", u"org.myapp.1.create", ]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [])
def test_match_observations_match_multimode(self): """ When a observer is observed to multiple observations each matching a given uri looked up, the observer is returned in each observation. """ obs_map = UriObservationMap() obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer( obs1, u"com.example.product.create", match=Subscribe.MATCH_EXACT) observation2, _, _ = obs_map.add_observer(obs1, u"com.example.product", match=Subscribe.MATCH_PREFIX) observation3, _, _ = obs_map.add_observer( obs1, u"com.example..create", match=Subscribe.MATCH_WILDCARD) observations = obs_map.match_observations( u"com.example.product.create") self.assertEqual(observations, [observation1, observation2, observation3]) self.assertEqual(observations[0].observers, set([obs1])) self.assertEqual(observations[1].observers, set([obs1])) self.assertEqual(observations[2].observers, set([obs1])) observations = obs_map.match_observations(u"com.example.foobar.create") self.assertEqual(observations, [observation3]) self.assertEqual(observations[0].observers, set([obs1])) observations = obs_map.match_observations( u"com.example.product.delete") self.assertEqual(observations, [observation2]) self.assertEqual(observations[0].observers, set([obs1]))
def test_match_observations_match_prefix(self): """ When a observer observes an URI (match prefix), the observer is returned for all uris upon lookup where the observed URI is a prefix. """ obs_map = UriObservationMap() obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, u"com.example", match=Subscribe.MATCH_PREFIX) # test matches for uri in [u"com.example.uri1.foobar.barbaz", u"com.example.uri1.foobar", u"com.example.uri1", u"com.example.topi", u"com.example.", u"com.example2", u"com.example"]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [observation1]) self.assertEqual(observations[0].observers, set([obs1])) # test non-matches for uri in [u"com.foobar.uri1", u"com.exampl.uri1", u"com.exampl", u"com", u""]: observations = obs_map.match_observations(uri) self.assertEqual(observations, [])
def __init__(self, router, options=None): """ :param router: The router this dealer is part of. :type router: Object that implements :class:`crossbar.router.interfaces.IRouter`. :param options: Router options. :type options: Instance of :class:`crossbar.router.types.RouterOptions`. """ self._router = router self._options = options or RouterOptions() # subscription map managed by this broker self._subscription_map = UriObservationMap() # map: session -> set of subscriptions (needed for detach) self._session_to_subscriptions = {} # check all topic URIs with strict rules self._option_uri_strict = self._options.uri_check == RouterOptions.URI_CHECK_STRICT # supported features from "WAMP Advanced Profile" self._role_features = role.RoleBrokerFeatures(publisher_identification=True, pattern_based_subscription=True, subscription_meta_api=True, subscriber_blackwhite_listing=True, publisher_exclusion=True, subscription_revocation=True)
def __init__(self, router, options=None): """ :param router: The router this dealer is part of. :type router: Object that implements :class:`crossbar.router.interfaces.IRouter`. :param options: Router options. :type options: Instance of :class:`crossbar.router.types.RouterOptions`. """ self._router = router self._options = options or RouterOptions() # generator for WAMP request IDs self._request_id_gen = util.IdGenerator() # registration map managed by this dealer self._registration_map = UriObservationMap(ordered=True) # map: session -> set of registrations (needed for detach) self._session_to_registrations = {} # pending callee invocation requests self._invocations = {} # check all procedure URIs with strict rules self._option_uri_strict = self._options.uri_check == RouterOptions.URI_CHECK_STRICT # supported features from "WAMP Advanced Profile" self._role_features = role.RoleDealerFeatures( caller_identification=True, pattern_based_registration=True, session_meta_api=True, registration_meta_api=True, shared_registration=True, progressive_call_results=True, registration_revocation=True)
def __init__(self, router, options=None): """ :param router: The router this dealer is part of. :type router: Object that implements :class:`crossbar.router.interfaces.IRouter`. :param options: Router options. :type options: Instance of :class:`crossbar.router.types.RouterOptions`. """ self._router = router self._options = options or RouterOptions() # generator for WAMP request IDs self._request_id_gen = util.IdGenerator() # registration map managed by this dealer self._registration_map = UriObservationMap(ordered=True) # map: session -> set of registrations (needed for detach) self._session_to_registrations = {} # map: session -> in-flight invocations self._callee_to_invocations = {} # BEWARE: this map must be kept up-to-date along with the # _invocations map below! Use the helper methods # _add_invoke_request and _remove_invoke_request # map: session -> in-flight invocations self._caller_to_invocations = {} # careful here: the 'request' IDs are unique per-session # (only) so we map from (session_id, call) tuples to in-flight invocations # map: (session_id, call) -> in-flight invocations self._invocations_by_call = {} # pending callee invocation requests self._invocations = {} # check all procedure URIs with strict rules self._option_uri_strict = self._options.uri_check == RouterOptions.URI_CHECK_STRICT # supported features from "WAMP Advanced Profile" self._role_features = role.RoleDealerFeatures(caller_identification=True, pattern_based_registration=True, session_meta_api=True, registration_meta_api=True, shared_registration=True, progressive_call_results=True, registration_revocation=True, payload_transparency=True, testament_meta_api=True, payload_encryption_cryptobox=True, call_canceling=True) # store for call queues if self._router._store: self._call_store = self._router._store.call_store else: self._call_store = None
def test_match_observations_empty(self): """ An empty observer map returns an empty observer set for any URI. """ obs_map = UriObservationMap() for uri in [u"com.example.uri1", u"com.example.uri2", u""]: obsvs = obs_map.match_observations(uri) self.assertEqual(obsvs, [])
def test_add_observer(self): """ When a observer is added, a observation is returned. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() observation, was_already_observed, is_first_observer = obs_map.add_observer(obs1, uri1) self.assertIsInstance(observation, ExactUriObservation) self.assertFalse(was_already_observed) self.assertTrue(is_first_observer)
def test_match_observations_match_exact(self): """ When a observer observes an URI (match exact), the observer is returned for the URI upon lookup. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, uri1) observations = obs_map.match_observations(uri1) self.assertEqual(observations, [observation1])
def __init__(self, router, reactor, options=None): """ :param router: The router this dealer is part of. :type router: Object that implements :class:`crossbar.router.interfaces.IRouter`. :param options: Router options. :type options: Instance of :class:`crossbar.router.types.RouterOptions`. """ self._router = router self._reactor = reactor self._options = options or RouterOptions() # generator for WAMP request IDs self._request_id_gen = util.IdGenerator() # subscription map managed by this broker self._subscription_map = UriObservationMap() # map: session -> set of subscriptions (needed for detach) self._session_to_subscriptions = {} # check all topic URIs with strict rules self._option_uri_strict = self._options.uri_check == RouterOptions.URI_CHECK_STRICT # supported features from "WAMP Advanced Profile" self._role_features = role.RoleBrokerFeatures( publisher_identification=True, pattern_based_subscription=True, session_meta_api=True, subscription_meta_api=True, subscriber_blackwhite_listing=True, publisher_exclusion=True, subscription_revocation=True, event_retention=True, payload_transparency=True, payload_encryption_cryptobox=True) # store for event history if self._router._store: self._event_store = self._router._store.event_store else: self._event_store = None # if there is a store, let the store attach itself to all the subscriptions # it is configured to track if self._event_store: self._event_store.attach_subscription_map(self._subscription_map)
def test_add_observer_is_first_observer(self): """ When a observer is added, the ``is_first_observer`` flag in the return is correct. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() obs2 = FakeObserver() _, _, is_first_observer = obs_map.add_observer(obs1, uri1) self.assertTrue(is_first_observer) _, _, is_first_observer = obs_map.add_observer(obs2, uri1) self.assertFalse(is_first_observer)
def test_add_observer_was_already_observed(self): """ When a observer is added, the ``was_already_observed`` flag in the return is correct. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() observation1, was_already_observed, _ = obs_map.add_observer(obs1, uri1) self.assertFalse(was_already_observed) observation2, was_already_observed, _ = obs_map.add_observer(obs1, uri1) self.assertTrue(was_already_observed) self.assertEqual(observation1, observation2)
def test_match_observations_match_exact_same(self): """ When multiple different observers observe the same URI (match exact), all get the same observation. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() obs2 = FakeObserver() obs3 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, uri1) observation2, _, _ = obs_map.add_observer(obs2, uri1) observation3, _, _ = obs_map.add_observer(obs3, uri1) observations = obs_map.match_observations(uri1) self.assertEqual(observations, [observation1]) self.assertEqual(observations[0].observers, set([obs1, obs2, obs3]))
def test_delete_observer(self): obs_map = UriObservationMap() uri = u"com.example.uri1" obs1 = FakeObserver() obs2 = FakeObserver() ob1, uri1, _ = obs_map.add_observer(obs1, uri) ob2, uri2, _ = obs_map.add_observer(obs2, uri) self.assertTrue(ob1 is ob2) obs_map.drop_observer(obs1, ob1) # error if we delete because there's still one observer with self.assertRaises(ValueError): obs_map.delete_observation(ob2) # drop last observer and delete obs_map.drop_observer(obs2, ob1) obs_map.delete_observation(ob2)
def test_match_observations_match_exact_multi(self): """ When the same observer is added multiple times to the same URI (match exact), the observation is only returned once, and every time the same observation ID is returned. """ obs_map = UriObservationMap() uri1 = u"com.example.uri1" obs1 = FakeObserver() observation1, _, _ = obs_map.add_observer(obs1, uri1) observation2, _, _ = obs_map.add_observer(obs1, uri1) observation3, _, _ = obs_map.add_observer(obs1, uri1) self.assertEqual(observation1, observation2) self.assertEqual(observation1, observation3) observations = obs_map.match_observations(uri1) self.assertEqual(observations, [observation1]) self.assertEqual(observations[0].observers, set([obs1]))