Last active
January 14, 2023 21:27
-
-
Save Tembrel/144d41c950bbdf781e13c9d508a43d1c to your computer and use it in GitHub Desktop.
Illustrates different semantics of FailureException, see https://github.com/failsafe-lib/failsafe/issues/356
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
package com.example.failsafe; | |
import dev.failsafe.*; | |
import java.io.IOException; | |
import java.util.concurrent.*; | |
import org.junit.*; | |
import static org.junit.Assert.*; | |
/** | |
* See https://github.com/failsafe-lib/failsafe/issues/356 | |
*/ | |
public class Issue356 { | |
static final int NORMAL = 1; | |
Bulkhead<Integer> BULKHEAD = Bulkhead.<Integer>of(1); | |
FailsafeExecutor<Integer> EXEC = Failsafe.with(BULKHEAD); | |
@Test(expected = BulkheadFullException.class) | |
public void synchronousBulkheadFull() { | |
if (!BULKHEAD.tryAcquirePermit()) | |
fail("Couldn't acquire permit"); | |
try { | |
Integer r = EXEC.get(() -> NORMAL); | |
fail("Shouldn't have returned result"); | |
} finally { | |
BULKHEAD.releasePermit(); | |
} | |
} | |
@Test(expected = BulkheadFullException.class) | |
public void asynchronousBulkheadFull() throws Throwable { | |
if (!BULKHEAD.tryAcquirePermit()) | |
fail("Couldn't acquire permit"); | |
try { | |
Integer r = EXEC.getAsync(() -> NORMAL).get(); | |
fail("Shouldn't have returned result"); | |
} catch (ExecutionException ex) { | |
throw ex.getCause(); // expecting it to be BulkheadFullException | |
} finally { | |
BULKHEAD.releasePermit(); | |
} | |
} | |
@Test(expected = IOException.class) | |
public void synchronousIOException() throws Throwable { | |
try { | |
Integer r = EXEC.get(() -> ioException()); | |
fail("Shouldn't have returned result"); | |
} catch (FailsafeException ex) { | |
throw ex.getCause(); | |
} | |
} | |
@Test(expected = IOException.class) | |
public void asynchronousIOException() throws Throwable { | |
try { | |
Integer r = EXEC.getAsync(() -> ioException()).get(); | |
fail("Shouldn't have returned result"); | |
} catch (ExecutionException ex) { | |
throw ex.getCause(); | |
} | |
} | |
@Test | |
public void synchronousSuccess() throws Throwable { | |
int r = EXEC.get(() -> NORMAL); | |
assertEquals(NORMAL, r); | |
} | |
@Test | |
public void asynchronousSuccess() throws Throwable { | |
int r = EXEC.getAsync(() -> NORMAL).get(); | |
assertEquals(NORMAL, r); | |
} | |
Integer ioException() throws IOException { | |
throw new IOException("test exception"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment