Skip to content
Browse files

expanded test. Still not ready for consumption.

  • Loading branch information...
1 parent bbf5127 commit fea15d4ee1328e1e76073c98b43b6d74d2ffe9c1 @jfhbrook committed
Showing with 95 additions and 16 deletions.
  1. +95 −16 tests/test_protocol.py
View
111 tests/test_protocol.py
@@ -1,34 +1,113 @@
from protocol import Protocol
+from copy import copy
-def test_class_object_behavior():
+def test_request_response():
"""
Tests to make sure that class/object wrapping behaves as expected.
Based on https://github.com/substack/dnode-protocol/blob/master/test/fn.js
"""
- Class Client(object):
- def ping(cb):
- cb('pong!')
-
Class Server(object):
def __init__(self, remote, conn):
- #test remote
- # * Has expected methods?
- # * note: dnode-protocol fills "remote" with null if not passed.
- #test conn later, mebbs---node version uses eventemitter stuff
- # * Some sort of "connect" event
- pass
+ self.y = 555
- server = protocol(Server) #__init__ the Serve with remote and conn
- # Hey, a class decorator!
+ @conn.on('ready')
+ def on_ready():
+ #tests to make sure remote is properly populated.
+ nt.assert_equals(remote.a, 1)
+ nt.assert_equals(remote.b, 2)
- client = protocol(Obj()) #Don't try to construct since it's an instance already
+ def x(self,f,g):
+ #original test uses setTimeout()'s for each of these at 50 and 100,
+ #respectively.
+ f(7,8,9)
+ g(['q', 'r'])
+
+
+ Class Client(object):
+ def __init__(self):
+ self.a = 1
+ self.b = 2
+
+ server = Protocol(Server) # __init__ the Server with remote and conn
+ client = Protocol(Obj()) # Don't try to construct since it's
+ # an instance already
s = server.create()
c = client.create()
- #client and server act as EEs, so we need some .on()'s
- pass
+ s_reqs = []
+ @s.on('request')
+ def server_request(req):
+ s_reqs.append(copy(req))
+ c.handle(req)
+
+ c_reqs = []
+ @c.on('request')
+ def client_request(req):
+ c_reqs.append(copy(req))
+ s.handle(req)
+
+"""
+ # This is related to client.x(self,f,g)
+ var tf = setTimeout(function () {
+ assert.fail('never called f');
+ }, 5000);
+
+ var tg = setTimeout(function () {
+ assert.fail('never called g');
+ }, 5000);
+"""
+
+ s.start()
+
+ #Once 's' starts, s.emits('request') with a method that looks like...
+ nt.assert_equal(s_reqs[0]['method'], 'methods')
+ nt.assert_equal(s_reqs[0]['callbacks'], { 0 : [ '0', 'x' ] })
+ nt.assert_equal(s_reqs[0]['arguments']['x'], '[Function]')
+ nt.assert_equal(s_reqs[0]['arguments']['y'], 555)
+
+ c.start();
+
+ #Once 'c' starts, c.emits('request') with a method that looks like...
+ nt.assert_equal(c_reqs[0]['method'], 'methods')
+ nt.assert_equal(c_reqs[0]['callbacks'], {})
+ nt.assert_equal(c_reqs[0]['arguments']['a'], 1)
+ nt.assert_equal(c_reqs[0]['arguments']['b'], 2)
+
+ #Not sure that this is the best pattern. WHATEVER
+ c_fxns = []
+
+ @c_fxns.append
+ def f(x,y,z):
+ #clearTimeout(tf)
+ nt.assert_equal((x, y, z), (7, 8, 9))
+
+ @c_fxns.append
+ def g(qr):
+ #clearTimeout(tg)
+ nt.assert_equal(qr, ['q', 'r'])
+
+ # Request method x w/ arguments "c_fxns" defined above.
+ c.request('x', c_fxns)
+
+ nt.assert_equal(c_reqs[1::]['method'], 'x')
+ nt.assert_equal(c_reqs[1::]['callbacks'], { 0 : [ '0' ], 1 : [ '1' ] })
+ nt.assert_equal(c_reqs[1::]['arguments'], [ '[Function]', '[Function]' ])
+
+ # Tests to make sure that c.parse correctly handles json?
+ """
+ var tt = setTimeout(function () {
+ assert.fail('broken json never emitted an error');
+ }, 5000);
+ c.on('error', function (err) {
+ clearTimeout(tt);
+ assert.ok(err.stack);
+ assert.ok(err.message.match(/^Error parsing JSON/));
+ assert.ok(err instanceof SyntaxError);
+ });
+ c.parse('{');
+ """
def test_request_response():

0 comments on commit fea15d4

Please sign in to comment.
Something went wrong with that request. Please try again.