def test_addOutbox(self): """addOutbox - adds a new outbox with the specified name. Component can then send to that inbox.""" class T(threadedadaptivecommscomponent): def __init__(self, dst): super(T, self).__init__() self.toTestCase = queue.Queue() self.fromTestCase = queue.Queue() self.dst = dst def main(self): try: boxname = self.addOutbox("newbox") self.link((self, boxname), self.dst) msg = self.fromTestCase.get() self.send(msg, boxname) self.toTestCase.put((False, msg)) # except Exception, e: except Exception: e = sys.exc_info()[1] self.toTestCase.put( (True, str(e.__clas__.__name__) + str(e.args))) return class Recv(component): def __init__(self): super(Recv, self).__init__() self.rec = [] def main(self): while 1: yield 1 if self.dataReady("inbox"): self.rec.append(self.recv("inbox")) sched = scheduler() r = Recv().activate(Scheduler=sched) t = T((r, "inbox")).activate(Scheduler=sched) t.fromTestCase.put("hello") while not t.toTestCase.qsize(): next(t) next(t) (err, msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:" + str(msg)) try: next(t) except StopIteration: pass try: next(r) next(r) except StopIteration: pass self.assert_( r.rec == ["hello"], "Data send through outbox corrupted; r.rec = " + str(r.rec))
def test_localprocessterminatesifInQueueFull(self): """threadedcomponent terminates when the thread terminates, even if data is clogged in one of the inqueues""" class Test(threadedcomponent): def __init__(self): super(Test, self).__init__(queuelengths=5) def main(self): while not self.dataReady("control"): pass sched = scheduler() t = Test().activate(Scheduler=sched) for i in range(0, 10): t._deliver( object(), "inbox" ) # fill the inbox with more data than the internal queues can hold t._deliver(object(), "control") n = 50 for s in sched.main(): time.sleep(0.05) n = n - 1 self.assert_(n > 0, "Thread (and scheduler) should have stopped by now")
def test_flow_out(self): """main() - can send data to the component's outbox(es) using the standard send() method.""" class ThreadedSender(threadedcomponent): def __init__(self, msg): super(ThreadedSender, self).__init__() self.msg = msg def main(self): self.send(self.msg) msg = "hello there!" sched = scheduler() t = ThreadedSender(msg).activate(Scheduler=sched) r = RecvFrom((t, "outbox")).activate(Scheduler=sched) for i in range(10): time.sleep(0.1) try: next(t) except StopIteration: pass try: next(r) except StopIteration: pass self.assert_(r.rec == [msg])
def setup_test(self): sample_rate = 10.0 read_threshold = 1.0 buffer_limit = 2.0 read_interval = 0.2 self.limitsize = int(sample_rate * buffer_limit) self.threshsize = int(sample_rate * read_threshold) self.chunksize = int(sample_rate * read_interval) self.read_interval = read_interval self.read_threshold = read_threshold self.readintervalsize = int(sample_rate * read_interval) # make a fake timer, and plumb it in clock = FakeTime(0.0) RawAudioMixer._time = clock # make rawaudiomixer, and hack its pause() functionality, to have zero timeout r = RawAudioMixer.RawAudioMixer( sample_rate, 1, "S16_LE", read_threshold, buffer_limit, read_interval) r.pause = lambda timeout=0.0, oldpause=r.pause : oldpause(0.0) self.rawaudiomixer = r self.clock = clock self.scheduler = scheduler() r.activate(Scheduler=self.scheduler) self.sink = Sponge().activate(Scheduler=self.scheduler) self.sink.link((r,"outbox"),(self.sink,"inbox")) self.sink.link((r,"signal"),(self.sink,"control")) self.run = self.scheduler.main()
def setup_test(self): sample_rate = 10.0 read_threshold = 1.0 buffer_limit = 2.0 read_interval = 0.2 self.limitsize = int(sample_rate * buffer_limit) self.threshsize = int(sample_rate * read_threshold) self.chunksize = int(sample_rate * read_interval) self.read_interval = read_interval self.read_threshold = read_threshold self.readintervalsize = int(sample_rate * read_interval) # make a fake timer, and plumb it in clock = FakeTime(0.0) RawAudioMixer._time = clock # make rawaudiomixer, and hack its pause() functionality, to have zero timeout r = RawAudioMixer.RawAudioMixer(sample_rate, 1, "S16_LE", read_threshold, buffer_limit, read_interval) r.pause = lambda timeout=0.0, oldpause=r.pause: oldpause(0.0) self.rawaudiomixer = r self.clock = clock self.scheduler = scheduler() r.activate(Scheduler=self.scheduler) self.sink = Sponge().activate(Scheduler=self.scheduler) self.sink.link((r, "outbox"), (self.sink, "inbox")) self.sink.link((r, "signal"), (self.sink, "control")) self.run = self.scheduler.main()
def setup_test(self, **args): self.children=[] self.scheduler = scheduler() scheduler.run = self.scheduler if "componentFactory" not in args: def defaultFactory(arg,parent=self): child = MockChild(self.children, arg) return child args["componentFactory"]=defaultFactory self.carousel = Carousel(**args) self.inSrc = Dummy() self.inSrc.link((self.inSrc,"outbox"), (self.carousel,"inbox")) self.inSrc.link((self.inSrc,"signal"), (self.carousel,"control")) self.outDest = Dummy() self.outDest.link((self.carousel,"outbox"), (self.outDest,"inbox")) self.outDest.link((self.carousel,"signal"), (self.outDest,"control")) self.nextFbk = Dummy() self.nextFbk.link((self.carousel,"requestNext"), (self.nextFbk,"inbox")) self.nextFbk.link((self.nextFbk,"outbox"), (self.carousel,"next")) self.carousel.activate(Scheduler=self.scheduler) self.inSrc.activate(Scheduler=self.scheduler) self.outDest.activate(Scheduler=self.scheduler) self.nextFbk.activate(Scheduler=self.scheduler) self.run = self.scheduler.main()
def test_NoSpaceInBoxExceptionsReachThread(self): """If a threadedcomponent outbox is linked to a size restricted inbox, then the thread can send at most inbox_size+internal_queue_size messages before it receives a noSpaceInBox exception.""" QSIZE=20 BSIZE=10 sched=scheduler() t=ThreadedSender(QSIZE,slow=0.05) d=DoesNothingComponent().activate(Scheduler=sched) d.inboxes['inbox'].setSize(BSIZE) d.link((t,"outbox"),(d,"inbox")) s=sched.main() for _ in range(0,10): next(s) t.activate(Scheduler=sched) try: t.howManyToSend.put(QSIZE+BSIZE+10) while t.feedback.qsize() == 0: next(s) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == QSIZE+BSIZE) except: t.howManyToSend.put("STOP") raise
def test_NoSpaceInBoxExceptionsReachThread(self): """If a threadedcomponent outbox is linked to a size restricted inbox, then the thread can send at most inbox_size+internal_queue_size messages before it receives a noSpaceInBox exception.""" QSIZE = 20 BSIZE = 10 sched = scheduler() t = ThreadedSender(QSIZE, slow=0.05) d = DoesNothingComponent().activate(Scheduler=sched) d.inboxes['inbox'].setSize(BSIZE) d.link((t, "outbox"), (d, "inbox")) s = sched.main() for _ in range(0, 10): next(s) t.activate(Scheduler=sched) try: t.howManyToSend.put(QSIZE + BSIZE + 10) while t.feedback.qsize() == 0: next(s) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == QSIZE + BSIZE) except: t.howManyToSend.put("STOP") raise
def setup_test(self, **args): self.children = [] self.scheduler = scheduler() scheduler.run = self.scheduler if "componentFactory" not in args: def defaultFactory(arg, parent=self): child = MockChild(self.children, arg) return child args["componentFactory"] = defaultFactory self.carousel = Carousel(**args) self.inSrc = Dummy() self.inSrc.link((self.inSrc, "outbox"), (self.carousel, "inbox")) self.inSrc.link((self.inSrc, "signal"), (self.carousel, "control")) self.outDest = Dummy() self.outDest.link((self.carousel, "outbox"), (self.outDest, "inbox")) self.outDest.link((self.carousel, "signal"), (self.outDest, "control")) self.nextFbk = Dummy() self.nextFbk.link((self.carousel, "requestNext"), (self.nextFbk, "inbox")) self.nextFbk.link((self.nextFbk, "outbox"), (self.carousel, "next")) self.carousel.activate(Scheduler=self.scheduler) self.inSrc.activate(Scheduler=self.scheduler) self.outDest.activate(Scheduler=self.scheduler) self.nextFbk.activate(Scheduler=self.scheduler) self.run = self.scheduler.main()
def run(self): if self.zap: X = scheduler() scheduler.run = X cat.coordinatingassistanttracker.basecat.zap() scheduler.run.waitForOne() scheduler.run.runThreads(slowmo = self.slowmo) background.lock.release()
def run(self): if self.zap: X = scheduler() scheduler.run = X cat.coordinatingassistanttracker.basecat.zap() scheduler.run.waitForOne() scheduler.run.runThreads(slowmo=self.slowmo) background.lock.release()
def test_addInbox(self): """addInbox - adds a new inbox with the specified name. Component can then receive from that inbox.""" class T(threadedadaptivecommscomponent): def __init__(self): super(T, self).__init__() self.toTestCase = queue.Queue() self.fromTestCase = queue.Queue() def main(self): try: boxname = self.addInbox("newbox") self.toTestCase.put((False, boxname)) self.fromTestCase.get() if not self.dataReady(boxname): self.toTestCase.put( (True, "Data should have been ready at the new inbox")) return self.toTestCase.put((False, self.recv(boxname))) # except Exception, e: except Exception: e = sys.exc_info()[1] self.toTestCase.put( (True, str(e.__clas__.__name__) + str(e.args))) return sched = scheduler() t = T().activate(Scheduler=sched) timeout = 10 next(t) while t.toTestCase.empty(): next(t) timeout = timeout - 1 time.sleep(0.05) self.assert_(timeout, "timed out") (err, msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:" + str(msg)) boxname = msg t._deliver("hello", boxname) try: next(t) next(t) except StopIteration: pass t.fromTestCase.put(1) (err, msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:" + str(msg)) self.assert_(msg == "hello", "Data send through inbox corrupted, received:" + str(msg))
def test_threadisseparate(self): """main() -runs in a separate thread of execution""" class Test(threadedcomponent): def __init__(self): super(Test,self).__init__() self.threadid = queue.Queue() def main(self): self.threadid.put( thread.get_ident() ) sched=scheduler() t=Test().activate(Scheduler=sched) next(t) # get the thread started self.assert_(t.threadid.get() != thread.get_ident(), "main() returns a different value for thread.get_ident()")
def test_localprocessterminates(self): """_localmain() microprocess also terminates when the thread terminates""" class Test(threadedcomponent): def main(self): pass sched=scheduler() t=Test().activate(Scheduler=sched) n=10 for s in sched.main(): time.sleep(0.05) n=n-1 self.assert_(n>0, "Thread (and scheduler) should have stopped by now")
def test_addOutbox(self): """addOutbox - adds a new outbox with the specified name. Component can then send to that inbox.""" class T(threadedadaptivecommscomponent): def __init__(self,dst): super(T,self).__init__() self.toTestCase = queue.Queue() self.fromTestCase = queue.Queue() self.dst = dst def main(self): try: boxname=self.addOutbox("newbox") self.link( (self,boxname), self.dst ) msg = self.fromTestCase.get() self.send(msg,boxname) self.toTestCase.put( (False, msg) ) # except Exception, e: except Exception: e = sys.exc_info()[1] self.toTestCase.put( (True, str(e.__clas__.__name__) + str(e.args)) ) return class Recv(component): def __init__(self): super(Recv,self).__init__() self.rec = [] def main(self): while 1: yield 1 if self.dataReady("inbox"): self.rec.append(self.recv("inbox")) sched=scheduler() r=Recv().activate(Scheduler=sched) t=T( (r,"inbox") ).activate(Scheduler=sched) t.fromTestCase.put("hello") while not t.toTestCase.qsize(): next(t) next(t) (err,msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:"+str(msg)) try: next(t) except StopIteration: pass try: next(r) next(r) except StopIteration: pass self.assert_(r.rec == ["hello"], "Data send through outbox corrupted; r.rec = "+str(r.rec))
def test_localprocessterminatesOnlyIfOutqueueFlushed(self): """threadedcomponent ensures that if the thread terminates, any messages still pending in outqueues (waiting to be sent out of outboxes) get sent, even if it is held up for a while by noSpaceInBox exceptions""" class Test(threadedcomponent): def __init__(self): super(Test, self).__init__(queuelengths=5) def main(self): self.count = 0 while 1: try: self.send(object(), "outbox") self.count = self.count + 1 except noSpaceInBox: # outqueue is clearly full now, so lets terminate quick! return sched = scheduler() t = Test() r = DoesNothingComponent() r.link((t, "outbox"), (r, "inbox")) r.inboxes["inbox"].setSize(1) r.activate(Scheduler=sched) t.activate(Scheduler=sched) s = sched.main() for n in range(0, 50): time.sleep(0.05) next(s) self.assert_(not t._isStopped(), "Thread component should not have finished yet") self.assert_(r.dataReady("inbox"), "Should be data waiting at the receiver's inbox") # now relax the inbox size restriction and start receiving items r.inboxes["inbox"].setSize(999) r.recv("inbox") count = 1 for _ in range(0, 50): time.sleep(0.05) next(s) # should expect to have received all t.count items sent while r.dataReady("inbox"): count = count + 1 r.recv("inbox") self.assert_(count == t.count) self.assert_(t._isStopped(), "Thread component should have finished by now")
def test_localprocessterminates(self): """_localmain() microprocess also terminates when the thread terminates""" class Test(threadedcomponent): def main(self): pass sched = scheduler() t = Test().activate(Scheduler=sched) n = 10 for s in sched.main(): time.sleep(0.05) n = n - 1 self.assert_(n > 0, "Thread (and scheduler) should have stopped by now")
def run(self): if self.zap: # print ("zapping", scheduler.run.threads) X = scheduler() scheduler.run = X # print ("zapped", scheduler.run.threads) cat.coordinatingassistanttracker.basecat.zap() # print ("Here? (run)") dummyComponent().activate() # to keep the scheduler from exiting immediately. # print ("zoiped", scheduler.run.threads) # TODO - what happens if the foreground calls scheduler.run.runThreads() ? We should stop this from happening. scheduler.run.runThreads(slowmo = self.slowmo) # print ("There?") background.lock.release()
def test_threadisseparate(self): """main() -runs in a separate thread of execution""" class Test(threadedcomponent): def __init__(self): super(Test, self).__init__() self.threadid = queue.Queue() def main(self): self.threadid.put(thread.get_ident()) sched = scheduler() t = Test().activate(Scheduler=sched) next(t) # get the thread started self.assert_( t.threadid.get() != thread.get_ident(), "main() returns a different value for thread.get_ident()")
def test_localprocessterminatesOnlyIfOutqueueFlushed(self): """threadedcomponent ensures that if the thread terminates, any messages still pending in outqueues (waiting to be sent out of outboxes) get sent, even if it is held up for a while by noSpaceInBox exceptions""" class Test(threadedcomponent): def __init__(self): super(Test,self).__init__(queuelengths=5) def main(self): self.count=0 while 1: try: self.send(object(),"outbox") self.count=self.count+1 except noSpaceInBox: # outqueue is clearly full now, so lets terminate quick! return sched=scheduler() t=Test() r=DoesNothingComponent() r.link((t,"outbox"),(r,"inbox")) r.inboxes["inbox"].setSize(1) r.activate(Scheduler=sched) t.activate(Scheduler=sched) s=sched.main() for n in range(0,50): time.sleep(0.05) next(s) self.assert_(not t._isStopped(), "Thread component should not have finished yet") self.assert_(r.dataReady("inbox"), "Should be data waiting at the receiver's inbox") # now relax the inbox size restriction and start receiving items r.inboxes["inbox"].setSize(999) r.recv("inbox") count=1 for _ in range(0,50): time.sleep(0.05) next(s) # should expect to have received all t.count items sent while r.dataReady("inbox"): count=count+1 r.recv("inbox") self.assert_(count==t.count) self.assert_(t._isStopped(), "Thread component should have finished by now")
def setup_initialise(self): self.scheduler = scheduler() scheduler.run = self.scheduler self.rpsit = ReassemblePSITables() self.inSrc = Dummy() self.inSrc.link((self.inSrc,"outbox"), (self.rpsit ,"inbox")) self.inSrc.link((self.inSrc,"signal"), (self.rpsit ,"control")) self.outDest = Dummy() self.outDest.link((self.rpsit ,"outbox"), (self.outDest,"inbox")) self.outDest.link((self.rpsit ,"signal"), (self.outDest,"control")) self.children=[ self.inSrc, self.rpsit, self.outDest ] self.run = self.scheduler.main()
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_initialise(self): self.scheduler = scheduler() scheduler.run = self.scheduler self.rpsit = ReassemblePSITables() self.inSrc = Dummy() self.inSrc.link((self.inSrc, "outbox"), (self.rpsit, "inbox")) self.inSrc.link((self.inSrc, "signal"), (self.rpsit, "control")) self.outDest = Dummy() self.outDest.link((self.rpsit, "outbox"), (self.outDest, "inbox")) self.outDest.link((self.rpsit, "signal"), (self.outDest, "control")) self.children = [self.inSrc, self.rpsit, self.outDest] self.run = self.scheduler.main()
def test_addInbox(self): """addInbox - adds a new inbox with the specified name. Component can then receive from that inbox.""" class T(threadedadaptivecommscomponent): def __init__(self): super(T,self).__init__() self.toTestCase = queue.Queue() self.fromTestCase = queue.Queue() def main(self): try: boxname=self.addInbox("newbox") self.toTestCase.put( (False,boxname) ) self.fromTestCase.get() if not self.dataReady(boxname): self.toTestCase.put( (True,"Data should have been ready at the new inbox") ) return self.toTestCase.put( (False,self.recv(boxname)) ) # except Exception, e: except Exception: e = sys.exc_info()[1] self.toTestCase.put( (True, str(e.__clas__.__name__) + str(e.args)) ) return sched=scheduler() t=T().activate(Scheduler=sched) timeout=10 next(t) while t.toTestCase.empty(): next(t) timeout=timeout-1 time.sleep(0.05) self.assert_(timeout,"timed out") (err,msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:"+str(msg)) boxname=msg t._deliver("hello",boxname) try: next(t) next(t) except StopIteration: pass t.fromTestCase.put(1) (err,msg) = t.toTestCase.get() self.assert_(not err, "Error in thread:"+str(msg)) self.assert_(msg=="hello", "Data send through inbox corrupted, received:"+str(msg))
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 test_RestrictedInboxSize(self): """Setting the inbox size means at most inbox_size+internal_queue_size messages can queue up before the sender receives a noSpaceInBox exception""" QSIZE=20 BSIZE=10 sched=scheduler() t=DoesNothingThread(QSIZE).activate(Scheduler=sched) t.inboxes['inbox'].setSize(BSIZE) d=DoesNothingComponent().activate(Scheduler=sched) d.link((d,"outbox"),(t,"inbox")) s=sched.main() for _ in range(0,10): next(s) for _ in range(QSIZE+BSIZE): d.send(object(),"outbox") for __ in range(0,10): next(s) self.failUnlessRaises(noSpaceInBox, d.send, object(), "outbox")
def test_RestrictedInboxSize(self): """Setting the inbox size means at most inbox_size+internal_queue_size messages can queue up before the sender receives a noSpaceInBox exception""" QSIZE = 20 BSIZE = 10 sched = scheduler() t = DoesNothingThread(QSIZE).activate(Scheduler=sched) t.inboxes['inbox'].setSize(BSIZE) d = DoesNothingComponent().activate(Scheduler=sched) d.link((d, "outbox"), (t, "inbox")) s = sched.main() for _ in range(0, 10): next(s) for _ in range(QSIZE + BSIZE): d.send(object(), "outbox") for __ in range(0, 10): next(s) self.failUnlessRaises(noSpaceInBox, d.send, object(), "outbox")
def test_localprocessterminatesifInQueueFull(self): """threadedcomponent terminates when the thread terminates, even if data is clogged in one of the inqueues""" class Test(threadedcomponent): def __init__(self): super(Test,self).__init__(queuelengths=5) def main(self): while not self.dataReady("control"): pass sched=scheduler() t=Test().activate(Scheduler=sched) for i in range(0,10): t._deliver(object(),"inbox") # fill the inbox with more data than the internal queues can hold t._deliver(object(),"control") n=50 for s in sched.main(): time.sleep(0.05) n=n-1 self.assert_(n>0, "Thread (and scheduler) should have stopped by now")
def test_flow_in(self): """main() - can receive data sent to the component's inbox(es) using the standard dataReady() and recv() methods.""" class ThreadedReceiver(threadedcomponent): def __init__(self): super(ThreadedReceiver,self).__init__() self.rec = [] def main(self): while 1: if self.dataReady("inbox"): self.rec.append(self.recv("inbox")) sched=scheduler() r = ThreadedReceiver().activate(Scheduler=sched) msg = "hello!" o=OneShotTo( (r,"inbox"), msg).activate(Scheduler=sched) next(r) next(o) next(r) time.sleep(0.1) next(r) self.assert_(r.rec==[msg])
def test_flow_in(self): """main() - can receive data sent to the component's inbox(es) using the standard dataReady() and recv() methods.""" class ThreadedReceiver(threadedcomponent): def __init__(self): super(ThreadedReceiver, self).__init__() self.rec = [] def main(self): while 1: if self.dataReady("inbox"): self.rec.append(self.recv("inbox")) sched = scheduler() r = ThreadedReceiver().activate(Scheduler=sched) msg = "hello!" o = OneShotTo((r, "inbox"), msg).activate(Scheduler=sched) next(r) next(o) next(r) time.sleep(0.1) next(r) self.assert_(r.rec == [msg])
def test_ThereIsADefaultOutgoingQueueSize(self): """There is a default limit on the number of messages that can queue up waiting to be sent out by the main thread.""" sched=scheduler() t=ThreadedSender().activate(Scheduler=sched) try: s=sched.main() for _ in range(0,10): next(s) t.howManyToSend.put(99999) while t.feedback.qsize() == 0: time.sleep(0.1) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result > 0) except: t.howManyToSend.put("STOP") raise
def test_ThereIsADefaultOutgoingQueueSize(self): """There is a default limit on the number of messages that can queue up waiting to be sent out by the main thread.""" sched = scheduler() t = ThreadedSender().activate(Scheduler=sched) try: s = sched.main() for _ in range(0, 10): next(s) t.howManyToSend.put(99999) while t.feedback.qsize() == 0: time.sleep(0.1) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result > 0) except: t.howManyToSend.put("STOP") raise
def test_CanSetOutgoingQueueSize(self): """Setting the queue size in the initializer limits the number of messages that can queue up waiting to be sent out by the main thread.""" QSIZE = 20 sched = scheduler() t = ThreadedSender(QSIZE).activate(Scheduler=sched) try: s = sched.main() for _ in range(0, 10): next(s) t.howManyToSend.put(99999) while t.feedback.qsize() == 0: time.sleep(0.1) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == QSIZE) except: t.howManyToSend.put("STOP") raise
def test_CanSetOutgoingQueueSize(self): """Setting the queue size in the initializer limits the number of messages that can queue up waiting to be sent out by the main thread.""" QSIZE=20 sched=scheduler() t=ThreadedSender(QSIZE).activate(Scheduler=sched) try: s=sched.main() for _ in range(0,10): next(s) t.howManyToSend.put(99999) while t.feedback.qsize() == 0: time.sleep(0.1) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result==QSIZE) except: t.howManyToSend.put("STOP") raise
def setup_initialise(self, *listargs, **dictargs): self.children = {} for key in dictargs.keys(): if key != "linkages": self.children[key] = dictargs[key] self.scheduler = scheduler() scheduler.run = self.scheduler self.graphline = Graphline(*listargs, **dictargs) self.inSrc = {} for box in ["inbox", "control"]: c = Dummy() c.link((c, "outbox"), (self.graphline, box)) self.inSrc[box] = c self.outDest = {} for box in ["outbox", "signal"]: c = Dummy() c.link((self.graphline, box), (c, "inbox")) self.outDest[box] = c self.run = self.scheduler.main()
def setup_initialise(self,*listargs,**dictargs): self.children = {} for key in dictargs.keys(): if key != "linkages": self.children[key] = dictargs[key] self.scheduler = scheduler() scheduler.run = self.scheduler self.graphline = Graphline(*listargs, **dictargs) self.inSrc = {} for box in ["inbox","control"]: c = Dummy() c.link((c,"outbox"), (self.graphline,box)) self.inSrc[box]=c self.outDest = {} for box in ["outbox","signal"]: c = Dummy() c.link((self.graphline,box), (c,"inbox")) self.outDest[box]=c self.run = self.scheduler.main()
def test_flow_out(self): """main() - can send data to the component's outbox(es) using the standard send() method.""" class ThreadedSender(threadedcomponent): def __init__(self,msg): super(ThreadedSender,self).__init__() self.msg=msg def main(self): self.send(self.msg) msg="hello there!" sched = scheduler() t = ThreadedSender(msg).activate(Scheduler=sched) r = RecvFrom( (t,"outbox") ).activate(Scheduler=sched) for i in range(10): time.sleep(0.1) try: next(t) except StopIteration: pass try: next(r) except StopIteration: pass self.assert_(r.rec == [msg])
def producersConsumersSystemTest(): class Producer(component): Inboxes = [] Outboxes = ["result"] def __init__(self): super(producersConsumersSystemTest, self).__init__() def main(self): i = 100 while (i): i = i - 1 self.send("hello", "result") yield 1 class Consumer(component): Inboxes = ["source"] Outboxes = ["result"] def __init__(self): super(Consumer, self).__init__() self.count = 0 self.i = 30 def doSomething(self): print(self.name, "Woo", self.i) if self.dataReady("source"): self.recv("source") self.count = self.count + 1 self.send(self.count, "result") def main(self): yield 1 while (self.i): self.i = self.i - 1 self.doSomething() yield 1 class testComponent(component): Inboxes = ["_output"] Outboxes = ["output"] def __init__(self): super(testComponent, self).__init__() self.lackofinterestingthingscount = 0 self.total = 0 self.producer, self.consumer = Producer(), Consumer() self.producer2, self.consumer2 = Producer(), Consumer() self.addChildren(self.producer, self.producer2, self.consumer, self.consumer2) self.link((self.producer, "result"), (self.consumer, "source")) linkage(self.producer2, self.consumer2, "result", "source", self.postoffice) linkage(self.consumer, self, "result", "_output", self.postoffice) linkage(self.consumer2, self, "result", "_output", self.postoffice) def childComponents(self): return [ self.producer, self.consumer, self.producer2, self.consumer2 ] def mainBody(self): if len(self.inboxes["_output"]) > 0: result = self.recv("_output") self.total = self.total + result print("Result recieved from consumer : ", result, "!") print("New Total : ", self.total, "!") else: self.lackofinterestingthingscount = self.lackofinterestingthingscount + 1 if self.lackofinterestingthingscount > 2: print("Exiting. Nothing interesting for ", self.lackofinterestingthingscount, " iterations") return 0 return 1 r = scheduler() p = testComponent() children = p.childComponents() p.activate() for p in children: p.activate() scheduler.run.runThreads(slowmo=0) # context = r.runThreads()
def producersConsumersSystemTest(): class Producer(component): Inboxes=[] Outboxes=["result"] def __init__(self): super(producersConsumersSystemTest, self).__init__() def main(self): i = 100 while(i): i = i -1 self.send("hello", "result") yield 1 class Consumer(component): Inboxes=["source"] Outboxes=["result"] def __init__(self): super(Consumer, self).__init__() self.count = 0 self.i = 30 def doSomething(self): print(self.name, "Woo",self.i) if self.dataReady("source"): self.recv("source") self.count = self.count +1 self.send(self.count, "result") def main(self): yield 1 while(self.i): self.i = self.i -1 self.doSomething() yield 1 class testComponent(component): Inboxes=["_output"] Outboxes=["output"] def __init__(self): super(testComponent, self).__init__() self.lackofinterestingthingscount = 0 self.total = 0 self.producer, self.consumer =Producer(), Consumer() self.producer2, self.consumer2 = Producer(), Consumer() self.addChildren(self.producer, self.producer2, self.consumer, self.consumer2) self.link((self.producer, "result"), (self.consumer, "source")) linkage(self.producer2, self.consumer2, "result", "source", self.postoffice) linkage(self.consumer,self,"result","_output", self.postoffice) linkage(self.consumer2,self,"result","_output", self.postoffice) def childComponents(self): return [self.producer, self.consumer,self.producer2, self.consumer2] def mainBody(self): if len(self.inboxes["_output"]) > 0: result = self.recv("_output") self.total = self.total + result print("Result recieved from consumer : ", result, "!") print("New Total : ", self.total, "!") else: self.lackofinterestingthingscount = self.lackofinterestingthingscount +1 if self.lackofinterestingthingscount > 2: print("Exiting. Nothing interesting for ", self.lackofinterestingthingscount, " iterations") return 0 return 1 r = scheduler() p = testComponent() children = p.childComponents() p.activate() for p in children: p.activate() scheduler.run.runThreads(slowmo=0)# context = r.runThreads()
def test_linksafe(self): """link() unlink() - thread safe when called. The postoffice link() and unlink() methods are not expected to be capable of re-entrant use.""" class ThreadedLinker(threadedcomponent): def main(self): for i in range(10): linkage = self.link((self, "outbox"), (self, "inbox")) self.unlink(thelinkage=linkage) sched = scheduler() t = ThreadedLinker().activate(Scheduler=sched) oldlink = t.postoffice.link oldunlink = t.postoffice.unlink safetycheck = threading.RLock() # re-entrancy permitting mutex failures = queue.Queue() def link_mock(*argL, **argD): # wrapper for postoffice.link() method if not safetycheck.acquire( False ): # returns False if should block (meaning its not thread safe!) failures.put(".link()") return False else: result = oldlink(*argL, **argD) time.sleep(0.05) safetycheck.release() return result def unlink_mock(*argL, **argD): if not safetycheck.acquire( False ): # returns False if should block (meaning its not thread safe!) failures.put(".unlink()") return False else: result = oldunlink(*argL, **argD) time.sleep(0.05) safetycheck.release() return result t.postoffice.link = link_mock t.postoffice.unlink = unlink_mock done = False for i in range(10): try: next(t) except StopIteration: done = True linkage = t.link((t, "signal"), (t, "control")) t.unlink(thelinkage=linkage) while not done: try: next(t) except StopIteration: done = True if failures.qsize(): failed = {} while failures.qsize(): failed[failures.get()] = 1 conj = "" errmsg = "threadedcomponent,postoffice" for method in failed.keys(): errmsg = errmsg + conj + method conj = " and " errmsg = errmsg + " should not be entered by more than one thread at once." self.fail(errmsg)
def initComponents(self,qty): scheduler.run = scheduler() self.schedthread = scheduler.run.main() return self.makeComponents(qty)
def test_TakingFromDestinationAllowsMoreToBeDelivered(self): """""" QSIZE = 20 BSIZE = 10 self.assert_(QSIZE > BSIZE) sched = scheduler() t = ThreadedSender(QSIZE) d = DoesNothingComponent().activate(Scheduler=sched) d.inboxes['inbox'].setSize(BSIZE) d.link((t, "outbox"), (d, "inbox")) s = sched.main() for _ in range(10): next(s) t.activate(Scheduler=sched) try: for _ in range(10): next(s) t.howManyToSend.put(QSIZE + BSIZE + 10) # wait for a response, verify it filled its in queue result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == QSIZE) # flush them through to the inbox queue of the destination for _ in range(BSIZE * 5): next(s) # let the thread fill the newly free slots t.howManyToSend.put(QSIZE + BSIZE + 10) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == BSIZE) # collect messages NUM_COLLECT = 0 while NUM_COLLECT < BSIZE / 2: while not d.dataReady("inbox"): next(s) if d.dataReady("inbox"): d.recv("inbox") NUM_COLLECT += 1 # let the main thread flush some message through from the thread for _ in range(50): next(s) t.howManyToSend.put(QSIZE + BSIZE + 10) while t.feedback.qsize() == 0: next(s) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == NUM_COLLECT) except: t.howManyToSend.put("STOP") raise
def test_TakingFromDestinationAllowsMoreToBeDelivered(self): """""" QSIZE=20 BSIZE=10 self.assert_(QSIZE > BSIZE) sched=scheduler() t=ThreadedSender(QSIZE) d=DoesNothingComponent().activate(Scheduler=sched) d.inboxes['inbox'].setSize(BSIZE) d.link((t,"outbox"),(d,"inbox")) s=sched.main() for _ in range(10): next(s) t.activate(Scheduler=sched) try: for _ in range(10): next(s) t.howManyToSend.put(QSIZE+BSIZE+10) # wait for a response, verify it filled its in queue result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == QSIZE) # flush them through to the inbox queue of the destination for _ in range(BSIZE*5): next(s) # let the thread fill the newly free slots t.howManyToSend.put(QSIZE+BSIZE+10) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == BSIZE) # collect messages NUM_COLLECT = 0 while NUM_COLLECT < BSIZE/2: while not d.dataReady("inbox"): next(s) if d.dataReady("inbox"): d.recv("inbox") NUM_COLLECT += 1 # let the main thread flush some message through from the thread for _ in range(50): next(s) t.howManyToSend.put(QSIZE+BSIZE+10) while t.feedback.qsize() == 0: next(s) result = t.feedback.get() self.assert_(result != "ALL SENT") self.assert_(result == NUM_COLLECT) except: t.howManyToSend.put("STOP") raise
producer().run() if test == 2: # This test passes, the consumer runs. # Issues with this test: CPU is caned. consumer().run() if test == 2.5: if nonOptimised: print "This test is a low level test and won't function 'as is' with standard Axon" # This test passes, the consumer runs. # This modified version also does NOT cane the CPU :-) # # In practice, it's worth observing that this is how "2" is implemented effectively # in normal axon, so there's no reason this can't stay this way. S = scheduler() C = consumer() C.activate(S) S.run() if test == 3: # Simple pipeline equivalent? Contains a scheduler. pipeline( producer(), consumer() ).run() if test == 4: if nonOptimised: print "This test is a low level test and won't function 'as is' with standard Axon" S = scheduler()
def test_linksafe(self): """link() unlink() - thread safe when called. The postoffice link() and unlink() methods are not expected to be capable of re-entrant use.""" class ThreadedLinker(threadedcomponent): def main(self): for i in range(10): linkage = self.link( (self,"outbox"),(self,"inbox") ) self.unlink(thelinkage=linkage) sched=scheduler() t=ThreadedLinker().activate(Scheduler=sched) oldlink = t.postoffice.link oldunlink = t.postoffice.unlink safetycheck = threading.RLock() # re-entrancy permitting mutex failures = queue.Queue() def link_mock(*argL,**argD): # wrapper for postoffice.link() method if not safetycheck.acquire(False): # returns False if should block (meaning its not thread safe!) failures.put(".link()") return False else: result = oldlink(*argL,**argD) time.sleep(0.05) safetycheck.release() return result def unlink_mock(*argL,**argD): if not safetycheck.acquire(False): # returns False if should block (meaning its not thread safe!) failures.put(".unlink()") return False else: result = oldunlink(*argL,**argD) time.sleep(0.05) safetycheck.release() return result t.postoffice.link = link_mock t.postoffice.unlink = unlink_mock done=False for i in range(10): try: next(t) except StopIteration: done=True linkage = t.link( (t,"signal"),(t,"control") ) t.unlink(thelinkage=linkage) while not done: try: next(t) except StopIteration: done=True if failures.qsize(): failed = {} while failures.qsize(): failed[failures.get()] = 1 conj="" errmsg="threadedcomponent,postoffice" for method in failed.keys(): errmsg=errmsg+conj+method conj=" and " errmsg=errmsg+" should not be entered by more than one thread at once." self.fail(errmsg)