def test_publish_with_reply(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() assertSocket(expected='PUB foo reply 3\r\n', response='') assertSocket(expected='msg\r\n', response='') c.publish('foo', 'msg', 'reply')
def __init__(self, config): self._connection = pynats.Connection(url=config.services_url.nats_url, verbose=True) self._subject = config.services_url.nats_subject self._log = logging.getLogger(type(self).__name__) self._log.info( 'CloudFoundry notifier will talk to NATS at %s on subject %s', config.services_url.nats_url, config.services_url.nats_subject)
def test_wait_with_handler_return_false(self): c = pynats.Connection(verbose=True) c.connect() def callback(msg): return False c.subscribe('foo', callback) self._send_message(0.1) c.wait()
def test_wait_receive_ping(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() def monkey(self, *expected_commands): return pynats.commands.PING, None c._recv = monkey assertSocket(expected='PONG\r\n', response='') c.wait(duration=0.1)
def test_wait_with_timeout(self): c = pynats.Connection(verbose=True) c.connect() def callback(msg): return True c.subscribe('foo', callback) self._send_message(0.1) c.wait(duration=0.1)
def test_multiline_send(self): c = pynats.Connection(verbose=True) c.connect() payload = "123\n456" def callback(msg): self.assertEquals(msg.data, payload) return False c.subscribe('foo', callback) self._send_message(msg=payload) c.wait()
def pushData(self, _, items): try: if self.client is None: self.client = pynats.Connection(self.serverUrl, verbose=True) self.client.connect() for item in items: json_str = json.dumps(item, separators=(',', ':')) self.client.publish( '%s.%s' % (self.subject, item.get('event', 'unknown')), json_str) return True, None except pynats.connection.SocketError as e: self.client = None return False, e
def test_wait_with_limit_foo(self): c = pynats.Connection(verbose=True) c.connect() def callback(msg): return True s = c.subscribe('foo', callback) c.unsubscribe(s, 3) self.assertEquals(1, len(c._subscriptions)) self._send_message(0.1) self._send_message(0.1) self._send_message(0.1) c.wait(count=3) self.assertEquals(0, len(c._subscriptions))
def test_request_without_msg(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() inbox = '_INBOX.kykblzisxpeou' def mocked_inbox(): return inbox c._build_inbox = mocked_inbox assertSocket(expected='SUB %s 1\r\n' % inbox, response='') assertSocket(expected='UNSUB 1 1\r\n', response='') assertSocket(expected='PUB request %s 0\r\n' % inbox, response='') assertSocket(expected='\r\n', response='') s = c.request('request', 'callback') self.assertEquals(s.subject, inbox)
def test_subscribe_and_unsubscribe(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() def handler(msg): pass assertSocket(expected='SUB foo 1\r\n', response='') subscription = c.subscribe('foo', handler) self.assertEquals(c._next_sid, 2) self.assertIsInstance(subscription, pynats.Subscription) self.assertEquals(subscription.sid, 1) self.assertEquals(subscription.subject, 'foo') self.assertEquals(subscription.callback, handler) assertSocket(expected='UNSUB 1\r\n', response='') c.unsubscribe(subscription) self.assertEquals(c._subscriptions, {})
def test_wait_receive_msg(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() def handler(msg): self.assertEquals(msg.sid, 1) self.assertEquals(msg.subject, 'foo') self.assertEquals(msg.size, 10) self.assertEquals(msg.reply, 'reply') return False assertSocket(expected='SUB foo 1\r\n', response='') c.subscribe('foo', handler) def monkey(self, *expected_commands): line = 'MSG foo 1 reply 10\r\n' return pynats.commands.MSG, pynats.commands.MSG.match(line) c._recv = monkey assertSocket(expected='PONG\r\n', response='') c.wait(count=1)
def test__build_inbox(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() inbox = c._build_inbox() self.assertEquals(20, len(inbox))
def test_ping(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect() assertSocket(expected='PING\r\n', response='PONG\r\n') c.ping()
def test_connect(self): c = pynats.Connection('nats://localhost:4444', 'foo') c.connect()
def test_socket_keepalive(self): c = pynats.Connection('nats://localhost:4444', 'foo', socket_keepalive=True) c.connect()
import pynats import string import time send_cnt=0 recv_cnt=0 flag=0 ping_delay=0.0 N=0 recv_cnt3=0 suber=pynats.Connection(verbose=True) suber.connect() suber_pong=pynats.Connection(verbose=True) suber_pong.connect() start=time.clock(); def callback(msg): global send_cnt global recv_cnt global flag global N global ping_delay global recv_cnt3 flag=1 send_cnt+=1 recv_cnt+=1 recv_cnt3+=1 now=time.time() N=800*string.atoi(msg.data[21]) stri=' %s'%(msg.data[22:1007])
import json import pynats import redis accutemp = [] curhour = None redis_client = None def handle(tempdata): global curhour, accutemp parsedtempdata = json.loads(tempdata) nxthour = int(parsedtempdata["time"].split(':')[0]) if curhour is None: curhour = nxthour elif nxthour > curhour: redis_client.hset("aggregatedtempdata", curhour, sum(accutemp) / len(accutemp)) accutemp = [] curhour = nxthour accutemp.append(int(parsedtempdata["temp"])) if __name__ == "__main__": nats_client = pynats.Connection(url=os.environ['NATS_URI'], verbose=True) nats_client.connect() nats_client.subscribe("tempdata", handle) redis_client = redis.Redis.from_url(os.environ['REDIS_URI']) while True: sleep(1)
def send(): c = pynats.Connection(verbose=True) c.connect() c.publish('foo', msg)
old_sec = 0 ping_delay_3sec = 0 send_cnt_3sec = 0 def callback(msg): '''global flag flag=1''' global recv_cnt, ping_delay_3sec, pong_delay #print msg.data[:34] recv_cnt += 1 ping_delay_3sec += (string.atof(msg.data[0:20]) - string.atof(msg.data[21:41])) puber = pynats.Connection(verbose=True) puber_check = pynats.Connection(verbose=True) puber.connect() puber_check.connect() puber_check.subscribe('pong.fiat', callback) def sender(): global old_sec, temp_cnt, old_cnt_sec, ping_delay_3sec, send_cnt_3sec, N, send_cnt, puber, r_msg while (1): sec = time.time() if (sec - old_sec > 1): old_cnt_sec += 1 temp_cnt = 0 old_sec = sec if (old_cnt_sec == 3):