From 898a41ca6180e228de7c60581d042e53a634cb43 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 12 Dec 2024 12:02:31 +0100 Subject: [PATCH] Improve test --- .../transfer/SimplexTransferListenerTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/SimplexTransferListenerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/SimplexTransferListenerTest.java index 36abafaf5b9..8907d8715fd 100644 --- a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/SimplexTransferListenerTest.java +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/SimplexTransferListenerTest.java @@ -33,7 +33,7 @@ class SimplexTransferListenerTest { @Test - void cancellation() throws InterruptedException { + void cancellation() { TransferListener delegate = new TransferListener() { @Override public void transferInitiated(TransferEvent event) throws TransferCancelledException { @@ -66,12 +66,12 @@ public void transferFailed(TransferEvent event) {} TransferResource resource = new TransferResource(null, null, "http://maven.org/test/test-resource", new File("file"), null); - DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false); // no close handle + DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false); - // for technical reasons we cannot throw here, even if delegate does cancel transfer + // Send initiated event and make it the "last" event to force blocking listener.transferInitiated(event(session, resource, TransferEvent.EventType.INITIATED)); - - Thread.sleep(500); // to make sure queue is processed, cancellation applied + // Use transferFailed as a sentinel to ensure the previous event was processed + listener.transferFailed(event(session, resource, TransferEvent.EventType.FAILED)); // subsequent call will cancel assertThrows( @@ -80,7 +80,7 @@ public void transferFailed(TransferEvent event) {} } @Test - void handlesAbsentTransferSource() throws InterruptedException, TransferCancelledException { + void handlesAbsentTransferSource() throws TransferCancelledException { TransferResource resource = new TransferResource(null, null, "http://maven.org/test/test-resource", null, null); RepositorySystemSession session = Mockito.mock(RepositorySystemSession.class); @@ -95,10 +95,9 @@ void handlesAbsentTransferSource() throws InterruptedException, TransferCancelle listener.transferInitiated(transferInitiatedEvent); listener.transferStarted(transferStartedEvent); listener.transferProgressed(transferProgressedEvent); + // The succeeded event will be the last one and will block until processed listener.transferSucceeded(transferSucceededEvent); - Thread.sleep(500); // to make sure queue is processed, cancellation applied - Mockito.verify(delegate).transferInitiated(transferInitiatedEvent); Mockito.verify(delegate).transferStarted(transferStartedEvent); Mockito.verify(delegate).transferProgressed(transferProgressedEvent);