def test_pattern_subscribe(self): # psubscribe doesn't return anything self.assertEquals(self.pubsub.psubscribe('f*'), None) # send a message self.assertEquals(self.client.publish('foo', 'hello foo'), 1) # there should be now 2 messages in the buffer, a subscribe and the # one we just published self.assertEquals(next(self.pubsub.listen()), { 'type': 'psubscribe', 'pattern': None, 'channel': 'f*', 'data': 1 }) self.assertEquals( next(self.pubsub.listen()), { 'type': 'pmessage', 'pattern': 'f*', 'channel': 'foo', 'data': b('hello foo') }) # unsubscribe self.assertEquals(self.pubsub.punsubscribe('f*'), None) # unsubscribe message should be in the buffer self.assertEquals(next(self.pubsub.listen()), { 'type': 'punsubscribe', 'pattern': None, 'channel': 'f*', 'data': 0 })
def test_slave_round_robin(cluster, sentinel): cluster.slaves = [ {'ip': 'slave0', 'port': 6379, 'is_odown': False, 'is_sdown': False}, {'ip': 'slave1', 'port': 6379, 'is_odown': False, 'is_sdown': False}, ] pool = SentinelConnectionPool('mymaster', sentinel) rotator = pool.rotate_slaves() assert next(rotator) in (('slave0', 6379), ('slave1', 6379)) assert next(rotator) in (('slave0', 6379), ('slave1', 6379)) # Fallback to master assert next(rotator) == ('127.0.0.1', 6379) with pytest.raises(SlaveNotFoundError): next(rotator)
def test_pattern_subscribe(self): # psubscribe doesn't return anything self.assertEquals( self.pubsub.psubscribe('f*'), None ) # send a message self.assertEquals(self.client.publish('foo', 'hello foo'), 1) # there should be now 2 messages in the buffer, a subscribe and the # one we just published self.assertEquals( next(self.pubsub.listen()), { 'type': 'psubscribe', 'pattern': None, 'channel': 'f*', 'data': 1 } ) self.assertEquals( next(self.pubsub.listen()), { 'type': 'pmessage', 'pattern': 'f*', 'channel': 'foo', 'data': b('hello foo') } ) # unsubscribe self.assertEquals( self.pubsub.punsubscribe('f*'), None ) # unsubscribe message should be in the buffer self.assertEquals( next(self.pubsub.listen()), { 'type': 'punsubscribe', 'pattern': None, 'channel': 'f*', 'data': 0 } )
def test_pattern_subscribe(self, r): p = r.pubsub() # psubscribe doesn't return anything assert p.psubscribe('f*') is None # send a message assert r.publish('foo', 'hello foo') == 1 # there should be now 2 messages in the buffer, a subscribe and the # one we just published assert next(p.listen()) == \ { 'type': 'psubscribe', 'pattern': None, 'channel': 'f*', 'data': 1 } assert next(p.listen()) == \ { 'type': 'pmessage', 'pattern': 'f*', 'channel': 'foo', 'data': b('hello foo') } # unsubscribe assert p.punsubscribe('f*') is None # unsubscribe message should be in the buffer assert next(p.listen()) == \ { 'type': 'punsubscribe', 'pattern': None, 'channel': 'f*', 'data': 0 }
def test_channel_subscribe(self, r): p = r.pubsub() # subscribe doesn't return anything assert p.subscribe('foo') is None # send a message assert r.publish('foo', 'hello foo') == 1 # there should be now 2 messages in the buffer, a subscribe and the # one we just published assert next(p.listen()) == \ { 'type': 'subscribe', 'pattern': None, 'channel': 'foo', 'data': 1 } assert next(p.listen()) == \ { 'type': 'message', 'pattern': None, 'channel': 'foo', 'data': b('hello foo') } # unsubscribe assert p.unsubscribe('foo') is None # unsubscribe message should be in the buffer assert next(p.listen()) == \ { 'type': 'unsubscribe', 'pattern': None, 'channel': 'foo', 'data': 0 }
def test_hold_connection(self): # SUBSCRIBE => UNSUBSCRIBE => SUBSCRIBE pubsub = self.client.pubsub(release_connection=False) pubsub.subscribe('foo') connection = pubsub.connection pubsub.unsubscribe('foo') pubsub.subscribe('bar') # Get a message self.client.publish('bar', 'baz') for i in range(4): message = next(pubsub.listen()) self.assertEquals(message, { 'type': 'message', 'pattern': None, 'channel': 'bar', 'data': b('baz'), } ) # Verify the connection hasn't been recycled self.assertEqual(id(connection), id(pubsub.connection))
def listen(): return next(pubsub.listen())