📄 pollablestest.java
字号:
m_timer.setCurrentTime(startTime); pDot1Smtp.getSchedule().schedule(); m_scheduler.next(); assertPoll(mDot1Smtp); assertTime(startTime+0); // 23:59:56 should poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+1000); // 23:59:57 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+2000); // 23:59:58 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+3000); // 23:59:59 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+4000); // 00:00:00 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+5000); // 00:00:01 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertNoPoll(mDot1Smtp); assertTime(startTime+6000); // 00:00:02 should not poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp); m_scheduler.next(); assertPoll(mDot1Smtp); assertTime(startTime+7000); // 00:00:03 should poll assertUp(pDot1Smtp); assertUnchanged(pDot1Smtp);} public void testLoadService() throws Exception { anticipateDown(mDot1Smtp); mDot1Smtp.bringDown(); pDot1Smtp.doPoll(); pDot1Smtp.processStatusChange(new Date()); verifyAnticipated(); // recreate the pollable network from the database m_network = createPollableNetwork(m_db, m_scheduler, m_pollerConfig, m_pollerConfig, m_pollContext); assignPollableMembers(m_network); assertDown(pDot1Smtp); anticipateUp(mDot1Smtp); mDot1Smtp.bringUp(); pDot1Smtp.doPoll(); pDot1Smtp.processStatusChange(new Date()); verifyAnticipated(); } public void testLoadInterface() throws Exception { anticipateDown(mDot1); mDot1.bringDown(); pDot1Smtp.doPoll(); pDot1.processStatusChange(new Date()); verifyAnticipated(); // recreate the pollable network from the database m_network = createPollableNetwork(m_db, m_scheduler, m_pollerConfig, m_pollerConfig, m_pollContext); assignPollableMembers(m_network); assertDown(pDot1Smtp); assertDown(pDot1Icmp); anticipateUp(mDot1); mDot1.bringUp(); pDot1Icmp.doPoll(); pDot1.processStatusChange(new Date()); verifyAnticipated(); } public void testLoadNode() throws Exception { anticipateDown(mNode1); mNode1.bringDown(); pDot1Smtp.doPoll(); pNode1.processStatusChange(new Date()); verifyAnticipated(); // recreate the pollable network from the database m_network = createPollableNetwork(m_db, m_scheduler, m_pollerConfig, m_pollerConfig, m_pollContext); assignPollableMembers(m_network); assertDown(pDot1Smtp); assertDown(pDot1Icmp); assertDown(pDot2Smtp); assertDown(pDot2Icmp); anticipateUp(mNode1); mNode1.bringUp(); pDot1Icmp.doPoll(); pNode1.processStatusChange(new Date()); verifyAnticipated(); } public void testLoadIndependentOutageEventsUpTogether() throws Exception { anticipateDown(mDot1Smtp); mDot1Smtp.bringDown(); pDot1Smtp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); anticipateDown(mNode1); mNode1.bringDown(); pDot1Icmp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); // recreate the pollable network from the database m_network = createPollableNetwork(m_db, m_scheduler, m_pollerConfig, m_pollerConfig, m_pollContext); assignPollableMembers(m_network); assertDown(pDot1Smtp); assertDown(pDot1Icmp); assertDown(pDot2Smtp); assertDown(pDot2Icmp); assertDown(pDot1); assertDown(pDot2); assertDown(pNode1); anticipateUp(mDot1Smtp); anticipateUp(mNode1); mNode1.bringUp(); pDot1Icmp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); } public void testLoadIndependentOutageEventsUpSeparately() throws Exception { anticipateDown(mDot1Smtp); mDot1Smtp.bringDown(); pDot1Smtp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); anticipateDown(mNode1); mNode1.bringDown(); pDot1Icmp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); // recreate the pollable network from the database m_network = createPollableNetwork(m_db, m_scheduler, m_pollerConfig, m_pollerConfig, m_pollContext); assignPollableMembers(m_network); assertDown(pDot1Smtp); assertDown(pDot1Icmp); assertDown(pDot2Smtp); assertDown(pDot2Icmp); assertDown(pDot1); assertDown(pDot2); assertDown(pNode1); anticipateUp(mNode1); m_outageAnticipator.deanticipateOutageClosed(mDot1Smtp, mNode1.createUpEvent()); mNode1.bringUp(); mDot1Smtp.bringDown(); pDot1Icmp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); anticipateUp(mDot1Smtp); mDot1Smtp.bringUp(); pDot1Smtp.doPoll(); m_network.processStatusChange(new Date()); verifyAnticipated(); } public void testLock() throws Exception { final Runnable r = new Runnable() { public void run() { m_lockCount++; assertEquals(1, m_lockCount); try { Thread.sleep(3000); } catch (InterruptedException e) {} m_lockCount--; assertEquals(0, m_lockCount); } }; final Runnable locker = new Runnable() { public void run() { pNode1.withTreeLock(r); } }; Thread[] threads = new Thread[5]; for(int i = 0; i < 5; i++) { threads[i] = new Thread(locker); threads[i].start(); } for(int i = 0; i < 5; i++) { threads[i].join(); } } public void testLockTimeout() throws Exception { final Runnable r = new Runnable() { public void run() { m_lockCount++; assertEquals(1, m_lockCount); try { Thread.sleep(5000); } catch (InterruptedException e) {} m_lockCount--; assertEquals(0, m_lockCount); } }; final Runnable locker = new Runnable() { public void run() { pNode1.withTreeLock(r); } }; final Runnable lockerWithTimeout = new Runnable() { public void run() { try { pNode1.withTreeLock(r, 500); fail("Expected LockUnavailable"); } catch (LockUnavailable e) { MockUtil.println("Received expected exception "+e); } } }; Thread[] threads = new Thread[5]; threads[0] = new Thread(locker); threads[0].start(); for(int i = 1; i < 5; i++) { threads[i] = new Thread(lockerWithTimeout); threads[i].start(); } for(int i = 0; i < 5; i++) { threads[i].join(); } } /** * @param i */ private void assertTime(long time) { assertEquals("Unexpected time", time, m_scheduler.getCurrentTime()); } /** * */ private void verifyAnticipated() { m_eventMgr.finishProcessingEvents(); MockUtil.printEvents("Missing Anticipated Events: ", m_anticipator.getAnticipatedEvents()); assertTrue("Expected events not forthcoming", m_anticipator.getAnticipatedEvents().isEmpty()); MockUtil.printEvents("Unanticipated: ", m_anticipator.unanticipatedEvents()); assertEquals("Received unexpected events", 0, m_anticipator.unanticipatedEvents().size()); 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()); resetAnticipated(); } /** * */ private void resetAnticipated() { m_anticipator.reset(); m_outageAnticipator.reset(); } /** * @param svc */ private void anticipateUp(MockElement element) { Event event = element.createUpEvent(); m_anticipator.anticipateEvent(event); m_outageAnticipator.anticipateOutageClosed(element, event); } /** * @param svc */ private void anticipateDown(MockElement element) { Event event = element.createDownEvent(); m_anticipator.anticipateEvent(event); m_outageAnticipator.anticipateOutageOpened(element, event); } private void anticipateUnresponsive(MockElement element) { MockVisitor visitor = new MockVisitorAdapter() { public void visitService(MockService svc) { m_anticipator.anticipateEvent(svc.createUnresponsiveEvent()); } }; element.visit(visitor); } private void anticipateResponsive(MockElement element) { MockVisitor visitor = new MockVisitorAdapter() { public void visitService(MockService svc) { m_anticipator.anticipateEvent(svc.createResponsiveEvent()); } }; element.visit(visitor); } private void assertPoll(MockService svc) { assertEquals(1, svc.getPollCount()); svc.resetPollCount(); } /** * @param network */ private void assertNoPoll(MockElement elem) { MockVisitor zeroAsserter = new MockVisitorAdapter() { public void visitService(MockService svc) { assertEquals("Unexpected poll count for "+svc, 0, svc.getPollCount()); } }; elem.visit(zeroAsserter); } private void assertChanged(PollableElement elem) { assertEquals(true, elem.isStatusChanged()); } private void assertUnchanged(PollableElement elem) { assertEquals(false, elem.isStatusChanged()); } private void assertUp(PollableElement elem) { assertEquals(PollStatus.STATUS_UP, elem.getStatus()); } private void assertDown(PollableElement elem) { assertEquals(PollStatus.STATUS_DOWN, elem.getStatus()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -