use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.
the class InMemoryEventBusTest method testSubscribe.
@Test
public void testSubscribe() throws Exception {
EventTopic topic = mock(EventTopic.class);
EventSubscriber subscriber = mock(EventSubscriber.class);
InMemoryEventBus bus = new InMemoryEventBus();
UUID uuid = bus.subscribe(topic, subscriber);
assertNotNull(uuid);
String uuidStr = uuid.toString();
assertTrue(UuidUtils.validateUUID(uuidStr));
assertTrue(bus.totalSubscribers() == 1);
bus.unsubscribe(uuid, subscriber);
assertTrue(bus.totalSubscribers() == 0);
}
use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.
the class InMemoryEventBusTest method testSubscribeFailSubscriber.
@Test(expected = EventBusException.class)
public void testSubscribeFailSubscriber() throws Exception {
EventTopic topic = mock(EventTopic.class);
InMemoryEventBus bus = new InMemoryEventBus();
bus.subscribe(topic, null);
}
use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.
the class InMemoryEventBus method subscribe.
@Override
public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws EventBusException {
if (subscriber == null || topic == null) {
throw new EventBusException("Invalid EventSubscriber/EventTopic object passed.");
}
UUID subscriberId = UUID.randomUUID();
subscribers.put(subscriberId, new Pair<EventTopic, EventSubscriber>(topic, subscriber));
return subscriberId;
}
use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.
the class InMemoryEventBus method publish.
@Override
public void publish(Event event) throws EventBusException {
if (subscribers == null || subscribers.isEmpty()) {
// no subscriber to publish to, so just return
return;
}
for (UUID subscriberId : subscribers.keySet()) {
Pair<EventTopic, EventSubscriber> subscriberDetails = subscribers.get(subscriberId);
// if the event matches subscribers interested event topic then call back the subscriber with the event
if (isEventMatchesTopic(event, subscriberDetails.first())) {
EventSubscriber subscriber = subscriberDetails.second();
subscriber.onEvent(event);
}
}
}
use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.
the class InMemoryEventBusTest method testUnsubscribe.
@Test
public void testUnsubscribe() throws Exception {
EventTopic topic = mock(EventTopic.class);
EventSubscriber subscriber = mock(EventSubscriber.class);
InMemoryEventBus bus = new InMemoryEventBus();
UUID uuid = bus.subscribe(topic, subscriber);
assertNotNull(uuid);
String uuidStr = uuid.toString();
assertTrue(UuidUtils.validateUUID(uuidStr));
assertTrue(bus.totalSubscribers() == 1);
//
bus.unsubscribe(uuid, subscriber);
assertTrue(bus.totalSubscribers() == 0);
}
Aggregations