-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added better parameter checking, async decorator, and unit tests.
- Loading branch information
1 parent
ceffff6
commit ce6272b
Showing
11 changed files
with
441 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import unittest | ||
from tests.utils import * | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
if '--json' in sys.argv: | ||
sys.argv.pop(sys.argv.index('--json')) | ||
from tests.json import * | ||
else: | ||
from tests.xml import * | ||
|
||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" The TornadoRPC tests """ | ||
|
||
from threading import Thread | ||
from tornadorpc import start_server | ||
import time | ||
|
||
def start_server(handler, port): | ||
""" Starts a background server thread """ | ||
thread = Thread(target=start_server, args=[handler,], kwargs={'port':port}) | ||
thread.daemon = True | ||
thread.start() | ||
# time to start server | ||
time.sleep(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from tests.xml import TestHandler, RPCTests | ||
from tornadorpc.json import JSONRPCHandler | ||
import jsonrpclib | ||
import unittest | ||
|
||
class TestJSONHandler(TestHandler, JSONRPCHandler): | ||
pass | ||
|
||
class JSONRPCTests(RPCTests, unittest.TestCase): | ||
port = 8003 | ||
handler = TestJSONHandler | ||
|
||
def get_client(self): | ||
client = jsonrpclib.Server('http://localhost:%d' % self.port) | ||
return client | ||
|
||
def test_private(self): | ||
client = self.get_client() | ||
self.assertRaises(jsonrpclib.ProtocolError, client.private) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import unittest | ||
from tornadorpc.utils import getcallargs | ||
|
||
class TestCallArgs(unittest.TestCase): | ||
""" Tries various argument settings """ | ||
|
||
def test_no_args(self): | ||
def test(): | ||
pass | ||
kwargs, xtra = getcallargs(test) | ||
self.assertTrue(kwargs == {}) | ||
self.assertTrue(xtra == []) | ||
|
||
def test_bad_no_args(self): | ||
def test(): | ||
pass | ||
self.assertRaises(TypeError, getcallargs, test, 5) | ||
|
||
def test_positional_args(self): | ||
def test(a, b): | ||
pass | ||
kwargs, xtra = getcallargs(test, 5, 6) | ||
self.assertTrue(kwargs == {'a':5, 'b': 6}) | ||
self.assertTrue(xtra == []) | ||
|
||
def test_extra_positional_args(self): | ||
def test(a, b, *args): | ||
pass | ||
kwargs, xtra = getcallargs(test, 5, 6, 7, 8) | ||
self.assertTrue(kwargs == {'a': 5, 'b': 6}) | ||
self.assertTrue(xtra == [7, 8]) | ||
|
||
def test_bad_positional_args(self): | ||
def test(a, b): | ||
pass | ||
self.assertRaises(TypeError, getcallargs, test, 5) | ||
|
||
def test_keyword_args(self): | ||
def test(a, b): | ||
pass | ||
kwargs, xtra = getcallargs(test, a=5, b=6) | ||
self.assertTrue(kwargs == {'a': 5, 'b':6}) | ||
self.assertTrue(xtra == []) | ||
|
||
def test_extra_keyword_args(self): | ||
def test(a, b, **kwargs): | ||
pass | ||
kwargs, xtra = getcallargs(test, a=5, b=6, c=7, d=8) | ||
self.assertTrue(kwargs == {'a':5, 'b':6, 'c':7, 'd':8}) | ||
self.assertTrue(xtra == []) | ||
|
||
def test_bad_keyword_args(self): | ||
def test(a, b): | ||
pass | ||
self.assertRaises(TypeError, getcallargs, test, a=1, b=2, c=5) | ||
|
||
def test_method(self): | ||
class Foo(object): | ||
def test(myself, a, b): | ||
pass | ||
foo = Foo() | ||
kwargs, xtra = getcallargs(foo.test, 5, 6) | ||
self.assertTrue(kwargs == {'a':5, 'b':6}) | ||
self.assertTrue(xtra == []) | ||
|
||
def test_default(self): | ||
def test(a, b, default=None): | ||
pass | ||
kwargs, xtra = getcallargs(test, a=5, b=6) | ||
self.assertTrue(kwargs == {'a':5, 'b':6, 'default':None}) | ||
self.assertTrue(xtra == []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import unittest | ||
import xmlrpclib | ||
import time | ||
import threading | ||
from tornadorpc.xml import XMLRPCHandler | ||
from tornado.httpclient import AsyncHTTPClient | ||
from tornadorpc import start_server, private, async | ||
|
||
class Tree(object): | ||
|
||
def power(self, base, power, modulo=None): | ||
result = pow(base, power, modulo) | ||
return result | ||
|
||
def _private(self): | ||
# Should not be callable | ||
return False | ||
|
||
class TestHandler(object): | ||
|
||
tree = Tree() | ||
|
||
def add(self, x, y): | ||
return x+y | ||
|
||
@async | ||
def async(self, url): | ||
async_client = AsyncHTTPClient() | ||
async_client.fetch(url, self._handle_response) | ||
|
||
def _handle_response(self, response): | ||
self.result(response.code) | ||
|
||
@private | ||
def private(self): | ||
# Should not be callable | ||
return False | ||
|
||
class TestXMLHandler(XMLRPCHandler, TestHandler): | ||
pass | ||
|
||
class TestServer(object): | ||
|
||
threads = {} | ||
|
||
@classmethod | ||
def start(cls, handler, port): | ||
if not cls.threads.get(port): | ||
cls.threads[port] = threading.Thread( | ||
target=start_server, | ||
args=[handler,], | ||
kwargs={'port':port} | ||
) | ||
cls.threads[port].daemon = True | ||
cls.threads[port].start() | ||
# Giving it time to start up | ||
time.sleep(1) | ||
|
||
class RPCTests(object): | ||
|
||
server = None | ||
handler = TestXMLHandler | ||
port = 8002 | ||
|
||
def setUp(self): | ||
server = TestServer.start(self.handler, self.port) | ||
|
||
def get_client(self): | ||
client = xmlrpclib.ServerProxy('http://localhost:%d' % self.port) | ||
return client | ||
|
||
def test_tree(self): | ||
client = self.get_client() | ||
result = client.tree.power(2, 6) | ||
self.assertTrue(result == 64) | ||
|
||
def test_add(self): | ||
client = self.get_client() | ||
result = client.add(5, 6) | ||
self.assertTrue(result == 11) | ||
|
||
def test_async(self): | ||
url = 'http://www.google.com' | ||
client = self.get_client() | ||
result = client.async(url) | ||
self.assertTrue(result == 200) | ||
|
||
class XMLRPCTests(RPCTests, unittest.TestCase): | ||
|
||
def test_private(self): | ||
client = self.get_client() | ||
result = client.private() | ||
self.assertTrue(result['faultCode'] == -32601) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.