From 89c5571e29995b24e3364ab0c82a7351a3379d18 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Fri, 15 May 2015 13:21:13 -0400 Subject: [PATCH] Fix intermittent ConfigPersisterTest failure 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 (cherry picked from commit e60d699bfe040f3ce570b4e634b0612eadeaca45) --- .../impl/osgi/ConfigPersisterTest.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java index 3be6ca9535..1f5d2c1d0f 100644 --- a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java @@ -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(); } -- 2.36.6