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