def test_gotRemoteRoot(self): """ Should add remote to builder and observers, add self to remote builders and observers, start static info fetching. """ hub = Hub() remote = FakeRemoteHub('foo') called = [] def makeFake(name): def f(*args): called.append((name, args)) return f hub.addBuilder = makeFake('addB') hub.addObserver = makeFake('addO') remote.addBuilder = makeFake('r_addB') remote.addObserver = makeFake('r_addO') remote.getStaticInfo = makeFake('r_getStaticInfo') hub.gotRemoteRoot(remote) self.assertEqual(set(called), set([ ('addB', (remote,)), ('addO', (remote,)), ('r_addB', (hub,)), ('r_addO', (hub,)), ('r_getStaticInfo', ()), ])) self.assertEqual(remote.hub, hub)
def test_remote_getUID(self): """ Should just return uid """ h = Hub() h.uid = 'foo' self.assertEqual(h.remote_getUID(), 'foo')
def test_remote_getName(self): """ Should just return the name """ h = Hub() h.name = 'name' self.assertEqual(h.remote_getName(), 'name')
def test_getPBServerFactory(self): """ Should return an instance of pb.PBServerFactory with self passed in. """ h = Hub() f = h.getPBServerFactory() self.assertTrue(isinstance(f, pb.PBServerFactory)) self.assertEqual(f.root, h)
def test_getShellServerFactory(self): """ Should return an instance of hub.shell.ShellFactory """ h = Hub() f = h.getShellServerFactory() self.assertTrue(isinstance(f, ShellFactory)) self.assertEqual(f.hub, h)
def test_buildReceived(self): """ Should just call emit """ h = Hub() called = [] h.emit = called.append h.buildReceived('something') self.assertEqual(called, ['something'])
def test_remote_build(self): """ Should have a remote build that just runs build """ h = Hub() called = [] h.build = called.append o = dict(foo='bar') h.remote_build(o) self.assertEqual(called, [o])
def test_addBuilder(self): """ Should add builder to _builders list """ h = Hub() o = object() h.addBuilder(o) self.assertEqual(h._builders, [o]) h.addBuilder(o) self.assertEqual(h._builders, [o])
def test__build(self): """ Should pass it along to all registered builders. """ class FakeBuilder: def build(self, request): self.called = request b = FakeBuilder() h = Hub() h.addBuilder(b) r = {} h._build(r) self.assertEqual(b.called, r)
def test_remote_remBuilder(self): """ Should wrap the remote then call remBuilder """ h = Hub() h.remoteHubFactory = FakeRemoteHub called = [] h.remBuilder = called.append h.remote_remBuilder('foo') self.assertEqual(len(called), 1) r = called[0] self.assertTrue(isinstance(r, FakeRemoteHub)) self.assertEqual(r.original, 'foo')
def test_remote_remObserver(self): """ Should wrap the remote and call removeObserver """ h = Hub() h.remoteHubFactory = FakeRemoteHub called = [] h.remObserver = called.append h.remote_remObserver('foo') self.assertEqual(len(called), 1) observer = called[0] self.assertTrue(isinstance(observer, FakeRemoteHub)) self.assertEqual(observer.original, 'foo')
def test_stopServer(self): """ You can have many servers running and stop them individually """ h = Hub() h.remote_echo = lambda x: x d = h.startServer(h.getPBServerFactory(), 'tcp:10999') d.addCallback(lambda x: h.startServer(h.getPBServerFactory(), 'tcp:10888')) def killOne(_): return h.stopServer('tcp:10888') d.addCallback(killOne) def testOne(_): # still can connect to the other self.clientPort = None client = clientFromString(reactor, 'tcp:host=127.0.0.1:port=10999') factory = pb.PBClientFactory() d = client.connect(factory) def saveClient(clientPort): self.clientPort = clientPort d.addCallback(saveClient) d.addCallback(lambda ign: factory.getRootObject()) d.addCallback(lambda obj: obj.callRemote('echo', 'foo')) d.addCallback(lambda res: self.assertEqual(res, 'foo')) d.addCallback(lambda ign: self.clientPort.transport.loseConnection()) d.addCallback(lambda ign: h.stopServer('tcp:10999')) return d d.addCallback(testOne) return d
def test_startServer(self): """ Should call twisted.internet.endpoints.serverFromString and hook that up to the factory """ h = Hub() h.remote_echo = lambda x: x h.startServer(h.getPBServerFactory(), 'tcp:10999') # connect to it self.clientPort = None client = clientFromString(reactor, 'tcp:host=127.0.0.1:port=10999') factory = pb.PBClientFactory() d = client.connect(factory) def saveClient(clientPort): self.clientPort = clientPort d.addCallback(saveClient) d.addCallback(lambda ign: factory.getRootObject()) d.addCallback(lambda obj: obj.callRemote('echo', 'foo')) d.addCallback(lambda res: self.assertEqual(res, 'foo')) d.addCallback(lambda ign: self.clientPort.transport.loseConnection()) d.addCallback(lambda ign: h.stopServer('tcp:10999')) return d
def test_connect(self): """ You can connect to other servers. This is functionalish """ server = Hub() client = Hub() called = [] client.remoteHubFactory = lambda x: 'foo' client.gotRemoteRoot = called.append server.startServer(server.getPBServerFactory(), 'tcp:10999') def check(r): self.assertEqual(called, ['foo'], "Should have called .gotRemoteRoot with wrapped remote") return r d = client.connect('tcp:host=127.0.0.1:port=10999') d.addCallback(check) d.addCallback(lambda x: client.disconnect( 'tcp:host=127.0.0.1:port=10999')) d.addCallback(lambda x: server.stopServer('tcp:10999')) return d
def test_remBuilder(self): """ Should remove from the _builders list """ h = Hub() o = object() h._builders = [o] h.remBuilder(o) self.assertEqual(h._builders, []) h.remBuilder(o) self.assertEqual(h._builders, [])
#!/usr/bin/python from twisted.internet import task from twisted.python import log from twisted.internet import reactor import sys log.startLogging(sys.stdout) from simplebb.hub import Hub h = Hub() h.connect('tcp:host=127.0.0.1:port=8100') def monitor(): txt = 'builders: %s, outgoing: %s, servers: %s' % ( len(h._builders), len(h._outgoingConns), len(h._servers)) log.msg(txt) t = task.LoopingCall(monitor) t.start(2) reactor.run()
from twisted.internet import reactor, task, utils from twisted.python import log import sys from simplebb.hub import Hub from simplebb.builder import FileBuilder log.startLogging(sys.stdout) fb = FileBuilder('example/projects') h = Hub() h.addBuilder(fb) h.connect('tcp:host=127.0.0.1:port=9222') h.startServer(h.getShellServerFactory(), 'tcp:9224') reactor.run()
#!/usr/bin/python from twisted.internet import task from twisted.python import log from twisted.internet import reactor import sys log.startLogging(sys.stdout) from simplebb.hub import Hub h = Hub() h.startServer('tcp:8100') def monitor(): txt = 'builders: %s, outgoing: %s, servers: %s' % ( len(h._builders), len(h._outgoingConns), len(h._servers)) log.msg(txt) t = task.LoopingCall(monitor) t.start(2) def ping(): for b in h._builders: b.getStaticInfo() task.LoopingCall(ping).start(2) reactor.run()
from twisted.internet import reactor from twisted.python import log import sys from simplebb.hub import Hub from simplebb.builder import FileBuilder log.startLogging(sys.stdout) fb = FileBuilder('example/projects') h = Hub() h.addBuilder(fb) h.startServer(h.getPBServerFactory(), 'tcp:9222') h.startServer(h.getShellServerFactory(), 'tcp:9223') reactor.run()