5
5
import org .mockito .MockedConstruction ;
6
6
7
7
import redis .clients .jedis .Connection ;
8
- import redis .clients .jedis .ConnectionPool ;
9
8
import redis .clients .jedis .DefaultJedisClientConfig ;
10
9
import redis .clients .jedis .EndpointConfig ;
11
10
import redis .clients .jedis .HostAndPort ;
21
20
import static org .mockito .Mockito .mockConstruction ;
22
21
import static org .mockito .Mockito .when ;
23
22
24
- public class MultiClusterDynamicEndpointUnitTest {
23
+ public class MultiDbConnectionProviderDynamicEndpointUnitTest {
25
24
26
25
private MultiDbConnectionProvider provider ;
27
26
private JedisClientConfig clientConfig ;
@@ -41,26 +40,26 @@ void setUp() {
41
40
provider = new MultiDbConnectionProvider (multiConfig );
42
41
}
43
42
44
- // Helper method to create cluster configurations
43
+ // Helper method to create database configurations
45
44
private DatabaseConfig createDatabaseConfig (HostAndPort hostAndPort , float weight ) {
46
45
// Disable health check for unit tests to avoid real connections
47
46
return DatabaseConfig .builder (hostAndPort , clientConfig ).weight (weight )
48
47
.healthCheckEnabled (false ).build ();
49
48
}
50
49
51
50
@ Test
52
- void testAddNewCluster () {
51
+ void testAddNewDatabase () {
53
52
DatabaseConfig newConfig = createDatabaseConfig (endpoint2 .getHostAndPort (), 2.0f );
54
53
55
54
// Should not throw exception
56
55
assertDoesNotThrow (() -> provider .add (newConfig ));
57
56
58
- // Verify the cluster was added by checking it can be retrieved
57
+ // Verify the database was added by checking it can be retrieved
59
58
assertNotNull (provider .getDatabase (endpoint2 .getHostAndPort ()));
60
59
}
61
60
62
61
@ Test
63
- void testAddDuplicateCluster () {
62
+ void testAddDuplicateDatabase () {
64
63
DatabaseConfig duplicateConfig = createDatabaseConfig (endpoint1 .getHostAndPort (), 2.0f );
65
64
66
65
// Should throw validation exception for duplicate endpoint
@@ -80,19 +79,18 @@ void testRemoveExistingCluster() {
80
79
81
80
try (MockedConstruction <TrackingConnectionPool > mockedPool = mockPool (mockConnection )) {
82
81
// Create initial provider with endpoint1
83
- DatabaseConfig clusterConfig1 = createDatabaseConfig (endpoint1 .getHostAndPort (), 1.0f );
82
+ DatabaseConfig dbConfig1 = createDatabaseConfig (endpoint1 .getHostAndPort (), 1.0f );
84
83
85
- MultiDbConfig multiConfig = MultiDbConfig .builder (new DatabaseConfig [] { clusterConfig1 })
86
- .build ();
84
+ MultiDbConfig multiConfig = MultiDbConfig .builder (new DatabaseConfig [] { dbConfig1 }).build ();
87
85
88
86
try (MultiDbConnectionProvider providerWithMockedPool = new MultiDbConnectionProvider (
89
87
multiConfig )) {
90
88
91
- // Add endpoint2 as second cluster
89
+ // Add endpoint2 as second database
92
90
DatabaseConfig newConfig = createDatabaseConfig (endpoint2 .getHostAndPort (), 2.0f );
93
91
providerWithMockedPool .add (newConfig );
94
92
95
- // Now remove endpoint1 (original cluster )
93
+ // Now remove endpoint1 (original database )
96
94
assertDoesNotThrow (() -> providerWithMockedPool .remove (endpoint1 .getHostAndPort ()));
97
95
98
96
// Verify endpoint1 was removed
@@ -119,8 +117,8 @@ void testRemoveNonExistentCluster() {
119
117
}
120
118
121
119
@ Test
122
- void testRemoveLastRemainingCluster () {
123
- // Should throw validation exception when trying to remove the last cluster
120
+ void testRemoveLastRemainingDatabase () {
121
+ // Should throw validation exception when trying to remove the last database
124
122
assertThrows (JedisValidationException .class , () -> provider .remove (endpoint1 .getHostAndPort ()));
125
123
}
126
124
@@ -132,7 +130,7 @@ void testRemoveNullEndpoint() {
132
130
133
131
@ Test
134
132
void testAddAndRemoveMultipleClusters () {
135
- // Add endpoint2 as second cluster
133
+ // Add endpoint2 as second database
136
134
DatabaseConfig config2 = createDatabaseConfig (endpoint2 .getHostAndPort (), 2.0f );
137
135
138
136
// Create a third endpoint for this test
@@ -142,30 +140,30 @@ void testAddAndRemoveMultipleClusters() {
142
140
provider .add (config2 );
143
141
provider .add (config3 );
144
142
145
- // Verify all clusters exist
143
+ // Verify all databases exist
146
144
assertNotNull (provider .getDatabase (endpoint1 .getHostAndPort ()));
147
145
assertNotNull (provider .getDatabase (endpoint2 .getHostAndPort ()));
148
146
assertNotNull (provider .getDatabase (endpoint3 ));
149
147
150
148
// Remove endpoint2
151
149
provider .remove (endpoint2 .getHostAndPort ());
152
150
153
- // Verify correct cluster was removed
151
+ // Verify correct database was removed
154
152
assertNull (provider .getDatabase (endpoint2 .getHostAndPort ()));
155
153
assertNotNull (provider .getDatabase (endpoint1 .getHostAndPort ()));
156
154
assertNotNull (provider .getDatabase (endpoint3 ));
157
155
}
158
156
159
157
@ Test
160
158
void testActiveClusterHandlingOnAdd () {
161
- // The initial cluster should be active
159
+ // The initial database should be active
162
160
assertNotNull (provider .getDatabase ());
163
161
164
162
// Add endpoint2 with higher weight
165
163
DatabaseConfig newConfig = createDatabaseConfig (endpoint2 .getHostAndPort (), 5.0f );
166
164
provider .add (newConfig );
167
165
168
- // Active cluster should still be valid (implementation may or may not switch)
166
+ // Active database should still be valid (implementation may or may not switch)
169
167
assertNotNull (provider .getDatabase ());
170
168
}
171
169
@@ -176,26 +174,25 @@ void testActiveClusterHandlingOnRemove() {
176
174
177
175
try (MockedConstruction <TrackingConnectionPool > mockedPool = mockPool (mockConnection )) {
178
176
// Create initial provider with endpoint1
179
- DatabaseConfig clusterConfig1 = createDatabaseConfig (endpoint1 .getHostAndPort (), 1.0f );
177
+ DatabaseConfig dbConfig1 = createDatabaseConfig (endpoint1 .getHostAndPort (), 1.0f );
180
178
181
- MultiDbConfig multiConfig = MultiDbConfig .builder (new DatabaseConfig [] { clusterConfig1 })
182
- .build ();
179
+ MultiDbConfig multiConfig = MultiDbConfig .builder (new DatabaseConfig [] { dbConfig1 }).build ();
183
180
184
181
try (MultiDbConnectionProvider providerWithMockedPool = new MultiDbConnectionProvider (
185
182
multiConfig )) {
186
183
187
- // Add endpoint2 as second cluster
184
+ // Add endpoint2 as second database
188
185
DatabaseConfig newConfig = createDatabaseConfig (endpoint2 .getHostAndPort (), 2.0f );
189
186
providerWithMockedPool .add (newConfig );
190
187
191
- // Get current active cluster
188
+ // Get current active database
192
189
Object initialActiveCluster = providerWithMockedPool .getDatabase ();
193
190
assertNotNull (initialActiveCluster );
194
191
195
- // Remove endpoint1 (original cluster , might be active)
192
+ // Remove endpoint1 (original database , might be active)
196
193
providerWithMockedPool .remove (endpoint1 .getHostAndPort ());
197
194
198
- // Should still have an active cluster
195
+ // Should still have an active database
199
196
assertNotNull (providerWithMockedPool .getDatabase ());
200
197
}
201
198
}
0 commit comments