def test_keeps_open_nonempty_room(self): ws1 = MockWebSocket() ws1.mock_incoming_message(json.dumps(subscribe_ride)) ws2 = MockWebSocket() ws2.mock_incoming_message(json.dumps(subscribe_ride)) ws2.mock_incoming_message(json.dumps(subscribe_car)) g1 = greenlet(self.client.open_connection) g1.switch(ws1) g2 = greenlet(self.client.open_connection) g2.switch(ws2) # Test that both rooms are created and contain # the correct connections self.assertHubRoomsEqual([room_ride, room_car]) r_car = self.getHubRoomByDict(room_car) self.assertEqual([ws2.connection], r_car.connections) r_ride = self.getHubRoomByDict(room_ride) self.assertSetEqual(set([ws1.connection, ws2.connection]), set(r_ride.connections)) # Close the second websocket, unsubscribing if for all the rooms ws2.close() # Test that the now empty room_car is closed self.assertHubRoomsEqual([room_ride]) # And test that the room_ride still contains ws1 self.assertEqual([ws1.connection], r_ride.connections)
def test_incoming_trigger_gets_published(self): ws1 = MockWebSocket() ws1.mock_incoming_message(json.dumps(subscribe_ride)) ws2 = MockWebSocket() ws2.mock_incoming_message(json.dumps(subscribe_ride)) ws2.mock_incoming_message(json.dumps(subscribe_car)) g1 = greenlet(self.client.open_connection) g1.switch(ws1) g2 = greenlet(self.client.open_connection) g2.switch(ws2) # Mock incoming ride trigger self.client.flask_test_client.post( '/trigger/', content_type='application/json', data=json.dumps({ 'rooms': [room_ride], 'data': [{ 'id': 1, }] })) # The first outgoing message is the allowed_rooms listing # The second one is about the successful subscribe # The third is the actual publish self.assertEqual(3, len(ws1.outgoing_messages)) self.assertEqual(json.dumps(publish_ride), ws1.outgoing_messages[2]) # ws2 has 2 subscribe success events self.assertEqual(4, len(ws2.outgoing_messages)) self.assertEqual(json.dumps(publish_ride), ws2.outgoing_messages[3]) ws2.close() self.client.flask_test_client.post( '/trigger/', content_type='application/json', data=json.dumps({ 'rooms': [room_ride], 'data': [{ 'id': 1, }] })) self.assertEqual(4, len(ws1.outgoing_messages)) self.assertEqual(json.dumps(publish_ride), ws1.outgoing_messages[3])