Пример #1
0
    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()
Пример #2
0
    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()
Пример #3
0
    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()
Пример #4
0
    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'})
Пример #5
0
    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'})
Пример #6
0
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'