Fix intermittent ConfigPersisterTest failure 39/21139/2
authorTom Pantelis <tpanteli@brocade.com>
Fri, 15 May 2015 17:21:13 +0000 (13:21 -0400)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 27 May 2015 02:43:00 +0000 (02:43 +0000)
testSuccessConflictingVersionException fails intermittently on Jenkins
with different errors. I think the problem is that it sets up the
"workingService" which the ConfigPusher is accessing createService on
another thread. In reading Mockito FAQs, you may get strange intermittent
results with multi-threading wrt to stubbing. Mocks can be accessed from
multiple threads but it's recommended that you setup all the stubbed
methods ahead of time. So I changed
testSuccessConflictingVersionException to do so.

After the fix, I ran the ConfigPersisterTest 550 times w/o failure.

Change-Id: Ia22936cfad82416e317935bd40b0eb3ccbdb232c
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
(cherry picked from commit e60d699bfe040f3ce570b4e634b0612eadeaca45)

opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java

index 3be6ca95350062bf679b7be3e68177c351071d3c..1f5d2c1d0f34dec1ada1bb45ab7fbbfb17954880 100644 (file)
@@ -13,12 +13,10 @@ import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
-
 import com.google.common.collect.Sets;
 import java.io.IOException;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.controller.config.api.ConflictingVersionException;
 import org.opendaylight.controller.netconf.api.Capability;
@@ -41,12 +39,15 @@ public class ConfigPersisterTest {
     private ConfigPersisterActivator configPersisterActivator;
     private TestingExceptionHandler handler;
 
-
-    private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception {
+    private void setUpContext(String requiredCapability) throws Exception {
         DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
         ctx = new MockedBundleContext(1000, 1000);
-        doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
         configPersisterActivator = new ConfigPersisterActivator();
+    }
+
+    private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception {
+        setUpContext(requiredCapability);
+        doReturn(conflictingService).when(ctx.serviceFactory).createService(anyString());
         configPersisterActivator.start(ctx.getBundleContext());
     }
 
@@ -73,9 +74,7 @@ public class ConfigPersisterTest {
 
     @Test
     public void testPersisterSuccessfulPush() throws Exception {
-        setUpContextAndStartPersister("cap1", getConflictingService());
-        NetconfOperationService service = getWorkingService(getOKDocument());
-        doReturn(service).when(ctx.serviceFactory).createService(anyString());
+        setUpContextAndStartPersister("cap1", getWorkingService(getOKDocument()));
         Thread.sleep(2000);
         assertCannotRegisterAsJMXListener_pushWasSuccessful();
     }
@@ -130,15 +129,20 @@ public class ConfigPersisterTest {
         }
     }
 
-    @Ignore("this test needs to be redesigned. Remove bundle bundle context and concurrency to make it more stable")
     @Test
     public void testSuccessConflictingVersionException() throws Exception {
-        setUpContextAndStartPersister("cap1", getConflictingService());
-        doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
-        Thread.sleep(500);
-        // working service:
-        LOG.info("Switching to working service **");
-        doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
+        LOG.info("testSuccessConflictingVersionException starting");
+
+        setUpContext("cap1");
+
+        NetconfOperationService conflictingService = getConflictingService();
+        NetconfOperationService workingService = getWorkingService(getOKDocument());
+
+        doReturn(conflictingService).doReturn(conflictingService).doReturn(conflictingService).
+            doReturn(workingService).when(ctx.serviceFactory).createService(anyString());
+
+        configPersisterActivator.start(ctx.getBundleContext());
+
         Thread.sleep(1000);
         assertCannotRegisterAsJMXListener_pushWasSuccessful();
     }