def setUp(self): """ Create a L{Store} with an L{AMPAvatar} installed on it. """ self.clock = Clock() self.store = Store() self.factory = StubBoxReceiverFactory(store=self.store, protocol=u"bar") self.store.powerUp(self.factory, IBoxReceiverFactory) self.router = Router() self.sender = CollectingSender() self.connector = _RouteConnector(self.clock, self.store, self.router) self.router.startReceivingBoxes(self.sender) self.router.bindRoute(self.connector, None).connectTo(None)
def test_connectRoute(self): """ L{connectRoute} takes an L{AMP}, a L{Router}, an L{IBoxReceiver} and a protocol name and issues a L{Connect} command for that protocol and for a newly created route associated with the given receiver over the L{AMP}. """ commands = [] results = [] class FakeAMP: def callRemote(self, cmd, **kw): commands.append((cmd, kw)) results.append(Deferred()) return results[-1] amp = FakeAMP() sender = CollectingSender() router = Router() router.startReceivingBoxes(sender) receiver = SomeReceiver() protocol = u"proto name" d = connectRoute(amp, router, receiver, protocol) self.assertEqual(commands, [(Connect, { 'origin': u'0', 'protocol': u'proto name' })]) results[0].callback({'route': u'remote route'}) def cbConnected(receiverAgain): self.assertIdentical(receiver, receiverAgain) self.assertTrue(receiver.started) receiver.sender.sendBox({'foo': 'bar'}) self.assertEqual(sender.boxes, [{ _ROUTE: 'remote route', 'foo': 'bar' }]) router.ampBoxReceived({_ROUTE: '0', 'baz': 'quux'}) self.assertEqual(receiver.boxes, [{'baz': 'quux'}]) d.addCallback(cbConnected) return d
def test_connectRoute(self): """ L{connectRoute} takes an L{AMP}, a L{Router}, an L{IBoxReceiver} and a protocol name and issues a L{Connect} command for that protocol and for a newly created route associated with the given receiver over the L{AMP}. """ commands = [] results = [] class FakeAMP: def callRemote(self, cmd, **kw): commands.append((cmd, kw)) results.append(Deferred()) return results[-1] amp = FakeAMP() sender = CollectingSender() router = Router() router.startReceivingBoxes(sender) receiver = SomeReceiver() protocol = u"proto name" d = connectRoute(amp, router, receiver, protocol) self.assertEqual( commands, [(Connect, {'origin': u'0', 'protocol': u'proto name'})]) results[0].callback({'route': u'remote route'}) def cbConnected(receiverAgain): self.assertIdentical(receiver, receiverAgain) self.assertTrue(receiver.started) receiver.sender.sendBox({'foo': 'bar'}) self.assertEqual( sender.boxes, [{_ROUTE: 'remote route', 'foo': 'bar'}]) router.ampBoxReceived({_ROUTE: '0', 'baz': 'quux'}) self.assertEqual(receiver.boxes, [{'baz': 'quux'}]) d.addCallback(cbConnected) return d
def setUp(self): """ Create a L{Store} with an L{AMPAvatar} installed on it. """ self.clock = Clock() self.store = Store() self.factory = StubBoxReceiverFactory( store=self.store, protocol=u"bar") self.store.powerUp(self.factory, IBoxReceiverFactory) self.router = Router() self.sender = CollectingSender() self.connector = _RouteConnector(self.clock, self.store, self.router) self.router.startReceivingBoxes(self.sender) self.router.bindRoute(self.connector, None).connectTo(None)
class RouteConnectorTests(TestCase): """ Tests for L{_RouteConnector}. """ def setUp(self): """ Create a L{Store} with an L{AMPAvatar} installed on it. """ self.clock = Clock() self.store = Store() self.factory = StubBoxReceiverFactory( store=self.store, protocol=u"bar") self.store.powerUp(self.factory, IBoxReceiverFactory) self.router = Router() self.sender = CollectingSender() self.connector = _RouteConnector(self.clock, self.store, self.router) self.router.startReceivingBoxes(self.sender) self.router.bindRoute(self.connector, None).connectTo(None) def test_accept(self): """ L{_RouteConnector.accept} returns a C{dict} with a C{'route'} key associated with a new route identifier which may be used to send AMP boxes to a new instance of the L{IBoxReceiver} indicated by the C{protocol} argument passed to C{connect}. """ firstIdentifier = self.connector.accept( "first origin", u"bar")['route'] firstReceiver = self.factory.receivers.pop() secondIdentifier = self.connector.accept( "second origin", u"bar")['route'] secondReceiver = self.factory.receivers.pop() self.clock.advance(0) self.router.ampBoxReceived( {_ROUTE: firstIdentifier, 'foo': 'bar'}) self.router.ampBoxReceived( {_ROUTE: secondIdentifier, 'baz': 'quux'}) self.assertEqual(firstReceiver.boxes, [{'foo': 'bar'}]) self.assertEqual(secondReceiver.boxes, [{'baz': 'quux'}]) def test_acceptResponseBeforeApplicationBox(self): """ If the protocol L{_RouteConnector.accept} binds to a new route sends a box in its C{startReceivingBoxes} method, that box is sent to the network after the I{Connect} response is sent. """ earlyBox = {'foo': 'bar'} class EarlyReceiver: def startReceivingBoxes(self, sender): sender.sendBox(earlyBox) object.__setattr__(self.factory, 'receiverFactory', EarlyReceiver) self.connector.ampBoxReceived({ COMMAND: Connect.commandName, ASK: 'unique-identifier', 'origin': 'an origin', 'protocol': 'bar'}) self.clock.advance(0) self.assertEqual(len(self.sender.boxes), 2) route, app = self.sender.boxes expectedBox = earlyBox.copy() expectedBox[_ROUTE] = 'an origin' self.assertEqual(app, expectedBox) def test_unknownProtocol(self): """ L{_RouteConnector.accept} raises L{ProtocolUnknown} if passed the name of a protocol for which no factory can be found. """ self.assertRaises( ProtocolUnknown, self.connector.accept, "origin", u"foo") def test_originRoute(self): """ The L{IBoxReceiver}s created by L{_RouteConnector.accept} are started with L{IBoxSender}s which are associated with the origin route specified to C{accept}. """ origin = u'origin route' self.connector.accept(origin, u'bar') self.clock.advance(0) [bar] = self.factory.receivers self.assertTrue(bar.started) bar.sender.sendBox({'foo': 'bar'}) self.assertEqual(self.sender.boxes, [{_ROUTE: origin, 'foo': 'bar'}]) bar.sender.unhandledError(Failure(RuntimeError("test failure"))) self.assertEqual(self.sender.errors, ["test failure"])
class RouteConnectorTests(TestCase): """ Tests for L{_RouteConnector}. """ def setUp(self): """ Create a L{Store} with an L{AMPAvatar} installed on it. """ self.clock = Clock() self.store = Store() self.factory = StubBoxReceiverFactory(store=self.store, protocol=u"bar") self.store.powerUp(self.factory, IBoxReceiverFactory) self.router = Router() self.sender = CollectingSender() self.connector = _RouteConnector(self.clock, self.store, self.router) self.router.startReceivingBoxes(self.sender) self.router.bindRoute(self.connector, None).connectTo(None) def test_accept(self): """ L{_RouteConnector.accept} returns a C{dict} with a C{'route'} key associated with a new route identifier which may be used to send AMP boxes to a new instance of the L{IBoxReceiver} indicated by the C{protocol} argument passed to C{connect}. """ firstIdentifier = self.connector.accept("first origin", u"bar")['route'] firstReceiver = self.factory.receivers.pop() secondIdentifier = self.connector.accept("second origin", u"bar")['route'] secondReceiver = self.factory.receivers.pop() self.clock.advance(0) self.router.ampBoxReceived({_ROUTE: firstIdentifier, 'foo': 'bar'}) self.router.ampBoxReceived({_ROUTE: secondIdentifier, 'baz': 'quux'}) self.assertEqual(firstReceiver.boxes, [{'foo': 'bar'}]) self.assertEqual(secondReceiver.boxes, [{'baz': 'quux'}]) def test_acceptResponseBeforeApplicationBox(self): """ If the protocol L{_RouteConnector.accept} binds to a new route sends a box in its C{startReceivingBoxes} method, that box is sent to the network after the I{Connect} response is sent. """ earlyBox = {'foo': 'bar'} class EarlyReceiver: def startReceivingBoxes(self, sender): sender.sendBox(earlyBox) object.__setattr__(self.factory, 'receiverFactory', EarlyReceiver) self.connector.ampBoxReceived({ COMMAND: Connect.commandName, ASK: 'unique-identifier', 'origin': 'an origin', 'protocol': 'bar' }) self.clock.advance(0) self.assertEqual(len(self.sender.boxes), 2) route, app = self.sender.boxes expectedBox = earlyBox.copy() expectedBox[_ROUTE] = 'an origin' self.assertEqual(app, expectedBox) def test_unknownProtocol(self): """ L{_RouteConnector.accept} raises L{ProtocolUnknown} if passed the name of a protocol for which no factory can be found. """ self.assertRaises(ProtocolUnknown, self.connector.accept, "origin", u"foo") def test_originRoute(self): """ The L{IBoxReceiver}s created by L{_RouteConnector.accept} are started with L{IBoxSender}s which are associated with the origin route specified to C{accept}. """ origin = u'origin route' self.connector.accept(origin, u'bar') self.clock.advance(0) [bar] = self.factory.receivers self.assertTrue(bar.started) bar.sender.sendBox({'foo': 'bar'}) self.assertEqual(self.sender.boxes, [{_ROUTE: origin, 'foo': 'bar'}]) bar.sender.unhandledError(Failure(RuntimeError("test failure"))) self.assertEqual(self.sender.errors, ["test failure"])