@Override public Void call() throws Exception { final Class<? extends AsyncInterceptor> interceptorClass = interceptor.getClass(); try { log.debug("Wait for all executions paths to be ready to perform calls"); barrier.await(); // test in a loop as the barrier is otherwise not enough to make sure // the different testing threads actually do make changes concurrently // 2000 is still almost nothing in terms of testsuite time. for (int i = 0; i < 2000; i++) { ic.removeInterceptor(interceptorClass); ic.addInterceptor(interceptor, 1); } return null; } finally { log.debug("Wait for all execution paths to finish"); barrier.await(); } } }
public void testBothOwnersSuspected(Method m) throws ExecutionException, InterruptedException { initAndCheck(m); CountDownLatch arrival = new CountDownLatch(2); CountDownLatch release = new CountDownLatch(1); AtomicInteger thrown = new AtomicInteger(); AtomicInteger retried = new AtomicInteger(); cache(0).getAdvancedCache().getAsyncInterceptorChain().addInterceptorAfter(new CheckOTEInterceptor(thrown, retried), StateTransferInterceptor.class); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release), 0); cache(2).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release), 0); Future<Object> future = fork(() -> cache(0).get(key)); assertTrue(arrival.await(10, TimeUnit.SECONDS)); installNewView(cache(0), cache(0)); // The entry was lost, so we'll get null assertNull(future.get()); // Since we've lost all owners, we get an OutdatedTopologyException and we retry assertEquals(1, thrown.get()); assertEquals(1, retried.get()); release.countDown(); }
private SkipIndexingFlagCheckCommandInterceptor init() { Iterator<CommandInterceptor> iterator = cacheManager.getCache(cacheName).getAdvancedCache().getInterceptorChain().iterator(); while (iterator.hasNext()) { CommandInterceptor commandInterceptor = iterator.next(); if (commandInterceptor instanceof SkipIndexingFlagCheckCommandInterceptor) return (SkipIndexingFlagCheckCommandInterceptor) commandInterceptor; } SkipIndexingFlagCheckCommandInterceptor ci = new SkipIndexingFlagCheckCommandInterceptor(); cacheManager.getCache(cacheName).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(ci, 1); return ci; }
public void testDelayed(Method m) { initAndCheck(m); CountDownLatch release = new CountDownLatch(1); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(null, release), 0); long requestStart = System.nanoTime(); assertEquals(m.getName(), cache(0).get(key)); long requestEnd = System.nanoTime(); long remoteTimeout = cache(0).getCacheConfiguration().clustering().remoteTimeout(); long delay = TimeUnit.NANOSECONDS.toMillis(requestEnd - requestStart); assertTrue(delay < remoteTimeout); release.countDown(); }
@TestCachePermission(AuthorizationPermission.ADMIN) public void testRemoveInterceptor_int(SecureCache<String, String> cache) { cache.getAsyncInterceptorChain().addInterceptor(interceptor, 0); cache.getAsyncInterceptorChain().removeInterceptor(0); }
private Future<?> blockedPutFromLoad(CyclicBarrier putFromLoadBarrier) throws InterruptedException, BrokenBarrierException, TimeoutException { BlockingInterceptor blockingInterceptor = new BlockingInterceptor(putFromLoadBarrier, ReadWriteKeyCommand.class, false, true); entityCache.getAsyncInterceptorChain().addInterceptor(blockingInterceptor, 0); cleanup.add(() -> entityCache.removeInterceptor(BlockingInterceptor.class)); // the putFromLoad should be blocked in the interceptor Future<?> putFromLoad = executor.submit(() -> withTxSessionApply(s -> { assertEquals("Original item", s.load(Item.class, itemId).getDescription()); return null; })); putFromLoadBarrier.await(WAIT_TIMEOUT, TimeUnit.SECONDS); blockingInterceptor.suspend(true); return putFromLoad; }
@TestCachePermission(AuthorizationPermission.ADMIN) public void testAddInterceptor_CommandInterceptor_int(SecureCache<String, String> cache) { cache.getAsyncInterceptorChain().addInterceptor(interceptor, 0); cache.getAsyncInterceptorChain().removeInterceptor(0); }
public void testOneOwnerSuspected(Method m) throws ExecutionException, InterruptedException { initAndCheck(m); CountDownLatch arrival = new CountDownLatch(2); CountDownLatch release1 = new CountDownLatch(1); CountDownLatch release2 = new CountDownLatch(1); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release1), 0); cache(2).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release2), 0); Future<?> future = fork(() -> { assertEquals(cache(0).get(key), m.getName()); }); assertTrue(arrival.await(10, TimeUnit.SECONDS)); installNewView(cache(0), cache(0), cache(1)); // suspection should not fail the operation assertFalse(future.isDone()); release1.countDown(); future.get(); release2.countDown(); }
@Override protected void createSites() { super.createSites(); failureInterceptor = new BaseBackupFailureTest.FailureInterceptor(); backup("LON").getAdvancedCache().getAsyncInterceptorChain().addInterceptor(failureInterceptor, 1); }
@Override protected void createSites() { super.createSites(); failureInterceptor = new BaseBackupFailureTest.FailureInterceptor(); cache("LON", 1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(failureInterceptor, 1); }
@Override protected void createSites() { super.createSites(); failureInterceptor = new BaseBackupFailureTest.FailureInterceptor(); backup("LON").getAdvancedCache().getAsyncInterceptorChain().addInterceptor(failureInterceptor, 1); }
@Override protected void createSites() { super.createSites(); failureInterceptor = new FailureInterceptor(); backup("LON").getAdvancedCache().getAsyncInterceptorChain().addInterceptor(failureInterceptor, 1); }
private FlagCheckCommandInterceptor init() { AsyncInterceptorChain interceptorChain = cacheManager.getCache(cacheName).getAdvancedCache().getAsyncInterceptorChain(); FlagCheckCommandInterceptor interceptor = interceptorChain.findInterceptorExtending(FlagCheckCommandInterceptor.class); if (interceptor != null) return interceptor; FlagCheckCommandInterceptor ci = new FlagCheckCommandInterceptor(); interceptorChain.addInterceptor(ci, 1); return ci; }
public void testExceptionFromOneOwnerOtherTimeout(Method m) { initAndCheck(m); CountDownLatch release = new CountDownLatch(1); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new FailingInterceptor(), 0); cache(2).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(null, release), 0); // It's not enough to test if the exception is TimeoutException as we want the remote get fail immediately // upon exception. // We cannot mock TimeService in ScheduledExecutor, so we have to measure if the response was fast // remoteTimeout is gracious enough (15s) to not cause false positives long requestStart = System.nanoTime(); try { expectException(RemoteException.class, CacheException.class, "Injected", () -> cache(0).get(key)); long exceptionThrown = System.nanoTime(); long remoteTimeout = cache(0).getCacheConfiguration().clustering().remoteTimeout(); long delay = TimeUnit.NANOSECONDS.toMillis(exceptionThrown - requestStart); assertTrue(delay < remoteTimeout); } finally { release.countDown(); } }
public void testTimeoutCleanup() throws Exception { final CountDownLatch block = new CountDownLatch(1); final BaseCustomAsyncInterceptor interceptor = new BaseCustomAsyncInterceptor() { @Override public Object visitPrepareCommand(TxInvocationContext ctx, PrepareCommand command) throws Throwable { block.await(); return invokeNext(ctx, command); } }; final AsyncInterceptorChain chain = TestingUtil.extractComponent(cache(1), AsyncInterceptorChain.class); final Object key = new MagicKey(cache(1)); try { chain.addInterceptor(interceptor, 0); tm(0).begin(); cache(0).put(key, "v"); tm(0).commit(); fail("Rollback expected!"); } catch (RollbackException e) { //expected } finally { block.countDown(); chain.removeInterceptor(0); } assertNoTransactions(); assertNoLocks(); }
@Override protected void createCacheManagers() throws Throwable { final ConfigurationBuilder c = getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC, false); c.clustering().hash().numOwners(1); createCluster(c, 2); waitForClusterToForm(); ci0 = new CheckInterceptor(); advancedCache(0).getAsyncInterceptorChain().addInterceptor(ci0, 1); ci1 = new CheckInterceptor(); advancedCache(1).getAsyncInterceptorChain().addInterceptor(ci1, 1); }
public void testOneOwnerSuspectedOtherTimeout(Method m) throws ExecutionException, InterruptedException { initAndCheck(m); CountDownLatch arrival = new CountDownLatch(2); CountDownLatch release = new CountDownLatch(1); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release), 0); cache(2).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new DelayingInterceptor(arrival, release), 0); Future<?> future = fork(() -> { long start = System.nanoTime(); Exceptions.expectException(TimeoutException.class, () -> cache(0).get(key)); long end = System.nanoTime(); long duration = TimeUnit.NANOSECONDS.toMillis(end - start); assertTrue("Request did not wait for long enough: " + duration, duration >= cache(0).getCacheConfiguration().clustering().remoteTimeout()); }); assertTrue(arrival.await(10, TimeUnit.SECONDS)); installNewView(cache(0), cache(0), cache(1)); // suspection should not fail the operation assertFalse(future.isDone()); future.get(); release.countDown(); }
public void testExceptionFromBothOwners(Method m) { initAndCheck(m); cache(1).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new FailingInterceptor(), 0); cache(2).getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new FailingInterceptor(), 0); expectException(RemoteException.class, CacheException.class, "Injected", () -> cache(0).get(key)); }
/** * Test method for {@link SpringRemoteCacheManager#getCache(String)}. */ @Test public final void springRemoteCacheManagerWithTimeoutShouldThrowTimeoutExceptions() { final SpringRemoteCacheManager objectUnderTest = new SpringRemoteCacheManager( remoteCacheManager, 500, 750); final Cache defaultCache = objectUnderTest.getCache(TEST_CACHE_NAME); assertEquals("getCache(" + TEST_CACHE_NAME + ") should have returned a cache name \"" + TEST_CACHE_NAME + "\". However, the returned cache has a different name.", TEST_CACHE_NAME, defaultCache.getName()); defaultCache.put("k1", "v1"); CountDownLatch latch = new CountDownLatch(1); cacheManager.getCache(TEST_CACHE_NAME).getAdvancedCache().getAsyncInterceptorChain() .addInterceptor(new DelayingInterceptor(latch, 700, 800), 0); Exceptions.expectException(CacheException.class, TimeoutException.class, () -> defaultCache.get("k1")); Exceptions.expectException(CacheException.class, TimeoutException.class, () -> defaultCache.put("k1", "v2")); }
public static TransactionTrackInterceptor injectInCache(Cache<?, ?> cache) { AsyncInterceptorChain chain = cache.getAdvancedCache().getAsyncInterceptorChain(); if (chain.containsInterceptorType(TransactionTrackInterceptor.class)) { return chain.findInterceptorWithClass(TransactionTrackInterceptor.class); } TransactionTrackInterceptor interceptor = new TransactionTrackInterceptor(); cache.getAdvancedCache().getComponentRegistry().wireDependencies(interceptor); TestingUtil.startComponent(interceptor); chain.addInterceptor(interceptor, 0); return interceptor; }