def test_RunsForever(self): """main - Run with no messages, keeps running""" S = Selector() S.activate() for i in xrange(1,100): try: S.next() except StopIteration: self.fail("Component should run until told to stop. Failed on iteration: "+str(i))
def setupCSA(self, sock): """\ setupCSA(sock) -> new ConnectedSocketAdapter component Creates a ConnectedSocketAdapter component for the socket, and wires up to it. Also sends the CSA to the "selector" service. """ selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: self.addChildren(newSelector) CSA = ConnectedSocketAdapter(sock, selectorService) # self.createConnectedSocket(sock) self.addChildren(CSA) self.link((self, "_selectorSignal"),selectorService) self.link((CSA, "CreatorFeedback"),(self,"_socketFeedback")) self.link((CSA, "outbox"), (self, "outbox"), passthrough=2) self.link((CSA, "sslready"), (self, "sslready"), passthrough=2) self.link((self, "inbox"), (CSA, "inbox"), passthrough=1) self.link((self, "makessl"), (CSA, "makessl"), passthrough=1) self.link((self, "control"), (CSA, "control"), passthrough=1) # propagate shutdown msgs self.send(newReader(CSA, ((CSA, "ReadReady"), sock)), "_selectorSignal") self.send(newWriter(CSA, ((CSA, "SendReady"), sock)), "_selectorSignal") self.CSA = CSA # We need this for shutdown later return self.childComponents()
def main(self): #TODO: Binding should handle errors gracefully self.sock.bind(self.local) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( ) if newSelector: newSelector.activate() yield 1 self.link((self, "_selectorSignal"), selectorService) self.send(newReader(self, ((self, "ReadReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: if self.dataReady("ReadReady"): # if 1: data = True while data: data = self.safeRecv(1024) if data: print "data", repr(data) self.send(data, "outbox") else: print newSelector self.pause() yield 1
def setupCSA(self, sock): """\ setupCSA(sock) -> new ConnectedSocketAdapter component Creates a ConnectedSocketAdapter component for the socket, and wires up to it. Also sends the CSA to the "selector" service. """ selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( self.tracker) if newSelector: self.addChildren(newSelector) CSA = ConnectedSocketAdapter( sock, selectorService) # self.createConnectedSocket(sock) self.addChildren(CSA) self.link((self, "_selectorSignal"), selectorService) self.link((CSA, "CreatorFeedback"), (self, "_socketFeedback")) self.link((CSA, "outbox"), (self, "outbox"), passthrough=2) self.link((CSA, "sslready"), (self, "sslready"), passthrough=2) self.link((self, "inbox"), (CSA, "inbox"), passthrough=1) self.link((self, "makessl"), (CSA, "makessl"), passthrough=1) self.link((self, "control"), (CSA, "control"), passthrough=1) # propagate shutdown msgs self.send(newReader(CSA, ((CSA, "ReadReady"), sock)), "_selectorSignal") self.send(newWriter(CSA, ((CSA, "SendReady"), sock)), "_selectorSignal") self.CSA = CSA # We need this for shutdown later return self.childComponents()
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"),selectorService) self.link((self, "_selectorShutdownSignal"),selectorShutdownService) self.selectorService = selectorService self.selectorShutdownService = selectorShutdownService self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: if not self.anyReady(): self.pause() if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection() # Data ready means that we have a connection waiting. if self.dataReady("control"): data = self.recv("control") if isinstance(data, serverShutdown): break yield 1 self.send(removeReader(self, self.listener), "_selectorSignal") # for i in xrange(100): yield 1 self.send(shutdown(), "_selectorShutdownSignal")
def main(self): #TODO: Binding should handle errors gracefully self.sock.bind(self.local) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices() if newSelector: newSelector.activate() yield 1 self.link((self, "_selectorSignal"), selectorService) self.send(newReader(self, ((self, "ReadReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: if self.dataReady("ReadReady"): # if 1: data = True while data: data = self.safeRecv(1024) if data: print "data", repr(data) self.send(data, "outbox") else: print newSelector self.pause() yield 1
def main(self): #TODO: Binding should handle errors gracefully self.sock.bind(self.local) self.sock.setblocking(0) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( ) if newSelector: newSelector.activate() yield 1 # Link to the selector's "notify" inbox self.link((self, "_selectorSignal"), selectorService) self.send(newReader(self, ((self, "ReadReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: if self.dataReady("ReadReady"): self.recv("ReadReady") data = True while data: data = self.safeRecv(1024) if data: self.send(data, "outbox") if not self.anyReady(): self.pause() yield 1
def main(self): if self.listener is None: self.send(shutdownMicroprocess, 'signal') # FIXME: Should probably be producerFinished. # FIXME: (ie advisory that I've finished, rather than # FIXME: demand next component to shutdown) yield 1 return # NOTE: Change from suggested fix. (Simplifies code logic/makes diff smaller) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"),selectorService) self.link((self, "_selectorShutdownSignal"),selectorShutdownService) self.selectorService = selectorService self.selectorShutdownService = selectorShutdownService self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: if not self.anyReady(): self.pause() yield 1 if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection() # Data ready means that we have a connection waiting. if self.dataReady("control"): data = self.recv("control") if isinstance(data, serverShutdown): break yield 1 self.send(removeReader(self, self.listener), "_selectorSignal") self.send(shutdown(), "_selectorShutdownSignal")
def main(self): #TODO: Binding should handle errors gracefully self.sock.bind(self.local) self.sock.setblocking(0) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices() if newSelector: newSelector.activate() yield 1 # Link to the selector's "notify" inbox self.link((self, "_selectorSignal"), selectorService) self.send(newReader(self, ((self, "ReadReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: if self.dataReady("ReadReady"): self.recv("ReadReady") data = True while data: data = self.safeRecv(1024) if data: self.send(data, "outbox") if not self.anyReady(): self.pause() yield 1
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) dataPending = "" waitingToStop=False self.shutdownMsg = None try: while 1: while dataPending: self.checkShutdown() try: self.send(dataPending,"outbox") dataPending="" except noSpaceInBox: self.pause() yield 1 while not dataPending: try: #dataPending=self.fh.read(self.maxReadChunkSize) dataPending = os.read(self.fh.fileno(), self.maxReadChunkSize) if dataPending=="": raise UserWarning( "STOP" ) except OSError,IOError: # no data available yet, need to wait if self.checkShutdown(): raise UserWarning( "STOP" ) if self.dataReady("ready"): self.recv("ready") else: self.send(newReader(self,((self, "ready"), self.fh)), "selector") while not self.dataReady("ready") and not self.checkShutdown(): self.pause() yield 1 if self.dataReady("ready"): self.recv("ready") except "STOP": pass # ordered to shutdown! self.send(removeReader(self,(self.fh)), "selector") try: self.fh.close() except: pass yield 1 yield 1 if not self.shutdownMsg: self.send(producerFinished(self), "signal") else: self.send(self.shutdownMsg,"signal")
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices( self.tracker ) # get a reference to a Selector component so we do not have to poll the file descriptor for readiness if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception: e = sys.exc_info()[1] print(e) return self.makeNonBlocking(self.fd) self.selectorWait(self.fd) self.done = False waiting = True while not self.done: #print ("main") yield 1 # we use inbox just to wake us up while self.dataReady("inbox"): msg = self.recv("inbox") # if we should send some more if we can if self.dataReady("_selectorready"): #print ("selector is ready") waiting = False msg = self.recv("_selectorready") if not waiting: readsomething = False while len(self.outboxes["outbox"] ) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated")
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) dataPending = "" waitingToStop=False self.shutdownMsg = None try: while 1: while dataPending: self.checkShutdown() try: self.send(dataPending,"outbox") dataPending="" except noSpaceInBox: self.pause() yield 1 while not dataPending: try: #dataPending=self.fh.read(self.maxReadChunkSize) dataPending = os.read(self.fh.fileno(), self.maxReadChunkSize) if dataPending=="": raise "STOP" except OSError,IOError: # no data available yet, need to wait if self.checkShutdown(): raise "STOP" if self.dataReady("ready"): self.recv("ready") else: self.send(newReader(self,((self, "ready"), self.fh)), "selector") while not self.dataReady("ready") and not self.checkShutdown(): self.pause() yield 1 if self.dataReady("ready"): self.recv("ready") except "STOP": pass # ordered to shutdown! self.send(removeReader(self,(self.fh)), "selector") try: self.fh.close() except: pass yield 1 yield 1 if not self.shutdownMsg: self.send(producerFinished(self), "signal") else: self.send(self.shutdownMsg,"signal")
def setupSelector(self): """ Get the selector service, and ensure it is activated and linked """ selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices() if newSelector: newSelector.activate() # Link to the selector's "notify" inbox self.link((self, "_selectorSignal"), selectorService)
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices( self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) self.send(newWriter(self, ((self, "ready"), self.fh)), "selector") dataPending = "" try: while 1: # no data pending if dataPending == "": while not self.dataReady("inbox"): self.checkShutdown(noNeedToWait=True) self.pause() yield 1 dataPending = self.recv("inbox") # now try to send it try: #self.fh.write(dataPending) byteswritten = os.write(self.fh.fileno(), dataPending) if byteswritten >= 0: dataPending = dataPending[byteswritten:] # dataPending="" except OSError, IOError: # data pending # wait around until stdin is ready if not self.dataReady("ready"): self.send(newWriter(self, ((self, "ready"), self.fh)), "selector") while not self.dataReady("ready"): self.checkShutdown(noNeedToWait=False) self.pause() yield 1 self.recv("ready") self.checkShutdown(noNeedToWait=False) except UserWarning: pass # ordered to shutdown! self.send(removeWriter(self, (self.fh)), "selector") try: self.fh.close() except: pass self.send(self.shutdownMsg, "signal")
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices(self.tracker) # get a reference to a Selector component so we do not have to poll the file descriptor for readiness if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception: e = sys.exc_info()[1] print (e) return self.makeNonBlocking(self.fd) self.selectorWait(self.fd) self.done = False waiting = True while not self.done: #print ("main") yield 1 # we use inbox just to wake us up while self.dataReady("inbox"): msg = self.recv("inbox") # if we should send some more if we can if self.dataReady("_selectorready"): #print ("selector is ready") waiting = False msg = self.recv("_selectorready") if not waiting: readsomething = False while len(self.outboxes["outbox"]) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated")
def test_SendingAReadableMessageResultsInItBeingSelectedAgainst(self): "main - If we send a newReader message to the notify inbox, it results in the selectable reader being selected on in the readers set" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHIS")), "notify") for i in xrange(100): S.next() func, args = MOCKSELECTORMODULE.log[0] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual(["LOOKINGFORTHIS"], args[0], "The selectable was added to the list of readables") self.assertEqual([], args[1], "Writable set should be empty") self.assertEqual([], args[2], "Exception set should be empty") self.assertEqual(0, args[3], "The select should be non-blocking")
def setupSelector(self): """ Get the selector service, and ensure it is activated and linked """ selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( ) if newSelector: newSelector.activate() # Link to the selector's "notify" inbox self.link((self, "_selectorSignal"), selectorService)
def test_SendingAReadableMessageResultsInItBeingSelectedAgainst(self): "main - If we send a newReader message to the notify inbox, it results in the selectable reader being selected on in the readers set" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHIS")),"notify") for i in xrange(100): S.next() func, args = MOCKSELECTORMODULE.log[0] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual(["LOOKINGFORTHIS"], args[0], "The selectable was added to the list of readables") self.assertEqual([], args[1], "Writable set should be empty") self.assertEqual([], args[2], "Exception set should be empty") self.assertEqual(0, args[3], "The select should be non-blocking")
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) self.send(newWriter(self,((self, "ready"), self.fh)), "selector") dataPending="" try: while 1: # no data pending if dataPending=="": while not self.dataReady("inbox"): self.checkShutdown(noNeedToWait=True) self.pause() yield 1 dataPending = self.recv("inbox") # now try to send it try: #self.fh.write(dataPending) byteswritten = os.write(self.fh.fileno(),dataPending) if byteswritten >= 0: dataPending = dataPending[byteswritten:] # dataPending="" except OSError,IOError: # data pending # wait around until stdin is ready if not self.dataReady("ready"): self.send(newWriter(self,((self, "ready"), self.fh)), "selector") while not self.dataReady("ready"): self.checkShutdown(noNeedToWait=False) self.pause() yield 1 self.recv("ready") self.checkShutdown(noNeedToWait=False) except UserWarning: pass # ordered to shutdown! self.send(removeWriter(self,(self.fh)), "selector") try: self.fh.close() except: pass self.send(self.shutdownMsg,"signal")
def test_WeSendTheSelectorAServiceAndSelectableOnlySelectsTheSelectable(self): "main - When we send the newReader message, it also includes a service upon which the selector can talk back to us. The selector only selects on the selectable part of the newReader message" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHIS") ),"notify") for i in xrange(100): S.next() func, args = MOCKSELECTORMODULE.log[0] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual(["LOOKINGFORTHIS"], args[0])#, "The selectable was added to the list of readables") self.assertEqual([], args[1], "Writable set should be empty") self.assertEqual([], args[2], "Exception set should be empty") self.assertEqual(0, args[3], "The select should be non-blocking")
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices() if newSelector: newSelector.activate() self.link((self, "_selectorSignal"), selectorService) self.send(newWriter(self, ((self, "WriteReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: #TODO: Write me :D pass
def test_WeSendTheSelectorAServiceAndSelectableOnlySelectsTheSelectable( self): "main - When we send the newReader message, it also includes a service upon which the selector can talk back to us. The selector only selects on the selectable part of the newReader message" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHIS")), "notify") for i in xrange(100): S.next() func, args = MOCKSELECTORMODULE.log[0] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual( ["LOOKINGFORTHIS"], args[0]) #, "The selectable was added to the list of readables") self.assertEqual([], args[1], "Writable set should be empty") self.assertEqual([], args[2], "Exception set should be empty") self.assertEqual(0, args[3], "The select should be non-blocking")
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( ) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"), selectorService) self.send(newWriter(self, ((self, "WriteReady"), self.sock)), "_selectorSignal") # TODO: Make me shutdown nicely while 1: #TODO: Write me :D pass
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"),selectorService) self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: # self.pause() if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection() # Data ready means that we have a connection waiting. yield 1
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices(self.tracker) if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception, e: print e return
def test_ActivityOnReaderResultsInMessageOnReadersService(self): "main - Activity on the selectable results in a message appearing in the service provided to the selector" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHIS")), "notify") for i in xrange(100): S.next() try: S.postoffice.next() except: pass self.assert_(not (len(D.inboxes["inbox"]) == 0)) selectable = D.recv("inbox") self.assertEqual( selectable, "LOOKINGFORTHIS" ) #, "The value returned should be the selectable we originally asked for")
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices(self.tracker) # get a reference to a Selector component so we do not have to poll the file descriptor for readiness if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception, e: print e return
def test_shutdownMessageCausesShutdown(self): """main - If the component recieves a shutdown() message, the component shuts down""" S = Selector() S.activate() S._deliver(shutdown(), "control") componentExit = False for i in xrange(2000): try: S.next() except StopIteration: componentExit = True break if not componentExit: self.fail( "When sent a shutdown message, the component should shutdown")
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices( self.tracker) if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception, e: print e return
def test_ActivityOnReaderResultsInMessageOnReadersService(self): "main - Activity on the selectable results in a message appearing in the service provided to the selector" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHIS") ),"notify") for i in xrange(100): S.next(); try: S.postoffice.next() except: pass self.assert_(not ( len(D.inboxes["inbox"]) == 0 ) ) selectable = D.recv("inbox") self.assertEqual(selectable,"LOOKINGFORTHIS")#, "The value returned should be the selectable we originally asked for")
def test_RunsForever(self): """main - Run with no messages, keeps running""" S = Selector() S.activate() for i in xrange(1, 100): try: S.next() except StopIteration: self.fail( "Component should run until told to stop. Failed on iteration: " + str(i))
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices( self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"), selectorService) self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: # self.pause() if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection( ) # Data ready means that we have a connection waiting. yield 1
def test_shutdownMessageCausesShutdown(self): """main - If the component recieves a shutdown() message, the component shuts down""" S = Selector() S.activate() S._deliver(shutdown(),"control") componentExit = False for i in xrange(2000): try: S.next() except StopIteration: componentExit = True break if not componentExit: self.fail("When sent a shutdown message, the component should shutdown")
def main(self): """Main loop""" if self.waitForTrigger: while not self.anyReady(): self.pause() yield 1 # stop the selector selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices(self.tracker) # get a reference to a link = self.link((self,"selector_shutdown"),selectorShutdownService) self.send(shutdown(),"selector_shutdown") self.unlink(thelinkage=link) if self.dataReady("control"): self.send(self.recv("control"), "signal") else: self.send(producerFinished(self), "signal")
def main(self): """Main loop""" if self.waitForTrigger: while not self.anyReady(): self.pause() yield 1 # stop the selector selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices( self.tracker) # get a reference to a link = self.link((self, "selector_shutdown"), selectorShutdownService) self.send(shutdown(), "selector_shutdown") self.unlink(thelinkage=link) if self.dataReady("control"): self.send(self.recv("control"), "signal") else: self.send(producerFinished(self), "signal")
def test_SmokeTest(self): """__init__ - Called with no arguments succeeds""" S = Selector() self.assert_(isinstance(S, Axon.Component.component))
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices( self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self, ((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self, ((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self, ((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): # \ #print self.name,"Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len(writeBuffer) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer) > 10000: writeBuffer = writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) > 0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success += 1 except OSError, e: success -= 1 # \ #print self.name,"Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer = writeBuffer[len(writeBuffer) / 2:] self.send( newWriter(self, ((self, "stdinready"), x.stdin)), "selector") # \ #print self.name,"OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer) break # Break out of this loop except: # \ #print self.name,"Unexpected error whilst trying to write to stdin:" print sys.exc_info()[0] break
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self, ((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self, ((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self, ((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): print self.name, "Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len( writeBuffer ) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer) > 10000: writeBuffer = writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) > 0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success += 1 except OSError, e: success -= 1 print self.name, "Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer = writeBuffer[len(writeBuffer) / 2 :] self.send(newWriter(self, ((self, "stdinready"), x.stdin)), "selector") print self.name, "OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer) break # Break out of this loop except: print self.name, "Unexpected error whilst trying to write to stdin:" print sys.exc_info()[0] break
def test_SendingMultipleReadersResultsInAllSelected(self): "main - Sending multiple newReader messages results in all being select()ed" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHIS") ),"notify") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHISTOO") ),"notify") S._deliver(newReader(S,( dummyservice, "LOOKINGFORANDTHIS") ),"notify") for i in xrange(100): S.next() lastfunc, lastargs = None, None i = 0 func, args = MOCKSELECTORMODULE.log[i] while not( (lastfunc, lastargs) == (func, args)): # Search for quiescent state i = i + 1 lastfunc, lastargs = func, args func, args = MOCKSELECTORMODULE.log[i] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual(["LOOKINGFORTHIS","LOOKINGFORTHISTOO","LOOKINGFORANDTHIS"], args[0])#, "The selectable was added to the list of readables")
def test_ActivityOnAnyExceptionalResultsInMessageOnThatExceptionalsService(self): "main - Activity on a selectable results in a message appearing in the service provided to the selector for that selectable" MOCKSELECTORMODULE = MockSelect(results=[ ([],[],["LOOKINGFORTHIS"]), ([],[],["THENFORTHIS"]), ([],[],["ANDTHENFORTHIS"]) ]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(5): S.next() D = Axon.Component.component() E = Axon.Component.component() F = Axon.Component.component() dummyservice1 = (D, "inbox") S._deliver(newExceptional(S,( dummyservice1, "LOOKINGFORTHIS") ),"notify") dummyservice2 = (E, "inbox") S._deliver(newExceptional(S,( dummyservice2, "THENFORTHIS") ),"notify") dummyservice3 = (F, "inbox") S._deliver(newExceptional(S,( dummyservice3, "ANDTHENFORTHIS") ),"notify") for i in xrange(5): S.next(); try: S.postoffice.next() except: pass selectable = D.recv("inbox") self.assertEqual(selectable,"LOOKINGFORTHIS")#, "The value returned should be the selectable we originally asked for") selectable = E.recv("inbox") self.assertEqual(selectable,"THENFORTHIS")#, "The value returned should be the selectable we originally asked for") selectable = F.recv("inbox") self.assertEqual(selectable,"ANDTHENFORTHIS")#, "The value returned should be the selectable we originally asked for")
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices( self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self, ((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self, ((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self, ((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): # \ #print (self.name,"Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len(writeBuffer)) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer) > 10000: writeBuffer = writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) > 0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success += 1 except OSError: e = sys.exc_info()[1] success -= 1 # \ #print (self.name,"Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer)) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer = writeBuffer[len(writeBuffer) / 2:] self.send( newWriter(self, ((self, "stdinready"), x.stdin)), "selector") # \ #print (self.name,"OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer)) break # Break out of this loop except: # \ #print (self.name,"Unexpected error whilst trying to write to stdin:") print(sys.exc_info()[0]) break # if count != len(d): # raise RuntimeError("Yay, we broke it") if stdout_ready: try: Y = os.read(x.stdout.fileno(), 2048) if len(Y) > 0: self.send(Y, "outbox") except OSError: e = sys.exc_info()[1] # print ("Mighty Bingle", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stdout_ready = 0 self.send( newReader(self, ((self, "stdoutready"), x.stdout)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stdout:") print(sys.exc_info()[0]) pass if stderr_ready: # FIXME: This needs fixing before release try: Y = os.read(x.stderr.fileno(), 2048) # TEMPORARY DIVERSION OF STDERR TO OUTBOX TOO # \ #if len(Y)>0: self.send(Y,"outbox") ## No particular plans for stderr except OSError: e = sys.exc_info()[1] # \ #print (self.name,"Mighty Jibble", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stderr_ready = 0 self.send( newReader(self, ((self, "stderrready"), x.stderr)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stderr:") print(sys.exc_info()[0]) pass if self.dataReady("control"): shutdownMessage = self.recv("control") self.send(removeWriter(self, (x.stdin)), "selector") yield 1 x.stdin.close() yield 1 # \ #print (self.name,"UnixPipe finishing up") while self.dataReady("stdoutready"): # \ #print (self.name,"flushing") self.recv("stdoutready") try: Y = os.read(x.stdout.fileno(), 10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(), 10) # \ #print (self.name,"Mighty Floogly") except OSError: e = sys.exc_info()[1] continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self, (x.stderr)), "selector") self.send(removeReader(self, (x.stdout)), "selector") self.send(removeWriter(self, (x.stdin)), "selector") # \ #print (self.name,"sending shutdown") if not shutdownMessage: # \ #print (self.name,"new signal") self.send(Axon.Ipc.producerFinished(), "signal") # \ #print (self.name,"...sent") else: # \ #print (self.name,"old signal") self.send(shutdownMessage, "signal")
def test_PausesUntilFirstMessage(self): """main - Before we recieve any messages telling us what to watch for, the system should pause and yield""" S = Selector() S.activate() V = S.next() self.assert_(S._isRunnable() is not True)
def test_ActivityOnAnyExceptionalResultsInMessageOnThatExceptionalsService( self): "main - Activity on a selectable results in a message appearing in the service provided to the selector for that selectable" MOCKSELECTORMODULE = MockSelect(results=[( [], [], ["LOOKINGFORTHIS"]), ([], [], ["THENFORTHIS"]), ([], [], ["ANDTHENFORTHIS"])]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(5): S.next() D = Axon.Component.component() E = Axon.Component.component() F = Axon.Component.component() dummyservice1 = (D, "inbox") S._deliver(newExceptional(S, (dummyservice1, "LOOKINGFORTHIS")), "notify") dummyservice2 = (E, "inbox") S._deliver(newExceptional(S, (dummyservice2, "THENFORTHIS")), "notify") dummyservice3 = (F, "inbox") S._deliver(newExceptional(S, (dummyservice3, "ANDTHENFORTHIS")), "notify") for i in xrange(5): S.next() try: S.postoffice.next() except: pass selectable = D.recv("inbox") self.assertEqual( selectable, "LOOKINGFORTHIS" ) #, "The value returned should be the selectable we originally asked for") selectable = E.recv("inbox") self.assertEqual( selectable, "THENFORTHIS" ) #, "The value returned should be the selectable we originally asked for") selectable = F.recv("inbox") self.assertEqual( selectable, "ANDTHENFORTHIS" ) #, "The value returned should be the selectable we originally asked for")
def test_SendingMultipleReadersResultsInAllSelected(self): "main - Sending multiple newReader messages results in all being select()ed" MOCKSELECTORMODULE = MockSelect() SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() dummyservice = (Axon.Component.component(), "inbox") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHIS")), "notify") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHISTOO")), "notify") S._deliver(newReader(S, (dummyservice, "LOOKINGFORANDTHIS")), "notify") for i in xrange(100): S.next() lastfunc, lastargs = None, None i = 0 func, args = MOCKSELECTORMODULE.log[i] while not ((lastfunc, lastargs) == (func, args)): # Search for quiescent state i = i + 1 lastfunc, lastargs = func, args func, args = MOCKSELECTORMODULE.log[i] self.assertEqual("select", func, "select was called in the main loop") self.assertEqual( ["LOOKINGFORTHIS", "LOOKINGFORTHISTOO", "LOOKINGFORANDTHIS"], args[0]) #, "The selectable was added to the list of readables")
def test_RemoveExceptional_ResultsInExceptionalNoLongerBeingSelectedOrWiredIn( self): "main - Sending a remove exceptional message unwires/links a component, and also removes it's selectable from the exceptionals list" MOCKSELECTORMODULE = MockSelect(results=[([], [], [])]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newExceptional(S, (dummyservice, "LOOKINGFORTHIS")), "notify") S._deliver(removeExceptional(S, "LOOKINGFORTHIS"), "notify") for i in xrange(100): S.next() try: S.postoffice.next() except: pass self.assert_(len(D.inboxes["inbox"]) == 0)
def test_RemoveExceptional_ResultsInExceptionalNoLongerBeingSelectedOrWiredIn(self): "main - Sending a remove exceptional message unwires/links a component, and also removes it's selectable from the exceptionals list" MOCKSELECTORMODULE = MockSelect(results=[ ([], [], [] )]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newExceptional(S,( dummyservice, "LOOKINGFORTHIS") ),"notify") S._deliver(removeExceptional(S,"LOOKINGFORTHIS"),"notify") for i in xrange(100): S.next(); try: S.postoffice.next() except: pass self.assert_( len(D.inboxes["inbox"]) == 0 )
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self,((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self,((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self,((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): # \ #print (self.name,"Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len(writeBuffer)) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer)>10000: writeBuffer=writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) >0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success +=1 except OSError: e =sys.exc_info()[1] success -=1 # \ #print (self.name,"Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer)) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer=writeBuffer[len(writeBuffer)/2:] self.send(newWriter(self,((self, "stdinready"), x.stdin)), "selector") # \ #print (self.name,"OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer)) break # Break out of this loop except: # \ #print (self.name,"Unexpected error whilst trying to write to stdin:") print (sys.exc_info()[0] ) break # if count != len(d): # raise RuntimeError("Yay, we broke it") if stdout_ready: try: Y = os.read(x.stdout.fileno(),2048) if len(Y)>0: self.send(Y, "outbox") except OSError: e = sys.exc_info()[1] # print ("Mighty Bingle", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stdout_ready = 0 self.send(newReader(self,((self, "stdoutready"), x.stdout)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stdout:") print (sys.exc_info()[0]) pass if stderr_ready: # FIXME: This needs fixing before release try: Y = os.read(x.stderr.fileno(),2048) # TEMPORARY DIVERSION OF STDERR TO OUTBOX TOO # \ #if len(Y)>0: self.send(Y,"outbox") ## No particular plans for stderr except OSError: e = sys.exc_info()[1] # \ #print (self.name,"Mighty Jibble", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stderr_ready = 0 self.send(newReader(self,((self, "stderrready"), x.stderr)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stderr:") print (sys.exc_info()[0]) pass if self.dataReady("control"): shutdownMessage = self.recv("control") self.send(removeWriter(self,(x.stdin)), "selector") yield 1 x.stdin.close() yield 1 # \ #print (self.name,"UnixPipe finishing up") while self.dataReady("stdoutready"): # \ #print (self.name,"flushing") self.recv("stdoutready") try: Y = os.read(x.stdout.fileno(),10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(),10) # \ #print (self.name,"Mighty Floogly") except OSError: e = sys.exc_info()[1] continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self,(x.stderr)), "selector") self.send(removeReader(self,(x.stdout)), "selector") self.send(removeWriter(self,(x.stdin)), "selector") # \ #print (self.name,"sending shutdown") if not shutdownMessage: # \ #print (self.name,"new signal") self.send(Axon.Ipc.producerFinished(), "signal") # \ #print (self.name,"...sent") else: # \ #print (self.name,"old signal") self.send(shutdownMessage, "signal")