def __init__(self, router): """ Initialize responder logic: receive a buttonclick event and respond by generating set button state, set button text and set numeric text events with predictable values. """ self._count = 0 self._router = router router.subscribe(32000, makeEventHandler(handler=self.clickhandler, uri="TestButtonClickResponder"), evtype=ButtonClickEvent) router.subscribe(32000, makeEventHandler(handler=self.resethandler, uri="TestButtonClickReset"), evtype=urljoin(EventBaseUri, "reset"))
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 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 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 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 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 testMakeEventHandler2(self): eh = makeEventHandler("EventHandlerUri") self.assertEqual(eh.getUri(), "EventHandlerUri") ev = makeEvent("EventUri", source=eh.getUri()) sh = eh.handleEvent(ev).syncValue() self.assertEqual(sh, StatusVal.OK) ss = eh.initSubscription(StatusVal.OK).syncValue() self.assertEqual(sh, StatusVal.OK) ss = eh.endSubscription(StatusVal.OK).syncValue() self.assertEqual(sh, StatusVal.OK) 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 testMakeEventHandler5(self): # Test response to event handler throwing an exception eh = makeEventHandler("EventHandlerUri", handleEventException, initSub, endSub) self.assertEqual(eh.getUri(), "EventHandlerUri") ev = makeEvent("EventUri", source=eh.getUri()) sh = eh.handleEvent(ev).syncValue() self.assertEqual(sh, Status(URI.Status_OK, ev.getType())) # The status message value here depends on the default status value set up by # EventHandler.handleEvent, which is currently the default supplied by the # Status object constructor. self.assertEqual(sh.getMessage(), "") return
def testWatchForwarding1(self): # watch R2 for subscription to R2Events/ev1 Weh = makeEventHandler(uri="Weh", handler=eventHandler) Weh.subcount = 0 Weh.evcount = 0 sts = self.R2.watch(60, Weh, evtype="R2Events/ev1") self.assertEqual(str(sts.syncValue()), str(StatusVal.SUBSCRIBED)) # subscribe R3 to R2Events/ev1 Seh = makeEventHandler(uri="Seh", handler=eventHandler) Seh.subcount = 0 Seh.evcount = 0 sts = self.R3.subscribe(60, Seh, evtype="R2Events/ev1") self.assertEqual(sts.syncValue(), StatusVal.SUBSCRIBED) # Check watch event was delivered self.assertEqual(Weh.evcount, 1) # unsubscribe R3 from R2Events/ev1 sts = self.R3.unsubscribe(Seh, evtype="R2Events/ev1") self.assertEqual(sts.syncValue(), StatusVal.UNSUBSCRIBED) # Check event was delivered to watcher self.assertEqual(Weh.evcount, 2) return
def testInterleavedEventDelivery(self): # Helpers def makePublishThread(es, cq, wait0, wait1, evlist): t = Thread(name=es, target=publishFromThread, args=(es, cq, wait0, wait1, evlist)) t.start() return def publishFromThread(es, cq, wait0, wait1, evlist): n = 0 for (et, es) in evlist: n += 1 time.sleep(wait0) wait0 = wait1 ev = makeEvent(evtype=et, source=es) sts = self.R3.publish(es, ev) cq.put(n) return # Main test case evtyp = "R3Events/ev1" # Select event type routed R3 -> R1 -> R2 eh = makeEventHandler(uri="eh", handler=eventHandlerQueueing, initSubscription=subHandler, endSubscription=unsubHandler) eh.subcount = 0 eh.evcount = 0 eh.event = None eh.queue = [] self.R2.subscribe(60, eh, evtype=evtyp, source=None) time.sleep(0.1) # Allow subscribe time to propagate completionQueue = Queue() makePublishThread("es1", completionQueue, 0.0, 1.0, ((evtyp, "Pub11"), (evtyp, "Pub12"))) makePublishThread("es2", completionQueue, 0.5, 1.0, ((evtyp, "Pub21"), (evtyp, "Pub22"))) c = completionQueue.get() # Wait for both threads to complete c = completionQueue.get() # Check state of handler time.sleep(0.1) # Allow events time to propagate self.assertEqual(eh.evcount, 4) self.assertEqual(eh.queue[0].getType(), evtyp) self.assertEqual(eh.queue[0].getSource(), "Pub11") self.assertEqual(eh.queue[1].getType(), evtyp) self.assertEqual(eh.queue[1].getSource(), "Pub21") self.assertEqual(eh.queue[2].getType(), evtyp) self.assertEqual(eh.queue[2].getSource(), "Pub12") self.assertEqual(eh.queue[3].getType(), evtyp) self.assertEqual(eh.queue[3].getSource(), "Pub22") return
def testMakeEventHandler1(self): eh = makeEventHandler("EventHandlerUri", handleEvent, initSub, endSub) self.assertEqual(eh.getUri(), "EventHandlerUri") ev = makeEvent("EventUri", source=eh.getUri()) sh = eh.handleEvent(ev).syncValue() self.assertEqual(sh, Status(URI.Status_TRIGGERED, ev.getType())) self.assertEqual(sh.getMessage(), ev.getType()) ss = eh.initSubscription(StatusVal.OK).syncValue() self.assertEqual(ss, Status(URI.Status_SUBSCRIBED, str(StatusVal.OK))) self.assertEqual(ss.getMessage(), str(StatusVal.OK)) su = eh.endSubscription(StatusVal.OK).syncValue() self.assertEqual(su, Status(URI.Status_UNSUBSCRIBED, str(StatusVal.OK))) self.assertEqual(su.getMessage(), str(StatusVal.OK)) 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 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 testInterleavedEventDelivery(self): # Helpers def makePublishThread( es, cq, wait0, wait1, evlist): t = Thread(name=es, target=publishFromThread, args=(es, cq, wait0, wait1, evlist)) t.start() return def publishFromThread( es, cq, wait0, wait1, evlist): n = 0 for (et,es) in evlist: n += 1 time.sleep(wait0) wait0 = wait1 ev = makeEvent(evtype=et, source=es) sts = self.R3.publish(es, ev) cq.put(n) return # Main test case evtyp = "R3Events/ev1" # Select event type routed R3 -> R1 -> R2 eh = makeEventHandler( uri="eh", handler=eventHandlerQueueing, initSubscription=subHandler, endSubscription=unsubHandler) eh.subcount = 0 eh.evcount = 0 eh.event = None eh.queue = [] self.R2.subscribe(60, eh, evtype=evtyp, source=None) time.sleep(0.1) # Allow subscribe time to propagate completionQueue = Queue() makePublishThread( "es1", completionQueue, 0.0, 1.0, ((evtyp,"Pub11"),(evtyp,"Pub12")) ) makePublishThread( "es2", completionQueue, 0.5, 1.0, ((evtyp,"Pub21"),(evtyp,"Pub22")) ) c = completionQueue.get() # Wait for both threads to complete c = completionQueue.get() # Check state of handler time.sleep(0.1) # Allow events time to propagate self.assertEqual(eh.evcount, 4) self.assertEqual(eh.queue[0].getType(), evtyp) self.assertEqual(eh.queue[0].getSource(), "Pub11") self.assertEqual(eh.queue[1].getType(), evtyp) self.assertEqual(eh.queue[1].getSource(), "Pub21") self.assertEqual(eh.queue[2].getType(), evtyp) self.assertEqual(eh.queue[2].getSource(), "Pub12") self.assertEqual(eh.queue[3].getType(), evtyp) self.assertEqual(eh.queue[3].getSource(), "Pub22") return
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 testEventHandlerEqual3(self): eh1 = makeEventHandler() eh2 = makeEventHandler() self.assertNotEqual(eh1, eh2)
def testEventHandlerEqual4(self): eh1 = makeEventHandler() eh2 = makeEventHandler(eh1.getUri(), handleEvent, initSub, endSub) self.assertEqual(eh1, eh2)
def testMakeEventHandler4(self): eh1 = makeEventHandler() eh2 = makeEventHandler() self.assertNotEqual(eh1.getUri(), eh2.getUri())
def testMakeEventHandler3(self): eh = makeEventHandler() eu = eh.getUri() assert eu.startswith(URI.EventTargetBase), "testMakeEventHandler3: " + eu
def testMakeEventHandler3(self): eh = makeEventHandler() eu = eh.getUri() assert eu.startswith( URI.EventTargetBase), "testMakeEventHandler3: " + eu