'Test that events are sent to listeners.'

def testEventNotification(self):

    NoClientListener.received_events = []
    worker = worker_test_lib.MockWorker(token=self.token)
    event = rdf_flows.GrrMessage(session_id=rdfvalue.SessionID(flow_name='SomeFlow'), name='test    message', payload=rdf_paths.PathSpec(path='foobar', pathtype='TSK'), source='aff4:/C.0000000000000001', auth_state='AUTHENTICATED')
    events.Events.PublishEvent('TestEvent', event, token=self.token)
    worker.Simulate()
    self.assertEqual(NoClientListener.received_events, [])
    event.source = 'Source'
    event.auth_state = rdf_flows.GrrMessage.AuthorizationState.UNAUTHENTICATED
    events.Events.PublishEvent('TestEvent', event, token=self.token)
    worker.Simulate()
    self.assertEqual(NoClientListener.received_events, [])
    event.auth_state = rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED
    events.Events.PublishEvent('TestEvent', event, token=self.token)
    worker.Simulate()
    self.assertEqual(len(NoClientListener.received_events), 1)
    self.assertEqual(NoClientListener.received_events[0][0].source, 'aff4:/Source')
    self.assertEqual(NoClientListener.received_events[0][1].path, 'foobar')
    NoClientListener.received_events = []
    for i in xrange(10):
        event.source = ('Source%d' % i)
        events.Events.PublishEvent('TestEvent', event, token=self.token)
    worker.Simulate()
    self.assertEqual(len(NoClientListener.received_events), 10)
    NoClientListener.received_events.sort(key=(lambda x: x[0].source))
    for i in range(10):
        self.assertEqual(NoClientListener.received_events[i][0].source, ('aff4:/Source%d' % i))
        self.assertEqual(NoClientListener.received_events[i][1].path, 'foobar')
