-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpc: remove complete timeout task after request is complete
Motivation: ReplyQueue uses ScheduledThreadPoolExecutor to collect expired request. However, non expired requests (those that get reply) not removed from the scheduler's queue. Modification: Configure ScheduledThreadPoolExecutor to remove canceled tasks. Add test to demonstrate this 'leak' pattern as well as improve overall test coverage. Result: Avoid potential memory leak for high number of requests with bit timeout values. Acked-by: Marina Sahakyan Target: master, 3.1 (cherry picked from commit 9328681) Signed-off-by: Tigran Mkrtchyan <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/ReplyQueueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.dcache.oncrpc4j.rpc; | ||
|
||
import java.io.EOFException; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.nio.channels.CompletionHandler; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.junit.Before; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class ReplyQueueTest { | ||
|
||
private ReplyQueue replyQueue; | ||
private SocketAddress addr; | ||
private CompletionHandler<RpcReply, RpcTransport> handler; | ||
|
||
@Before | ||
public void setUp() { | ||
replyQueue = new ReplyQueue(); | ||
addr = mock(InetSocketAddress.class); | ||
handler = mock(CompletionHandler.class); | ||
} | ||
|
||
@Test | ||
public void testRemoveCancel() throws EOFException { | ||
|
||
replyQueue.registerKey(1, addr, handler, 1, TimeUnit.MINUTES); | ||
|
||
assertFalse(replyQueue.getTimeoutQueue().isEmpty()); | ||
|
||
replyQueue.get(1); | ||
|
||
assertTrue(replyQueue.getTimeoutQueue().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testInvokeHandlerOnTimeout() throws EOFException, InterruptedException { | ||
|
||
replyQueue.registerKey(1, addr, handler, 1, TimeUnit.NANOSECONDS); | ||
|
||
TimeUnit.SECONDS.sleep(1); | ||
assertTrue(replyQueue.getPendingRequests().isEmpty()); | ||
assertTrue(replyQueue.getTimeoutQueue().isEmpty()); | ||
verify(handler).failed(any(), any()); | ||
} | ||
|
||
@Test | ||
public void testRequestWithoutOnTimeout() throws EOFException, InterruptedException { | ||
|
||
replyQueue.registerKey(1, addr, handler); | ||
assertFalse(replyQueue.getPendingRequests().isEmpty()); | ||
assertTrue(replyQueue.getTimeoutQueue().isEmpty()); | ||
} | ||
|
||
} |