Search in sources :

Example 11 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventIpcManagerDefaultImplTest method testSlowEventHandlerCausesDiscards.

public void testSlowEventHandlerCausesDiscards() throws InterruptedException {
    AtomicInteger counter = new AtomicInteger();
    AtomicInteger rejected = new AtomicInteger();
    EventHandler handler = new EventHandler() {

        @Override
        public Runnable createRunnable(Log eventLog, boolean synchronous) {
            return createRunnable(eventLog);
        }

        @Override
        public Runnable createRunnable(Log eventLog) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
                    } catch (InterruptedException e) {
                    }
                    counter.incrementAndGet();
                }
            };
        }
    };
    EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
    manager.setEventHandler(handler);
    manager.setHandlerPoolSize(1);
    manager.setHandlerQueueLength(5);
    manager.afterPropertiesSet();
    //
    for (int i = 0; i < 10; i++) {
        EventBuilder bldr = new EventBuilder("uei.opennms.org/foo/" + i, "testDiscardWhenFullWithSlowEventListener");
        Event event = bldr.getEvent();
        try {
            manager.sendNow(event);
        } catch (RejectedExecutionException e) {
            rejected.incrementAndGet();
        }
    }
    await().pollInterval(1, TimeUnit.SECONDS).untilAtomic(counter, is(equalTo(6)));
    await().pollInterval(1, TimeUnit.SECONDS).untilAtomic(rejected, is(equalTo(4)));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Log(org.opennms.netmgt.xml.event.Log) EventHandler(org.opennms.netmgt.events.api.EventHandler) Event(org.opennms.netmgt.xml.event.Event) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 12 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventIpcManagerDefaultImplTest method testBroadcastNowSync.

public void testBroadcastNowSync() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    final EventListener slowListener = new EventListener() {

        @Override
        public String getName() {
            return "testBroadcastNowSync";
        }

        @Override
        public void onEvent(Event event) {
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(2));
            } catch (InterruptedException e) {
            }
            counter.incrementAndGet();
        }
    };
    EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
    manager.setHandlerPoolSize(5);
    DefaultEventHandlerImpl handler = new DefaultEventHandlerImpl(m_registry);
    manager.setEventHandler(handler);
    manager.afterPropertiesSet();
    manager.addEventListener(slowListener);
    EventBuilder bldr = new EventBuilder("uei.opennms.org/foo", "testBroadcastNowSync");
    Event e = bldr.getEvent();
    // Verify the initial state
    assertEquals(0, counter.get());
    // Broadcast synchronously - this should block until our event listener returns
    manager.broadcastNow(e, true);
    // broadcastNow() returned, so the counter should have been increased
    assertEquals(1, counter.get());
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener)

Example 13 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class BroadcastEventProcessorTest method testReload.

public void testReload() {
    MockEventIpcManager eventIpcManager = new MockEventIpcManager();
    BroadcastEventProcessor processor = new BroadcastEventProcessor(eventIpcManager, m_eventConfDao);
    EventBuilder eventBuilder = new EventBuilder(EventConstants.EVENTSCONFIG_CHANGED_EVENT_UEI, "dunno");
    // Expect a call to reload the EventConfDao
    m_eventConfDao.reload();
    m_mocks.replayAll();
    processor.onEvent(eventBuilder.getEvent());
    m_mocks.verifyAll();
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager)

Example 14 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventIpcManagerDefaultImplTest method testAsyncVsSyncSendNow.

public void testAsyncVsSyncSendNow() throws InterruptedException {
    ThreadRecordingEventHandler threadRecordingEventHandler = new ThreadRecordingEventHandler();
    m_manager.setEventHandler(threadRecordingEventHandler);
    EventBuilder bldr = new EventBuilder("uei.opennms.org/foo", "testAsyncVsSyncSendNow");
    Event e = bldr.getEvent();
    // Async: When invoking sendNow, the Runnable should be ran from thread other than the callers
    m_manager.sendNow(e);
    threadRecordingEventHandler.waitForEvent();
    assertNotEquals(Thread.currentThread().getId(), threadRecordingEventHandler.getThreadId());
    // Sync: When invoking sendNowSync, the Runnable should be ran from the callers thread
    m_manager.sendNowSync(e);
    assertEquals(Thread.currentThread().getId(), threadRecordingEventHandler.getThreadId());
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Event(org.opennms.netmgt.xml.event.Event)

Example 15 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventUtilHibernateIT method testExpandParms.

@Test
@JUnitTemporaryDatabase
public void testExpandParms() {
    String testString = "%uei%:%nodeid%:%nodelabel%:%nodelocation%";
    /*
         * Checking default location
         */
    Event event1 = new EventBuilder("testUei", "testSource").setNodeid(1).getEvent();
    String string1 = eventUtilDaoImpl.expandParms(testString, event1);
    assertEquals("testUei:1:node1:Default", string1);
    /*
         * Checking custom location
         */
    OnmsNode onmsNode = m_populator.getNodeDao().get(2);
    onmsNode.setLocation(m_populator.getMonitoringLocationDao().get("RDU"));
    m_populator.getNodeDao().update(onmsNode);
    Event event2 = new EventBuilder("testUei", "testSource").setNodeid(2).getEvent();
    String string2 = eventUtilDaoImpl.expandParms(testString, event2);
    assertEquals("testUei:2:node2:RDU", string2);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) Event(org.opennms.netmgt.xml.event.Event) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Aggregations

EventBuilder (org.opennms.netmgt.model.events.EventBuilder)275 Event (org.opennms.netmgt.xml.event.Event)75 Test (org.junit.Test)72 Date (java.util.Date)19 InetAddress (java.net.InetAddress)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 AlarmData (org.opennms.netmgt.xml.event.AlarmData)14 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)11 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)11 ServletException (javax.servlet.ServletException)9 EventHandler (org.opennms.netmgt.events.api.annotations.EventHandler)9 ArrayList (java.util.ArrayList)8 SyntaxToEvent (org.opennms.netmgt.model.events.snmp.SyntaxToEvent)7 Transactional (org.springframework.transaction.annotation.Transactional)7 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)6 Parm (org.opennms.netmgt.xml.event.Parm)6 Event (org.opennms.netmgt.xml.eventconf.Event)6 HttpSession (javax.servlet.http.HttpSession)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 Log (org.opennms.netmgt.xml.event.Log)5