class IntrospectorTask(Task): def __init__(self, bus=None): Task.__init__(self, bus) self.p = None self.visualiser_addr = None def start_task(self): from Axon.Introspector import Introspector from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Internet.TCPClient import TCPClient self.p = Pipeline(Introspector(), TCPClient(*self.visualiser_addr)) self.p.activate() def stop_task(self): from Axon.Ipc import shutdownMicroprocess from Kamaelia.Util.OneShot import OneShot o = OneShot(msg=shutdownMicroprocess()) o.link((o, 'outbox'), (self.p, 'control')) o.activate()
class IntrospectorTask(Task): def __init__(self, bus=None): Task.__init__(self, bus) self.p = None self.visualiser_addr = None def start_task(self): from Axon.Introspector import Introspector from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Internet.TCPClient import TCPClient self.p = Pipeline(Introspector(), TCPClient(*self.visualiser_addr)) self.p.activate() def stop_task(self): from Axon.Ipc import shutdownMicroprocess from Kamaelia.Util.OneShot import OneShot o = OneShot(msg=shutdownMicroprocess()) o.link((o, 'outbox'), (self.p, 'control')) o.activate()
class Test_Pipeline(unittest.TestCase): def setup_test(self, *children, **otherargs): self.setup_initialise(*children, **otherargs) self.setup_activate() def setup_initialise(self, *children, **otherargs): self.children = children[:] self.scheduler = scheduler() scheduler.run = self.scheduler self.pipeline = Pipeline(*children, **otherargs) self.inSrc = Dummy() self.inSrc.link((self.inSrc, "outbox"), (self.pipeline, "inbox")) self.inSrc.link((self.inSrc, "signal"), (self.pipeline, "control")) self.outDest = Dummy() self.outDest.link((self.pipeline, "outbox"), (self.outDest, "inbox")) self.outDest.link((self.pipeline, "signal"), (self.outDest, "control")) self.run = self.scheduler.main() def setup_activate(self): self.pipeline.activate(Scheduler=self.scheduler) self.inSrc.activate(Scheduler=self.scheduler) self.outDest.activate(Scheduler=self.scheduler) def sendToInbox(self, data): self.inSrc.send(data, "outbox") def sendToControl(self, data): self.inSrc.send(data, "signal") def dataReadyOutbox(self): return self.outDest.dataReady("inbox") def dataReadySignal(self): return self.outDest.dataReady("control") def recvOutbox(self): return self.outDest.recv("inbox") def recvSignal(self): return self.outDest.recv("control") def collectOutbox(self): out = [] while self.dataReadyOutbox(): out.append(self.recvOutbox()) return out def collectSignal(self): out = [] while self.dataReadySignal(): out.append(self.recvSignal()) return out def runFor(self, cycles): numcycles = cycles * (3 + len(self.children) ) # approx this many components in the system for i in range(0, numcycles): self.run.next() def test_PipelineActivatesChildrenOnlyWhenActivated(self): """Children are activated as soon as the Pipeline itself is activated, but no sooner.""" self.setup_initialise(MockChild(), MockChild(), MockChild()) for child in self.children: self.assert_(not child.wasActivated) self.setup_activate() self.runFor(cycles=1) self.runFor(cycles=3) for child in self.children: self.assert_(child.wasActivated) def test_PipelineTerminatesOnlyWhenAllChildrenHaveTerminated(self): self.setup_test(MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) self.assert_(not self.pipeline._isStopped()) for child in self.children: self.assert_(not self.pipeline._isStopped()) # stop each child one at a time, and check that it is the only thing to # have stopped for child in self.children: # check the pipeline is still running self.assert_(not self.pipeline._isStopped()) child.stopNow() self.runFor(cycles=100) # by now the pipeline should have stopped too self.assert_(self.pipeline._isStopped()) def test_PipelineChildrenWiredOutboxInbox(self): """Pipeline wires up children so one child's "outbox" outbox feeds to the next's "inbox" inbox.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[0].send(MSG, "outbox") for child in self.children[1:-1]: # all except first and last # expect to have received: self.assert_(child.recv("inbox") == MSG) # now send MSG = object() child.send(MSG, "outbox") self.assert_(self.children[-1].recv("inbox") == MSG) def test_PipelineChildrenWiredSignalControl(self): """Pipeline wires up children so one child's "signal" outbox feeds to the next's "control" inbox.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[0].send(MSG, "signal") for child in self.children[1:-1]: # all except first and last # expect to have received: self.assert_(child.recv("control") == MSG) # now send MSG = object() child.send(MSG, "signal") self.assert_(self.children[-1].recv("control") == MSG) def test_PipelineFirstChildWiredToParentInboxes(self): """Pipeline wires up the first child's "inbox" and "control" inboxes to receive from the pipeline's "inbox" and "control" inboxes.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.sendToInbox(MSG) self.assert_(self.children[0].recv("inbox") == MSG) MSG = object() self.sendToControl(MSG) self.assert_(self.children[0].recv("control") == MSG) def test_PipelineLastChildWiredToParentOutboxes(self): """Pipeline wires up the last child's "outbox" and "signal" outboxes to send out of the pipeline's "outbox" and "signal" outboxes.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[-1].send(MSG, "outbox") self.assert_(self.recvOutbox() == MSG) MSG = object() self.children[-1].send(MSG, "signal") self.assert_(self.recvSignal() == MSG)
# self.pause() yield 1 backplane = Backplane("SAMPLE") backplane.activate() producer = Producer() consumer = Consumer() published = PublishTo("SAMPLE") subscribe = SubscribeTo("SAMPLE") pipe1 = Pipeline( producer, published, ) pipe1.activate() pipe2 = Pipeline( subscribe, consumer ) consumer2 = Consumer() consumer2.activate() pipe1.link((pipe1,'signal'),(pipe2,'control')) pipe2.link((pipe2,'signal'),(backplane,'control')) backplane.link((backplane,'signal'),(consumer2,'control')) import threading class A(threading.Thread):
class Test_Pipeline(unittest.TestCase): def setup_test(self, *children, **otherargs): self.setup_initialise(*children,**otherargs) self.setup_activate() def setup_initialise(self,*children,**otherargs): self.children=children[:] self.scheduler = scheduler() scheduler.run = self.scheduler self.pipeline = Pipeline(*children, **otherargs) self.inSrc = Dummy() self.inSrc.link((self.inSrc,"outbox"), (self.pipeline,"inbox")) self.inSrc.link((self.inSrc,"signal"), (self.pipeline,"control")) self.outDest = Dummy() self.outDest.link((self.pipeline,"outbox"), (self.outDest,"inbox")) self.outDest.link((self.pipeline,"signal"), (self.outDest,"control")) self.run = self.scheduler.main() def setup_activate(self): self.pipeline.activate(Scheduler=self.scheduler) self.inSrc.activate(Scheduler=self.scheduler) self.outDest.activate(Scheduler=self.scheduler) def sendToInbox(self,data): self.inSrc.send(data,"outbox") def sendToControl(self,data): self.inSrc.send(data,"signal") def dataReadyOutbox(self): return self.outDest.dataReady("inbox") def dataReadySignal(self): return self.outDest.dataReady("control") def recvOutbox(self): return self.outDest.recv("inbox") def recvSignal(self): return self.outDest.recv("control") def collectOutbox(self): out=[] while self.dataReadyOutbox(): out.append(self.recvOutbox()) return out def collectSignal(self): out=[] while self.dataReadySignal(): out.append(self.recvSignal()) return out def runFor(self, cycles): numcycles=cycles*(3+len(self.children)) # approx this many components in the system for i in range(0,numcycles): self.run.next() def test_PipelineActivatesChildrenOnlyWhenActivated(self): """Children are activated as soon as the Pipeline itself is activated, but no sooner.""" self.setup_initialise(MockChild(), MockChild(), MockChild()) for child in self.children: self.assert_(not child.wasActivated) self.setup_activate() self.runFor(cycles=1) self.runFor(cycles=3) for child in self.children: self.assert_(child.wasActivated) def test_PipelineTerminatesOnlyWhenAllChildrenHaveTerminated(self): self.setup_test(MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) self.assert_(not self.pipeline._isStopped()) for child in self.children: self.assert_(not self.pipeline._isStopped()) # stop each child one at a time, and check that it is the only thing to # have stopped for child in self.children: # check the pipeline is still running self.assert_(not self.pipeline._isStopped()) child.stopNow() self.runFor(cycles=100) # by now the pipeline should have stopped too self.assert_(self.pipeline._isStopped()) def test_PipelineChildrenWiredOutboxInbox(self): """Pipeline wires up children so one child's "outbox" outbox feeds to the next's "inbox" inbox.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[0].send(MSG, "outbox") for child in self.children[1:-1]: # all except first and last # expect to have received: self.assert_(child.recv("inbox") == MSG) # now send MSG = object() child.send(MSG, "outbox") self.assert_(self.children[-1].recv("inbox") == MSG) def test_PipelineChildrenWiredSignalControl(self): """Pipeline wires up children so one child's "signal" outbox feeds to the next's "control" inbox.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[0].send(MSG, "signal") for child in self.children[1:-1]: # all except first and last # expect to have received: self.assert_(child.recv("control") == MSG) # now send MSG = object() child.send(MSG, "signal") self.assert_(self.children[-1].recv("control") == MSG) def test_PipelineFirstChildWiredToParentInboxes(self): """Pipeline wires up the first child's "inbox" and "control" inboxes to receive from the pipeline's "inbox" and "control" inboxes.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.sendToInbox(MSG) self.assert_(self.children[0].recv("inbox")==MSG) MSG = object() self.sendToControl(MSG) self.assert_(self.children[0].recv("control")==MSG) def test_PipelineLastChildWiredToParentOutboxes(self): """Pipeline wires up the last child's "outbox" and "signal" outboxes to send out of the pipeline's "outbox" and "signal" outboxes.""" self.setup_test(MockChild(), MockChild(), MockChild(), MockChild()) self.runFor(cycles=100) MSG = object() self.children[-1].send(MSG,"outbox") self.assert_(self.recvOutbox()==MSG) MSG = object() self.children[-1].send(MSG,"signal") self.assert_(self.recvSignal()==MSG)