def testBlockingEventDelivery(self): es = makeEventAgent(uri="es") eh = makeEventHandler(uri="eh", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh.subcount = 0 eh.evcount = 0 evtyp = "R3Events1/ev1" # Select event routed R3 -> R1 -> R2 .. evsrc = "R3Source1/src1" # .. (see setUp) ev = makeEvent(evtype=evtyp, source=evsrc) sts = self.R2.subscribe(60, eh, evtype=evtyp, source=evsrc) Trace("Subscribe returned", "EventLib.TestEventRouterThreaded") time.sleep(0.1) # Allow subscribe time to propagate Trace("Subscribed", "EventLib.TestEventRouterThreaded") self.assertEqual(eh.evcount, 0) sts = self.R3.publish(es, ev) self.assertEqual(eh.evcount, 0) # Not delivered immediately ... Trace("Before sleep", "EventLib.TestEventRouterThreaded") time.sleep(1.5) # Note: evcent handler blocks for 1 sec Trace("After sleep: eh.evcount %d" % (eh.evcount), "EventLib.TestEventRouterThreaded") self.assertEqual(eh.evcount, 1) # ... but sometime after a second self.assertEqual(eh.event.getType(), evtyp) self.assertEqual(eh.event.getSource(), evsrc) Trace("testBlockingEventDelivery OK", "EventLib.TestEventRouterThreaded") return
def doTestPublish(self,typid,srcid,expectTriggered): src = makeEventAgent(typid+srcid) evt = makeEvent(evtype=typid,source=srcid) sts = self.router.publish(src, evt) self.assertEqual( sts.syncValue(), StatusVal.OK, str(sts.syncValue())+" is not StatusVal.OK" ) mapAssert(evTriggerEq, self.handlers, expectTriggered, "Trigger "+typid+srcid) return
def eventHandlerIncoming(h, e): h.evcount += 1 h.event = e if h.event.getType() == "to/router/original": rootlogger.warn("== Received an incoming event, event number: %s ==", router.getCount()) # Increment the counter for the incoming events. Sleep so that we don't have a continuous loop of events. router.inC() time.sleep(0.5) # Make and publish an event back to Router2Console eventType = "to/router/2" eventSource = "source/router1" router1EventAgentOutgoing = makeEventAgent(uri="router1EventAgentOutgoing") router1EventHandlerOutgoing = makeEventHandler( uri="router1EventAgentOutgoing", handler=eventHandlerOutgoing, initSubscription=subHandlerOutgoing, endSubscription=unsubHandlerOutgoing, ) router1EventHandlerOutgoing.subcount = 0 router1EventHandlerOutgoing.evcount = 0 event = makeEvent(evtype=eventType, source=eventSource, payload=router.getCount()) rootlogger.warn("== Sending out event, number: %s ==", router.getCount()) status2 = router.getR1().publish( router1EventHandlerOutgoing, event ) ## R1 publishes the main event, R2C picks it up and forwards it to TestEventHTTPRouter1Console.py rootlogger.warn("== The event has completed being published ==") return makeDeferred(StatusVal.OK)
def testBlockingEventDelivery(self): es = makeEventAgent(uri="es") eh = makeEventHandler( uri="eh", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh.subcount = 0 eh.evcount = 0 evtyp = "R3Events1/ev1" # Select event routed R3 -> R1 -> R2 .. evsrc = "R3Source1/src1" # .. (see setUp) ev = makeEvent(evtype=evtyp, source=evsrc) sts = self.R2.subscribe(60, eh, evtype=evtyp, source=evsrc) Trace("Subscribe returned", "EventLib.TestEventRouterThreaded") time.sleep(0.1) # Allow subscribe time to propagate Trace("Subscribed", "EventLib.TestEventRouterThreaded") self.assertEqual(eh.evcount, 0) sts = self.R3.publish(es, ev) self.assertEqual(eh.evcount, 0) # Not delivered immediately ... Trace("Before sleep", "EventLib.TestEventRouterThreaded") time.sleep(1.5) # Note: evcent handler blocks for 1 sec Trace("After sleep: eh.evcount %d"%(eh.evcount), "EventLib.TestEventRouterThreaded") self.assertEqual(eh.evcount, 1) # ... but sometime after a second self.assertEqual(eh.event.getType(), evtyp) self.assertEqual(eh.event.getSource(), evsrc) Trace("testBlockingEventDelivery OK", "EventLib.TestEventRouterThreaded") return
def sendTestEvent(self): print ("sendTestEvent start") self.setUpRouter() # Listen for incoming events using a custom handler. eventType = "to/router/original" eventSource = "source/router2" router1EventAgentIncoming = makeEventAgent(uri="router1EventAgentIncoming") router1EventHandlerIncoming = makeEventHandler( uri="router1EventAgentIncoming", handler=eventHandlerIncoming, initSubscription=subHandlerIncoming, endSubscription=unsubHandlerIncoming, ) router1EventHandlerIncoming.subcount = 0 router1EventHandlerIncoming.evcount = 0 status = self.R1.subscribe( 60, router1EventHandlerIncoming, evtype=eventType, source=eventSource ) # R1 subscribes to eventType to send a response back # Set the counter to 1, make our first event. self.inC() eventType = "to/router/2" eventSource = "source/router1" event = makeEvent(evtype=eventType, source=eventSource, payload=router.getCount()) self.sendTestEventDo(self.R3C, self.R1, event, eventType, eventSource) # Send the event print ("sendTestEvent end")
def doSubscriptionForwarding(self, r1, r2, evtype, source, evmatch, evdrop, delay=0.0): R1es = makeEventAgent(uri="R1es") R1eh = makeEventHandler(uri="R1eh", handler=eventHandler, initSubscription=subHandler, endSubscription=unsubHandler) R1eh.subcount = 0 R1eh.evcount = 0 # Initial tests self.assertEqual(r1.getSubscriptionCount(), 0) self.assertEqual(r2.getSubscriptionCount(), 0) # subscribe sts = r1.subscribe(60, R1eh, evtype=evtype, source=source) self.assertEqual(sts.syncValue(), StatusVal.SUBSCRIBED) time.sleep(delay) self.assertEqual(R1eh.dosub, StatusVal.SUBSCRIBED) self.assertEqual(R1eh.subcount, 1) self.assertEqual(r1.getSubscriptionCount(), 1) self.assertEqual(r2.getSubscriptionCount(), 0) self.assertEqual(r1.getForwardCount(), 1) self.assertEqual(r2.getForwardCount(), 1) # publish matching event sts = r2.publish(R1es, evmatch) self.assertEqual(sts.syncValue(), StatusVal.OK) time.sleep(delay) self.assertEqual(R1eh.evcount, 1) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) # publish non-matching event sts = r2.publish(R1es, evdrop) self.assertEqual(sts.syncValue(), StatusVal.OK) self.assertEqual(R1eh.evcount, 1) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) # publish matching event sts = r2.publish(R1es, evmatch) self.assertEqual(sts.syncValue(), StatusVal.OK) time.sleep(delay) self.assertEqual(R1eh.evcount, 2) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) # unsubscribe sts = r1.unsubscribe(R1eh, evtype=evtype, source=source) self.assertEqual(str(sts.syncValue()), str(StatusVal.UNSUBSCRIBED)) self.assertEqual(R1eh.unsub, StatusVal.UNSUBSCRIBED) self.assertEqual(R1eh.subcount, 0) self.assertEqual(r1.getSubscriptionCount(), 0) self.assertEqual(r2.getSubscriptionCount(), 0) self.assertEqual(r1.getForwardCount(), 1) self.assertEqual(r2.getForwardCount(), 1) return
def doSubscriptionForwarding(self, r1, r2, evtype, source, evmatch, evdrop, r1fwd=0, rvia=None, r1rtr=None, r2rtr=None, delay=0.0): R1es = makeEventAgent(uri="R1es") R1eh = makeEventHandler( uri="R1eh", handler=eventHandler, initSubscription=subHandler, endSubscription=unsubHandler) R1eh.subcount = 0 R1eh.evcount = 0 r1rtr = r1rtr or r1 r2rtr = r2rtr or r2 # Initial tests self.assertEqual(r1.getSubscriptionCount(), 0) self.assertEqual(r2.getSubscriptionCount(), 0) # subscribe sts = r1.subscribe(60, R1eh, evtype=evtype, source=source) self.assertEqual(sts.syncValue(), StatusVal.SUBSCRIBED) time.sleep(delay) self.assertEqual(R1eh.dosub, StatusVal.SUBSCRIBED) self.assertEqual(R1eh.subcount, 1) self.assertEqual(R1eh.evcount, 0) # Was subscribe event caught? self.assertEqual(r1.getSubscriptionCount(), 1) self.assertEqual(r1rtr.getForwardCount(), r1fwd) self.assertEqual(r2.getSubscriptionCount(), 0) self.assertEqual(r2rtr.getForwardCount(), 1) if rvia: self.assertEqual(rvia.getSubscriptionCount(), 0) self.assertEqual(rvia.getForwardCount(), 1) # publish matching event sts = r2.publish(R1es, evmatch) self.assertEqual(sts.syncValue(), StatusVal.OK) time.sleep(delay) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) self.assertEqual(R1eh.evcount, 1) # publish non-matching event sts = r2.publish(R1es, evdrop) self.assertEqual(sts.syncValue(), StatusVal.OK) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) self.assertEqual(R1eh.evcount, 1) # publish matching event sts = r2.publish(R1es, evmatch) self.assertEqual(sts.syncValue(), StatusVal.OK) time.sleep(delay) self.assertEqual(R1eh.event.getType(), evmatch.getType()) self.assertEqual(R1eh.event.getSource(), evmatch.getSource()) self.assertEqual(R1eh.evcount, 2) # unsubscribe sts = r1.unsubscribe(R1eh, evtype=evtype, source=source) self.assertEqual(str(sts.syncValue()), str(StatusVal.UNSUBSCRIBED)) self.assertEqual(R1eh.unsub, StatusVal.UNSUBSCRIBED) self.assertEqual(R1eh.subcount, 0) self.assertEqual(r1.getSubscriptionCount(), 0) self.assertEqual(r2.getSubscriptionCount(), 0) if rvia: self.assertEqual(rvia.getSubscriptionCount(), 0) return
def sendTestEvent(self): print("sendTestEvent start") self.setUpRouter() eventType = "to/router/2" eventSource = "source/router1" router2EventAgent1 = makeEventAgent(uri="router2EventAgent1") router2EventHandler1 = makeEventHandler( uri="router2EventAgent1", handler=eventHandler, initSubscription=subHandler, endSubscription=unsubHandler) router2EventHandler1.subcount = 0 router2EventHandler1.evcount = 0 # Subscribe to incoming events and then pass them to the event handler to generate outgoing events. status = self.R1.subscribe(60, router2EventHandler1, evtype=eventType, source=eventSource) #R1 subscribes to eventType to send a response back print("sendTestEvent finished")
def sendTestEventDo(self, r1, r2, evt, eventType, eventSource): # Send the first event, using a custom handler router1EventAgent = makeEventAgent(uri="router1EventAgent") router1EventHandler = makeEventHandler( uri="router1EventAgent", handler=eventHandler, initSubscription=subHandler, endSubscription=unsubHandler ) router1EventHandler.subcount = 0 router1EventHandler.evcount = 0 status = r1.subscribe( 60, router1EventHandler, evtype=eventType, source=eventSource ) ## R2C subscribes to all interested events (e.g. eventType) time.sleep(1) rootlogger.warn("== Router 1 has subscribed to incoming events ==") status2 = r2.publish( router1EventHandler, evt ) ## R1 publishes the main event, R3C picks it up and forwards it to TestEventHTTPRouter2Console.py rootlogger.warn("== First event has finished publishing ==")
def sendBackResponse(count): # Send back an event that the Router1Console's INCOMING handler will pickup. eventType = "to/router/original" eventSource = "source/router2" router2EventAgentOutgoing = makeEventAgent(uri="router2EventAgentOutgoing") router2EventHandlerOutgoing = makeEventHandler( uri="router2EventAgentOutgoing", handler=eventHandlerOutgoing, initSubscription=subHandlerOutgoing, endSubscription=unsubHandlerOutgoing) router2EventHandlerOutgoing.subcount = 0 router2EventHandlerOutgoing.evcount = 0 event = makeEvent(evtype=eventType, source=eventSource) # Only subscribe the first time around. if count == 1: status = router.getR2C().subscribe(60, router2EventHandlerOutgoing, evtype=eventType, source=eventSource) ## R2C subscribes to all interested events (e.g. eventType) # Publish the event to Router1Console status2 = router.getR1().publish(router2EventHandlerOutgoing, event) ## R1 publishes the main event, R2C picks it up and forwards it to TestEventHTTPRouter1Console.py rootlogger.warn("== Router 2 sent out event ==")
def testWatch(self): es = makeEventAgent(uri="es") eh1 = makeEventHandler(uri="eh1", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh2 = makeEventHandler(uri="eh2", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh3 = makeEventHandler(uri="eh3", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh1.subcount = 0 eh1.evcount = 0 eh2.subcount = 0 eh2.evcount = 0 eh3.subcount = 0 eh3.evcount = 0 self.R1.subscribe(30, eh3, '') self.R1.watch(30, eh1, 'http://id.webbrick.co.uk/events/config/get') # self.R1.subscribe( 30, eh1, 'http://id.webbrick.co.uk/events/config/set' ) # self.R1.subscribe( 30, eh2, 'http://id.webbrick.co.uk/events/time/minute' ) self.R1.subscribe(30, eh2, 'http://id.webbrick.co.uk/events/config/get') self.R1.unsubscribe(eh3, '') self.R1.endWatch(eh1, 'http://id.webbrick.co.uk/events/config/get') # self.R1.unsubscribe( eh1, 'http://id.webbrick.co.uk/events/config/set' ) # self.R1.unsubscribe( eh2, 'http://id.webbrick.co.uk/events/time/minute' ) self.R1.unsubscribe(eh2, 'http://id.webbrick.co.uk/events/config/get') self.assertEqual(eh1.subcount, 0) self.assertEqual(eh2.subcount, 0) self.assertEqual(eh3.subcount, 0) time.sleep(1.5) # Note: evcent handler blocks for 1 sec return
def testWatch(self): es = makeEventAgent(uri="es") eh1 = makeEventHandler( uri="eh1", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh2 = makeEventHandler( uri="eh2", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh3 = makeEventHandler( uri="eh3", handler=eventHandlerBlocking, initSubscription=subHandler, endSubscription=unsubHandler) eh1.subcount = 0 eh1.evcount = 0 eh2.subcount = 0 eh2.evcount = 0 eh3.subcount = 0 eh3.evcount = 0 self.R1.subscribe( 30, eh3, '' ) self.R1.watch( 30, eh1, 'http://id.webbrick.co.uk/events/config/get' ) # self.R1.subscribe( 30, eh1, 'http://id.webbrick.co.uk/events/config/set' ) # self.R1.subscribe( 30, eh2, 'http://id.webbrick.co.uk/events/time/minute' ) self.R1.subscribe( 30, eh2, 'http://id.webbrick.co.uk/events/config/get' ) self.R1.unsubscribe( eh3, '' ) self.R1.endWatch( eh1, 'http://id.webbrick.co.uk/events/config/get' ) # self.R1.unsubscribe( eh1, 'http://id.webbrick.co.uk/events/config/set' ) # self.R1.unsubscribe( eh2, 'http://id.webbrick.co.uk/events/time/minute' ) self.R1.unsubscribe( eh2, 'http://id.webbrick.co.uk/events/config/get' ) self.assertEqual(eh1.subcount, 0) self.assertEqual(eh2.subcount, 0) self.assertEqual(eh3.subcount, 0) time.sleep(1.5) # Note: evcent handler blocks for 1 sec return
def testMakeEventAgent1(self): es = makeEventAgent("EventAgentUri") self.assertEqual(es.getUri(), "EventAgentUri")
def testMakeEventAgent2(self): es = makeEventAgent() eu = es.getUri() assert eu.startswith(URI.EventAgentBase), "testMakeEventAgent2: "+eu
def testEventAgentEqual3(self): es1 = makeEventAgent() es2 = makeEventAgent() self.assertNotEqual(es1, es2)
def testMakeEventAgent3(self): es1 = makeEventAgent() es2 = makeEventAgent() self.assertNotEqual(es1.getUri(), es2.getUri())
def testEventAgentEqual5(self): es1 = makeEventAgent() es2 = makeEventAgent() self.assertNotEqual(None, es2)
def testEventAgentEqual4(self): es1 = makeEventAgent() es2 = makeEventAgent(es1.getUri()) self.assertEqual(es1, es2)
def testEventAgentEqual6(self): es1 = makeEventAgent() es2 = makeEventAgent() self.assertNotEqual(None, es2)
def testEventAgentEqual7(self): # test against object that is not an event es1 = makeEventAgent() self.assertNotEqual(es1, DummyClass(), "testEventAgentEqual6")
def testEventAgentEqual5(self): es1 = makeEventAgent() es2 = makeEventAgent() self.assertNotEqual(es1, None)
def testEventAgentEqual6(self): # test against object that is not an event es1 = makeEventAgent() self.assertNotEqual(es1, DummyClass(), "testEventAgentEqual6")