2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.dom.broker;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.opendaylight.mdsal.dom.broker.TestUtils.TEST_CHILD;
22 import com.google.common.collect.Multimap;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.Test;
31 import org.opendaylight.mdsal.dom.api.DOMNotification;
32 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
33 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
34 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
35 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
36 import org.opendaylight.yangtools.util.ListenerRegistry;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
39 public class DOMNotificationRouterTest {
42 public void create() throws Exception {
43 assertNotNull(DOMNotificationRouter.create(1024));
46 @SuppressWarnings("checkstyle:IllegalCatch")
48 public void complexTest() throws Exception {
49 final DOMNotificationSubscriptionListener domNotificationSubscriptionListener =
50 mock(DOMNotificationSubscriptionListener.class);
51 doNothing().when(domNotificationSubscriptionListener).onSubscriptionChanged(any());
53 final CountDownLatch latch = new CountDownLatch(1);
54 final DOMNotificationListener domNotificationListener = new TestListener(latch);
55 final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1024);
57 Multimap<Absolute, ?> listeners = domNotificationRouter.listeners();
59 assertTrue(listeners.isEmpty());
60 assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener,
61 Absolute.of(TestModel.TEST_QNAME)));
62 assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener,
63 Absolute.of(TestModel.TEST2_QNAME)));
65 listeners = domNotificationRouter.listeners();
67 assertFalse(listeners.isEmpty());
69 ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
70 domNotificationRouter.subscriptionListeners();
72 assertEquals(0, subscriptionListeners.streamListeners().count());
73 assertNotNull(domNotificationRouter.registerSubscriptionListener(domNotificationSubscriptionListener));
75 subscriptionListeners = domNotificationRouter.subscriptionListeners();
76 assertSame(domNotificationSubscriptionListener,
77 subscriptionListeners.streamListeners().findAny().orElseThrow());
79 final DOMNotification domNotification = mock(DOMNotification.class);
80 doReturn("test").when(domNotification).toString();
81 doReturn(Absolute.of(TestModel.TEST_QNAME)).when(domNotification).getType();
82 doReturn(TEST_CHILD).when(domNotification).getBody();
84 assertNotNull(domNotificationRouter.offerNotification(domNotification));
87 assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
88 assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
89 } catch (Exception e) {
90 // FIXME: what is the point here?!
91 assertTrue(e instanceof UnsupportedOperationException);
94 assertNotNull(domNotificationRouter.putNotification(domNotification));
98 public void offerNotification() throws Exception {
99 final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1024);
100 final DOMNotification domNotification = mock(DOMNotification.class);
101 doReturn(Absolute.of(TestModel.TEST_QNAME)).when(domNotification).getType();
102 doReturn(TEST_CHILD).when(domNotification).getBody();
103 assertNotNull(domNotificationRouter.putNotification(domNotification));
104 assertNotNull(domNotificationRouter.offerNotification(domNotification));
105 assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
109 public void testOfferNotificationWithBlocking() throws Exception {
110 final CountDownLatch latch = new CountDownLatch(1);
111 final TestListener testListener = new TestListener(latch);
112 final DOMNotification domNotification = mock(DOMNotification.class);
113 doReturn("test").when(domNotification).toString();
114 doReturn(Absolute.of(TestModel.TEST_QNAME)).when(domNotification).getType();
115 doReturn(TEST_CHILD).when(domNotification).getBody();
117 try (TestRouter testRouter = new TestRouter(1)) {
118 assertNotNull(testRouter.registerNotificationListener(testListener, Absolute.of(TestModel.TEST_QNAME)));
119 assertNotNull(testRouter.registerNotificationListener(testListener, Absolute.of(TestModel.TEST2_QNAME)));
121 assertNotEquals(DOMNotificationPublishService.REJECTED,
122 testRouter.offerNotification(domNotification, 3, TimeUnit.SECONDS));
123 assertTrue("Listener was not notified", latch.await(5, TimeUnit.SECONDS));
124 assertEquals("Received notifications", 1, testListener.getReceivedNotifications().size());
126 assertEquals(DOMNotificationPublishService.REJECTED,
127 testRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
128 assertEquals("Received notifications", 1, testListener.getReceivedNotifications().size());
133 public void close() throws Exception {
134 final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1024);
135 final ExecutorService executor = domNotificationRouter.executor();
136 final ExecutorService observer = domNotificationRouter.observer();
138 assertFalse(executor.isShutdown());
139 assertFalse(observer.isShutdown());
140 domNotificationRouter.close();
141 assertTrue(executor.isShutdown());
142 assertTrue(observer.isShutdown());
145 private static class TestListener implements DOMNotificationListener {
146 private final CountDownLatch latch;
147 private final List<DOMNotification> receivedNotifications = new ArrayList<>();
149 TestListener(final CountDownLatch latch) {
154 public void onNotification(final DOMNotification notification) {
155 receivedNotifications.add(notification);
159 public List<DOMNotification> getReceivedNotifications() {
160 return receivedNotifications;
164 private static class TestRouter extends DOMNotificationRouter {
166 private boolean triggerRejected = false;
168 TestRouter(final int queueDepth) {
173 ListenableFuture<? extends Object> publish(final DOMNotification notification,
174 final Collection<AbstractListenerRegistration<? extends DOMNotificationListener>> subscribers) {
175 if (triggerRejected) {
179 triggerRejected = true;
180 return super.publish(notification, subscribers);
184 public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
185 throws InterruptedException {
187 return super.putNotification(notification);