Skip to content

Commit

Permalink
Unsubscribe from a queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Élysson MR authored and wpjunior committed May 11, 2017
1 parent cc41b7e commit 2137277
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,21 @@ def test_nack(self):
b'message-id:321\n'
b'subscription:123\n\n'
b'\x00')

def test_unsubscribe(self):
self.stomp.stream = MagicMock()

self.stomp.subscribe("queue/dummyqueue")

last_subscribe_id = str(self.stomp._last_subscribe_id)
subscription = self.stomp._subscriptions[last_subscribe_id]

self.stomp.unsubscribe(subscription)

self.assertFalse(last_subscribe_id in self.stomp._subscriptions.keys())
self.assertEqual(self.stomp.stream.write.call_count, 1)
self.assertEqual(
self.stomp.stream.write.call_args[0][0],
b'UNSUBSCRIBE\n'
b'id:1\n\n'
b'\x00')
13 changes: 13 additions & 0 deletions torstomp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def subscribe(self, destination, ack='auto', extra_headers={},
if self.connected:
self._send_subscribe_frame(subscription)

def unsubscribe(self, subscription):
subscription_id = str(subscription.id)

if subscription_id in self._subscriptions.keys():
self._send_unsubscribe_frame(subscription)
del self._subscriptions[subscription_id]

def send(self, destination, body='', headers={}, send_content_length=True):
headers['destination'] = destination

Expand Down Expand Up @@ -245,3 +252,9 @@ def _send_subscribe_frame(self, subscription):
headers.update(subscription.extra_headers)

return self._send_frame('SUBSCRIBE', headers)

def _send_unsubscribe_frame(self, subscription):
headers = {
'id': subscription.id,
}
return self._send_frame('UNSUBSCRIBE', headers)

0 comments on commit 2137277

Please sign in to comment.