Skip to content

Commit

Permalink
Add more unsubscribe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Stam committed Sep 7, 2017
1 parent 6cbb30d commit 1517bd3
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions tests/test_unsubscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
'room': room_car,
}

subscribe_car_2 = {
'requestId': 'car_2',
'type': 'subscribe',
'room': room_car,
}

unsubscribe_car = {
'requestId': 'car',
'type': 'unsubscribe',
}

unsubscribe_car_2 = {
'requestId': 'car_2',
'type': 'unsubscribe',
}


class TestUnsubscribe(TestCase):
def setUp(self):
Expand Down Expand Up @@ -56,11 +67,45 @@ def test_no_more_publish(self):
g.switch()
self.assertEqual(4, len(ws.outgoing_messages))

# test doesnt receive messages
#
# test room closes if empty
#
# test subscription removed from connection
#
# test cannot unsubscribe from non subscription
def test_sub_removed_from_room(self):
ws1 = MockWebSocket()
ws1.mock_incoming_message(json.dumps(subscribe_car))
ws2 = MockWebSocket()
ws2.mock_incoming_message(json.dumps(subscribe_car_2))

g1 = greenlet(self.client.open_connection)
g1.switch(ws1)
g2 = greenlet(self.client.open_connection)
g2.switch(ws2)

room = self.getHubRoomByDict(room_car)
c1 = room.connections[0]
c2 = room.connections[1]

self.assertHubRoomsEqual([room_car])
self.assertEqual([c1, c2], room.connections)
self.assertEqual([room], [s.room for s in c1.subscriptions.values()])
self.assertEqual([room], [s.room for s in c2.subscriptions.values()])

ws2.mock_incoming_message(json.dumps(unsubscribe_car_2))
g2.switch()

self.assertHubRoomsEqual([room_car])
self.assertEqual([c1], room.connections)
self.assertEqual([room], [s.room for s in c1.subscriptions.values()])
self.assertEqual(0, len(c2.subscriptions))

ws1.mock_incoming_message(json.dumps(unsubscribe_car))
g1.switch()

self.assertHubRoomsEqual([])
self.assertEqual(0, len(c1.subscriptions))
self.assertEqual(0, len(c2.subscriptions))

def test_cannot_unsubscribe_when_not_subscribed(self):
ws = MockWebSocket()
ws.mock_incoming_message(json.dumps(unsubscribe_car))

self.client.open_connection(ws)
self.assertEqual(2, len(ws.outgoing_messages))
self.assertEqual({'requestId': 'car', 'code': 'error', 'message': 'not-subscribed'}, json.loads(ws.outgoing_messages[1]))

0 comments on commit 1517bd3

Please sign in to comment.