public static <T> void replaceField(Object owner, String fieldName, Function<T, T> func) { replaceField(owner.getClass(), owner, fieldName, func); }
/** * Add a hook to cache startup sequence that will allow to replace existing component with a mock. * @param cacheContainer * @param consumer */ public static void addCacheStartingHook(CacheContainer cacheContainer, BiConsumer<String, ComponentRegistry> consumer) { GlobalComponentRegistry gcr = extractGlobalComponentRegistry(cacheContainer); extractField(gcr, "moduleLifecycles"); TestingUtil.<Collection<ModuleLifecycle>>replaceField(gcr, "moduleLifecycles", moduleLifecycles -> { Collection<ModuleLifecycle> copy = new ArrayList<>(moduleLifecycles); copy.add(new ModuleLifecycle() { @Override public void cacheStarting(ComponentRegistry cr, Configuration configuration, String cacheName) { consumer.accept(cacheName, cr); } }); return copy; }); }
public static CountingRequestRepository replaceDispatcher(EmbeddedCacheManager cacheManager) { GlobalComponentRegistry gcr = cacheManager.getGlobalComponentRegistry(); JGroupsTransport transport = (JGroupsTransport) gcr.getComponent(Transport.class); RequestRepository requestRepository = (RequestRepository) TestingUtil.extractField(JGroupsTransport.class, transport, "requests"); CountingRequestRepository instance = new CountingRequestRepository(requestRepository); TestingUtil.replaceField(instance, "requests", transport, JGroupsTransport.class); return instance; }
private void doWhenSourceIterationReaches(String key, TestCluster cluster, String cacheName, IterationCallBack callback) { cluster.getEmbeddedCaches(cacheName).forEach(c -> { PersistenceManager pm = extractComponent(c, PersistenceManager.class); RemoteStore remoteStore = pm.getStores(RemoteStore.class).iterator().next(); RemoteCacheImpl remoteCache = TestingUtil.extractField(remoteStore, "remoteCache"); RemoteCacheImpl spy = spy(remoteCache); doAnswer(invocation -> { Object[] params = invocation.getArguments(); CallbackRemoteIterator<Object> remoteCloseableIterator = new CallbackRemoteIterator<>(spy.getOperationsFactory(), (int) params[1], null, true, spy.getDataFormat()); remoteCloseableIterator.addCallback(callback, key); remoteCloseableIterator.start(); return remoteCloseableIterator; }).when(spy).retrieveEntriesWithMetadata(isNull(), anyInt()); TestingUtil.replaceField(spy, "remoteCache", remoteStore, RemoteStore.class); }); }
protected void skipTxCompletion(final AdvancedCache<Object, Object> cache, final CountDownLatch releaseLocksLatch) { RpcManager rpcManager = new AbstractDelegatingRpcManager(cache.getRpcManager()) { @Override protected <T> void performSend(Collection<Address> targets, ReplicableCommand command, Function<ResponseCollector<T>, CompletionStage<T>> invoker) { if (command instanceof TxCompletionNotificationCommand) { releaseLocksLatch.countDown(); log.tracef("Skipping TxCompletionNotificationCommand"); } else { super.performSend(targets, command, invoker); } } }; //not too nice: replace the rpc manager in the class that builds the Sync objects final TransactionTable transactionTable = TestingUtil.getTransactionTable(cache(1)); TestingUtil.replaceField(rpcManager, "rpcManager", transactionTable, TransactionTable.class); TxControlInterceptor txControlInterceptor = new TxControlInterceptor(); txControlInterceptor.prepareProgress.countDown(); txControlInterceptor.commitProgress.countDown(); advancedCache(1).addInterceptor(txControlInterceptor, 1); }
@Override protected void createCacheManagers() throws Throwable { ConfigurationBuilder configuration = defaultRecoveryConfig(); createCluster(configuration, 2); waitForClusterToForm(); XaTransactionTable txTable = tt(0); recoveryManager = new PostCommitRecoveryStateTest.RecoveryManagerDelegate( TestingUtil.extractComponent(cache(0), RecoveryManager.class)); TestingUtil.replaceField(recoveryManager, "recoveryManager", txTable, XaTransactionTable.class); }
public void testStartSameCache() { final EmbeddedCacheManager cacheManager = manager(0); GlobalComponentRegistry registry = (GlobalComponentRegistry) TestingUtil.extractField(cacheManager, "globalComponentRegistry"); List<ModuleLifecycle> lifecycles = new LinkedList<>(); TestingUtil.replaceField(lifecycles, "moduleLifecycles", registry, GlobalComponentRegistry.class); lifecycles.add(new ModuleLifecycle() { @Override public void cacheStarted(ComponentRegistry cr, String cacheName) { Cache cache = cacheManager.getCache("single"); cache.put("k1", "v1"); } }); Cache<Object, Object> some = cacheManager.getCache("single"); some.put("k2", "v2"); assertEquals("v1", cacheManager.getCache("single").get("k1")); assertEquals("v2", cacheManager.getCache("single").get("k2")); } }
GlobalComponentRegistry registry = (GlobalComponentRegistry) TestingUtil.extractField(cacheManager, "globalComponentRegistry"); List<ModuleLifecycle> lifecycles = new LinkedList<ModuleLifecycle>(); TestingUtil.replaceField(lifecycles, "moduleLifecycles", registry, GlobalComponentRegistry.class); lifecycles.add(new ModuleLifecycle() { @Override
@Override protected void createCacheManagers() throws Throwable { ConfigurationBuilder configuration = getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC, true); configuration .locking().useLockStriping(false) .transaction() .transactionManagerLookup(new EmbeddedTransactionManagerLookup()) .useSynchronization(false) .recovery().enable() .clustering().stateTransfer().fetchInMemoryState(false); createCluster(configuration, 2); waitForClusterToForm(); ComponentRegistry componentRegistry = this.cache(0).getAdvancedCache().getComponentRegistry(); XaTransactionTable txTable = (XaTransactionTable) componentRegistry.getComponent(TransactionTable.class); TestingUtil.replaceField( new RecoveryManagerDelegate(TestingUtil.extractComponent(cache(0), RecoveryManager.class)), "recoveryManager", txTable, XaTransactionTable.class); }