Skip to content

Commit cb36ac6

Browse files
committed
ClusterTopologyRefreshOptions builder should fail when enableAdaptiveRefreshTrigger is called without options #2575
Improve usage by throwing an exception to early fail on improper API usage.
1 parent 6ef7817 commit cb36ac6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/main/java/io/lettuce/core/cluster/ClusterTopologyRefreshOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public Builder enableAdaptiveRefreshTrigger(RefreshTrigger... refreshTrigger) {
165165

166166
LettuceAssert.notNull(refreshTrigger, "RefreshTriggers must not be null");
167167
LettuceAssert.noNullElements(refreshTrigger, "RefreshTriggers must not contain null elements");
168+
LettuceAssert.notEmpty(refreshTrigger, "RefreshTriggers must not contain at least one element");
168169

169170
adaptiveRefreshTriggers.addAll(Arrays.asList(refreshTrigger));
170171
return this;

src/test/java/io/lettuce/core/cluster/ClusterTopologyRefreshOptionsUnitTests.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.lettuce.core.cluster;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.time.Duration;
2121
import java.util.concurrent.TimeUnit;
@@ -25,6 +25,8 @@
2525
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger;
2626

2727
/**
28+
* Unit tests for {@link ClusterTopologyRefreshOptions}.
29+
*
2830
* @author Mark Paluch
2931
*/
3032
class ClusterTopologyRefreshOptionsUnitTests {
@@ -80,16 +82,16 @@ void testDefault() {
8082

8183
assertThat(options.getRefreshPeriod()).isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD_DURATION);
8284
assertThat(options.isCloseStaleConnections()).isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_CLOSE_STALE_CONNECTIONS);
83-
assertThat(options.isPeriodicRefreshEnabled())
84-
.isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_PERIODIC_REFRESH_ENABLED).isFalse();
85+
assertThat(options.isPeriodicRefreshEnabled()).isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_PERIODIC_REFRESH_ENABLED)
86+
.isFalse();
8587
assertThat(options.useDynamicRefreshSources()).isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_DYNAMIC_REFRESH_SOURCES)
8688
.isTrue();
87-
assertThat(options.getAdaptiveRefreshTimeout()).isEqualTo(
88-
ClusterTopologyRefreshOptions.DEFAULT_ADAPTIVE_REFRESH_TIMEOUT_DURATION);
89-
assertThat(options.getAdaptiveRefreshTriggers()).isEqualTo(
90-
ClusterTopologyRefreshOptions.DEFAULT_ADAPTIVE_REFRESH_TRIGGERS);
91-
assertThat(options.getRefreshTriggersReconnectAttempts()).isEqualTo(
92-
ClusterTopologyRefreshOptions.DEFAULT_REFRESH_TRIGGERS_RECONNECT_ATTEMPTS);
89+
assertThat(options.getAdaptiveRefreshTimeout())
90+
.isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_ADAPTIVE_REFRESH_TIMEOUT_DURATION);
91+
assertThat(options.getAdaptiveRefreshTriggers())
92+
.isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_ADAPTIVE_REFRESH_TRIGGERS);
93+
assertThat(options.getRefreshTriggersReconnectAttempts())
94+
.isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_REFRESH_TRIGGERS_RECONNECT_ATTEMPTS);
9395
}
9496

9597
@Test
@@ -102,4 +104,13 @@ void testEnabled() {
102104
assertThat(options.getAdaptiveRefreshTriggers()).contains(RefreshTrigger.ASK_REDIRECT, RefreshTrigger.MOVED_REDIRECT,
103105
RefreshTrigger.PERSISTENT_RECONNECTS);
104106
}
107+
108+
@Test
109+
void emptyTriggersShouldFail() {
110+
111+
ClusterTopologyRefreshOptions.Builder builder = ClusterTopologyRefreshOptions.builder();
112+
113+
assertThatIllegalArgumentException().isThrownBy(builder::enableAdaptiveRefreshTrigger);
114+
}
115+
105116
}

0 commit comments

Comments
 (0)