Skip to content

Commit 7fc5dd3

Browse files
Merge pull request ably#629 from ably/refactor-hosts-unit-tests
Refactor unit tests related to hosts and environmental fallbacks
2 parents 61a44e3 + e7b2fc6 commit 7fc5dd3

1 file changed

Lines changed: 99 additions & 105 deletions

File tree

Lines changed: 99 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
package io.ably.lib.transport;
22

3+
import static org.hamcrest.Matchers.contains;
34
import static org.hamcrest.Matchers.containsInAnyOrder;
45
import static org.hamcrest.Matchers.is;
5-
import static org.hamcrest.Matchers.isEmptyOrNullString;
66
import static org.hamcrest.Matchers.not;
7-
import static org.hamcrest.Matchers.notNullValue;
87
import static org.hamcrest.Matchers.nullValue;
98
import static org.junit.Assert.assertThat;
10-
import static org.junit.Assert.assertTrue;
119

1210
import io.ably.lib.types.AblyException;
11+
import io.ably.lib.types.ClientOptions;
1312
import java.util.ArrayList;
14-
import java.util.Arrays;
15-
1613
import java.util.List;
1714
import org.junit.Test;
1815

19-
import io.ably.lib.types.ClientOptions;
20-
2116
public class HostsTest {
2217

18+
private final ClientOptions options = new ClientOptions();
19+
2320
/**
2421
* Tests for Hosts class (fallback hosts).
2522
*/
2623
@Test
2724
public void hosts_fallback() throws AblyException {
28-
ClientOptions options = new ClientOptions();
25+
// When
2926
Hosts hosts = new Hosts(Defaults.HOST_REALTIME, Defaults.HOST_REALTIME, options);
30-
String host = hosts.getFallback(Defaults.HOST_REALTIME);
31-
/* Expect given fallback host string is (relatively) valid */
32-
assertThat(host, not(isEmptyOrNullString()));
33-
/* Expect multiple calls will provide different (relatively) valid fallback hosts */
34-
String host2 = hosts.getFallback(host);
35-
assertThat(host2, not(isEmptyOrNullString()));
36-
assertThat(host2, not(host));
37-
/* Expect a null, when we requested more than available fallback hosts */
38-
for (int i = Defaults.HOST_FALLBACKS.length - 1; i > 0; i--) {
39-
assertThat(host2, notNullValue());
40-
host2 = hosts.getFallback(host2);
41-
}
27+
28+
// Then
29+
List<String> fallbackHosts = collectFallbackHosts(hosts);
30+
assertThat(hosts.getPrimaryHost(), is(Defaults.HOST_REALTIME));
31+
// the returned fallback hosts should have the same elements as default host fallbacks
32+
assertThat(fallbackHosts, containsInAnyOrder(Defaults.HOST_FALLBACKS));
33+
// expect the fallback hosts to be shuffled
34+
assertThat(fallbackHosts, not(contains(Defaults.HOST_FALLBACKS)));
4235
}
4336

4437
/**
4538
* Expect an exception when setting both realtimeHost and environment.
4639
*/
4740
@Test(expected = AblyException.class)
4841
public void hosts_host_and_environment() throws AblyException {
49-
ClientOptions options = new ClientOptions();
42+
// Given
5043
options.environment = "myenv";
44+
45+
// When
5146
new Hosts("overridden.ably.io", Defaults.HOST_REALTIME, options);
5247
}
5348

@@ -56,11 +51,14 @@ public void hosts_host_and_environment() throws AblyException {
5651
*/
5752
@Test
5853
public void hosts_fallback_empty_array() throws AblyException {
59-
ClientOptions options = new ClientOptions();
60-
options.fallbackHosts = new String[]{};
54+
// Given
55+
options.fallbackHosts = new String[] {};
56+
57+
// When
6158
Hosts hosts = new Hosts(Defaults.HOST_REALTIME, Defaults.HOST_REALTIME, options);
62-
String host = hosts.getFallback(Defaults.HOST_REALTIME);
63-
assertThat(host, nullValue());
59+
60+
// Then
61+
assertThat(hosts.getFallback(Defaults.HOST_REALTIME), nullValue());
6462
}
6563

6664
/**
@@ -69,65 +67,52 @@ public void hosts_fallback_empty_array() throws AblyException {
6967
*/
7068
@Test
7169
public void hosts_fallback_custom_hosts() throws AblyException {
72-
ClientOptions options = new ClientOptions();
70+
// Given
7371
String[] customHosts = { "F.ably-realtime.com", "G.ably-realtime.com", "H.ably-realtime.com", "I.ably-realtime.com", "J.ably-realtime.com", "K.ably-realtime.com" };
7472
options.fallbackHosts = customHosts;
73+
74+
// When
7575
Hosts hosts = new Hosts(Defaults.HOST_REALTIME, Defaults.HOST_REALTIME, options);
76-
int idx;
77-
String host = Defaults.HOST_REALTIME;
78-
boolean shuffled = false;
79-
boolean allFound = true;
80-
for (idx = 0; ; ++idx) {
81-
host = hosts.getFallback(host);
82-
if (host == null)
83-
break;
84-
int found = Arrays.asList(customHosts).indexOf(host);
85-
if (found < 0)
86-
allFound = false;
87-
else if (found != idx)
88-
shuffled = true;
89-
}
90-
assertThat(idx, is(customHosts.length));
91-
assertTrue(allFound);
92-
assertTrue(shuffled);
76+
77+
// Then
78+
List<String> fallbackHosts = collectFallbackHosts(hosts);
79+
assertThat(hosts.getPrimaryHost(), is(Defaults.HOST_REALTIME));
80+
// the returned fallback hosts should have the same elements as custom host fallbacks
81+
assertThat(fallbackHosts, containsInAnyOrder(customHosts));
82+
// expect the fallback hosts to be shuffled
83+
assertThat(fallbackHosts, not(contains(customHosts)));
9384
}
9485

9586
/**
9687
* Expect that returned host is contained within default host list
9788
*/
9889
@Test
9990
public void hosts_fallback_no_custom_hosts() throws AblyException {
100-
ClientOptions options = new ClientOptions();
91+
// When
10192
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
102-
int idx;
103-
String host = Defaults.HOST_REALTIME;
104-
boolean shuffled = false;
105-
boolean allFound = true;
106-
for (idx = 0; ; ++idx) {
107-
host = hosts.getFallback(host);
108-
if (host == null)
109-
break;
110-
int found = Arrays.asList(Defaults.HOST_FALLBACKS).indexOf(host);
111-
if (found < 0)
112-
allFound = false;
113-
else if (found != idx)
114-
shuffled = true;
115-
}
116-
assertThat(idx, is(Defaults.HOST_FALLBACKS.length));
117-
assertTrue(allFound);
118-
assertTrue(shuffled);
93+
94+
// Then
95+
List<String> fallbackHosts = collectFallbackHosts(hosts);
96+
assertThat(hosts.getPrimaryHost(), is(Defaults.HOST_REALTIME));
97+
// the returned fallback hosts should have the same elements as default host fallbacks
98+
assertThat(fallbackHosts, containsInAnyOrder(Defaults.HOST_FALLBACKS));
99+
// expect the fallback hosts to be shuffled
100+
assertThat(fallbackHosts, not(contains(Defaults.HOST_FALLBACKS)));
119101
}
120102

121103
/**
122104
* Expect a null, when realtimeHost is non-default
123105
*/
124106
@Test
125107
public void hosts_fallback_overridden_host() throws AblyException {
126-
ClientOptions options = new ClientOptions();
108+
// Given
127109
String host = "overridden.ably.io";
110+
111+
// When
128112
Hosts hosts = new Hosts(host, Defaults.HOST_REALTIME, options);
129-
host = hosts.getFallback(host);
130-
assertThat(host, nullValue());
113+
114+
// Then
115+
assertThat(hosts.getFallback(host), nullValue());
131116
}
132117

133118
/**
@@ -136,26 +121,17 @@ public void hosts_fallback_overridden_host() throws AblyException {
136121
*/
137122
@Test
138123
public void hosts_fallback_use_default() throws AblyException {
139-
ClientOptions options = new ClientOptions();
124+
// Given
140125
options.fallbackHostsUseDefault = true;
141126
String host = "overridden.ably.io";
127+
128+
// When
142129
Hosts hosts = new Hosts(host, Defaults.HOST_REALTIME, options);
143-
int idx;
144-
boolean shuffled = false;
145-
boolean allFound = true;
146-
for (idx = 0; ; ++idx) {
147-
host = hosts.getFallback(host);
148-
if (host == null)
149-
break;
150-
int found = Arrays.asList(Defaults.HOST_FALLBACKS).indexOf(host);
151-
if (found < 0)
152-
allFound = false;
153-
else if (found != idx)
154-
shuffled = true;
155-
}
156-
assertThat(idx, is(Defaults.HOST_FALLBACKS.length));
157-
assertTrue(allFound);
158-
assertTrue(shuffled);
130+
131+
// Then
132+
assertThat(hosts.getPrimaryHost(), is(host));
133+
// the returned fallback hosts should have the same elements as default host fallbacks
134+
assertThat(collectFallbackHosts(hosts), containsInAnyOrder(Defaults.HOST_FALLBACKS));
159135
}
160136

161137
/**
@@ -164,10 +140,11 @@ else if (found != idx)
164140
*/
165141
@Test(expected = AblyException.class)
166142
public void hosts_fallback_use_default_and_set_fallback_hosts() throws AblyException {
167-
ClientOptions options = new ClientOptions();
143+
// Given
168144
options.fallbackHostsUseDefault = true;
169145
options.fallbackHosts = new String[] { "custom.ably-realtime.com" };
170146

147+
// When
171148
new Hosts(null, Defaults.HOST_REALTIME, options);
172149
}
173150

@@ -176,48 +153,47 @@ public void hosts_fallback_use_default_and_set_fallback_hosts() throws AblyExcep
176153
*/
177154
@Test
178155
public void hosts_fallback_use_environment() throws AblyException {
179-
ClientOptions options = new ClientOptions();
156+
// Given
180157
options.environment = "sandbox";
181158
String[] expectedEnvironmentFallbackHosts = Defaults.getEnvironmentFallbackHosts(options.environment);
182-
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
183159

184-
String host = "sandbox-" + Defaults.HOST_REALTIME;
185-
List<String> returnedEnvironmentFallbackHosts = new ArrayList<>();
186-
while ((host = hosts.getFallback(host)) != null){
187-
returnedEnvironmentFallbackHosts.add(host);
188-
}
160+
// When
161+
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
189162

190-
assertThat(returnedEnvironmentFallbackHosts, containsInAnyOrder(expectedEnvironmentFallbackHosts));
163+
// Then
164+
assertThat(hosts.getPrimaryHost(), is("sandbox-" + Defaults.HOST_REALTIME));
165+
assertThat(collectFallbackHosts(hosts), containsInAnyOrder(expectedEnvironmentFallbackHosts));
191166
}
192167

193168
/**
194169
* Expect that returned default fallback hosts without environment according to RSC15g4.
195170
*/
196171
@Test
197172
public void hosts_fallback_use_default_fallback_hosts_and_environment() throws AblyException {
198-
ClientOptions options = new ClientOptions();
173+
// Given
199174
options.fallbackHostsUseDefault = true;
200175
options.environment = "sandbox";
201-
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
202-
String host = "sandbox-" + Defaults.HOST_REALTIME;
203-
List<String> returnedEnvironmentFallbackHosts = new ArrayList<>();
204176

205-
while ((host = hosts.getFallback(host)) != null){
206-
returnedEnvironmentFallbackHosts.add(host);
207-
}
177+
// When
178+
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
208179

209-
assertThat(returnedEnvironmentFallbackHosts, containsInAnyOrder(Defaults.HOST_FALLBACKS));
180+
// Then
181+
assertThat(hosts.getPrimaryHost(), is("sandbox-" + Defaults.HOST_REALTIME));
182+
assertThat(collectFallbackHosts(hosts), containsInAnyOrder(Defaults.HOST_FALLBACKS));
210183
}
211184

212185
/**
213186
* Expect no fallback hosts if the custom port is specified.
214187
*/
215188
@Test
216189
public void hosts_no_fallback_when_port_is_defined() throws AblyException {
217-
ClientOptions options = new ClientOptions();
190+
// Given
218191
options.port = 8080;
192+
193+
// When
219194
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
220195

196+
// Then
221197
assertThat(hosts.getFallback(Defaults.HOST_REALTIME), nullValue());
222198
}
223199

@@ -226,10 +202,13 @@ public void hosts_no_fallback_when_port_is_defined() throws AblyException {
226202
*/
227203
@Test
228204
public void hosts_no_fallback_when_tlsport_is_defined() throws AblyException {
229-
ClientOptions options = new ClientOptions();
205+
// Given
230206
options.tlsPort = 8081;
207+
208+
// When
231209
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
232210

211+
// Then
233212
assertThat(hosts.getFallback(Defaults.HOST_REALTIME), nullValue());
234213
}
235214

@@ -238,11 +217,14 @@ public void hosts_no_fallback_when_tlsport_is_defined() throws AblyException {
238217
*/
239218
@Test
240219
public void hosts_fallback_when_fallback_hosts_and_port_are_defined() throws AblyException {
241-
ClientOptions options = new ClientOptions();
220+
// Given
242221
options.port = 8081;
243222
options.fallbackHosts = new String[] { "custom-fallback.ably.com" };
223+
224+
// When
244225
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
245226

227+
// Then
246228
assertThat(hosts.getFallback(Defaults.HOST_REALTIME), is("custom-fallback.ably.com"));
247229
}
248230

@@ -251,21 +233,33 @@ public void hosts_fallback_when_fallback_hosts_and_port_are_defined() throws Abl
251233
*/
252234
@Test
253235
public void hosts_fallback_when_host_and_fallback_hosts_and_port_are_defined() throws AblyException {
254-
ClientOptions options = new ClientOptions();
236+
// Given
255237
options.tlsPort = 8081;
256238
options.fallbackHosts = new String[] { "custom-fallback.ably.com" };
239+
240+
// When
257241
Hosts hosts = new Hosts("custom.ably.com", Defaults.HOST_REALTIME, options);
258242

243+
// Then
259244
assertThat(hosts.getFallback("custom.ably.com"), is("custom-fallback.ably.com"));
260245
}
261246

262247
@Test(expected = AblyException.class)
263248
public void hosts_use_default_fallback_hosts_and_tlsport_are_defined() throws AblyException {
264-
ClientOptions options = new ClientOptions();
249+
// Given
265250
options.tlsPort = 8081;
266251
options.fallbackHostsUseDefault = true;
267-
Hosts hosts = new Hosts(null, Defaults.HOST_REALTIME, options);
268252

269-
assertThat(hosts.getFallback(Defaults.HOST_REALTIME), nullValue());
253+
// When
254+
new Hosts(null, Defaults.HOST_REALTIME, options);
255+
}
256+
257+
private List<String> collectFallbackHosts(Hosts hosts) {
258+
List<String> fallbackHosts = new ArrayList<>();
259+
String host = hosts.getPrimaryHost();
260+
while ((host = hosts.getFallback(host)) != null){
261+
fallbackHosts.add(host);
262+
}
263+
return fallbackHosts;
270264
}
271265
}

0 commit comments

Comments
 (0)