def testGreen(self): net = MockNet() gta = EventletThread() va, ra = net.addRPCHandler('X', '', {}, t_model=gta) gta.start(True) gtb = EventletThread() vb, rb = net.addRPCHandler('Y', '', {}, t_model=gtb) gtb.start(True) va['data'] = {'name': 'Tom'} pb = rb.makeProxy('data', 'X') th = TestHandler() def pr(fn, arg): th.handle('message', {'message': fn(arg)}) # The reason we have to call pa[] in gta (green thread a) # is because if the cb.wait() is called by this thread # cb.main_loop and cb.resume end up being in different threads. with th.expect(1): gtb.callFromThread(pr, pb.__getitem__, 'name') self.assertEqual(th.received, ['Tom']) gta.stop() gtb.stop()
def test(self): net = MockNet() ls, lrpc = net.dictRPCHandler('server') bs, brpc = net.dictRPCHandler('browser') # Server one-way call. ls['obj'] = {'key': 'value'} set = JCBoundMethod(lrpc, 'obj', '__setitem__', 'server') set('key', 'new') self.assertEquals(ls['obj']['key'], 'new') # Browser one-way call. bs['obj'] = {'name': 'Fred'} bset = JCBoundMethod(lrpc, 'obj', '__setitem__', 'browser') self.assertEquals(bset('name', 'Ginger'), None) self.assertEquals(bs['obj']['name'], 'Ginger') # Should be able to send the bound method itself. bset('name', bset) b_bset = bs['obj']['name'] self.assertEquals(type(b_bset), JCBoundMethod) # NOTE: really transported, not just ref to same. self.assertEquals(b_bset.handler(), brpc) del net, ls, lrpc, bs, brpc self.assertEquals(bset('name', 'Barney'), False)
def testSendFailure(self): net = MockNet() node = net.addNode('B') cb = MockHandler() node.send('A', {}) # no such node, no callback, fail silently. node.send('A', {}, errh=cb.failure) self.assertEqual(type(cb.exc), KeyError)
def testFacetProxy(self): net = MockNet() va, ra = net.addRPCHandler('A', '', {}) vb, rb = net.addRPCHandler('B', '', {}) main_ref = va.makeRef(TestData(24)) public = va.makeRef(main_ref._getFacet('public')) bdata = vb.makeRef(TestData()) bproxy = ra.makeProxy(bdata._path, 'B') # proxy for bdata in A. # To test serialization of the public facet we will # attempt to pass it to the remote object. bproxy.set(public) # NOTE: bdata is not a facet so get/set both work. # Check the proxy to facet-ref arrived OK. self.assertEqual(type(bdata.value), Proxy) # Check it is really a proxy to the facet. self.assertRaises(AttributeError, bproxy.doSet, 25) main_ref.setMaster(True) # switch to master and try again. bproxy.doSet(25) self.assertEqual(bdata.value.get(), 25) # NOTE: that we can't send a facet-ref directly because # that would reveal the slot path of the main object to the # remote user. self.assertRaises(SerializationError, bproxy.set, public._get())
def testCall(self): net = MockNet() nodea, va = net.addRPCHandler('A', '', {}) nodeb, vb = net.addRPCHandler('B', '', {}) nodea['addr'] = TestObject() cb = vb.call('A', 'addr', 'incr', [1]) self.assertEqual(cb.result, 2)
def testWorker(self): net = MockNet() gta = EventletThread() va, ra = net.addRPCHandler('X', '', {}, t_model=gta) gta.start(True) worker = EventletThread() vb, rb = net.addRPCHandler('Y', '', {}, t_model=worker) worker.start() va['data'] = {'name': 'Tom'} pb = rb.makeProxy('data', 'X') self.assertEqual(pb['name'], 'Tom')
def testRPC(self): net = MockNet() ta = net.addNode('browser') tb = net.addNode('server') na = RPCHandler(ta, {}) nb = RPCHandler(tb, {}) oa = Model() na.provide('1', oa) rupdate = makeBoundMethod(nb, {'o':'1', 'm':'update'}) rupdate({'x': '1'}) self.assertEqual(oa.get('x'), '1')
def testRPC(self): net = MockNet() ta = net.addNode('browser') tb = net.addNode('server') na = RPCHandler(ta, {}) nb = RPCHandler(tb, {}) oa = Model() na.provide('1', oa) rupdate = makeBoundMethod(nb, {'o': '1', 'm': 'update'}) rupdate({'x': '1'}) self.assertEqual(oa.get('x'), '1')
def testAddRPCHandler(self): net = MockNet() va, ra = net.addRPCHandler('A', '', {}) vb1, rb1 = net.addRPCHandler('B', '', {}) #vb2, rb2 = net.addRPCHandler('B', '2', {}) va['df'] = {'name': u'Fred'} vb1['db'] = {'name': u'Barney'} p = rb1.makeProxy('df', 'A') p2 = rb1.makeProxy('db', 'B') self.assertEqual(p['name'], u'Fred') self.assertEqual(p2['name'], u'Barney') #self.assertEqual(net.node['A'].getRPCHandlerId('df'), '1') self.assertEqual(type(vb1['db']), dict)
def test(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) nb, vb = net.addRPCHandler('B', '', {}) na['a'] = Time() na['o'] = TestObject() self.assertEqual(na.cache.values(), []) nb['ap'] = vb.makeProxy('a', 'A') nb['op'] = vb.makeProxy('o', 'A') nb['d'] = Data({'name': u'Fred'}) nb['b'] = Time() nb['br'] = nb.getRef('b') b = nb['b'] self.assertEqual(type(b), Time) br = nb['br'] self.assertEqual(type(br), Ref) ap = nb['ap'] self.assertEqual(type(ap), Proxy) d = nb['d'] self.assertEqual(type(d), Data) # This activity is all on node B calling through to node A. op = nb['op'] op.setProxy(ap) # ap is a Proxy self.assertEqual(op.callTime(), 'TestObject.callTime: Tea-time') #op.setProxy(b) # b is a Time capability #op.callTime() op.setProxy(br) # br is a Ref op.callTime() op.setProxy(d) # d is a Data op.setName(u'Barney') self.assertEqual(d['name'], u'Barney') self.assertEqual(_str(b), "Time()") self.assertEqual(_str(ap), "ref(path='a', node='A')") self.assertEqual(_str([(1, 2), {'a': 5}]), "[(1, 2), {'a': 5}]")
def test(self): net = MockNet() e = net.addNode('node') d = Dispatcher(e) hfb = d.addEndpoint('foo/bar') lfb = Listener(hfb) hf = d.addEndpoint('foo') lf = Listener(hf) h = d.addEndpoint('') l = Listener(h) bm_to_fred = {'pcol': 'json', 'message': '{"o": "fred"}'} e.notify('message', bm_to_fred) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, []) self.assertEqual(lfb.received, []) msg_to_ffred = {'pcol': 'serf', 'message': encodes('foo/fred')} e.notify('message', msg_to_ffred) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred]) self.assertEqual(lfb.received, []) msg_to_fbb = {'pcol': 'serf', 'message': encodes('foo/bar/baz')} e.notify('message', msg_to_fbb) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred]) self.assertEqual(lfb.received, [msg_to_fbb]) msg_to_fbark = {'pcol': 'serf', 'message': encodes('foo/bark')} e.notify('message', msg_to_fbark) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred, msg_to_fbark]) self.assertEqual(lfb.received, [msg_to_fbb])
def test(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) nb, vb = net.addRPCHandler('B', '', {}) na['a'] = Time() na['o'] = TestObject() self.assertEqual(na.cache.values(), []) nb['ap'] = vb.makeProxy('a', 'A') nb['op'] = vb.makeProxy('o', 'A') nb['d'] = Data({'name': u'Fred'}) nb['b'] = Time() nb['br'] = nb.getRef('b') b = nb['b'] self.assertEqual(type(b), Time) br = nb['br'] self.assertEqual(type(br), Ref) ap = nb['ap'] self.assertEqual(type(ap), Proxy) d = nb['d'] self.assertEqual(type(d), Data) # This activity is all on node B calling through to node A. op = nb['op'] op.setProxy(ap) # ap is a Proxy self.assertEqual(op.callTime(), 'TestObject.callTime: Tea-time') #op.setProxy(b) # b is a Time capability #op.callTime() op.setProxy(br) # br is a Ref op.callTime() op.setProxy(d) # d is a Data op.setName(u'Barney') self.assertEqual(d['name'], u'Barney') self.assertEqual(_str(b), "Time()") self.assertEqual(_str(ap), "ref(path='a', node='A')") self.assertEqual(_str([(1,2),{'a':5}]), "[(1, 2), {'a': 5}]")
def testWithStorage(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) nb, vb = net.addRPCHandler('B', '', {}) na['TOB'] = Data({}) # store a data object # call it from node b c1 = vb.call('A', 'TOB', '__setitem__', ['foo', 1]) # NOTE: the caches are now WeakValueDictionaries so they # should really be empty and clearing the cache should be # a no-op. In fact it isn't empty, perhaps because the # rather complex Data object has a reference cycle. # simulate restart of node a na.cache.clear() # retrieve saved value from node a c2 = vb.call('A', 'TOB', '__getitem__', ['foo']) self.assertEqual([c1.wait(), c2.wait()], [None, 1])
def testProxy(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) nb, vb = net.addRPCHandler('B', '', {}) ob = TestObject() pr = va.provide('addr', ob) self.assertEqual(pr.incr(1), 2) # remote call increment # wrong argument type returns exception self.assertRaises(TypeError, pr.incr, 'x') # NOTE: attribute error occurs only when called. self.assertRaises(AttributeError, pr.foo) pb = vb.provide('addrb', TestObject()) pr.setProxy(pb) # pass pb to ob. self.assertNotEqual(ob.proxy, None) # get object at pb to increment self.assertEqual(pr.callIncr(2), 3)
def testFuture(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) nb, vb = net.addRPCHandler('B', '', {}) ob = TestObject() pr = va.provide('addr', ob) f = pr.incr_f(1) # remote call increment self.assertEqual(f.wait(), 2) f = pr.incr_f('x') # wrong argument type returns exception self.assertRaises(TypeError, f.wait) f = pr.foo_f() self.assertRaises(AttributeError, f.wait) pb = vb.provide('addrb', TestObject()) pr.setProxy_f(pb) # pass pb to ob. self.assertNotEqual(ob.proxy, None) f = pr.callIncr_f(2) # get object at pb to increment self.assertEqual(f.wait(), 3)
def test(self): net = MockNet() node_a = net.addNode('A') handler = MockHandler(node_a) node_a.send('A', {'foo':'bar'}) self.assertEqual(handler.received, [{'foo':'bar'}])
def testNonexistentNode(self): net = MockNet() na, va = net.addRPCHandler('A', '', {}) cb = va.call('B', 'x', 'method', []) self.assertRaises(KeyError, cb.wait)
def test(self): net = MockNet() va, ra = net.addRPCHandler('A', '', TestFS()) vb, rb = net.addRPCHandler('B', '', TestFS()) ea, eb = va.resources['#env'], vb.resources['#env'] cl = va.makeRef(CallLog(ea)) # We'll use a call log as the replication target. clb = vb.makeRef(CallLog(eb)) p = ra.makeProxy(clb._path, 'B') cl.say('hi') cl.say('bye') r = cl.addReader('1', p, 0) self.assertEqual(cl.end(), 2) self.assertEqual(cl[0], ['say', ['hi']]) self.assertEqual(clb.end(), 0) r.start() self.assertEqual(clb.end(), 2) self.assertEqual(clb[0], ['say', ['hi']]) cl.say('foo') self.assertEqual(clb.end(), 3) self.assertEqual(clb[2], ['say', ['foo']]) net.goOffline('B') cl.say('bar') self.assertEqual(cl.end(), 4) self.assertEqual(clb.end(), 3) net.goOnline('B') self.assertEqual(clb.end(), 4) self.assertEqual(clb[3], ['say', ['bar']]) # Check everything is persistent. del r self.assertEqual(va.cache.values(), []) self.assertEqual(vb.cache.values(), []) cl.say('baz') self.assertEqual(clb.end(), 5) self.assertEqual(clb[4], ['say', ['baz']]) r = cl.getReader('1') r.stop() cl.say('garply') self.assertEqual(clb.end(), 5) net.goOffline('B') r.start() self.assertEqual(clb.end(), 5) net.goOnline('B') self.assertEqual(clb.end(), 6) self.assertEqual(clb[5], ['say', ['garply']]) cl.removeReader('1') cl.say('xyzzy') self.assertEqual(clb.end(), 6) self.assertEqual(cl.end(), 7) r = cl.addReader('2', p, 6) r.start() self.assertEqual(clb.end(), 7)