|
| 1 | +package io.kurrent.dbclient.connection; |
| 2 | + |
| 3 | +import io.grpc.Status; |
| 4 | +import io.grpc.StatusRuntimeException; |
| 5 | +import io.kurrent.dbclient.*; |
| 6 | +import io.kurrent.dbclient.databases.DockerContainerDatabase; |
| 7 | +import org.junit.jupiter.api.*; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +public class ConnectionTests { |
| 15 | + static private DockerContainerDatabase database; |
| 16 | + static private Logger logger; |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + public void setup() { |
| 20 | + database = (DockerContainerDatabase) DatabaseFactory.spawn(); |
| 21 | + logger = LoggerFactory.getLogger(PersistentSubscriptionsTests.class); |
| 22 | + } |
| 23 | + |
| 24 | + @AfterEach |
| 25 | + public void cleanup() { |
| 26 | + unpauseDatabase(); |
| 27 | + database.dispose(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + @Timeout(value = 30, unit = TimeUnit.SECONDS) |
| 32 | + public void testCallTerminationWhenServerUnreachable() throws Throwable { |
| 33 | + KurrentDBClient client = database.defaultClient(); |
| 34 | + |
| 35 | + ReadResult initialResult = client.readAll(ReadAllOptions.get()).get(5, TimeUnit.SECONDS); |
| 36 | + |
| 37 | + Assertions.assertFalse(initialResult.getEvents().isEmpty()); |
| 38 | + |
| 39 | + pauseDatabase(); |
| 40 | + |
| 41 | + ExecutionException e = Assertions.assertThrows(ExecutionException.class, () -> |
| 42 | + client.readAll().get(30, TimeUnit.SECONDS) |
| 43 | + ); |
| 44 | + |
| 45 | + StatusRuntimeException status = (StatusRuntimeException) e.getCause(); |
| 46 | + Assertions.assertEquals(Status.Code.UNAVAILABLE, status.getStatus().getCode()); |
| 47 | + } |
| 48 | + |
| 49 | + static void pauseDatabase() { |
| 50 | + logger.debug("Pausing database container: {}", database.getContainerId()); |
| 51 | + database.getDockerClient().pauseContainerCmd(database.getContainerId()).exec(); |
| 52 | + } |
| 53 | + |
| 54 | + static void unpauseDatabase() { |
| 55 | + logger.debug("Unpausing database container: {}", database.getContainerId()); |
| 56 | + database.getDockerClient().unpauseContainerCmd(database.getContainerId()).exec(); |
| 57 | + } |
| 58 | +} |
0 commit comments