Skip to content

Commit

Permalink
Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 12, 2024
1 parent d1787ab commit 898a41c
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 898a41c

Please sign in to comment.