Skip to content

Commit 45af8c2

Browse files
committed
Integration test
1 parent 92476e6 commit 45af8c2

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

pom.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>io.lettuce</groupId>
55
<artifactId>lettuce-core</artifactId>
6-
<version>6.7.0.BUILD-OBSERVABILITY-SNAPSHOT</version>
6+
<version>6.7.0.BUILD-SNAPSHOT</version>
77
<packaging>jar</packaging>
88

99
<name>Lettuce</name>
@@ -90,14 +90,16 @@
9090
</scm>
9191

9292
<distributionManagement>
93-
<repository>
94-
<id>github</id>
95-
<url>https://maven.pkg.github.com/ggivo/lettuce</url>
96-
</repository>
9793
<snapshotRepository>
98-
<id>github</id>
99-
<url>https://maven.pkg.github.com/ggivo/lettuce</url>
94+
<id>ossrh</id>
95+
<name>Sonatype Nexus Snapshots</name>
96+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
10097
</snapshotRepository>
98+
<repository>
99+
<id>ossrh</id>
100+
<name>Nexus Release Repository</name>
101+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
102+
</repository>
101103
</distributionManagement>
102104

103105
<dependencyManagement>

src/main/java/io/lettuce/core/protocol/ConnectionWatchdog.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public int getAttempts() {
165165
return attempts.get();
166166
}
167167

168-
public void increment() {
169-
attempts.incrementAndGet();
168+
public int incrementAndGet() {
169+
return attempts.incrementAndGet();
170170
}
171171

172172
}
@@ -341,8 +341,7 @@ public void scheduleReconnect() {
341341
if ((channel == null || !channel.isActive()) && reconnectSchedulerSync.compareAndSet(false, true)) {
342342
// attempts++;
343343
// final int attempt = attempts;
344-
attempts.increment();
345-
final int attempt = attempts.getAttempts();
344+
final int attempt = attempts.incrementAndGet();
346345
connectionMonitor.incrementReconnectionAttempts(epid);
347346

348347
Duration delay = reconnectDelay.createDelay(attempt);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.lettuce.core;
2+
3+
import io.lettuce.core.api.StatefulRedisConnection;
4+
import io.lettuce.core.api.sync.RedisCommands;
5+
import io.lettuce.core.metrics.MicrometerConnectionMonitor;
6+
import io.lettuce.core.metrics.MicrometerOptions;
7+
import io.lettuce.core.resource.ClientResources;
8+
import io.lettuce.test.LettuceExtension;
9+
import io.lettuce.test.Wait;
10+
import io.lettuce.test.resource.TestClientResources;
11+
import io.micrometer.core.instrument.MeterRegistry;
12+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
13+
import org.junit.jupiter.api.Tag;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.extension.ExtendWith;
16+
17+
import java.time.Duration;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import static io.lettuce.TestTags.INTEGRATION_TEST;
21+
import static io.lettuce.core.metrics.MicrometerConnectionMonitor.METRIC_CONNECTION_INACTIVE_TIME;
22+
import static io.lettuce.core.metrics.MicrometerConnectionMonitor.METRIC_CONNECTION_RECONNECTION_ATTEMPTS;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Mark Paluch
27+
*/
28+
@Tag(INTEGRATION_TEST)
29+
@ExtendWith(LettuceExtension.class)
30+
class ConnectionMonitorIntegrationTests extends TestSupport {
31+
32+
private final MeterRegistry meterRegistry = new SimpleMeterRegistry();
33+
34+
private final ClientResources clientResources = TestClientResources.get();
35+
36+
@Test
37+
void metricConnectionInactiveTime() throws InterruptedException {
38+
39+
MicrometerOptions options = MicrometerOptions.create();
40+
MicrometerConnectionMonitor monitor = new MicrometerConnectionMonitor(meterRegistry, options);
41+
ClientResources resources = clientResources.mutate().connectionMonitor(monitor).build();
42+
RedisClient client = RedisClient.create(resources, RedisURI.Builder.redis(host, port).build());
43+
44+
try ( StatefulRedisConnection<String, String> connection = client.connect()) {
45+
RedisCommands<String, String> redis = connection.sync();
46+
47+
// Force disconnection
48+
redis.quit();
49+
Wait.untilTrue(() -> !connection.isOpen()).during(Duration.ofSeconds(1)).waitOrTimeout();
50+
51+
// Wait for successful reconnection
52+
Wait.untilTrue(() -> connection.isOpen()).during(Duration.ofSeconds(1)).waitOrTimeout();
53+
54+
// At least one reconnect attempt
55+
assertThat(meterRegistry.find(METRIC_CONNECTION_RECONNECTION_ATTEMPTS).counter().count()).isGreaterThanOrEqualTo(1);
56+
assertThat(meterRegistry.find(METRIC_CONNECTION_INACTIVE_TIME).timers()).isNotEmpty();
57+
assertThat(meterRegistry.find(METRIC_CONNECTION_INACTIVE_TIME).timer().count()).isEqualTo(1);
58+
assertThat(meterRegistry.find(METRIC_CONNECTION_INACTIVE_TIME).timer().totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)