-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement isClosedForSend / isClosedForReceive (#42)
- Loading branch information
Showing
8 changed files
with
221 additions
and
65 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
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
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
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
92 changes: 92 additions & 0 deletions
92
core/src/test/java/com/softwaremill/jox/ChannelClosedTest.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,92 @@ | ||
package com.softwaremill.jox; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.Semaphore; | ||
|
||
import static com.softwaremill.jox.TestUtil.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ChannelClosedTest { | ||
@Test | ||
void testClosed_noValues_whenError() { | ||
// given | ||
Channel<Integer> c = new Channel<>(); | ||
|
||
// when | ||
c.error(new RuntimeException()); | ||
|
||
// then | ||
assertTrue(c.isClosedForReceive()); | ||
assertTrue(c.isClosedForSend()); | ||
} | ||
|
||
@Test | ||
void testClosed_noValues_whenDone() { | ||
// given | ||
Channel<Integer> c = new Channel<>(); | ||
|
||
// when | ||
c.done(); | ||
|
||
// then | ||
assertTrue(c.isClosedForReceive()); | ||
assertTrue(c.isClosedForSend()); | ||
} | ||
|
||
@Test | ||
void testClosed_hasSuspendedValues_whenDone() throws InterruptedException, ExecutionException { | ||
// given | ||
Channel<Integer> c = new Channel<>(); | ||
|
||
// when | ||
scoped(scope -> { | ||
var f = forkCancelable(scope, () -> { | ||
c.send(1); | ||
}); | ||
|
||
try { | ||
Thread.sleep(100); // let the send suspend | ||
c.done(); | ||
|
||
// then | ||
assertFalse(c.isClosedForReceive()); | ||
assertTrue(c.isClosedForSend()); | ||
} finally { | ||
f.cancel(); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
void testClosed_hasBufferedValues_whenDone() throws InterruptedException { | ||
// given | ||
Channel<Integer> c = new Channel<>(5); | ||
|
||
// when | ||
c.send(1); | ||
c.send(2); | ||
c.done(); | ||
|
||
// then | ||
assertFalse(c.isClosedForReceive()); | ||
assertTrue(c.isClosedForSend()); | ||
} | ||
|
||
@Test | ||
void testClosed_hasValues_whenError() throws InterruptedException { | ||
// given | ||
Channel<Integer> c = new Channel<>(5); | ||
|
||
// when | ||
c.send(1); | ||
c.send(2); | ||
c.error(new RuntimeException()); | ||
|
||
// then | ||
assertTrue(c.isClosedForReceive()); | ||
assertTrue(c.isClosedForSend()); | ||
} | ||
} |
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
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
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