def testStateOtherStatesIM(self): '''test alert state NEW in memory''' self.teal = Teal('data/alert_test/test.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False) j_in_dq = Journal('j_in_DQ', 'data/alert_test/inject_DQ_alerts.json') tq = ListenableQueue('test LQ') ql = CheckAlertStateListener(7) tq.register_listener(ql) j_in_dq.inject_queue(tq, progress_cb=None, fail_on_invalid=False, no_delay=True) ql.my_event.wait() self.assertEquals(ql.count, 7) ta1 = ql.alerts[0] am = get_service(SERVICE_ALERT_MGR) # TODO: Should not be hardcoded rec ids after this self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 5) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 6) self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.reopen, ta1.rec_id) am.close(ta1.rec_id) self.assertEquals(ta1.state, ALERT_STATE_CLOSED) self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.close, ta1.rec_id) self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.close, 23456) self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[4].state, ALERT_STATE_CLOSED) self.assertEquals(ql.alerts[5].state, ALERT_STATE_CLOSED) self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN) # reopen it self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.reopen, 23456) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 5) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 6) am.reopen(ta1.rec_id) self.assertEquals(ta1.state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN) am.close(3) self.assertEquals(ta1.state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[2].state, ALERT_STATE_CLOSED) self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN) am.reopen(3) self.assertEquals(ta1.state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN) self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN) self.teal.shutdown() return
def testBasicTwo(self): '''Test that two listeners work''' lq = ListenableQueue('Queue 02') listener1 = SampleQueueListenerT('Listener 01T', lq) listener2 = SampleQueueListenerT('Listener 02T', lq) self.assertEqual(len(listener1.notifications), 0) self.assertEqual(len(listener2.notifications), 0) item = 'Item 02' lq.put_nowait(item) time.sleep(1) self.assertEqual(len(listener1.notifications), 1) self.assertTrue(item in listener1.notifications) self.assertEqual(len(listener2.notifications), 1) self.assertTrue(item in listener2.notifications) # unregister listener lq.unregister_listener(listener1) item2 = 'Item 03' lq.put_nowait(item2) time.sleep(1) self.assertEqual(len(listener1.notifications),1) self.assertTrue(item in listener1.notifications) self.assertEqual(len(listener2.notifications), 2) self.assertTrue(item in listener2.notifications) self.assertTrue(item2 in listener2.notifications) lq.shutdown() self.assertFalse(listener1.shutdown_immediate_flag) # It was unregistered before shutdown! self.assertTrue(listener2.shutdown_immediate_flag) self.assertEqual(len(listener1.notifications), 1) # It was unregistered before shutdown! self.assertEqual(len(listener2.notifications), 2) # EOD is not sent return
def testNoUnregister(self): '''Test that one listener works''' lq = ListenableQueue('Queue 01') listener1 = SampleQueueListenerWD('Listener 01T', lq) self.assertEqual(len(listener1.notifications), 0) item = 'Item 01' lq.put_nowait(item) time.sleep(1) self.assertEqual(len(listener1.notifications), 1) self.assertTrue(item in listener1.notifications) lq.shutdown() self.assertTrue(listener1.shutdown_immediate_flag) self.assertEqual(len(listener1.notifications), 1) # EOD is not sent return
def testBasicOne(self): '''Test that one listener works''' lq = ListenableQueue('Queue 01') listener1 = SampleQueueListenerT('Listener 01T', lq) self.assertEqual(len(listener1.notifications), 0) item = 'Item 01' lq.put_nowait(item) time.sleep(1) self.assertEqual(len(listener1.notifications), 1) self.assertTrue(item in listener1.notifications) lq.shutdown() self.assertFalse(listener1.shutdown_immediate_flag) self.assertEqual(len(listener1.notifications), 2) return
def testJournalQueue(self): ''' Test injecting a Journal containing optional fields through a queue''' lq = ListenableQueue('test journal queue') j = Journal('test journal', file='data/journal_test/data_sample_002_NEW.json') j_rec = Journal('j_rec') lq.register_listener(j_rec) j.inject_queue(lq) while len(j) != len(j_rec): # p rint ('waiting for queue to process %s of %s' % (str(len(j_rec)), str(len(j)))) sleep(1.0) #p rint j #p rint j_rec self.assertTrue(j.deep_match(j)) self.assertTrue(j.deep_match(j_rec, ignore_delay=True)) return
def testNoCallback(self): '''Test callback isn't called when 2nd True''' lq = ListenableQueue('Queue 03', self.process_callback) listener1 = SampleQueueListenerF('Listener 01F', lq) listener2 = SampleQueueListenerT('Listener 02T', lq) self.assertEqual(len(listener1.notifications), 0) self.assertEqual(len(listener2.notifications), 0) item = 'Item 02' lq.put_nowait(item) self.my_event.wait(2) self.assertEqual(self.callback_occurred, False) self.assertEqual(self.callback_item, None) self.assertEqual(len(listener1.notifications), 1) self.assertTrue(item in listener1.notifications) self.assertEqual(len(listener2.notifications), 1) self.assertTrue(item in listener2.notifications) lq.shutdown() self.assertTrue(listener1.shutdown_immediate_flag) self.assertTrue(listener2.shutdown_immediate_flag) self.assertEqual(len(listener1.notifications), 1) # EOD not sent self.assertEqual(len(listener2.notifications), 1) # EOD not sent return
def testCallback(self): '''Test that callback work when all false ''' lq = ListenableQueue('Queue 03', self.process_callback) listener1 = SampleQueueListenerF('Listener 01F', lq) listener2 = SampleQueueListenerF('Listener 02F', lq) self.assertEqual(len(listener1.notifications), 0) self.assertEqual(len(listener2.notifications), 0) item = 'Item 02' lq.put_nowait(item) self.my_event.wait(2) self.assertEqual(self.callback_occurred, True) self.assertEqual(self.callback_item, item) self.assertEqual(len(listener1.notifications), 1) self.assertTrue(item in listener1.notifications) self.assertEqual(len(listener2.notifications), 1) self.assertTrue(item in listener2.notifications) lq.shutdown() self.assertFalse(listener1.shutdown_immediate_flag) self.assertFalse(listener2.shutdown_immediate_flag) self.assertEqual(len(listener1.notifications), 2) self.assertEqual(len(listener2.notifications), 2) return
def testAnalyzerBasicA(self): '''Test basic Analyzer creation using a Sample analyzer asynch''' inQ1 = ListenableQueue('inQ1') outQ1 = ListenableQueue('outQ1') outQL1 = SampleQueueListenerT('outQL1') outQ1.register_listener(outQL1) SimpleEventAnalyzerAllAlert('TestAnalyzer1', inQ1, outQ1, number=3) right_now = datetime.now() te1 = teal.Event.fromDict({EVENT_ATTR_REC_ID:1, EVENT_ATTR_EVENT_ID:'E1', EVENT_ATTR_TIME_OCCURRED: right_now}) inQ1.put_nowait(te1) time.sleep(2) self.assertEqual(len(outQL1.notifications), 1) alert = outQL1.notifications[0] self.assertEquals(alert.get_rec_id(), 1) self.assertEquals(alert.alert_id, 'Alert 01') return
def testStateOtherStatesDB(self): '''test alert state NEW in memory''' self.prepare_db() self.teal = Teal('data/alert_test/test.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=True, commit_checkpoints=False) j_in_dq = Journal('j_in_DQ', 'data/alert_test/inject_DQ_alerts.json') tq = ListenableQueue('test LQ') ql = CheckAlertStateListener(7) tq.register_listener(ql) j_in_dq.inject_queue(tq, progress_cb=None, fail_on_invalid=False, no_delay=True) ql.my_event.wait() self.assertEquals(ql.count, 7) ta1 = ql.alerts[0] am = get_service(SERVICE_ALERT_MGR) self.assertEquals(ta1.state, ALERT_STATE_OPEN) # TODO: Really should query to get the recid to use for hardcoded ones in rest of this test case self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 5) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 6) self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.reopen, ta1.rec_id) am.close(ta1.rec_id) # Get duplicates of this one self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.close, ta1.rec_id) self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.close, 23456) # Note that in memory won't be updated ... only in DB # so lets get it from the DB dbi = get_service(SERVICE_DB_INTERFACE) event_cnxn, cursor =_get_connection(dbi) self.assert_alert_closed(dbi, cursor, ta1.rec_id) self.assert_alert_open(dbi, cursor, 2) self.assert_alert_open(dbi, cursor, 3) self.assert_alert_open(dbi, cursor, 4) self.assert_alert_closed(dbi, cursor, 5) self.assert_alert_closed(dbi, cursor, 6) self.assert_alert_open(dbi, cursor, 7) self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.reopen, 23456) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 5) self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 6) # reopen it am.reopen(ta1.rec_id) event_cnxn, cursor =_get_connection(dbi, event_cnxn) self.assert_alert_open(dbi, cursor, ta1.rec_id) self.assert_alert_open(dbi, cursor, 2) self.assert_alert_open(dbi, cursor, 3) self.assert_alert_open(dbi, cursor, 4) self.assert_alert_open(dbi, cursor, 5) self.assert_alert_open(dbi, cursor, 6) self.assert_alert_open(dbi, cursor, 7) am.close(3) event_cnxn, cursor =_get_connection(dbi, event_cnxn) self.assert_alert_open(dbi, cursor, ta1.rec_id) self.assert_alert_open(dbi, cursor, 2) self.assert_alert_closed(dbi, cursor, 3) self.assert_alert_open(dbi, cursor, 4) self.assert_alert_open(dbi, cursor, 5) self.assert_alert_open(dbi, cursor, 6) self.assert_alert_open(dbi, cursor, 7) am.reopen(3) event_cnxn, cursor =_get_connection(dbi, event_cnxn) self.assert_alert_open(dbi, cursor, ta1.rec_id) self.assert_alert_open(dbi, cursor, 2) self.assert_alert_open(dbi, cursor, 3) self.assert_alert_open(dbi, cursor, 4) self.assert_alert_open(dbi, cursor, 5) self.assert_alert_open(dbi, cursor, 6) self.assert_alert_open(dbi, cursor, 7) event_cnxn.close() self.teal.shutdown() return