Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: python

cache:
directories:
- $HOME/gnatsd

python:
- 3.4
- 3.5

env:
- DEBUG_NATS_TEST=true

before_install:
- bash ./script/install_gnatsd.sh

before_script:
- export PATH=$HOME/gnatsd:$PATH

script:
- ./script/test.sh

notifications:
email: false

sudo: false
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# NATS - Python Client for Asyncio

An asyncio-based [PEP 3156](https://www.python.org/dev/peps/pep-3156/) Python client for the [NATS messaging system](https://nats.io).
An asyncio-based ([PEP 3156](https://www.python.org/dev/peps/pep-3156/)) Python client for the [NATS messaging system](https://nats.io).

[![License MIT](https://img.shields.io/npm/l/express.svg)](http://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/nats-io/asyncio-nats.svg?branch=master)](http://travis-ci.org/nats-io/asyncio-nats)

## Supported platforms

Expand Down
15 changes: 15 additions & 0 deletions script/install_gnatsd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# check to see if gnatsd folder is empty
if [ ! "$(ls -A $HOME/gnatsd)" ]; then
(
mkdir -p $HOME/gnatsd;
cd $HOME/gnatsd
wget https://github.com/nats-io/gnatsd/releases/download/v0.7.2/gnatsd-v0.7.2-linux-amd64.tar.gz -O gnatsd.tar.gz;
tar -xvf gnatsd.tar.gz;
)
else
echo 'Using cached directory.';
fi
4 changes: 4 additions & 0 deletions script/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

export PYTHONPATH=$(pwd)
python tests/test.py
29 changes: 15 additions & 14 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_publish(self):
yield from nc.publish("", b'')

yield from nc.close()
yield from asyncio.sleep(1, loop=self.loop)
self.assertEqual(100, nc.stats['out_msgs'])
self.assertEqual(100, nc.stats['out_bytes'])

Expand Down Expand Up @@ -172,12 +173,7 @@ def subscription_handler(msg):
with self.assertRaises(KeyError):
nc._subs[sid].received

yield from nc.close()
self.assertEqual(2, nc.stats['in_msgs'])
self.assertEqual(2, nc.stats['in_bytes'])
self.assertEqual(4, nc.stats['out_msgs'])
self.assertEqual(4, nc.stats['out_bytes'])

yield from asyncio.sleep(1, loop=self.loop)
endpoint = '127.0.0.1:{port}'.format(port=self.server_pool[0].http_port)
httpclient = http.client.HTTPConnection(endpoint, timeout=5)
httpclient.request('GET', '/connz')
Expand All @@ -190,6 +186,11 @@ def subscription_handler(msg):
self.assertEqual(2, connz['connections'][0]['out_msgs'])
self.assertEqual(2, connz['connections'][0]['out_bytes'])

yield from nc.close()
self.assertEqual(2, nc.stats['in_msgs'])
self.assertEqual(2, nc.stats['in_bytes'])
self.assertEqual(4, nc.stats['out_msgs'])
self.assertEqual(4, nc.stats['out_bytes'])

@async_test
def test_timed_request(self):
Expand All @@ -213,9 +214,9 @@ def slow_worker_handler(msg):
yield from nc.subscribe("help", cb=worker_handler)
yield from nc.subscribe("slow.help", cb=slow_worker_handler)

response = yield from nc.timed_request("help", b'please')
response = yield from nc.timed_request("help", b'please', timeout=1)
self.assertEqual(b'Reply:1', response.data)
response = yield from nc.timed_request("help", b'please')
response = yield from nc.timed_request("help", b'please', timeout=1)
self.assertEqual(b'Reply:2', response.data)

with self.assertRaises(ErrTimeout):
Expand Down Expand Up @@ -377,18 +378,18 @@ def worker_handler(msg):
yield from nc.subscribe("two", cb=worker_handler)
yield from nc.subscribe("three", cb=worker_handler)

response = yield from nc.timed_request("one", b'Help!')
response = yield from nc.timed_request("one", b'Help!', timeout=1)
self.assertEqual(b'Reply:1', response.data)

# Stop the first server and connect to another one asap.
yield from self.loop.run_in_executor(None, self.server_pool[0].stop)

# FIXME: Find better way to wait for the server to be stopped.
yield from asyncio.sleep(0.5, loop=self.loop)
yield from nc.publish("two", b'...')

for i in range(3, 5):
response = yield from nc.timed_request("three", b'Help!')
self.assertEqual('Reply:{}'.format(i).encode(), response.data)
yield from asyncio.sleep(0.1, loop=self.loop)
response = yield from nc.timed_request("three", b'Help!', timeout=1)
self.assertEqual('Reply:2'.encode(), response.data)
yield from asyncio.sleep(0.5, loop=self.loop)
yield from nc.close()
self.assertEqual(1, nc.stats['reconnects'])
self.assertEqual(1, closed_count)
Expand Down
3 changes: 2 additions & 1 deletion tests/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from nats.aio.client import Subscription
from nats.protocol.parser import *
from tests.utils import NatsTestCase

class MockNatsClient:

Expand All @@ -27,7 +28,7 @@ def _process_msg(self, sid, subject, reply, payload):
def _process_err(self, err=None):
pass

class ProtocolParserTest(unittest.TestCase):
class ProtocolParserTest(NatsTestCase):

def test_parse_ping(self):
ps = Parser(MockNatsClient())
Expand Down
16 changes: 16 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys
import unittest

from tests.parser_test import *
from tests.client_test import *

if __name__ == '__main__':
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(ProtocolParserTest))
test_suite.addTest(unittest.makeSuite(ClientUtilsTest))
test_suite.addTest(unittest.makeSuite(ClientTest))
test_suite.addTest(unittest.makeSuite(ClientReconnectTest))
runner = unittest.TextTestRunner(stream=sys.stdout)
result = runner.run(test_suite)
if not result.wasSuccessful():
sys.exit(1)
11 changes: 7 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def stop(self):
if self.debug:
print("[\033[0;33mDEBUG\033[0;0m] Server listening on %d will stop." % self.port)

if self.debug and self.proc is None:
print("[\033[0;31mDEBUG\033[0;0m] Failed terminating server listening on port %d" % self.port)
elif self.proc.returncode is not None:
print("[\033[0;31mDEBUG\033[0;0m] Server listening on port {port} finished running already with exit {ret}".format(port=self.port, ret=self.proc.returncode))
if self.debug:
if self.proc is None:
print("[\033[0;31mDEBUG\033[0;0m] Failed terminating server listening on port %d" % self.port)

if self.proc.returncode is not None:
if self.debug:
print("[\033[0;31mDEBUG\033[0;0m] Server listening on port {port} finished running already with exit {ret}".format(port=self.port, ret=self.proc.returncode))
else:
os.kill(self.proc.pid, signal.SIGKILL)
self.proc.wait()
Expand Down