def test_call(self): handler = ApplicationSession() MockTransport(handler) res = yield handler.call(u'com.myapp.procedure1') self.assertEqual(res, 100) res = yield handler.call(u'com.myapp.procedure1', 1, 2, 3) self.assertEqual(res, 100) res = yield handler.call(u'com.myapp.procedure1', 1, 2, 3, foo=23, bar='hello') self.assertEqual(res, 100) res = yield handler.call(u'com.myapp.procedure1', options=types.CallOptions(timeout=10000)) self.assertEqual(res, 100) res = yield handler.call(u'com.myapp.procedure1', 1, 2, 3, foo=23, bar='hello', options=types.CallOptions(timeout=10000)) self.assertEqual(res, 100)
def test_invoke_progressive_result_just_kwargs(self): handler = ApplicationSession() MockTransport(handler) @inlineCallbacks def bing(details=None): self.assertTrue(details is not None) self.assertTrue(details.progress is not None) details.progress(key='word') yield succeed(True) returnValue(42) got_progress = Deferred() def progress(key=None): got_progress.callback(key) # see MockTransport, must start with "com.myapp.myproc" yield handler.register( bing, u'com.myapp.myproc2', types.RegisterOptions(details_arg='details'), ) res = yield handler.call( u'com.myapp.myproc2', options=types.CallOptions(on_progress=progress), ) self.assertEqual(42, res) self.assertTrue(got_progress.called) self.assertEqual('word', got_progress.result)
def test_invoke_progressive_result(self): handler = ApplicationSession() MockTransport(handler) @inlineCallbacks def bing(details=None): self.assertTrue(details is not None) self.assertTrue(details.progress is not None) for i in range(10): details.progress(i) yield succeed(i) returnValue(42) progressive = list(map(lambda _: Deferred(), range(10))) def progress(arg): progressive[arg].callback(arg) # see MockTransport, must start with "com.myapp.myproc" yield handler.register( bing, u'com.myapp.myproc2', types.RegisterOptions(details_arg='details'), ) res = yield handler.call( u'com.myapp.myproc2', options=types.CallOptions(on_progress=progress), ) self.assertEqual(42, res) # make sure we got *all* our progressive results for i in range(10): self.assertTrue(progressive[i].called) self.assertEqual(i, progressive[i].result)
def test_invoke_progressive_result_error(self): handler = ApplicationSession() MockTransport(handler) @inlineCallbacks def bing(arg, details=None, key=None): self.assertTrue(details is not None) self.assertTrue(details.progress is not None) self.assertEqual(key, 'word') self.assertEqual('arg', arg) details.progress('life', something='nothing') yield succeed('meaning of') returnValue(42) got_progress = Deferred() progress_error = NameError('foo') logged_errors = [] def got_error(e, msg): logged_errors.append((e.value, msg)) handler.onUserError = got_error def progress(arg, something=None): self.assertEqual('nothing', something) got_progress.callback(arg) raise progress_error # see MockTransport, must start with "com.myapp.myproc" yield handler.register( bing, u'com.myapp.myproc2', types.RegisterOptions(details_arg='details'), ) res = yield handler.call( u'com.myapp.myproc2', 'arg', options=types.CallOptions(on_progress=progress), key='word', ) self.assertEqual(42, res) # our progress handler raised an error, but not before # recording success. self.assertTrue(got_progress.called) self.assertEqual('life', got_progress.result) # make sure our progress-handler error was logged self.assertEqual(1, len(logged_errors)) self.assertEqual(progress_error, logged_errors[0][0])
def onJoin(self, details): self.channel_layer = channel_layers["default"] Channel('wamp.join').send({'session': details.session}) while self.is_connected(): yield self.publish('com.example.bonjour', 2, options=types.PublishOptions(exclude_me=False)) self.call('com.example.hello', 'hey!') channel_name, result = self.channel_layer.receive_many( self.channels) if channel_name is None: yield sleep(SLEEP_TIME) continue elif channel_name == 'wamp.call': uri = result['uri'] args = result.get('args', []) kwargs = result.get('kwargs', {}) options = result.get('options', {}) if options: kwargs['options'] = types.CallOptions(**options) registration = self.call(uri, *args, **kwargs) elif channel_name == 'wamp.publish': topic = result['topic'] args = result.get('args', []) kwargs = result.get('kwargs', {}) options = result.get('options', {}) if options: kwargs['options'] = types.PublishOptions(**options) self.publish(topic, *args, **kwargs) elif channel_name == 'wamp.subscribe': func_path = result['func_path'] topic = result['topic'] options = result.get('options', {}) or {} subscribe_options = types.SubscribeOptions(**options) subscription = yield self.forward_subscriber( func_path, topic, subscribe_options) self.forward_subscription(result['reply_channel'], subscription) elif channel_name == 'wamp.unsubscribe': subscription_id = result['subscription_id'] subscription = self.subscriptions.pop(subscription_id) yield subscription.unsubscribe() elif channel_name == 'wamp.register': func_path = result['func_path'] uri = result['uri'] options = result.get('options', {}) or {} register_options = types.RegisterOptions(**options) registration = yield self.forward_procedure( func_path, uri, register_options) self.forward_registration(result['reply_channel'], registration) elif channel_name == 'wamp.unregister': registration_id = result['registration_id'] registration = self.subscriptions.pop(registration_id) yield registration.unregister() elif channel_name in self.reply_channels: self.reply_channels[channel_name].callback( *result['args'], **result['kwargs']) yield sleep(SLEEP_TIME) self.log.info('disconnected!') Channel('wamp.disconnect').send({ 'reason': 'not connected', }) self.disconnect() reactor.stop()