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 callRemote(): thread = EventletThread() thread.start() transport = Net('127.0.0.1:6503') vat = RPCHandler('127.0.0.1:6503', '', {}, node=transport, t_model=thread) cb = vat.call('127.0.0.1:6502', 'OBJ', 'fun_b', [5]) print cb.wait()
def testAutoSave(self): store = TestStore() data = Data({}) data.ref = store data['name'] = 'Fred' self.assertEqual(store.count, 0) # no save yet data.save() self.assertEqual(store.count, 1) self.assertEqual(store.value, {'name': 'Fred'}) sync = Synchronous() saver = AutoSave(data, sync) data.change_obs._add(saver) # with a synchronous save, every update is saved immediately data['age'] = 25 self.assertEqual(store.count, 2) self.assertEqual(store.value['age'], 25) data['age'] = 26 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # remove it, back to manual saving. data.change_obs._remove(saver) data['age'] = 27 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # green-threads are more efficient since a save will be done # only once per yield. green = EventletThread() green.start(thread=True) saver = AutoSave(data, green) data.change_obs._add(saver) cb = Callback() def makeUpdates(): data['age'] = 28 data['pet'] = 'Fido' data['address'] = 'Wiltshire' cb.success(None) green.callFromThread(makeUpdates) cb.wait() self.assertEqual(store.count, 4) self.assertEqual(store.value['address'], 'Wiltshire') green.stop()
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 test(self): th = TestHandler() eth = EventletTestHandler() thread = EventletThread() thread.start() with eth.expect(1): thread.callFromThread(eth.handle, 'message', {'message': 'hi'}) with eth.expect(1): thread.callAfter(0, eth.msg, 'foo') cb = thread.makeCallback() thread.callAfter(0, cb.failure, Exception('bar')) self.assertRaises(Exception, cb.wait) thread.stop() thread.start(True) # now in a different thread with th.expect(1): thread.callFromThread(th.handle, 'message', {'message': 'lo'})
store = FSDict('/var/lib/serf/tables') storage = Storage(store) if 'table' not in storage: storage['table'] = Table() TABLE = storage['table'] JC_OPTS = dict(hooks=JC_HOOKS, safe=['serf.tables'], auto_proxy=True) def handle(transport): thread = EventletThread() thread.callFromThread = thread.call handler = RPCHandler(transport, {}, t_model=thread, jc_opts=JC_OPTS) handler.provide('table', TABLE) transport.handle() if __name__ == '__main__': print 'Table Server', SERF_NODE transport = Transport(SERF_NODE) rpc = RPCHandler(transport, storage, thread) rpc.safe.append('serf.tables') thread.start() eventlet.spawn_n(transport.serve) ws_transport = WSTransport(9900, handler=handle) ws_transport.serve() thread.stop() print '\nBye'
class ClientConsole(InteractiveConsole): def raw_input(self, prompt): sys.stdout.write(prompt) sys.stdout.flush() select.select([sys.stdin],[],[]) s = sys.stdin.readline() if not s: raise EOFError() return s.strip() try: # thread.start(True) means start a new thread, while False means # use the current thread. When RUN_CONSOLE is true we run Transport and RPCHandler # calls in the main thread. thread.start(not RUN_CONSOLE) thread.callFromThread(net.listen) thread.callFromThread(net.start) o = s0.getn('o') ro = s0.getn('o-remote') p = s0.getn('printer') server = s0.getn('server') if RUN_CONSOLE: console = ClientConsole(locals()) console.interact() else: o, ro, p, server, s0 = map(wrap, [o, ro, p, server, s0]) except NoSuchName:
class ClientConsole(InteractiveConsole): def raw_input(self, prompt): sys.stdout.write(prompt) sys.stdout.flush() select.select([sys.stdin], [], []) s = sys.stdin.readline() if not s: raise EOFError() return s.strip() try: # thread.start(True) means start a new thread, while False means # use the current thread. When RUN_CONSOLE is true we run Transport and RPCHandler # calls in the main thread. thread.start(not RUN_CONSOLE) thread.callFromThread(net.listen) thread.callFromThread(net.start) o = s0.getn('o') ro = s0.getn('o-remote') p = s0.getn('printer') server = s0.getn('server') if RUN_CONSOLE: console = ClientConsole(locals()) console.interact() else: o, ro, p, server, s0 = map(wrap, [o, ro, p, server, s0]) except NoSuchName:
The 'fred' value is a proxy to a persistent UserCaps object to which we can add 'capabilities', other persistent objects that are available to the 'fred' user for getting things done. """ from serf.rpc_handler import RPCHandler from serf.transport import Transport from serf.eventlet_thread import EventletThread from serf.proxy import Proxy from serf.repl_proxy import REPLProxy SERVER = '127.0.0.1:6508' net = Transport() thread = EventletThread() rpc = RPCHandler(net, {}, t_model=thread) rpc.safe.append('serf.tables') def remote(x): return REPLProxy(rpc.makeProxy(x, SERVER), thread) thread.start(True) thread.callFromThread(net.start) admin = remote('admin') login = remote('login') users = remote('users')