def testEventSelector(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        host_pid = "%s_%d" % (thisHost, os.getpid())

        topic = "test_events_11_%s" % host_pid

        runid = 'test_runid_11_%d' % os.getpid()

        rec = events.EventReceiver(broker, topic, "%s = '%s'" % (events.Event.RUNID, runid))

        #
        # send a test event, and wait to receive it
        #
        self.sendEvent(runid, broker, topic)

        # we'll get the second event, not the first
        val = rec.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        self.assertTrue(ps.exists(events.Event.RUNID))
        self.assertEqual(ps.get(events.Event.RUNID),runid)

        self.sendEvent("invalid", broker, topic)
        # shouldn't receive anything else
        val2 = rec.receiveEvent(1)
        self.assertIsNone(val2)
Beispiel #2
0
    def testHostNameAndProcessId(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER="+testEnv.getBroker()+"\n"
        confStr += "log4j.appender.EA.TOPIC="+topic+"\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
        log.MDCRemove("x")
        e = recv.receiveEvent()
        self.assertValidMessage(e, "This is TRACE")
        ps = e.getPropertySet()

        # the host name should be the same since we're sending and receiving
        # from the same host
        host = ps.get(events.LogEvent.ORIG_HOSTNAME)
        self.assertEqual(host, socket.gethostname())

        # the process id should be the same since we're sending and receiving
        # from the same process
        pid = ps.get(events.LogEvent.ORIG_PROCESSID)
        self.assertEqual(os.getpid(), pid)

        t = ps.get(events.LogEvent.TYPE)
        self.assertEqual(t, events.EventTypes.LOG)
Beispiel #3
0
    def testIllegalFilterableStatusEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.A")

        #
        # send a test event, and wait to receive it
        #
        trans = events.EventTransmitter(broker, topic)

        root = PropertySet()
        root.set(events.StatusEvent.TOPIC, topic)
        root.set("myname","myname")
        root.set(events.StatusEvent.STATUS, "my special status")

        filter = PropertySet()
        filter.set("FOO", "bar")
        filter.set("PLOUGH", 123)
        filter.set("PLOVER.FIRST", 1)
        filter.set("PLOVER.SECOND", 2)

        eventSystem = events.EventSystem.getDefaultEventSystem();
        locationID = eventSystem.createOriginatorId()
        event = events.StatusEvent(locationID, root, filter)

        self.assertRaises(ex.Exception, trans.publishEvent, event)
    def testQueueOrder(self):
        # this tests the order in which the sender and receiver are created
        # and event is created and sent before the receiver exists, and the
        # receiver should still get the message

        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        eventSystem = events.EventSystem.getDefaultEventSystem()

        # test with default port
        queue = self.createDestination("test_queues4")
        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createEnqueuer(broker, queue)

        prop = self.createPropertySet()

        # create and publish an event without a runid, BEFORE
        # creating a receiver
        event = events.Event(prop)
        eventSystem.publishEvent(queue, event)

        eventSystem.createDequeuer(broker, queue)

        # should receive the event
        receivedEvent = eventSystem.receiveEvent(queue)
        self.assertIsNotNone(receivedEvent)

        # should be able to get the PropertySet we sent
        recvProp = receivedEvent.getPropertySet()
        self.assertIsNotNone(recvProp)

        # verify PropertySet contents
        self.verifyPropertySet(recvProp)
    def testMultiTopicSend(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        pid = os.getpid()

        host_pid = "%s_%d" % (thisHost, pid)

        topic1 = "test_events_6_%s" % host_pid
        topic2 = "test_events_6a_%s" % host_pid

        y1 = events.EventReceiver(broker, topic1)

        y2 = events.EventReceiver(broker, topic2)

        #
        # send a test event on both topics at once, and have each receiver wait to
        # receive it
        #
        self.sendEvent(broker, topic1 + "," + topic2)

        val = y1.receiveEvent()
        self.assertIsNotNone(val)

        val = y2.receiveEvent()
        self.assertIsNotNone(val)
    def testHostNameAndProcessId(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
        log.MDCRemove("x")
        e = recv.receiveEvent()
        self.assertValidMessage(e, "This is TRACE")
        ps = e.getPropertySet()

        # the host name should be the same since we're sending and receiving
        # from the same host
        host = ps.get(events.LogEvent.ORIG_HOSTNAME)
        self.assertEqual(host, socket.gethostname())

        # the process id should be the same since we're sending and receiving
        # from the same process
        pid = ps.get(events.LogEvent.ORIG_PROCESSID)
        self.assertEqual(os.getpid(), pid)

        t = ps.get(events.LogEvent.TYPE)
        self.assertEqual(t, events.EventTypes.LOG)
    def testPlainStatusEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.A")
        receiver = self.createReceiver(broker, topic)
        #
        # send a test event, and wait to receive it
        #
        self.sendPlainStatusEvent(broker, topic)

        val = receiver.receiveEvent()

        self.assertIsNotNone(val)
        values = [
            events.Event.EVENTTIME, events.StatusEvent.ORIG_HOSTNAME,
            events.StatusEvent.ORIG_LOCALID, events.StatusEvent.ORIG_PROCESSID,
            events.Event.PUBTIME, events.Event.STATUS, events.Event.TOPIC,
            events.Event.TYPE
        ]
        customValues = ['myname', 'logger']
        self.assertValid(val, values, customValues)

        self.assertNotEqual(val.getEventTime(), 0)
        self.assertNotEqual(val.getPubTime(), 0)
        self.assertGreater(val.getPubTime(), val.getEventTime())
        self.assertEqual(val.getType(), events.EventTypes.STATUS)
    def testEventTransmitterCreatedFirst(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        port = testEnv.getPort()
        thisHost = platform.node()

        topic = "test_events_queue2_%s_%d" % (thisHost, os.getpid())

        # Integer tests

        #
        # send two test events
        #
        self.sendEvent(broker, topic, port, 300)
        self.sendEvent(broker, topic, port, 200)

        recv = events.EventDequeuer(broker, topic, port);

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 300 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 300)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 200)

        # check to see no other events are waiting
        val = recv.receiveEvent(1)
        self.assertIsNone(val)
Beispiel #9
0
    def testBadPort(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        port = 1
        testEnv = EventsEnvironment()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER="+broker+"\n"
        confStr += "log4j.appender.EA.PORT="+str(port)+"\n"

        self.assertConfigOutput(confStr, "log4cxx: Couldn't reach broker " + broker + " at port " + str(port))
Beispiel #10
0
    def testBadBroker(self):
        testEnv = EventsEnvironment()
        badBroker = "badbroker.lsstcorp.org"
        port = testEnv.getPort()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER="+badBroker+"\n"

        #this should raise an exception because the broker couldn't be reached
        #self.assertRaises(ex.Exception, self.configure, confStr)
        self.assertConfigOutput(confStr, "log4cxx: Couldn't reach broker " + badBroker + " at port " + str(port))
    def testQueueSelector(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()
        eventSystem = events.EventSystem.getDefaultEventSystem()

        queue3 = self.createDestination("test_queue3")
        runid = "id007"

        eventSystem.createEnqueuer(broker, queue3)
        eventSystem.createDequeuer(broker, queue3, "%s = '%s'" % (events.Event.RUNID, runid))
        self.performEventSelectorTest(runid, queue3)
    def testQueueSpecifiedPort(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        eventSystem = events.EventSystem.getDefaultEventSystem()

        thisHost = platform.node()
        # test with specified port
        queue2 = self.createDestination("test_queues2")
        eventSystem.createEnqueuer(broker, queue2, testEnv.getPort())
        eventSystem.createDequeuer(broker, queue2, testEnv.getPort())

        self.performEventTest(queue2)
    def testTopicSelector(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        eventSystem = events.EventSystem.getDefaultEventSystem()

        topic3 = self.createDestination("test_topics3")
        runid = "id007"

        eventSystem.createTransmitter(broker, topic3)
        eventSystem.createReceiver(broker, topic3, "%s  = '%s'" % (events.Event.RUNID, runid))
        self.performEventSelectorTest(runid, topic3)
    def testTopicDefault(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        eventSystem = events.EventSystem.getDefaultEventSystem()

        # test with default port
        topic1 = self.createDestination("test_topics1")
        eventSystem.createTransmitter(broker, topic1)
        eventSystem.createReceiver(broker, topic1)

        self.performEventTest(topic1)
    def testDestinationPropertyName(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        port = testEnv.getPort()
        thisHost = platform.node()

        topic = "test_events_queue3_%s_%d" % (thisHost, os.getpid())
        recv = events.EventDequeuer(broker, topic, port);
        trans = events.EventDequeuer(broker, topic, port);

        self.assertEqual(events.Event.QUEUE, recv.getDestinationPropertyName());
        self.assertEqual(events.Event.QUEUE, trans.getDestinationPropertyName());
    def testQueueDefaultPort(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()
        eventSystem = events.EventSystem.getDefaultEventSystem()

        # test with default port
        queue1 = self.createDestination("test_queues1")
        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createEnqueuer(broker, queue1)
        eventSystem.createDequeuer(broker, queue1)

        self.performEventTest(queue1)
    def testTopicSpecified(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        eventSystem = events.EventSystem.getDefaultEventSystem()

        # test with specified port
        topic2 = self.createDestination("test_topics2")

        eventSystem.createTransmitter(broker, topic2, testEnv.getPort())
        eventSystem.createReceiver(broker, topic2, testEnv.getPort())

        self.performEventTest(topic2)
    def testBadPort(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        port = 1
        testEnv = EventsEnvironment()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + broker + "\n"
        confStr += "log4j.appender.EA.PORT=" + str(port) + "\n"

        self.assertConfigOutput(
            confStr, "log4cxx: Couldn't reach broker " + broker + " at port " +
            str(port))
    def testBadBroker(self):
        testEnv = EventsEnvironment()
        badBroker = "badbroker.lsstcorp.org"
        port = testEnv.getPort()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + badBroker + "\n"

        #this should raise an exception because the broker couldn't be reached
        #self.assertRaises(ex.Exception, self.configure, confStr)
        self.assertConfigOutput(
            confStr, "log4cxx: Couldn't reach broker " + badBroker +
            " at port " + str(port))
Beispiel #20
0
    def testFilterableStatusEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.B")
        receiver = self.createReceiver(broker, topic)
        #
        # send a test event, and wait to receive it
        #
        self.sendFilterableStatusEvent(broker, topic)
        val = receiver.receiveEvent()

        self.assertIsNotNone(val)
class MultiThreadTestCase(lsst.utils.tests.TestCase):
    """Test multitreaded sends"""
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testLockRelease(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        flag = Flag()
        t = EventThread(broker, flag)

        # wait until the thread has started up
        with flag.lock:
            # print "starting thread"
            flag.markers.append("thread starting")
            t.start()
            flag.lock.wait(2.0)

        # by the time we wake up, the other thread should be listening
        # for an event (that will never come :-o)
        time.sleep(0.2)
        # print "waiting for receive"
        # sys.stdout.flush()
        flag.markers.append("waiting")
        t.join()

        self.assertEquals(flag.markers[2], "waiting",
                          "receive failed to release GIL")
    def testEventSystemReceive(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_%s_%d" % (thisHost, os.getpid())

        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createTransmitter(broker, topic)
        eventSystem.createReceiver(broker, topic)

        #
        # send a test event, and wait to receive it
        #
        self.sendEvent(topic)

        val = eventSystem.receiveEvent(topic)
        self.assertIsNotNone(val)

        cpNames = val.getCustomPropertyNames()
        names = ["DATE", "PID", "HOST", "IP", "EVNT", "misc1", "misc2", "value", "array1", "array2", "array3"]

        self.assertEqual(len(cpNames), len(names))
        for x in names:
            self.assertTrue(x in cpNames)

        ps = val.getCustomPropertySet()

        self.assertEqual(ps.nameCount(), len(names))
        self.assertEqual(ps.get("DATE"), "2007-07-01T14:28:32.546012")
        self.assertEqual(ps.get("PID"), 200)
        self.assertEqual(ps.get("HOST"), "lsstcorp.org")
        self.assertEqual(ps.get("IP"), "1.2.3.4")
        self.assertEqual(ps.get("EVNT"), "test")
        self.assertEqual(ps.get("misc1"), "data 1")
        self.assertEqual(ps.get("misc2"), "data 2")
        self.assertEqual(ps.get("value"), 3.14)
        self.assertTupleEqual(ps.get("array1"), ('a', 'b', 'c', 'd'))
        self.assertTupleEqual(ps.get("array2"), (1, 2, 3, 4, 5))
        self.assertTupleEqual(ps.get("array3"), (6.1, 6.2, 6.3, 6.4, 6.5))

        #
        # wait a short time to receive an event.  none was sent, so we should
        # time out and confirm that we didn't get anything
        #
        val = eventSystem.receiveEvent(topic, 1000)
        self.assertIsNone(val)
Beispiel #23
0
    def testFilterableStatusEventWithRunID(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.D")

        receiver = self.createReceiver(broker, topic)

        self.sendFilterableStatusEvent(broker, topic, "test_runID_10")

        val = receiver.receiveEvent()

        # should receive an event
        self.assertIsNotNone(val)
        values = [events.Event.EVENTTIME, 'FOO', events.StatusEvent.ORIG_HOSTNAME, events.StatusEvent.ORIG_LOCALID, events.StatusEvent.ORIG_PROCESSID, 'PLOUGH', 'PLOVER', events.Event.PUBTIME, events.Event.RUNID, events.Event.STATUS, events.Event.TOPIC, events.Event.TYPE]
        customValues = ['myname']
        self.assertValid(val, values, customValues)
Beispiel #24
0
    def testEventTransmitter(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_5_%s_%d" % (thisHost, os.getpid())
        recv = events.EventReceiver(broker, topic)


        # Integer tests

        #
        # send two test events
        #
        self.sendEvent(broker, topic, 300)
        self.sendEvent(broker, topic, 200)
        self.sendEvent(broker, topic, ( 6, 7, 8, 9, 6))

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 300 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 300)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 200)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertTupleEqual(ps.get("FOO"), (6, 7, 8, 9, 6))

        # check to see no other events are waiting
        val = recv.receiveEvent(1)
        self.assertIsNone(val)
    def testBrokerOption(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")
Beispiel #26
0
    def testBrokerOption(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER="+testEnv.getBroker()+"\n"
        confStr += "log4j.appender.EA.TOPIC="+topic+"\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")
    def testEventTransmitter(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_5_%s_%d" % (thisHost, os.getpid())
        recv = events.EventReceiver(broker, topic)

        # Integer tests

        #
        # send two test events
        #
        self.sendEvent(broker, topic, 300)
        self.sendEvent(broker, topic, 200)
        self.sendEvent(broker, topic, (6, 7, 8, 9, 6))

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 300 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 300)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 200)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertTupleEqual(ps.get("FOO"), (6, 7, 8, 9, 6))

        # check to see no other events are waiting
        val = recv.receiveEvent(1)
        self.assertIsNone(val)
class EventTransmitterTestCase(lsst.utils.tests.TestCase):
    """Test the EventTransmitter"""
    def sendEvent(self, broker, topicName, value):
        root = base.PropertySet()
        root.addInt("FOO", value)
        trans = events.EventTransmitter(broker, topicName)
        event = events.Event("myrunid", root)
        trans.publishEvent(event)

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testEventTransmitter(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_5_%s_%d" % (thisHost, os.getpid())
        recv = events.EventReceiver(broker, topic)

        # Integer tests

        #
        # send two test events
        #
        self.sendEvent(broker, topic, 300)
        self.sendEvent(broker, topic, 200)
        self.sendEvent(broker, topic, (6, 7, 8, 9, 6))

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 300 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 300)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertEqual(ps.get("FOO"), 200)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check to see we got 200 back
        ps = val.getPropertySet()
        self.assertTupleEqual(ps.get("FOO"), (6, 7, 8, 9, 6))

        # check to see no other events are waiting
        val = recv.receiveEvent(1)
        self.assertIsNone(val)
    def testFilterableStatusEventWithRunID(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.D")

        receiver = self.createReceiver(broker, topic)

        self.sendFilterableStatusEvent(broker, topic, "test_runID_10")

        val = receiver.receiveEvent()

        # should receive an event
        self.assertIsNotNone(val)
        values = [
            events.Event.EVENTTIME, 'FOO', events.StatusEvent.ORIG_HOSTNAME,
            events.StatusEvent.ORIG_LOCALID, events.StatusEvent.ORIG_PROCESSID,
            'PLOUGH', 'PLOVER', events.Event.PUBTIME, events.Event.RUNID,
            events.Event.STATUS, events.Event.TOPIC, events.Event.TYPE
        ]
        customValues = ['myname']
        self.assertValid(val, values, customValues)
Beispiel #30
0
    def testPlainStatusEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic = self.createTopicName("test_events_10_%s.A")
        receiver = self.createReceiver(broker, topic)
        #
        # send a test event, and wait to receive it
        #
        self.sendPlainStatusEvent(broker, topic)

        val = receiver.receiveEvent()

        self.assertIsNotNone(val)
        values = [events.Event.EVENTTIME, events.StatusEvent.ORIG_HOSTNAME, events.StatusEvent.ORIG_LOCALID, events.StatusEvent.ORIG_PROCESSID, events.Event.PUBTIME, events.Event.STATUS, events.Event.TOPIC, events.Event.TYPE]
        customValues = ['myname', 'logger']
        self.assertValid(val, values, customValues)

        self.assertNotEqual(val.getEventTime(), 0)
        self.assertNotEqual(val.getPubTime(), 0)
        self.assertGreater(val.getPubTime(), val.getEventTime())
        self.assertEqual(val.getType(), events.EventTypes.STATUS)
Beispiel #31
0
    def testCombinedEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic1 = "test_events_3_%s_%d" % (platform.node(), os.getpid())
        topic2 = "test_events_3a_%s_%d" % (platform.node(), os.getpid())
        combinedTopic = topic1 + "," + topic2
        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createReceiver(broker, topic1)
        eventSystem.createReceiver(broker, topic2)
        eventSystem.createTransmitter(broker, combinedTopic)

        #
        # send a test event on both topics at once, and have each receiver wait to
        # receive it
        #
        self.sendEvent(combinedTopic)

        val = eventSystem.receiveEvent(topic1)
        self.assertIsNotNone(val)

        val = eventSystem.receiveEvent(topic2)
        self.assertIsNotNone(val)
    def testCombinedEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic1 = "test_events_3_%s_%d" % (platform.node(), os.getpid())
        topic2 = "test_events_3a_%s_%d" % (platform.node(), os.getpid())
        combinedTopic = topic1 + "," + topic2
        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createReceiver(broker, topic1)
        eventSystem.createReceiver(broker, topic2)
        eventSystem.createTransmitter(broker, combinedTopic)

        #
        # send a test event on both topics at once, and have each receiver wait to
        # receive it
        #
        self.sendEvent(combinedTopic)

        val = eventSystem.receiveEvent(topic1)
        self.assertIsNotNone(val)

        val = eventSystem.receiveEvent(topic2)
        self.assertIsNotNone(val)
Beispiel #33
0
class StatusEventOriginatorTestCase(lsst.utils.tests.TestCase):
    """Test StatusEvent Originator"""
    def setUp(self):
        locationId = events.LocationId()
        locationId.reset()

    def sendEvent(self, brokerName, topic):
        """Send an Event."""
        trans = events.EventTransmitter(brokerName, topic)

        root = PropertySet()
        root.set(events.Event.TOPIC, topic)
        root.set("myname", "myname")
        root.set(events.StatusEvent.STATUS, "my special status")
        root.set(events.StatusEvent.RUNID, "srptestrun")
        root.set("bazinga", "sheldon")

        originatorId = events.LocationId()
        event = events.StatusEvent("srptestrun", originatorId, root)

        # ok...now publish it
        trans.publishEvent(event)

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testStatusEventOriginator(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_12_%s_%d" % (thisHost, os.getpid())

        recv = events.EventReceiver(broker, topic)

        #
        # send a test event, and wait to receive it
        #
        self.sendEvent(broker, topic)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        eventSystem = events.EventSystem.getDefaultEventSystem()
        statusevent = eventSystem.castToStatusEvent(val)

        originatorID = statusevent.getOriginator()
        self.assertEqual(originatorID.getLocalID(), 0)
        self.assertEqual(originatorID.getProcessID(), os.getpid())
        self.assertEqual(platform.node(), originatorID.getHostName())
    def testLockRelease(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        flag = Flag()
        t = EventThread(broker, flag)

        # wait until the thread has started up
        with flag.lock:
            # print "starting thread"
            flag.markers.append("thread starting")
            t.start()
            flag.lock.wait(2.0)

        # by the time we wake up, the other thread should be listening
        # for an event (that will never come :-o)
        time.sleep(0.2)
        # print "waiting for receive"
        # sys.stdout.flush()
        flag.markers.append("waiting")
        t.join()

        self.assertEquals(flag.markers[2], "waiting",
                          "receive failed to release GIL")
Beispiel #35
0
    def testLockRelease(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        flag = Flag()
        t = EventThread(broker, flag)

        # wait until the thread has started up
        with flag.lock:
            # print "starting thread"
            flag.markers.append("thread starting")
            t.start()
            flag.lock.wait(2.0)

        # by the time we wake up, the other thread should be listening
        # for an event (that will never come :-o)
        time.sleep(0.2)
        # print "waiting for receive"
        # sys.stdout.flush()
        flag.markers.append("waiting")
        t.join()

        self.assertEquals(flag.markers[2], "waiting",
                          "receive failed to release GIL")
Beispiel #36
0
    def testStatusEventOriginator(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_12_%s_%d" % (thisHost, os.getpid())

        recv = events.EventReceiver(broker, topic)

        #
        # send a test event, and wait to receive it
        #
        self.sendEvent(broker, topic)

        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        eventSystem = events.EventSystem.getDefaultEventSystem()
        statusevent = eventSystem.castToStatusEvent(val)

        originatorID = statusevent.getOriginator()
        self.assertEqual(originatorID.getLocalID(), 0)
        self.assertEqual(originatorID.getProcessID(), os.getpid())
        self.assertEqual(platform.node(), originatorID.getHostName())
class MultiTopicSendTestCase(lsst.utils.tests.TestCase):
    """Send a multitopic event"""
    def sendEvent(self, broker, topic):
        trans = events.EventTransmitter(broker, topic)

        root = base.PropertySet()
        root.set("DATE", "2007-07-01T14:28:32.546012")
        root.setInt("PID", 200)
        root.set("HOST", "lsstcorp.org")
        root.set("IP", "1.2.3.4")
        root.set("EVNT", "test")
        root.set("misc1", "data 1")
        root.set("misc2", "data 2")
        root.set("value", 3.14)

        event = events.Event("runid_test6", root)
        trans.publishEvent(event)

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testMultiTopicSend(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        pid = os.getpid()

        host_pid = "%s_%d" % (thisHost, pid)

        topic1 = "test_events_6_%s" % host_pid
        topic2 = "test_events_6a_%s" % host_pid

        y1 = events.EventReceiver(broker, topic1)

        y2 = events.EventReceiver(broker, topic2)

        #
        # send a test event on both topics at once, and have each receiver wait to
        # receive it
        #
        self.sendEvent(broker, topic1 + "," + topic2)

        val = y1.receiveEvent()
        self.assertIsNotNone(val)

        val = y2.receiveEvent()
        self.assertIsNotNone(val)
class EventSelectorTestCase(lsst.utils.tests.TestCase):
    """Test receiving events using the selector mechanism"""

    def sendEvent(self, runid, brokerName, topic):
        trans = events.EventTransmitter(brokerName, topic)

        root = PropertySet()
        root.set(events.Event.TOPIC, topic)
        root.set("myname","myname")
        root.set(events.Event.STATUS, "my special status")

        locationID = events.LocationId()

        event = events.StatusEvent(runid, locationID, root)
        # ok...now publish it
        trans.publishEvent(event)

    @unittest.skipUnless(EventsEnvironment().validTestDomain(), "not within valid domain")
    def testEventSelector(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        host_pid = "%s_%d" % (thisHost, os.getpid())

        topic = "test_events_11_%s" % host_pid

        runid = 'test_runid_11_%d' % os.getpid()

        rec = events.EventReceiver(broker, topic, "%s = '%s'" % (events.Event.RUNID, runid))

        #
        # send a test event, and wait to receive it
        #
        self.sendEvent(runid, broker, topic)

        # we'll get the second event, not the first
        val = rec.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        self.assertTrue(ps.exists(events.Event.RUNID))
        self.assertEqual(ps.get(events.Event.RUNID),runid)

        self.sendEvent("invalid", broker, topic)
        # shouldn't receive anything else
        val2 = rec.receiveEvent(1)
        self.assertIsNone(val2)
class CombinedEventTestCase(lsst.utils.tests.TestCase):
    """test sending one message two multiple topics with one publish"""
    def sendEvent(self, topicName):

        root = base.PropertySet()
        root.set("DATE", "2007-07-01T14:28:32.546012")
        root.setInt("PID", 200)
        root.set("HOST", "lsstcorp.org")
        root.set("IP", "1.2.3.4")
        root.set("EVNT", "test")
        root.set("misc1", "data 1")
        root.set("misc2", "data 2")
        root.set("value", 3.14)

        eventSystem = events.EventSystem.getDefaultEventSystem()
        event = events.Event("runid_es6", root)
        eventSystem.publishEvent(topicName, event)

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testCombinedEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()

        topic1 = "test_events_3_%s_%d" % (platform.node(), os.getpid())
        topic2 = "test_events_3a_%s_%d" % (platform.node(), os.getpid())
        combinedTopic = topic1 + "," + topic2
        eventSystem = events.EventSystem.getDefaultEventSystem()
        eventSystem.createReceiver(broker, topic1)
        eventSystem.createReceiver(broker, topic2)
        eventSystem.createTransmitter(broker, combinedTopic)

        #
        # send a test event on both topics at once, and have each receiver wait to
        # receive it
        #
        self.sendEvent(combinedTopic)

        val = eventSystem.receiveEvent(topic1)
        self.assertIsNotNone(val)

        val = eventSystem.receiveEvent(topic2)
        self.assertIsNotNone(val)
Beispiel #40
0
    def testCombinedReceive(self):
        # Test sending on two topics, and receiving bother on a third.
        #
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        pid = os.getpid()
        host_pid = "%s_%d" % (thisHost,pid)

        topicA = "test_events_9_%s.A" % host_pid
        topicB = "test_events_9_%s.B" % host_pid
        topicC = "test_events_9_%s.*" % host_pid

        recvA = events.EventReceiver(broker, topicA)
        recvB = events.EventReceiver(broker, topicB)
        recvC = events.EventReceiver(broker, topicC)

        # send a test event on topicA and topicB
        self.sendEvent(broker, topicA)
        self.sendEvent(broker, topicB)

        # receive event on topicA, and check to see it's the right one
        val = recvA.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        name = ps.get(events.Event.TOPIC)
        self.assertEqual(name, topicA)

        # receiving no more messages on topicA
        val = recvA.receiveEvent(1)
        self.assertIsNone(val)

        # receive event on topicB, and check to see it's the right one
        val = recvB.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        name = ps.get(events.Event.TOPIC)
        self.assertEqual(name, topicB)

        # receiving no more messages on topicB
        val = recvB.receiveEvent(1)
        self.assertIsNone(val)

        # receive event on topicC, and check to see it's the one sent to topicA
        val = recvC.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        name = ps.get(events.Event.TOPIC)
        self.assertEqual(name, topicA)

        # receive event on topicC, and check to see it's the one sent to topicB
        val = recvC.receiveEvent()
        self.assertIsNotNone(val)
        ps = val.getPropertySet()
        name = ps.get(events.Event.TOPIC)
        self.assertEqual(name, topicB)

        # receiving no more messages on topicC
        val = recvC.receiveEvent(1)
        self.assertIsNone(val)
class TestLog(lsst.utils.tests.TestCase):
    class StderrCapture(object):
        """Redirect stderr to a file."""
        def __init__(self, filename):
            self.prevfd = None
            self.prev = None

        def __enter__(self):
            F = tempfile.NamedTemporaryFile()
            self.prevfd = os.dup(sys.stderr.fileno())
            os.dup2(F.fileno(), sys.stderr.fileno())
            self.prev = sys.stderr
            sys.stderr = os.fdopen(self.prevfd, "w")
            return F

        def __exit__(self, exc_type, exc_value, traceback):
            os.dup2(self.prevfd, self.prev.fileno())
            sys.stderr = self.prev

    def setUp(self):
        """Make a temporary directory and a log file in it."""
        self.tempDir = tempfile.mkdtemp()
        self.outputFilename = os.path.join(self.tempDir, "log.out")

    def tearDown(self):
        """Remove the temporary directory."""
        shutil.rmtree(self.tempDir)

    def configure(self, configuration):
        """
        Create a configuration file in the temporary directory and populate
        it with the provided string.
        """
        log.configure_prop(configuration.format(self.outputFilename))

    def assertConfigOutput(self, configProperties, value):
        with TestLog.StderrCapture(self) as fd:
            self.configure(configProperties)
            fd.seek(0)
            log4cxxOutput = ""
            for line in fd:
                log4cxxOutput += line.decode('UTF-8').rstrip()
            self.assertEquals(log4cxxOutput, value)

    def assertValidMessage(self, event, message):
        ps = event.getPropertySet()
        self.assertEqual(ps.get("message"), message)

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBrokerOption(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBrokerPortOption(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.PORT=" + str(testEnv.getPort()) + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBrokerTopicOption(self):
        testEnv = EventsEnvironment()
        weirdTopic = testEnv.getLoggingTopic() + "_" + testEnv.getLoggingTopic(
        )
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.PORT=" + str(testEnv.getPort()) + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + weirdTopic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), weirdTopic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBrokerAlternateTopicOption(self):
        testEnv = EventsEnvironment()
        topic = platform.node() + "_" + str(os.getpid())
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.PORT=" + str(testEnv.getPort()) + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)

        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testMissingBroker(self):
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"

        # this should raise an exception because no broker is specified
        #self.assertRaises(ex.Exception, self.configure, confStr)
        self.assertConfigOutput(
            confStr,
            "log4cxx: Appender requires broker option to be specified")

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBadBroker(self):
        testEnv = EventsEnvironment()
        badBroker = "badbroker.lsstcorp.org"
        port = testEnv.getPort()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + badBroker + "\n"

        #this should raise an exception because the broker couldn't be reached
        #self.assertRaises(ex.Exception, self.configure, confStr)
        self.assertConfigOutput(
            confStr, "log4cxx: Couldn't reach broker " + badBroker +
            " at port " + str(port))

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testBadPort(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        port = 1
        testEnv = EventsEnvironment()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + broker + "\n"
        confStr += "log4j.appender.EA.PORT=" + str(port) + "\n"

        self.assertConfigOutput(
            confStr, "log4cxx: Couldn't reach broker " + broker + " at port " +
            str(port))

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testRunidSelector(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.RUNID=" + str(os.getpid()) + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        # receive for all events
        recvALL = events.EventReceiver(testEnv.getBroker(), topic)

        # receive for events for this runid
        recv = events.EventReceiver(testEnv.getBroker(), topic,
                                    "RUNID = '%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we got all the events we should have
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # reconfigure with a new run id
        confStr2 = "log4j.rootLogger=TRACE, EA\n"
        confStr2 += "log4j.appender.EA=EventAppender\n"
        confStr2 += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr2 += "log4j.appender.EA.RUNID=" + "blah_" + str(
            os.getpid()) + "\n"
        confStr2 += "log4j.appender.EA.TOPIC=" + topic + "\n"
        self.configure(confStr2)

        # set up a receiver for the new run id
        recv2 = events.EventReceiver(testEnv.getBroker(), topic,
                                     "RUNID = 'blah_%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we didn't receive any events from another run id
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events we should have
        self.assertValidMessage(recv2.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv2.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv2.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv2.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events, for all messages on this topic
        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recvALL.receiveEvent(100)
        self.assertIsNone(ev)

###############################################################################

    @unittest.skipUnless(EventsEnvironment().validTestDomain(),
                         "not within valid domain")
    def testHostNameAndProcessId(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        recv = events.EventReceiver(testEnv.getBroker(), topic)
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
        log.MDCRemove("x")
        e = recv.receiveEvent()
        self.assertValidMessage(e, "This is TRACE")
        ps = e.getPropertySet()

        # the host name should be the same since we're sending and receiving
        # from the same host
        host = ps.get(events.LogEvent.ORIG_HOSTNAME)
        self.assertEqual(host, socket.gethostname())

        # the process id should be the same since we're sending and receiving
        # from the same process
        pid = ps.get(events.LogEvent.ORIG_PROCESSID)
        self.assertEqual(os.getpid(), pid)

        t = ps.get(events.LogEvent.TYPE)
        self.assertEqual(t, events.EventTypes.LOG)
    def testRunidSelector(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr += "log4j.appender.EA.RUNID=" + str(os.getpid()) + "\n"
        confStr += "log4j.appender.EA.TOPIC=" + topic + "\n"

        self.configure(confStr)

        # receive for all events
        recvALL = events.EventReceiver(testEnv.getBroker(), topic)

        # receive for events for this runid
        recv = events.EventReceiver(testEnv.getBroker(), topic,
                                    "RUNID = '%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we got all the events we should have
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # reconfigure with a new run id
        confStr2 = "log4j.rootLogger=TRACE, EA\n"
        confStr2 += "log4j.appender.EA=EventAppender\n"
        confStr2 += "log4j.appender.EA.BROKER=" + testEnv.getBroker() + "\n"
        confStr2 += "log4j.appender.EA.RUNID=" + "blah_" + str(
            os.getpid()) + "\n"
        confStr2 += "log4j.appender.EA.TOPIC=" + topic + "\n"
        self.configure(confStr2)

        # set up a receiver for the new run id
        recv2 = events.EventReceiver(testEnv.getBroker(), topic,
                                     "RUNID = 'blah_%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we didn't receive any events from another run id
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events we should have
        self.assertValidMessage(recv2.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv2.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv2.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv2.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events, for all messages on this topic
        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recvALL.receiveEvent(100)
        self.assertIsNone(ev)
Beispiel #43
0
    def testLocationId(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        locationID = events.LocationId()

        # check to see if localID is 0
        localId = locationID.getLocalID()
        self.assertEqual(localId, 0)

        # check host name
        hostname = locationID.getHostName()
        self.assertEqual(hostname, socket.gethostname())

        # check process id
        processId = locationID.getProcessID()
        self.assertEqual(processId, os.getpid())

        locationID2 = events.LocationId()

        # check to see if localID is 1
        localId = locationID2.getLocalID()
        self.assertEqual(localId, 1)

        self.assertEqual(locationID.getLocalID(), 0)

        # check host name
        hostname = locationID.getHostName()
        self.assertEqual(hostname, socket.gethostname())

        # check process id
        processId = locationID2.getProcessID()
        self.assertEqual(processId, os.getpid())


        root = PropertySet()
        root.set("myname","myname")
        status = "my special status"
        root.set(events.Event.STATUS, status)

        statusEvent = events.StatusEvent("my runid", locationID, root)
        statusEvent2 = events.StatusEvent("my runid", locationID2, root)

        topic = "mytopic_%s_%d" % (thisHost, os.getpid())
        transmitter = events.EventTransmitter(broker, topic)

        hostname = locationID2.getHostName()

        # create a receiver selector with the hostname, process id and local id
        sel = "%s = '%s' and %s = %d and %s = %d" % (events.StatusEvent.ORIG_HOSTNAME, hostname, events.StatusEvent.ORIG_PROCESSID, os.getpid(), events.StatusEvent.ORIG_LOCALID, 1)

        receiver = events.EventReceiver(broker, topic, sel)

        # transmit the events
        transmitter.publishEvent(statusEvent)
        transmitter.publishEvent(statusEvent2)

        # should receive event with with the process id
        returnedEvent = receiver.receiveEvent()
        ps = returnedEvent.getPropertySet()
        self.assertEqual(ps.get('ORIG_HOSTNAME'), hostname)
        self.assertEqual(ps.get('ORIG_PROCESSID'), os.getpid())
        self.assertEqual(ps.get('ORIG_LOCALID'), 1)

        # should NOT receive another event, because it was filtered out by
        # the broker
        returnedEvent2 = receiver.receiveEvent(1)
        self.assertIsNone(returnedEvent2)
Beispiel #44
0
    def testCommandEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_command_%s_%d" % (thisHost, os.getpid())

        # send a command event
        receiver = events.EventReceiver(broker, topic)
        trans = events.EventTransmitter(broker, topic)

        originatorId = events.LocationId()
        root = PropertySet()
        root.set(events.StatusEvent.TOPIC,topic)
        root.set("myname","myname")
        root.set(events.StatusEvent.STATUS, "my special status")
        event = events.StatusEvent("srptestrun", originatorId, root)

        statusOriginatorId = event.getOriginator()
        destinationID = events.LocationId(statusOriginatorId)

        commandOriginatorID = events.LocationId()
        root2 = PropertySet()
        root2.set(events.CommandEvent.TOPIC,topic)
        root2.set("myname","myname2")
        root2.set(events.CommandEvent.STATUS, "my special status2")
        event2 = events.CommandEvent("srptestrun", commandOriginatorID, destinationID, root2)

        trans.publishEvent(event2)
        val = receiver.receiveCommandEvent()

        # be sure we received an event
        self.assertIsNotNone(val)
        # these are the filterable names we expect to see
        names = [events.CommandEvent.DEST_HOSTNAME, events.CommandEvent.DEST_LOCALID, events.CommandEvent.DEST_PROCESSID,
                events.CommandEvent.EVENTTIME, events.CommandEvent.ORIG_HOSTNAME, events.CommandEvent.ORIG_LOCALID,
                events.CommandEvent.ORIG_PROCESSID, events.CommandEvent.PUBTIME, events.CommandEvent.RUNID,
                events.CommandEvent.STATUS, events.CommandEvent.TOPIC, events.CommandEvent.TYPE]
        self.assertValid(val, names, commandOriginatorID.getLocalID(), destinationID.getLocalID())


        # send a command event with additional filterable properties
        receiver = events.EventReceiver(broker, topic)

        trans = events.EventTransmitter(broker, topic)

        originatorId = events.LocationId()
        event = events.StatusEvent("srptestrun", originatorId, root)

        statusOriginatorId = event.getOriginator()
        destinationID = events.LocationId(statusOriginatorId)

        commandOriginatorID = events.LocationId()

        filterable = PropertySet()
        filterable.set("FOO", 12.3)
        filterable.set("BAR", .45)

        event2 = events.CommandEvent("srptestrun", commandOriginatorID, destinationID, root2, filterable)

        trans.publishEvent(event2)

        val = receiver.receiveCommandEvent()

        # be sure we received an event
        self.assertIsNotNone(val)

        # these are the filterable names we expect to see
        names = [events.CommandEvent.DEST_HOSTNAME, events.CommandEvent.DEST_LOCALID, events.CommandEvent.DEST_PROCESSID,
                events.CommandEvent.EVENTTIME, events.CommandEvent.ORIG_HOSTNAME, events.CommandEvent.ORIG_LOCALID,
                events.CommandEvent.ORIG_PROCESSID, events.CommandEvent.PUBTIME, events.CommandEvent.RUNID,
                events.CommandEvent.STATUS, events.CommandEvent.TOPIC, events.CommandEvent.TYPE, 'FOO', 'BAR']
        self.assertValid(val, names, commandOriginatorID.getLocalID(), destinationID.getLocalID())

        self.assertTrue(event.getType(), events.EventTypes.COMMAND)
Beispiel #45
0
    def testSendEvent(self):
        """Send an Event"""
        testEnv = EventsEnvironment()
        self.thisHost = platform.node()
        self.broker = testEnv.getBroker()

        topic = "test_events_3_%s_%d" % (self.thisHost, os.getpid())

        runID = "test3_runid"
        recv = events.EventReceiver(self.broker, topic)

        trans = events.EventTransmitter(self.broker, topic)


        DATE = "date"
        DATE_VAL = "2007-07-01T14:28:32.546012"

        BLANK = "blank"
        BLANK_VAL = ""

        PID = "pid"
        PID_VAL = os.getpid()

        HOST = "host"
        HOST_VAL = "lsstcorp.org"

        IP = "ip"
        IP_VAL = "1.2.3.4"

        EVNT = "evnt"
        EVNT_VAL = "test"

        MISC1 = "misc1"
        MISC1_VAL = "data 1"

        MISC2 = "misc2"
        MISC2_VAL = "data 2"

        MISC3 = "misc3"
        MISC3_VAL = ""

        DATA = "data"
        DATA_VAL = 3.14

        root = PropertySet()
        root.set(DATE, DATE_VAL)
        root.set(BLANK, BLANK_VAL)
        root.setInt(PID, PID_VAL)
        root.set(HOST, HOST_VAL)
        root.set(IP, IP_VAL)
        root.set(EVNT, EVNT_VAL)
        root.set(MISC1, MISC1_VAL)
        root.set(MISC2, MISC2_VAL)
        root.set(MISC3, MISC3_VAL)
        root.setDouble(DATA, DATA_VAL)

        event = events.Event(runID, root)
        trans.publishEvent(event)


        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        # check the validity of all sent values
        ps = val.getPropertySet()
        self.assertEqual(ps.get(DATE), DATE_VAL)
        self.assertEqual(ps.get(BLANK), BLANK_VAL)
        self.assertEqual(ps.get(PID), PID_VAL)
        self.assertEqual(ps.get(HOST), HOST_VAL)
        self.assertEqual(ps.get(IP), IP_VAL)
        self.assertEqual(ps.get(EVNT), EVNT_VAL)
        self.assertEqual(ps.get(MISC1), MISC1_VAL)
        self.assertEqual(ps.get(MISC2), MISC2_VAL)
        self.assertEqual(ps.get(MISC3), MISC3_VAL)
        self.assertEqual(ps.get(DATA), DATA_VAL)

        self.assertGreater(ps.get(events.Event.EVENTTIME), 0)
        self.assertGreater(ps.get(events.Event.PUBTIME), 0)
        self.assertEqual(ps.get(events.Event.RUNID), runID)
        self.assertEqual(ps.get(events.Event.STATUS), "unknown")
        self.assertEqual(ps.get(events.Event.TOPIC), topic)
        self.assertEqual(ps.get(events.Event.TYPE), events.EventTypes.EVENT)

        #
        # wait a short time to receive an event.  none was sent, so we should
        # time out and confirm that we didn't get anything
        #
        val = recv.receiveEvent(1000)
        self.assertIsNone(val)
Beispiel #46
0
    def testRunidSelector(self):
        testEnv = EventsEnvironment()
        topic = testEnv.getLoggingTopic()
        confStr = "log4j.rootLogger=TRACE, EA\n"
        confStr += "log4j.appender.EA=EventAppender\n"
        confStr += "log4j.appender.EA.BROKER="+testEnv.getBroker()+"\n"
        confStr += "log4j.appender.EA.RUNID="+str(os.getpid())+"\n";
        confStr += "log4j.appender.EA.TOPIC="+topic+"\n"

        self.configure(confStr)

        # receive for all events
        recvALL = events.EventReceiver(testEnv.getBroker(), topic)

        # receive for events for this runid
        recv = events.EventReceiver(testEnv.getBroker(), topic, "RUNID = '%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we got all the events we should have
        self.assertValidMessage(recv.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # reconfigure with a new run id
        confStr2 = "log4j.rootLogger=TRACE, EA\n"
        confStr2 += "log4j.appender.EA=EventAppender\n"
        confStr2 += "log4j.appender.EA.BROKER="+testEnv.getBroker()+"\n"
        confStr2 += "log4j.appender.EA.RUNID="+"blah_"+str(os.getpid())+"\n";
        confStr2 += "log4j.appender.EA.TOPIC="+topic+"\n"
        self.configure(confStr2)

        # set up a receiver for the new run id
        recv2 = events.EventReceiver(testEnv.getBroker(), topic, "RUNID = 'blah_%s'" % str(os.getpid()))

        # send log messages
        log.MDC("x", 3)
        with log.LogContext("component"):
            log.trace("This is TRACE")
            log.info("This is INFO")
            log.debug("This is DEBUG")
        log.MDCRemove("x")

        # make sure we didn't receive any events from another run id
        ev = recv.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events we should have
        self.assertValidMessage(recv2.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recv2.receiveEvent(), "This is INFO")
        self.assertValidMessage(recv2.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recv2.receiveEvent(100)
        self.assertIsNone(ev)

        # make sure we got all the events, for all messages on this topic
        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        self.assertValidMessage(recvALL.receiveEvent(), "This is TRACE")
        self.assertValidMessage(recvALL.receiveEvent(), "This is INFO")
        self.assertValidMessage(recvALL.receiveEvent(), "This is DEBUG")

        # make sure we didn't get any more than we should have
        ev = recvALL.receiveEvent(100)
        self.assertIsNone(ev)
Beispiel #47
0
    def testFilterableSendEvent(self):
        testEnv = EventsEnvironment()
        broker = testEnv.getBroker()
        thisHost = platform.node()

        topic = "test_events_filters_%s_%d" % (thisHost, os.getpid())

        runId = "test_filters_runid"
        recv = events.EventReceiver(broker, topic)

        trans = events.EventTransmitter(broker, topic)

        root = PropertySet()

        DATE = "date"
        DATE_VAL = "2007-07-01T14:28:32.546012"
        root.set(DATE, DATE_VAL)

        BLANK = "blank"
        BLANK_VAL = ""
        root.set(BLANK, BLANK_VAL)

        PID = "pid"
        PID_VAL = os.getpid()
        root.setInt(PID, PID_VAL)

        HOST = "host"
        HOST_VAL = "lsstcorp.org"
        root.set(HOST, HOST_VAL)

        IP = "ip"
        IP_VAL = "1.2.3.4"
        root.set(IP, IP_VAL)

        EVNT = "evnt"
        EVNT_VAL = "test"
        root.set(EVNT, EVNT_VAL)

        MISC1 = "misc1"
        MISC1_VAL = "data 1"
        root.set(MISC1, MISC1_VAL)

        MISC2 = "misc2"
        MISC2_VAL = "data 2"
        root.set(MISC2, MISC2_VAL)

        MISC3 = "misc3"
        MISC3_VAL = ""
        root.set(MISC3, MISC3_VAL)

        DATA = "data"
        DATA_VAL = 3.14
        root.setDouble(DATA, DATA_VAL)

        filterable = PropertySet()
        filterable.set("FOO", "bar")
        filterable.set("XYZZY", 123)
        filterable.set("PLOUGH", 0.867)

        event = events.Event(runId, root, filterable)
        trans.publishEvent(event)


        val = recv.receiveEvent()
        self.assertIsNotNone(val)

        ps = val.getPropertySet()

        self.assertEqual(ps.get(DATE), DATE_VAL)
        self.assertEqual(ps.get(BLANK), BLANK_VAL)
        self.assertEqual(ps.get(PID), PID_VAL)
        self.assertEqual(ps.get(HOST), HOST_VAL)
        self.assertEqual(ps.get(IP), IP_VAL)
        self.assertEqual(ps.get(EVNT), EVNT_VAL)
        self.assertEqual(ps.get(MISC1), MISC1_VAL)
        self.assertEqual(ps.get(MISC2), MISC2_VAL)
        self.assertEqual(ps.get(MISC3), MISC3_VAL)
        self.assertEqual(ps.get(DATA), DATA_VAL)

        self.assertGreater(ps.get(events.Event.EVENTTIME), 0)
        self.assertGreater(ps.get(events.Event.PUBTIME), 0)
        self.assertEqual(ps.get(events.Event.RUNID), runId)
        self.assertEqual(ps.get(events.Event.STATUS), "unknown")
        self.assertEqual(ps.get(events.Event.TOPIC), topic)
        self.assertEqual(ps.get(events.Event.TYPE), events.EventTypes.EVENT)
        self.assertEqual(ps.get("FOO"), "bar")
        self.assertEqual(ps.get("XYZZY"), 123)
        self.assertEqual(ps.get("PLOUGH"), 0.867)


        names = val.getFilterablePropertyNames()

        values = [events.Event.EVENTTIME, 'FOO', 'PLOUGH', events.Event.PUBTIME, events.Event.RUNID, events.Event.STATUS,
                events.Event.TOPIC, events.Event.TYPE, 'XYZZY']

        for x in values:
            self.assertTrue(x in names)

        #
        # wait a short time to receive an event.  none was sent, so we should
        # time out and confirm that we didn't get anything
        #
        val = recv.receiveEvent(1000)
        self.assertIsNone(val)