📄 pollertest.java
字号:
node.bringUp(); verifyAnticipated(10000); } // test whole node down public void testNodeOutageProcessingEnabled() throws Exception { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockNode node = m_network.getNode(1); // start the poller startDaemons(); resetAnticipated(); anticipateDown(node); // brind down the node (duh) node.bringDown(); // make sure the correct events are recieved verifyAnticipated(10000); resetAnticipated(); anticipateUp(node); // bring the node back up node.bringUp(); // make sure the up events are received verifyAnticipated(10000); } public void testNodeLostRegainedService() throws Exception { testElementDownUp(m_network.getService(1, "192.168.1.1", "SMTP")); } public void testInterfaceDownUp() { testElementDownUp(m_network.getInterface(1, "192.168.1.1")); } public void testNodeDownUp() { testElementDownUp(m_network.getNode(1)); } private void testElementDownUp(MockElement element) { startDaemons(); resetAnticipated(); anticipateDown(element); MockUtil.println("Bringing down element: "+element); element.bringDown(); MockUtil.println("Finished bringing down element: "+element); verifyAnticipated(5000); sleep(2000); resetAnticipated(); anticipateUp(element); MockUtil.println("Bringing up element: "+element); element.bringUp(); MockUtil.println("Finished bringing up element: "+element); verifyAnticipated(8000); } public void testPolling() throws Exception { m_pollerConfig.setNodeOutageProcessingEnabled(false); // create a poll anticipator PollAnticipator anticipator = new PollAnticipator(); // register it with the interfaces services MockInterface iface = m_network.getInterface(1, "192.168.1.2"); iface.addAnticipator(anticipator); // // first ensure that polls are working while it is up // // anticipate three polls on all the interfaces services anticipator.anticipateAllServices(iface); anticipator.anticipateAllServices(iface); anticipator.anticipateAllServices(iface); // start the poller startDaemons(); // wait for the polls to occur while its up... 1 poll per second plus // overhead assertEquals(0, anticipator.waitForAnticipated(4000L).size()); // ensure that the 192.168.1.3/HTTP service is only polled by TestPkg2 MockService svc = m_network.getService(2, "192.168.1.3", "HTTP"); assertEquals(1, svc.getPollingPackages().size()); assertEquals("TestPkg2", svc.getPollingPackages().iterator().next()); // ensure that another service has the TestPackage package MockService svc2 = m_network.getService(1, "192.168.1.2", "SMTP"); assertEquals(1, svc2.getPollingPackages().size()); assertEquals("TestPackage", svc2.getPollingPackages().iterator().next()); } // test open outages for unmanaged services public void testUnmangedWithOpenOutageAtStartup() { // before we start we need to initialize the database // create an outage for the service MockService svc = m_network.getService(1, "192.168.1.1", "SMTP"); MockInterface iface = m_network.getInterface(1, "192.168.1.2"); Event svcLostEvent = MockUtil.createNodeLostServiceEvent("Test", svc); m_db.writeEvent(svcLostEvent); createOutages(svc, svcLostEvent); Event ifaceDownEvent = MockUtil.createInterfaceDownEvent("Test", iface); m_db.writeEvent(ifaceDownEvent); createOutages(iface, ifaceDownEvent); // mark the service as unmanaged m_db.setServiceStatus(svc, 'U'); m_db.setInterfaceStatus(iface, 'U'); // assert that we have an open outage assertEquals(1, m_db.countOpenOutagesForService(svc)); assertEquals(1, m_db.countOutagesForService(svc)); assertEquals(iface.getServices().size(), m_db.countOutagesForInterface(iface)); assertEquals(iface.getServices().size(), m_db.countOpenOutagesForInterface(iface)); startDaemons(); // assert that we have no open outages assertEquals(0, m_db.countOpenOutagesForService(svc)); assertEquals(1, m_db.countOutagesForService(svc)); assertEquals(0, m_db.countOpenOutagesForInterface(iface)); assertEquals(iface.getServices().size(), m_db.countOutagesForInterface(iface)); } public void testReparentCausesStatusChange() { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockNode node1 = m_network.getNode(1); MockNode node2 = m_network.getNode(2); MockInterface dotOne = m_network.getInterface(1, "192.168.1.1"); MockInterface dotTwo = m_network.getInterface(1, "192.168.1.2"); MockInterface dotThree = m_network.getInterface(2, "192.168.1.3"); // // Plan to bring down both nodes except the reparented interface // the node owning the interface should be up while the other is down // after reparenting we should got the old owner go down while the other // comes up. // anticipateDown(node2); anticipateDown(dotOne); // bring down both nodes but bring iface back up node1.bringDown(); node2.bringDown(); dotTwo.bringUp(); Event reparentEvent = MockUtil.createReparentEvent("Test", "192.168.1.2", 1, 2); startDaemons(); verifyAnticipated(2000); m_db.reparentInterface(dotTwo.getIpAddr(), dotTwo.getNodeId(), node2.getNodeId()); dotTwo.moveTo(node2); resetAnticipated(); anticipateDown(node1, true); anticipateUp(node2, true); anticipateDown(dotThree, true); m_eventMgr.sendEventToListeners(reparentEvent); verifyAnticipated(20000); } // send a nodeGainedService event: // EventConstants.NODE_GAINED_SERVICE_EVENT_UEI public void testSendNodeGainedService() { testSendNodeGainedService(false); } public void testSendNodeGainedServiceNodeOutages() { testSendNodeGainedService(true); } public void testSendNodeGainedService(boolean nodeOutageProcessing) { m_pollerConfig.setNodeOutageProcessingEnabled(nodeOutageProcessing); startDaemons(); MockNode node = m_network.addNode(99, "TestNode"); m_db.writeNode(node); MockInterface iface = m_network.addInterface(99, "10.1.1.1"); m_db.writeInterface(iface); MockService element = m_network.addService(99, "10.1.1.1", "HTTP"); m_db.writeService(element); m_pollerConfig.addService(element); MockService smtp = m_network.addService(99, "10.1.1.1", "SMTP"); m_db.writeService(smtp); m_pollerConfig.addService(smtp); MockVisitor gainSvcSender = new MockVisitorAdapter() { public void visitService(MockService svc) { Event event = MockUtil.createNodeGainedServiceEvent("Test", svc); m_eventMgr.sendEventToListeners(event); } }; node.visit(gainSvcSender); PollAnticipator anticipator = new PollAnticipator(); element.addAnticipator(anticipator); anticipator.anticipateAllServices(element); assertEquals(0, anticipator.waitForAnticipated(10000).size()); anticipateDown(element); element.bringDown(); verifyAnticipated(10000); } public void testSuspendPollingResumeService() { long start = System.currentTimeMillis(); MockService svc = m_network.getService(1, "192.168.1.2", "SMTP"); startDaemons(); sleep(2000); assertTrue(0 < svc.getPollCount()); m_eventMgr.sendEventToListeners(MockUtil.createSuspendPollingServiceEvent("Test", svc)); svc.resetPollCount(); sleep(5000); assertEquals(0, svc.getPollCount()); m_eventMgr.sendEventToListeners(MockUtil.createResumePollingServiceEvent("Test", svc)); sleep(2000); assertTrue(0 < svc.getPollCount()); } // // Utility methods // private void startDaemons() {// m_outageMgr.init(); m_poller.init();// m_outageMgr.start(); m_poller.start(); m_daemonsStarted = true; } private void stopDaemons() { if (m_daemonsStarted) { m_poller.stop();// m_outageMgr.stop(); } } private void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { } } private void verifyAnticipated(long millis) { // make sure the down events are received MockUtil.printEvents("Events we're still waiting for: ", m_anticipator.waitForAnticipated(millis)); assertTrue("Expected events not forthcoming", m_anticipator.waitForAnticipated(0).isEmpty()); sleep(2000); MockUtil.printEvents("Unanticipated: ", m_anticipator.unanticipatedEvents()); assertEquals("Received unexpected events", 0, m_anticipator.unanticipatedEvents().size()); sleep(1000); m_eventMgr.finishProcessingEvents(); assertEquals("Wrong number of outages opened", m_outageAnticipator.getExpectedOpens(), m_outageAnticipator.getActualOpens()); assertEquals("Wrong number of outages in outage table", m_outageAnticipator.getExpectedOutages(), m_outageAnticipator.getActualOutages()); assertTrue("Created outages don't match the expected outages", m_outageAnticipator.checkAnticipated()); } private void anticipateUp(MockElement element) { anticipateUp(element, false); } private void anticipateUp(MockElement element, boolean force) { if (force || element.getPollStatus() != ServiceMonitor.SERVICE_AVAILABLE) { Event event = element.createUpEvent(); m_anticipator.anticipateEvent(event); m_outageAnticipator.anticipateOutageClosed(element, event); } } private void anticipateDown(MockElement element) { anticipateDown(element, false); } private void anticipateDown(MockElement element, boolean force) { if (force || element.getPollStatus() != ServiceMonitor.SERVICE_UNAVAILABLE) { Event event = element.createDownEvent(); m_anticipator.anticipateEvent(event); m_outageAnticipator.anticipateOutageOpened(element, event); } } private void anticipateDownEvent(MockElement element) { m_anticipator.anticipateEvent(element.createDownEvent()); } private void anticipateServicesUp(MockElement node) { MockVisitor eventCreator = new MockVisitorAdapter() { public void visitService(MockService svc) { anticipateUp(svc); } }; node.visit(eventCreator); } private void anticipateServicesDown(MockElement node) { MockVisitor eventCreator = new MockVisitorAdapter() { public void visitService(MockService svc) { anticipateDown(svc); } }; node.visit(eventCreator); } private void createOutages(MockElement element, final Event event) { MockVisitor outageCreater = new MockVisitorAdapter() { public void visitService(MockService svc) { m_db.createOutage(svc, event); } }; element.visit(outageCreater); } private void bringDownCritSvcs(MockElement element) { MockVisitor markCritSvcDown = new MockVisitorAdapter() { public void visitService(MockService svc) { if ("ICMP".equals(svc.getName())) { svc.bringDown(); } } }; element.visit(markCritSvcDown); } class OutageChecker extends Querier { private Event m_lostSvcEvent; private Timestamp m_lostSvcTime; private MockService m_svc; private Event m_regainedSvcEvent; private Timestamp m_regainedSvcTime; OutageChecker(MockService svc, Event lostSvcEvent) throws Exception { this(svc, lostSvcEvent, null); } OutageChecker(MockService svc, Event lostSvcEvent, Event regainedSvcEvent) { super(m_db, "select * from outages where nodeid = ? and ipAddr = ? and serviceId = ?"); m_svc = svc; m_lostSvcEvent = lostSvcEvent; m_lostSvcTime = m_db.convertEventTimeToTimeStamp(m_lostSvcEvent.getTime()); m_regainedSvcEvent = regainedSvcEvent; if (m_regainedSvcEvent != null) m_regainedSvcTime = m_db.convertEventTimeToTimeStamp(m_regainedSvcEvent.getTime()); } public void processRow(ResultSet rs) throws SQLException { assertEquals(m_svc.getNodeId(), rs.getInt("nodeId")); assertEquals(m_svc.getIpAddr(), rs.getString("ipAddr")); assertEquals(m_svc.getId(), rs.getInt("serviceId")); assertEquals(m_lostSvcEvent.getDbid(), rs.getInt("svcLostEventId")); assertEquals(m_lostSvcTime, rs.getTimestamp("ifLostService")); assertEquals(getRegainedEventId(), rs.getObject("svcRegainedEventId")); assertEquals(m_regainedSvcTime, rs.getTimestamp("ifRegainedService")); } private Integer getRegainedEventId() { if (m_regainedSvcEvent == null) return null; return new Integer(m_regainedSvcEvent.getDbid()); } }; // TODO: test multiple polling packages // TODO: test overlapping polling packages // TODO: test two packages both with the crit service and status propagation // TODO: how does unmanaging a node/iface/service work with the poller // TODO: test over lapping poll outages public static void main(String[] args) { junit.textui.TestRunner.run(PollerTest.class); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -