def test_subscribe(self): subs = SubscriptionsManager(mock.MagicMock()) observer_a = subs.subscribe(channels.DeviceStateChanges(device_id='A')) observer_b = subs.subscribe(channels.DeviceStateChanges(device_id='B')) observer_c = subs.subscribe(channels.DeviceStateChanges()) self.assertIsNot(observer_a, observer_b) self.assertIsNot(observer_b, observer_c) future_a = observer_a.next().defer() future_b = observer_b.next().defer() future_c = observer_c.next().defer() self.assertIsNot(future_a, future_b) self.assertIsNot(future_b, future_c) # a valid, but irrelevant channel subs.notify({ channels.ChannelIdentifiers.async_responses: [ dict(a=1, b=2, device_id='A') ] }) time.sleep(0.01) self.assertFalse(future_a.ready()) self.assertFalse(future_b.ready()) self.assertFalse(future_c.ready()) # a valid channel, relevant for DeviceState subs.notify({ channels.ChannelIdentifiers.reg_updates: [ dict(a=1, b=2, device_id='A') ] }) result = future_a.get(timeout=2) self.assertDictContainsSubset(dict(a=1, b=2), result) self.assertFalse(future_b.ready()) # possible race condition, so an unreliable check. # just because one observer (`a`) is ready, does not mean another (`c`) is. # self.assertTrue(future_c.ready()) result = future_c.get() self.assertDictContainsSubset(dict(a=1, b=2), result)
def test_subscribe_custom_threadpool(self): subs = SubscriptionsManager(mock.MagicMock()) custom_threads = ThreadPool(processes=1) observer_a = subs.subscribe(channels.DeviceStateChanges(device_id='A'), provider=custom_threads) with mock.patch.object(custom_threads, 'apply_async') as custom: observer_a.next().defer() observer_a.next().defer() # prove that we used the threadpool we provided self.assertEqual(custom_threads.apply_async.call_count, 2)
def test_notify_de_registrations(self): subs = SubscriptionsManager(mock.MagicMock()) observer = subs.subscribe(channels.DeviceStateChanges()) future = observer.next().defer() subs.notify( {channels.ChannelIdentifiers.de_registrations: ["device_id_test"]}) output = future.get(timeout=2) self.assertEqual(output, { "device_id": "device_id_test", "channel": "de_registrations" })
def test_notify_reg_updates(self): subs = SubscriptionsManager(mock.MagicMock()) observer = subs.subscribe(channels.DeviceStateChanges()) future = observer.next().defer() subs.notify({ channels.ChannelIdentifiers.reg_updates: [{ "ep": "test_endpoint_name", "original_ep": "test_original_ep", "ept": "test_ept", "q": "test_q", "resources": [{ "path": "test_path", "if": "test_if", "rt": "test_rt", "ct": "test_ct", "obs": "test_obs" }] }] }) output = future.get(timeout=2) self.assertEqual( output, { "channel": "reg_updates", "device_id": "test_endpoint_name", "alias": "test_original_ep", "device_type": "test_ept", "queue_mode": "test_q", "resources": [{ "path": "test_path", "type": "test_rt", "content_type": "test_ct", "observable": "test_obs" }] })