|
| 1 | +package io.kurrent.dbclient.connection; |
| 2 | + |
| 3 | +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; |
| 7 | + |
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | + |
| 14 | +public class ConnectionShutdownTests { |
| 15 | + @Test |
| 16 | + @Timeout(value = 1, unit = TimeUnit.MINUTES) |
| 17 | + public void testDatabaseCleanupWithActiveSubscription() throws Throwable { |
| 18 | + Database testDatabase = DatabaseFactory.spawn(); |
| 19 | + KurrentDBClient client = testDatabase.defaultClient(); |
| 20 | + |
| 21 | + final AtomicInteger count = new AtomicInteger(0); |
| 22 | + final AtomicInteger retryCount = new AtomicInteger(-1); |
| 23 | + final AtomicBoolean cancellationReceived = new AtomicBoolean(false); |
| 24 | + final CountDownLatch cancellationLatch = new CountDownLatch(1); |
| 25 | + final AtomicReference<Throwable> reconnectError = new AtomicReference<>(); |
| 26 | + |
| 27 | + SubscriptionListener listener = new SubscriptionListener() { |
| 28 | + @Override |
| 29 | + public void onEvent(Subscription subscription, ResolvedEvent event) { |
| 30 | + count.incrementAndGet(); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onCancelled(Subscription subscription, Throwable throwable) { |
| 35 | + cancellationReceived.set(true); |
| 36 | + |
| 37 | + retryCount.incrementAndGet(); |
| 38 | + |
| 39 | + try { |
| 40 | + client.subscribeToAll(this).get(10, TimeUnit.SECONDS); |
| 41 | + } catch (Throwable ex) { |
| 42 | + reconnectError.set(ex); |
| 43 | + } finally { |
| 44 | + cancellationLatch.countDown(); |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + client.subscribeToAll(listener).get(); |
| 50 | + |
| 51 | + testDatabase.dispose(); |
| 52 | + |
| 53 | + boolean callbackReceived = cancellationLatch.await(30, TimeUnit.SECONDS); |
| 54 | + Assertions.assertTrue(callbackReceived); |
| 55 | + Assertions.assertTrue(cancellationReceived.get()); |
| 56 | + Assertions.assertTrue(count.get() > 0); |
| 57 | + Assertions.assertEquals(2, retryCount.get()); |
| 58 | + |
| 59 | + Throwable ex = reconnectError.get(); |
| 60 | + Assertions.assertInstanceOf(ConnectionShutdownException.class, ex.getCause()); |
| 61 | + } |
| 62 | +} |
0 commit comments