/** timer task for expiring the given TXStates */ public void expireDisconnectedClientTransactions(Set<TXId> txIds, boolean distribute) { // increase the client transaction timeout setting to avoid a late in-flight client operation // preventing the expiration of the client transaction. long timeout = (long) (TimeUnit.SECONDS.toMillis(getTransactionTimeToLive()) * 1.1); if (timeout <= 0) { removeHostedTXState(txIds); } synchronized (this.hostedTXStates) { Iterator<Map.Entry<TXId, TXStateProxy>> iterator = this.hostedTXStates.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<TXId, TXStateProxy> entry = iterator.next(); if (txIds.contains(entry.getKey())) { scheduleToRemoveClientTransaction(entry.getKey(), timeout); } } } if (distribute) { expireClientTransactionsOnRemoteServer(txIds); } }
@Test public void clientTransactionsNotToBeDistributedAreNotSentToRemoveServer() { Set<TXId> txIds = mock(Set.class); spyTxMgr.expireDisconnectedClientTransactions(txIds, false); verify(spyTxMgr, never()).expireClientTransactionsOnRemoteServer(eq(txIds)); }
@Test public void clientTransactionsToBeExpiredAndDistributedAreSentToRemoveServer() { Set<TXId> txIds = mock(Set.class); spyTxMgr.expireDisconnectedClientTransactions(txIds, true); verify(spyTxMgr, times(1)).expireClientTransactionsOnRemoteServer(eq(txIds)); }
@Test public void clientTransactionsToBeRemovedAndDistributedAreSentToRemoveServerIfWithNoTimeout() { Set<TXId> txIds = (Set<TXId>) mock(Set.class); doReturn(0).when(spyTxMgr).getTransactionTimeToLive(); when(txIds.iterator()).thenAnswer(new Answer<Iterator<TXId>>() { @Override public Iterator<TXId> answer(InvocationOnMock invocation) throws Throwable { return Arrays.asList(txid, mock(TXId.class)).iterator(); } }); spyTxMgr.expireDisconnectedClientTransactions(txIds, true); verify(spyTxMgr, times(1)).expireClientTransactionsOnRemoteServer(eq(txIds)); }
@Test public void clientTransactionsToBeExpiredAreRemovedAndNotDistributedIfWithNoTimeout() { doReturn(1).when(spyTxMgr).getTransactionTimeToLive(); TXId txId1 = mock(TXId.class); TXId txId2 = mock(TXId.class); TXId txId3 = mock(TXId.class); tx1 = spyTxMgr.getOrSetHostedTXState(txId1, msg); tx2 = spyTxMgr.getOrSetHostedTXState(txId2, msg); Set<TXId> txIds = spy(new HashSet<>()); txIds.add(txId1); doReturn(0).when(spyTxMgr).getTransactionTimeToLive(); when(txIds.iterator()).thenAnswer(new Answer<Iterator<TXId>>() { @Override public Iterator<TXId> answer(InvocationOnMock invocation) throws Throwable { return Arrays.asList(txId1, txId3).iterator(); } }); assertEquals(2, spyTxMgr.getHostedTXStates().size()); spyTxMgr.expireDisconnectedClientTransactions(txIds, false); verify(spyTxMgr, never()).expireClientTransactionsOnRemoteServer(eq(txIds)); verify(spyTxMgr, times(1)).removeHostedTXState(eq(txIds)); verify(spyTxMgr, times(1)).removeHostedTXState(eq(txId1)); verify(spyTxMgr, times(1)).removeHostedTXState(eq(txId3)); assertEquals(tx2, spyTxMgr.getHostedTXStates().get(txId2)); assertEquals(1, spyTxMgr.getHostedTXStates().size()); }