public boolean isOverTransactionTimeoutLimit() { if (getCurrentTime() - getLastOperationTimeFromClient() > TimeUnit.SECONDS .toMillis(txMgr.getTransactionTimeToLive())) { return true; } return false; }
/** 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 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 isOverTransactionTimeoutLimitReturnsTrueIfHavingRecentOperation() { TXStateProxyImpl tx = spy(new TXStateProxyImpl(cache, txManager, txId, false)); doReturn(0L).when(tx).getLastOperationTimeFromClient(); doReturn(1001L).when(tx).getCurrentTime(); when(txManager.getTransactionTimeToLive()).thenReturn(1); assertThat(tx.isOverTransactionTimeoutLimit()).isEqualTo(true); }
@Test public void isOverTransactionTimeoutLimitReturnsFalseIfNotHavingRecentOperation() { TXStateProxyImpl tx = spy(new TXStateProxyImpl(cache, txManager, txId, false)); doReturn(0L).when(tx).getLastOperationTimeFromClient(); doReturn(1000L).when(tx).getCurrentTime(); when(txManager.getTransactionTimeToLive()).thenReturn(1); assertThat(tx.isOverTransactionTimeoutLimit()).isEqualTo(false); }
return; long timeout = getTransactionTimeToLive() * 1000L; if (timeout <= 0) { removeTransactionsSentFromDepartedProxy(proxyServer);
@Test public void clientTransactionsToBeExpiredIsScheduledToBeRemoved() { 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> set = new HashSet<>(); set.add(txId1); set.add(txId2); spyTxMgr.expireDisconnectedClientTransactions(set, false); verify(spyTxMgr, times(1)).scheduleToRemoveClientTransaction(eq(txId1), eq(1100L)); verify(spyTxMgr, times(1)).scheduleToRemoveClientTransaction(eq(txId2), eq(1100L)); verify(spyTxMgr, never()).scheduleToRemoveClientTransaction(eq(txId3), eq(1100L)); }
@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()); }