From d55b18d8b2c026fd64d7f9cae0d40a16d2f433e2 Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Mon, 4 Jan 2016 16:55:29 -0800 Subject: [PATCH 1/2] Add travis ci build --- .travis.yml | 26 ++++++++++++++++++++++++++ script/install_gnatsd.sh | 15 +++++++++++++++ script/test.sh | 6 ++++++ 3 files changed, 47 insertions(+) create mode 100644 .travis.yml create mode 100755 script/install_gnatsd.sh create mode 100755 script/test.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..92a13c0b2 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/script/install_gnatsd.sh b/script/install_gnatsd.sh new file mode 100755 index 000000000..7ad8e13b8 --- /dev/null +++ b/script/install_gnatsd.sh @@ -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 diff --git a/script/test.sh b/script/test.sh new file mode 100755 index 000000000..b9288ece5 --- /dev/null +++ b/script/test.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +export PYTHONPATH=$(pwd) + +python tests/client_test.py +python tests/parser_test.py From 37690b29ed8684f7a36c8e50ea7a1f2b4760459a Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Mon, 4 Jan 2016 17:15:33 -0800 Subject: [PATCH 2/2] Add travis-ci and test fixes --- readme.md | 3 ++- script/test.sh | 4 +--- tests/client_test.py | 29 +++++++++++++++-------------- tests/parser_test.py | 3 ++- tests/test.py | 16 ++++++++++++++++ tests/utils.py | 11 +++++++---- 6 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 tests/test.py diff --git a/readme.md b/readme.md index e0ef5bbee..e816b7324 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/script/test.sh b/script/test.sh index b9288ece5..f318ec986 100755 --- a/script/test.sh +++ b/script/test.sh @@ -1,6 +1,4 @@ #!/bin/bash export PYTHONPATH=$(pwd) - -python tests/client_test.py -python tests/parser_test.py +python tests/test.py diff --git a/tests/client_test.py b/tests/client_test.py index 766242051..1962164be 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -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']) @@ -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') @@ -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): @@ -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): @@ -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) diff --git a/tests/parser_test.py b/tests/parser_test.py index 6d9585ad1..03922e417 100644 --- a/tests/parser_test.py +++ b/tests/parser_test.py @@ -2,6 +2,7 @@ import unittest from nats.aio.client import Subscription from nats.protocol.parser import * +from tests.utils import NatsTestCase class MockNatsClient: @@ -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()) diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 000000000..9ed6f65a1 --- /dev/null +++ b/tests/test.py @@ -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) diff --git a/tests/utils.py b/tests/utils.py index d307ce01b..fd902c6cd 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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()