Source:
public void testStrategyRaceCondition() { int noopStrategyCount = 0; MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider(); Facilities facilities = new MockFacilities(clusterInfoProvider); DarkClusterDispatcher darkClusterDispatcher = new DefaultDarkClusterDispatcherImpl(new MockClient(false)); DarkClusterStrategyFactory strategyFactory = new DarkClusterStrategyFactoryImpl(facilities, SOURCE_CLUSTER_NAME, darkClusterDispatcher, new DoNothingNotifier(), new Random(SEED), new CountingVerifierManager()); strategyFactory.start(); DarkClusterConfig darkClusterConfig1 = new DarkClusterConfig().setMultiplier(0.5f); clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1); DarkClusterStrategy strategy = strategyFactory.getOrCreate(DARK_CLUSTER_NAME, darkClusterConfig1); Assert.assertTrue(strategy instanceof ConstantMultiplierDarkClusterStrategy); Assert.assertEquals(((ConstantMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier"); clusterInfoProvider.registerClusterListener(new DeletingClusterListener(clusterInfoProvider)); ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); final CountDownLatch latch = new CountDownLatch(1); try { scheduledExecutorService.scheduleAtFixedRate(() -> { clusterInfoProvider.triggerClusterRefresh(DARK_CLUSTER_NAME); latch.countDown(); }, 0, 1, TimeUnit.MILLISECONDS); if (!latch.await(30, TimeUnit.SECONDS)) { fail("unable to execute task on executor"); } for (int i = 0; i< 100000; i++) { strategy = strategyFactory.getOrCreate(DARK_CLUSTER_NAME, darkClusterConfig1); Assert.assertNotNull(strategy, "null at iteration: " + i); if (strategy instanceof NoOpDarkClusterStrategy) { noopStrategyCount++; } } System.out.println("noopStrategyCount: " + noopStrategyCount); } catch (InterruptedException ie) { fail("got interrupted exception", ie); } finally { scheduledExecutorService.shutdown(); } }

Target:
public void testStrategyRaceCondition() { int noopStrategyCount = 0; DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f); _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1); DarkClusterStrategy strategy = _strategyFactory.getOrCreate(DARK_CLUSTER_NAME, darkClusterConfig1); Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy); Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier"); _clusterInfoProvider.registerClusterListener(new DeletingClusterListener(_clusterInfoProvider)); ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); final CountDownLatch latch = new CountDownLatch(1); try { scheduledExecutorService.scheduleAtFixedRate(() -> { _clusterInfoProvider.notifyListenersClusterAdded(DARK_CLUSTER_NAME); latch.countDown(); }, 0, 1, TimeUnit.MILLISECONDS); if (!latch.await(30, TimeUnit.SECONDS)) { fail("unable to execute task on executor"); } for (int i = 0; i< 100000; i++) { strategy = _strategyFactory.getOrCreate(DARK_CLUSTER_NAME, darkClusterConfig1); Assert.assertNotNull(strategy, "null at iteration: " + i); if (strategy instanceof NoOpDarkClusterStrategy) { noopStrategyCount++; } } System.out.println("noopStrategyCount: " + noopStrategyCount); } catch (InterruptedException ie) { fail("got interrupted exception", ie); } finally { scheduledExecutorService.shutdown(); } }