Bug 2517: Catch RuntimeExceptions thrown from the DCL in DataChangeListener
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerTest.java
index 101a73782b498943acb14b0cb03be2dde5df1f87..25d47388fe49579fdb66271e4f33ca7effeef314 100644 (file)
@@ -4,6 +4,7 @@ import akka.actor.ActorRef;
 import akka.actor.DeadLetter;
 import akka.actor.Props;
 import akka.testkit.JavaTestKit;
+import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
@@ -12,6 +13,7 @@ import org.opendaylight.controller.cluster.datastore.messages.EnableNotification
 import org.opendaylight.controller.md.cluster.datastore.model.CompositeModel;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class DataChangeListenerTest extends AbstractActorTest {
 
@@ -70,19 +72,59 @@ public class DataChangeListenerTest extends AbstractActorTest {
             final Props props = DataChangeListener.props(mockListener);
             final ActorRef subject = getSystem().actorOf(props, "testDataChangedWithNoSender");
 
-            // Let the DataChangeListener know that notifications should be enabled
-            subject.tell(new EnableNotification(true), ActorRef.noSender());
+            getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
 
             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
                     ActorRef.noSender());
 
-            getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
-            new Within(duration("1 seconds")) {
-                @Override
-                protected void run() {
-                    expectNoMsg();
+            // Make sure no DataChangedReply is sent to DeadLetters.
+            while(true) {
+                DeadLetter deadLetter;
+                try {
+                    deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
+                } catch (AssertionError e) {
+                    // Timed out - got no DeadLetter - this is good
+                    break;
                 }
-            };
+
+                // We may get DeadLetters for other messages we don't care about.
+                Assert.assertFalse("Unexpected DataChangedReply",
+                        deadLetter.message() instanceof DataChangedReply);
+            }
+        }};
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Test
+    public void testDataChangedWithListenerRuntimeEx(){
+        new JavaTestKit(getSystem()) {{
+            AsyncDataChangeEvent mockChangeEvent1 = Mockito.mock(AsyncDataChangeEvent.class);
+            AsyncDataChangeEvent mockChangeEvent2 = Mockito.mock(AsyncDataChangeEvent.class);
+            AsyncDataChangeEvent mockChangeEvent3 = Mockito.mock(AsyncDataChangeEvent.class);
+
+            AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
+            Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataChanged(mockChangeEvent2);
+
+            Props props = DataChangeListener.props(mockListener);
+            ActorRef subject = getSystem().actorOf(props, "testDataChangedWithListenerRuntimeEx");
+
+            // Let the DataChangeListener know that notifications should be enabled
+            subject.tell(new EnableNotification(true), getRef());
+
+            SchemaContext schemaContext = CompositeModel.createTestContext();
+
+            subject.tell(new DataChanged(schemaContext, mockChangeEvent1),getRef());
+            expectMsgClass(DataChangedReply.class);
+
+            subject.tell(new DataChanged(schemaContext, mockChangeEvent2),getRef());
+            expectMsgClass(DataChangedReply.class);
+
+            subject.tell(new DataChanged(schemaContext, mockChangeEvent3),getRef());
+            expectMsgClass(DataChangedReply.class);
+
+            Mockito.verify(mockListener).onDataChanged(mockChangeEvent1);
+            Mockito.verify(mockListener).onDataChanged(mockChangeEvent2);
+            Mockito.verify(mockListener).onDataChanged(mockChangeEvent3);
         }};
     }
 }