mocknetworktest.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 742 行 · 第 1/2 页

JAVA
742
字号
        anticipator.reset();        m_eventMgr.sendNow(nodeEvent);        m_eventMgr.finishProcessingEvents();        assertEquals(0, anticipator.waitForAnticipated(0).size());        assertEquals(1, anticipator.unanticipatedEvents().size());    }    public void testInvalidPoll() throws UnknownHostException {        m_network.resetInvalidPollCount();        IPv4NetworkInterface addr = new MockNetworkInterface("1.1.1.1");        ServiceMonitor monitor = m_pollerConfig.getServiceMonitor("ICMP");        try {            monitor.poll(addr, new HashMap(), m_pollerConfig.getPackage("TestPackage"));            fail("expected exception");        } catch (Exception e) {            // expected this        }        assertEquals(1, m_network.getInvalidPollCount());    }    public void testLookupNotThere() {        assertNotNull(m_network.getService(1, "192.168.1.1", "ICMP"));        assertNull(m_network.getService(7, "192.168.1.1", "ICMP"));        assertNull(m_network.getService(1, "192.168.1.175", "ICMP"));        assertNull(m_network.getService(1, "192.168.1.1", "ICMG"));    }    public void testPollerConfig() {        m_pollerConfig.setNodeOutageProcessingEnabled(true);        m_pollerConfig.setPollInterval("HTTP", 750L);        m_pollerConfig.setPollerThreads(5);        m_pollerConfig.setCriticalService("YAHOO");        PollerConfig pollerConfig = m_pollerConfig;        // test the nodeOutageProcessing setting works        assertTrue(pollerConfig.nodeOutageProcessingEnabled());        // test to ensure that the poller has packages        Enumeration pkgs = pollerConfig.enumeratePackage();        assertNotNull(pkgs);        int pkgCount = 0;        Package pkg = null;        while (pkgs.hasMoreElements()) {            pkg = (Package) pkgs.nextElement();            pkgCount++;        }        assertTrue(pkgCount > 0);        // ensure a sample interface is in the package        assertTrue(pollerConfig.interfaceInPackage("192.168.1.1", pkg));        Enumeration svcs = pkg.enumerateService();        assertNotNull(svcs);        while (svcs.hasMoreElements()) {            Service svc = (Service) svcs.nextElement();            if ("ICMP".equals(svc.getName()))                assertEquals(500L, svc.getInterval());            else if ("HTTP".equals(svc.getName()))                assertEquals(750L, svc.getInterval());            else                assertEquals(1000L, svc.getInterval());        }        // ensure that setting the thread worked        assertEquals(5, pollerConfig.getThreads());        // ensure that setting the critical service worked        assertEquals("YAHOO", pollerConfig.getCriticalService());        // ensure that we have service monitors to the sevices        assertNotNull(pollerConfig.getServiceMonitor("SMTP"));    }    public void testPollOutageConfig() {        PollOutagesConfig pollOutagesConfig = m_pollerConfig;        assertNotNull(pollOutagesConfig);    }    public void testPollStatus() {        MockNode node = m_network.getNode(1);        MockInterface iface = m_network.getInterface(1, "192.168.1.2");        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, node.getPollStatus());        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, iface.getPollStatus());        node.bringDown();        assertEquals(ServiceMonitor.SERVICE_UNAVAILABLE, node.getPollStatus());        assertEquals(ServiceMonitor.SERVICE_UNAVAILABLE, iface.getPollStatus());        iface.bringUp();        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, node.getPollStatus());        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, iface.getPollStatus());        node.bringUp();        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, node.getPollStatus());        assertEquals(ServiceMonitor.SERVICE_AVAILABLE, iface.getPollStatus());    }    public void testQueryManager() throws Exception {        QueryManager queryManager = new MockQueryManager(m_network);        assertNotNull(queryManager);        assertTrue(queryManager.activeServiceExists("Test", 1, "192.168.1.1", "ICMP"));        assertFalse(queryManager.activeServiceExists("Test", 1, "192.168.1.17", "ICMP"));        Map nameToIdMap = new HashMap();        Map idToNameMap = new HashMap();        queryManager.buildServiceNameToIdMaps(nameToIdMap, idToNameMap);        MockService service = m_network.getService(1, "192.168.1.1", "SMTP");        int serviceId = service.getId();        assertEquals(service.getName(), idToNameMap.get(new Integer(serviceId)));        assertEquals(serviceId, ((Integer) nameToIdMap.get(service.getName())).intValue());        MockInterface iface = m_network.getInterface(1, "192.168.1.2");        Collection expectedSvcs = iface.getServices();        List svcs = queryManager.getActiveServiceIdsForInterface("192.168.1.2");        Iterator it = expectedSvcs.iterator();        while (it.hasNext()) {            MockService svc = (MockService) it.next();            assertTrue(svcs.contains(new Integer(svc.getId())));        }        List ifKeys = queryManager.getInterfacesWithService("HTTP");        MockInterface httpIf = m_network.getInterface(2, "192.168.1.3");        assertEquals(1, ifKeys.size());        IfKey key = (IfKey) ifKeys.get(0);        assertEquals(httpIf.getNode().getNodeId(), key.getNodeId());        assertEquals(httpIf.getIpAddr(), key.getIpAddr());        int findNodeId = queryManager.getNodeIDForInterface("192.168.1.3");        assertEquals(httpIf.getNode().getNodeId(), findNodeId);        assertEquals("Router", queryManager.getNodeLabel(1));        assertEquals(2, queryManager.getServiceCountForInterface("192.168.1.1"));    }    public void testRemove() {        assertNotNull(m_network.getService(1, "192.168.1.1", "SMTP"));        m_network.removeService(m_network.getService(1, "192.168.1.1", "SMTP"));        assertNull(m_network.getService(1, "192.168.1.1", "SMTP"));        assertNotNull(m_network.getInterface(1, "192.168.1.1"));        m_network.removeInterface(m_network.getInterface(1, "192.168.1.1"));        assertNull(m_network.getInterface(1, "192.168.1.1"));        assertNull(m_network.getService(1, "192.168.1.1", "ICMP"));        assertNotNull(m_network.getNode(1));        m_network.removeNode(m_network.getNode(1));        assertNull(m_network.getNode(1));        assertNull(m_network.getInterface(1, "192.168.1.2"));        MockInterface iface = m_network.getInterface(2, "192.168.1.3");        assertNotNull(iface);        m_network.removeElement(iface);        assertNull(m_network.getInterface(2, "192.168.1.3"));    }    public void testScheduledOutages() {        long now = System.currentTimeMillis();        long tenMinutes = 600000L;        m_pollerConfig.addScheduledOutage("outage1", now - tenMinutes, now + tenMinutes, "192.168.1.1");        m_pollerConfig.addScheduledOutage("outage2", now - tenMinutes, now, "192.168.1.2");                try { Thread.sleep(1000); } catch (InterruptedException e) {}        Package pkg = m_pollerConfig.getPackage("TestPackage");        assertNotNull(pkg);        Collection outages = pkg.getOutageCalendarCollection();        assertTrue(outages.contains("outage1"));        assertTrue(outages.contains("outage2"));        // test isInterfaceInOutage        assertTrue(m_pollerConfig.isInterfaceInOutage("192.168.1.1", "outage1"));        assertFalse(m_pollerConfig.isInterfaceInOutage("192.168.1.2", "outage1"));        assertTrue(m_pollerConfig.isInterfaceInOutage("192.168.1.2", "outage2"));        assertFalse(m_pollerConfig.isInterfaceInOutage("192.168.1.1", "outage2"));        // test isCurTimeInOutage        assertTrue(m_pollerConfig.isCurTimeInOutage("outage1"));        assertFalse(m_pollerConfig.isCurTimeInOutage("outage2"));    }    private void testServicePoll(MockElement element) throws UnknownHostException {        element.resetPollCount();        m_downChecker.resetServiceCount();        element.bringDown();        element.visit(m_downChecker);        assertEquals(m_downChecker.getServiceCount(), element.getPollCount());        element.resetPollCount();        m_upChecker.resetServiceCount();        element.bringUp();        element.visit(m_upChecker);        assertEquals(m_upChecker.getServiceCount(), element.getPollCount());    }    public void testSetPollStatus() throws Exception {        // service poll status        testServicePoll(m_network.getService(1, "192.168.1.1", "SMTP"));        testServicePoll(m_network.getService(1, "192.168.1.1", "ICMP"));        testServicePoll(m_network.getService(1, "192.168.1.2", "SMTP"));        testServicePoll(m_network.getService(2, "192.168.1.3", "HTTP"));        // interface poll status        testServicePoll(m_network.getInterface(1, "192.168.1.1"));        // node poll status        testServicePoll(m_network.getNode(2));    }    public void testVisitor() {        ElementCounter counter = new ElementCounter();        m_network.visit(counter);        assertEquals(1, counter.getNetworkCount());        assertEquals(2, counter.getNodeCount());        assertEquals(4, counter.getInterfaceCount());        assertEquals(6, counter.getServiceCount());        assertEquals(7, counter.getContainerCount());        assertEquals(13, counter.getElementCount());    }    public void testWaitForEvent() throws Throwable {        MockNode node = m_network.getNode(1);        final Event event1 = MockUtil.createNodeDownEvent("Test", node);        final Event event2 = MockUtil.createNodeDownEvent("Test", node);        final Event event3 = MockUtil.createNodeDownEvent("Test", m_network.getNode(2));        EventAnticipator anticipator = m_eventMgr.getEventAnticipator();        anticipator.anticipateEvent(event1);        anticipator.anticipateEvent(event3);        class EventSender extends Thread {            Throwable m_t = null;            public void assertSuccess() throws Throwable {                if (m_t != null)                    throw m_t;            }            public void run() {                try {                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                    }                    m_eventMgr.sendNow(event2);                    m_eventMgr.sendNow(event2);                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                    }                    m_eventMgr.sendNow(event3);                } catch (Throwable t) {                    m_t = t;                }            }        }        ;        EventSender eventSender = new EventSender();        eventSender.start();        eventSender.assertSuccess();        assertEquals(1, anticipator.waitForAnticipated(1500).size());        assertEquals(0, anticipator.waitForAnticipated(1000).size());        assertEquals(1, anticipator.unanticipatedEvents().size());    }    public void testWaitForPoll() throws Throwable {        final PollAnticipator anticipator = new PollAnticipator();        // locate the elements and register the pollAnticipator with them        final MockService smtpService = m_network.getService(1, "192.168.1.1", "SMTP");        smtpService.resetPollCount();        smtpService.addAnticipator(anticipator);        final MockService icmpService = m_network.getService(2, "192.168.1.3", "ICMP");        icmpService.resetPollCount();        icmpService.addAnticipator(anticipator);        final MockInterface iface = m_network.getInterface(1, "192.168.1.2");        iface.resetPollCount();        iface.addAnticipator(anticipator);        anticipator.anticipateAllServices(smtpService);        anticipator.anticipateAllServices(icmpService);        anticipator.anticipateAllServices(iface);        // poll the services        m_upChecker.resetServiceCount();        smtpService.visit(m_upChecker);        icmpService.visit(m_upChecker);        iface.visit(m_upChecker);        // poll the icmpService an extra time so we get one unanticipated poll        icmpService.visit(m_upChecker);        // assert the the polls all occurred        assertEquals(0, anticipator.waitForAnticipated(0L).size());        assertEquals(1, anticipator.unanticipatedPolls().size());        // reset the anticipator so we can use it again        anticipator.reset();        // visit the elements and ensure that we anticpate polls on them        anticipator.anticipateAllServices(smtpService);        anticipator.anticipateAllServices(icmpService);        anticipator.anticipateAllServices(iface);        // anticipate icmp twice        anticipator.anticipateAllServices(icmpService);        smtpService.bringDown();        icmpService.bringDown();        iface.bringDown();        m_downChecker.resetServiceCount();        class PollerThread extends Thread {            Throwable m_t = null;            public void assertSuccess() throws Throwable {                if (m_t != null)                    throw m_t;            }            public void run() {                try {                    smtpService.visit(m_downChecker);                    icmpService.visit(m_downChecker);                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                    }                    iface.visit(m_downChecker);                    icmpService.visit(m_downChecker);                } catch (Throwable t) {                    m_t = t;                }            }        }        ;        PollerThread pollerThread = new PollerThread();        pollerThread.start();        assertEquals(0, anticipator.waitForAnticipated(3000L).size());        assertEquals(0, anticipator.unanticipatedPolls().size());        pollerThread.assertSuccess();        // add one because we polled icmp an extra time        assertEquals(m_upChecker.getServiceCount(), m_downChecker.getServiceCount());    }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?