def test_call_cancel_two_sessions(self): """ this has 2 different session using the same ID (42) for their Call requests to confirm we deal with the fact that these IDs are only unique per-session properly """ messages = [] def session_send(msg): messages.append(msg) session0 = mock.Mock() session0._transport.send = session_send session0._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} session1 = mock.Mock() session1._transport.send = session_send session1._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} dealer = self.router._dealer dealer.attach(session0) dealer.attach(session1) def authorize(*args, **kwargs): return defer.succeed({'allow': True, 'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session0, message.Register(1, 'com.example.my.proc', 'exact', message.Register.INVOKE_SINGLE, 2)) registered_msg = messages[-1] self.assertIsInstance(registered_msg, message.Registered) # two calls outstanding to the endpoint, both happen to use # the same ID (42) which is legal dealer.processCall(session0, message.Call(42, 'com.example.my.proc', [])) invocation_msg0 = messages[-1] self.assertIsInstance(invocation_msg0, message.Invocation) dealer.processCall(session1, message.Call(42, 'com.example.my.proc', [])) invocation_msg1 = messages[-1] self.assertIsInstance(invocation_msg1, message.Invocation) # now, cancel the first session's call dealer.processCancel(session0, message.Cancel( 42, "kill", )) # should receive an INTERRUPT from the dealer now (for the # correct session only) interrupt_msg0 = messages[-1] self.assertIsInstance(interrupt_msg0, message.Interrupt) self.assertEqual(interrupt_msg0.request, invocation_msg0.request) dealer.processInvocationError( session0, message.Error(message.Invocation.MESSAGE_TYPE, invocation_msg0.request, 'wamp.error.canceled'))
def test_call_cancel_nonowned_call(self): last_message = {'1': []} def session_send(msg): last_message['1'] = msg session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} dealer = self.router._dealer dealer.attach(session) def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, u'com.example.my.proc', u'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = last_message['1'] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(session, message.Call( 2, u'com.example.my.proc', [] )) invocation_msg = last_message['1'] self.assertIsInstance(invocation_msg, message.Invocation) bad_session = mock.Mock() bad_session._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} dealer.attach(bad_session) def attempt_bad_cancel(): dealer.processCancel(bad_session, message.Cancel( 2 )) self.failUnlessRaises(ProtocolError, attempt_bad_cancel)
def test_call_cancel(self): last_message = {'1': []} def session_send(msg): last_message['1'] = msg session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} dealer = self.router._dealer dealer.attach(session) def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, u'com.example.my.proc', u'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = last_message['1'] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(session, message.Call( 2, u'com.example.my.proc', [] )) invocation_msg = last_message['1'] self.assertIsInstance(invocation_msg, message.Invocation) dealer.processCancel(session, message.Cancel( 2 )) # should receive an INTERRUPT from the dealer now interrupt_msg = last_message['1'] self.assertIsInstance(interrupt_msg, message.Interrupt) self.assertEqual(interrupt_msg.request, invocation_msg.request) dealer.processInvocationError(session, message.Error( message.Invocation.MESSAGE_TYPE, invocation_msg.request, u'wamp.error.canceled' )) call_error_msg = last_message['1'] self.assertIsInstance(call_error_msg, message.Error) self.assertEqual(message.Call.MESSAGE_TYPE, call_error_msg.request_type) self.assertEqual(u'wamp.error.canceled', call_error_msg.error)
def test_call_timeout_with_callee_and_caller_support(self): messages = [] def session_send(msg): messages.append(msg) session = ApplicationSession() session._transport = mock.Mock() session._transport.send = session_send session._session_roles = { 'callee': role.RoleCalleeFeatures(call_canceling=True), 'caller': role.RoleCallerFeatures(call_canceling=True), } dealer = self.router._dealer dealer.attach(session) dealer._cancel_timers.call_later = mock.Mock() def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister( session, message.Register(1, u'com.example.my.proc', u'exact', message.Register.INVOKE_SINGLE, 1)) registered_msg = messages[-1] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall( session, message.Call( 2, u'com.example.my.proc', [], timeout=1, )) invocation_msg = messages[-1] self.assertIsInstance(invocation_msg, message.Invocation) # induce a timeout # get the last time-out that was added... mc = dealer._cancel_timers.call_later.mock_calls[0] timeout_fn = mc[1][1] # ...and call it timeout_fn() # caller gets Error (should see it) # callee gets Interrupt (should see it) self.assertTrue(any( isinstance(msg, message.Error) for msg in messages)) self.assertTrue( any(isinstance(msg, message.Interrupt) for msg in messages))
def test_call_cancel(self): messages = [] def session_send(msg): messages.append(msg) session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} dealer = self.router._dealer dealer.attach(session) def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, u'com.example.my.proc', u'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = messages[-1] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(session, message.Call( 2, u'com.example.my.proc', [] )) invocation_msg = messages[-1] self.assertIsInstance(invocation_msg, message.Invocation) dealer.processCancel(session, message.Cancel( 2 )) # we should receive an INTERRUPT from the dealer now -- note # that our session is both the caller and the callee in this # test, so we'll get an INTERRUPT *and* an ERROR -- in that # order. interrupt_msg = messages[-2] self.assertIsInstance(interrupt_msg, message.Interrupt) self.assertEqual(interrupt_msg.request, invocation_msg.request) call_error_msg = messages[-1] self.assertIsInstance(call_error_msg, message.Error) self.assertEqual(message.Call.MESSAGE_TYPE, call_error_msg.request_type) self.assertEqual(u'wamp.error.canceled', call_error_msg.error)
def test_caller_detach_interrupt_cancel_not_supported(self): last_message = {'1': []} def session_send(msg): last_message['1'] = msg session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures()} caller_session = mock.Mock() dealer = self.router._dealer dealer.attach(session) dealer.attach(caller_session) def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, u'com.example.my.proc', u'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = last_message['1'] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(caller_session, message.Call( 2, u'com.example.my.proc', [] )) invocation_msg = last_message['1'] self.assertIsInstance(invocation_msg, message.Invocation) dealer.detach(caller_session) # reset recorded message to make sure we don't receive anything last_message['1'] = None # should NOT receive an INTERRUPT from the dealer now because we don't support cancellation self.assertIsNone(last_message['1'])
def test_call_cancel_without_callee_support(self): last_message = {'1': []} def session_send(msg): last_message['1'] = msg session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures()} dealer = self.router._dealer dealer.attach(session) def authorize(*args, **kwargs): return defer.succeed({'allow': True, 'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, 'com.example.my.proc', 'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = last_message['1'] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(session, message.Call( 2, 'com.example.my.proc', [] )) invocation_msg = last_message['1'] self.assertIsInstance(invocation_msg, message.Invocation) dealer.processCancel(session, message.Cancel( 2 )) # set message to None to make sure that we get nothing back last_message['1'] = None # should NOT receive an INTERRUPT from the dealer now interrupt_msg = last_message['1'] self.assertIsNone(interrupt_msg)
def test_caller_detach_interrupt_cancel_supported(self): last_message = {'1': []} def session_send(msg): last_message['1'] = msg session = mock.Mock() session._transport.send = session_send session._session_roles = {'callee': role.RoleCalleeFeatures(call_canceling=True)} caller_session = mock.Mock() dealer = self.router._dealer dealer.attach(session) dealer.attach(caller_session) def authorize(*args, **kwargs): return defer.succeed({'allow': True, 'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) dealer.processRegister(session, message.Register( 1, 'com.example.my.proc', 'exact', message.Register.INVOKE_SINGLE, 1 )) registered_msg = last_message['1'] self.assertIsInstance(registered_msg, message.Registered) dealer.processCall(caller_session, message.Call( 2, 'com.example.my.proc', [] )) invocation_msg = last_message['1'] self.assertIsInstance(invocation_msg, message.Invocation) dealer.detach(caller_session) # should receive an INTERRUPT from the dealer now interrupt_msg = last_message['1'] self.assertIsInstance(interrupt_msg, message.Interrupt) self.assertEqual(interrupt_msg.request, invocation_msg.request)
def join(self, realm, authmethods = None, authid = None): """ Implements :func:`autobahn.wamp.interfaces.ISession.join` """ if six.PY2 and type(realm) == str: realm = six.u(realm) if self._session_id: raise Exception("already joined") self._goodbye_sent = False roles = [ role.RolePublisherFeatures(), role.RoleSubscriberFeatures(), role.RoleCallerFeatures(), role.RoleCalleeFeatures() ] msg = message.Hello(realm, roles, authmethods, authid) self._realm = realm self._transport.send(msg)
def generate_test_messages(): """ List of WAMP test message used for serializers. Expand this if you add more options or messages. This list of WAMP message does not contain any binary app payloads! """ some_bytes = os.urandom(32) some_unicode = '\u3053\u3093\u306b\u3061\u306f\u4e16\u754c' some_uri = 'com.myapp.foobar' some_unicode_uri = 'com.myapp.\u4f60\u597d\u4e16\u754c.baz' some_args = [1, 2, 3, 'hello', some_bytes, some_unicode, {'foo': 23, 'bar': 'hello', 'baz': some_bytes, 'moo': some_unicode}] some_kwargs = {'foo': 23, 'bar': 'hello', 'baz': some_bytes, 'moo': some_unicode, 'arr': some_args} msgs = [ message.Hello("realm1", {'subscriber': role.RoleSubscriberFeatures()}), message.Hello("realm1", {'publisher': role.RolePublisherFeatures()}), message.Hello("realm1", {'caller': role.RoleCallerFeatures()}), message.Hello("realm1", {'callee': role.RoleCalleeFeatures()}), message.Hello("realm1", { 'subscriber': role.RoleSubscriberFeatures(), 'publisher': role.RolePublisherFeatures(), 'caller': role.RoleCallerFeatures(), 'callee': role.RoleCalleeFeatures(), }), message.Goodbye(), message.Yield(123456), message.Yield(123456, args=some_args), message.Yield(123456, args=[], kwargs=some_kwargs), message.Yield(123456, args=some_args, kwargs=some_kwargs), message.Yield(123456, progress=True), message.Interrupt(123456), message.Interrupt(123456, mode=message.Interrupt.KILL), message.Invocation(123456, 789123), message.Invocation(123456, 789123, args=some_args), message.Invocation(123456, 789123, args=[], kwargs=some_kwargs), message.Invocation(123456, 789123, args=some_args, kwargs=some_kwargs), message.Invocation(123456, 789123, timeout=10000), message.Result(123456), message.Result(123456, args=some_args), message.Result(123456, args=[], kwargs=some_kwargs), message.Result(123456, args=some_args, kwargs=some_kwargs), message.Result(123456, progress=True), message.Cancel(123456), message.Cancel(123456, mode=message.Cancel.KILL), message.Call(123456, some_uri), message.Call(123456, some_uri, args=some_args), message.Call(123456, some_uri, args=[], kwargs=some_kwargs), message.Call(123456, some_uri, args=some_args, kwargs=some_kwargs), message.Call(123456, some_uri, timeout=10000), message.Call(123456, some_unicode_uri), message.Call(123456, some_unicode_uri, args=some_args), message.Call(123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Call(123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Call(123456, some_unicode_uri, timeout=10000), message.Unregistered(123456), message.Unregister(123456, 789123), message.Registered(123456, 789123), message.Register(123456, some_uri), message.Register(123456, some_uri, match='prefix'), message.Register(123456, some_uri, invoke='roundrobin'), message.Register(123456, some_unicode_uri), message.Register(123456, some_unicode_uri, match='prefix'), message.Register(123456, some_unicode_uri, invoke='roundrobin'), message.Event(123456, 789123), message.Event(123456, 789123, args=some_args), message.Event(123456, 789123, args=[], kwargs=some_kwargs), message.Event(123456, 789123, args=some_args, kwargs=some_kwargs), message.Event(123456, 789123, publisher=300), message.Published(123456, 789123), message.Publish(123456, some_uri), message.Publish(123456, some_uri, args=some_args), message.Publish(123456, some_uri, args=[], kwargs=some_kwargs), message.Publish(123456, some_uri, args=some_args, kwargs=some_kwargs), message.Publish(123456, some_uri, exclude_me=False, exclude=[300], eligible=[100, 200, 300]), message.Publish(123456, some_unicode_uri), message.Publish(123456, some_unicode_uri, args=some_args), message.Publish(123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Publish(123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Publish(123456, some_unicode_uri, exclude_me=False, exclude=[300], eligible=[100, 200, 300]), message.Unsubscribed(123456), message.Unsubscribe(123456, 789123), message.Subscribed(123456, 789123), message.Subscribe(123456, some_uri), message.Subscribe(123456, some_uri, match=message.Subscribe.MATCH_PREFIX), message.Subscribe(123456, some_unicode_uri), message.Subscribe(123456, some_unicode_uri, match=message.Subscribe.MATCH_PREFIX), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=some_args), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=[], kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=some_args, kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=some_args), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Result(123456), message.Result(123456, args=some_args), message.Result(123456, args=some_args, kwargs=some_kwargs), ] return [(False, msg) for msg in msgs]
def test_concurrency_with_error(self): """ register a concurrency=2 method, called with errors """ callee_messages = [] caller_messages = [] def callee_send(msg): callee_messages.append(msg) session = mock.Mock() session._transport.send = callee_send session._session_roles = {'callee': role.RoleCalleeFeatures()} def caller_send(msg): caller_messages.append(msg) caller_session = mock.Mock() caller_session._transport.send = caller_send dealer = self.router._dealer dealer.attach(session) dealer.attach(caller_session) def authorize(*args, **kwargs): return defer.succeed({u'allow': True, u'disclose': False}) self.router.authorize = mock.Mock(side_effect=authorize) # we register out procedure, with concurrency=1 dealer.processRegister( session, message.Register(request=1, procedure=u'com.example.my.proc', match=u'exact', invoke=message.Register.INVOKE_SINGLE, concurrency=1)) registered_msg = callee_messages[-1] self.assertIsInstance(registered_msg, message.Registered) # we have registered our procedure that has concurrency=1 # and now we call it dealer.processCall(caller_session, message.Call(2, u'com.example.my.proc', [])) # we pretend that the call caused an error of some sort invocation_msg = callee_messages[-1] self.assertIsInstance(invocation_msg, message.Invocation) dealer.processInvocationError( session, message.Error( message.Call.MESSAGE_TYPE, invocation_msg.request, u"wamp.error.foo", )) self.assertEqual(1, len(caller_messages)) self.assertEqual( u"wamp.error.foo", caller_messages[-1].error, ) # now we call it again, which should work because the # previously-outstanding call was resolved with an error # (before bug 1105 being fixed this wouldn't work properly) dealer.processCall(caller_session, message.Call(3, u'com.example.my.proc', ['foo'])) invocation_msg = callee_messages[-1] self.assertIsInstance(invocation_msg, message.Invocation) self.assertEqual(1, len(caller_messages), "got an extra unexpected message") dealer.processYield( session, message.Yield( invocation_msg.request, args=['a result'], )) result_msg = caller_messages[-1] self.assertIsInstance(result_msg, message.Result) self.assertEqual(result_msg.args, ['a result'])
def send(msg): bytes, _ = serializer.serialize(msg) l = struct.pack("!I", len(bytes)) sys.stdout.write(l) sys.stdout.write(bytes) msgs = [] ## HELLO ## roles = [ role.RolePublisherFeatures(), role.RoleSubscriberFeatures(), role.RoleCallerFeatures(), role.RoleCalleeFeatures() ] msgs.append(message.Hello("foobar", roles)) ## CHALLENGE ## msgs.append(message.Challenge("cookie")) ## HEARTBEAT ## msgs.append(message.Heartbeat(3, 7, "throw me away")) for msg in msgs: send(msg)