def test_trigger_outbound_update(self): class TestBinding(WebsocketBinding): model = User stream = 'test' fields = ['__all__'] @classmethod def group_names(cls, instance, action): return ["users2"] def has_permission(self, user, action, pk): return True # Make model and clear out pending sends user = User.objects.create(username='******', email='*****@*****.**') consumer_finished.send(sender=None) with apply_routes([route('test', TestBinding.consumer)]): client = HttpClient() client.join_group('users2') user.username = '******' user.save() consumer_finished.send(sender=None) received = client.receive() self.assertTrue('payload' in received) self.assertTrue('action' in received['payload']) self.assertTrue('data' in received['payload']) self.assertTrue('username' in received['payload']['data']) self.assertTrue('email' in received['payload']['data']) self.assertTrue('password' in received['payload']['data']) self.assertTrue('last_name' in received['payload']['data']) self.assertTrue('model' in received['payload']) self.assertTrue('pk' in received['payload']) self.assertEqual(received['payload']['action'], 'update') self.assertEqual(received['payload']['model'], 'auth.user') self.assertEqual(received['payload']['pk'], user.pk) self.assertEqual(received['payload']['data']['email'], '*****@*****.**') self.assertEqual(received['payload']['data']['username'], 'test_new') self.assertEqual(received['payload']['data']['password'], '') self.assertEqual(received['payload']['data']['last_name'], '') received = client.receive() self.assertIsNone(received)
def foo(self, channel, content, message, match): consumer, kwargs = match if self.callback: self.callback(channel, message) try: logger.debug("Dispatching message on %s to %s", channel, name_that_thing(consumer)) # Send consumer started to manage lifecycle stuff consumer_started.send(sender=self.__class__, environ={}) # Run consumer consumer(message, **kwargs) except DenyConnection: # They want to deny a WebSocket connection. if message.channel.name != "websocket.connect": raise ValueError( "You cannot DenyConnection from a non-websocket.connect handler." ) message.reply_channel.send({"close": True}) except ChannelSocketException as e: e.run(message) except ConsumeLater: # They want to not handle it yet. Re-inject it with a number-of-tries marker. content['__retries__'] = content.get("__retries__", 0) + 1 # If we retried too many times, quit and error rather than # spinning forever if content['__retries__'] > self.message_retries: logger.warning( "Exceeded number of retries for message on channel %s: %s", channel, repr(content)[:100], ) return # Try to re-insert it a few times then drop it for _ in range(10): try: self.channel_layer.send(channel, content) except self.channel_layer.ChannelFull: gevent.sleep(0.05) print "full" else: break except: logger.exception("Error processing message with consumer %s:", name_that_thing(consumer)) finally: # Send consumer finished so DB conns close etc. consumer_finished.send(sender=self.__class__)
def test_trigger_outbound_update(self): class TestBinding(WebsocketBinding): model = User stream = 'test' fields = ['__all__'] @classmethod def group_names(cls, instance, action): return ["users2"] def has_permission(self, user, action, pk): return True # Make model and clear out pending sends user = User.objects.create(username='******', email='*****@*****.**') consumer_finished.send(sender=None) with apply_routes([route('test', TestBinding.consumer)]): client = HttpClient() client.join_group('users2') user.username = '******' user.save() consumer_finished.send(sender=None) received = client.receive() self.assertTrue('payload' in received) self.assertTrue('action' in received['payload']) self.assertTrue('data' in received['payload']) self.assertTrue('username' in received['payload']['data']) self.assertTrue('email' in received['payload']['data']) self.assertTrue('password' in received['payload']['data']) self.assertTrue('last_name' in received['payload']['data']) self.assertTrue('model' in received['payload']) self.assertTrue('pk' in received['payload']) self.assertEqual(received['payload']['action'], 'update') self.assertEqual(received['payload']['model'], 'auth.user') self.assertEqual(received['payload']['pk'], user.pk) self.assertEqual(received['payload']['data']['email'], '*****@*****.**') self.assertEqual(received['payload']['data']['username'], 'test_new') self.assertEqual(received['payload']['data']['password'], '') self.assertEqual(received['payload']['data']['last_name'], '') received = client.receive() self.assertIsNone(received)
def __init__(self, response): assert isinstance(response, (HttpResponse, StreamingHttpResponse)) self._response = response consumer_finished.send(sender=self.__class__) super(FakeAsgiHandler, self).__init__()
def __init__(self, response): assert isinstance(response, (HttpResponse, StreamingHttpResponse)) self._response = response consumer_finished.send(sender=self.__class__) super(FakeAsgiHandler, self).__init__()