@@ -205,80 +205,72 @@ class TestServiceInfoProvider : public ServiceInfoProvider {
205205};
206206
207207TEST (AutoClusterFailoverTest, testFailoverToFirstAvailableSecondaryAfterDelay) {
208- try {
209- ProbeTcpServer availableSecondary;
210- ProbeTcpServer unavailableSecondary;
211- const auto primaryUrl = unavailableSecondary.getServiceUrl ();
212- unavailableSecondary.stop ();
213-
214- ProbeTcpServer skippedSecondary;
215- const auto skippedSecondaryUrl = skippedSecondary.getServiceUrl ();
216- skippedSecondary.stop ();
217-
218- const auto availableSecondaryUrl = availableSecondary.getServiceUrl ();
219- ServiceUrlObserver observer;
220- AutoClusterFailover provider =
221- AutoClusterFailover::Builder (ServiceInfo (primaryUrl), {ServiceInfo (skippedSecondaryUrl),
222- ServiceInfo (availableSecondaryUrl)})
223- .withCheckInterval (20ms)
224- .withFailoverThreshold (6 )
225- .withSwitchBackThreshold (6 )
226- .build ();
227-
228- ASSERT_EQ (provider.initialServiceInfo ().serviceUrl (), primaryUrl);
229-
230- provider.initialize ([&observer](const ServiceInfo &serviceInfo) { observer.onUpdate (serviceInfo); });
231-
232- ASSERT_TRUE (waitUntil (1s, [&observer] { return observer.size () >= 1 ; }));
233- ASSERT_EQ (observer.last (), primaryUrl);
234- ASSERT_FALSE (waitUntil (
235- 80ms, [&observer, &availableSecondaryUrl] { return observer.last () == availableSecondaryUrl; }));
236- ASSERT_TRUE (waitUntil (
237- 2s, [&observer, &availableSecondaryUrl] { return observer.last () == availableSecondaryUrl; }));
238-
239- const auto updates = observer.snapshot ();
240- ASSERT_EQ (updates.size (), 2u );
241- ASSERT_EQ (updates[0 ], primaryUrl);
242- ASSERT_EQ (updates[1 ], availableSecondaryUrl);
243- } catch (const ASIO_SYSTEM_ERROR &e) {
244- GTEST_SKIP () << " Cannot bind local probe server in this environment: " << e.what ();
245- }
208+ ProbeTcpServer availableSecondary;
209+ ProbeTcpServer unavailableSecondary;
210+ const auto primaryUrl = unavailableSecondary.getServiceUrl ();
211+ unavailableSecondary.stop ();
212+
213+ ProbeTcpServer skippedSecondary;
214+ const auto skippedSecondaryUrl = skippedSecondary.getServiceUrl ();
215+ skippedSecondary.stop ();
216+
217+ const auto availableSecondaryUrl = availableSecondary.getServiceUrl ();
218+ ServiceUrlObserver observer;
219+ AutoClusterFailover provider =
220+ AutoClusterFailover::Builder (ServiceInfo (primaryUrl),
221+ {ServiceInfo (skippedSecondaryUrl), ServiceInfo (availableSecondaryUrl)})
222+ .withCheckInterval (20ms)
223+ .withFailoverThreshold (6 )
224+ .withSwitchBackThreshold (6 )
225+ .build ();
226+
227+ ASSERT_EQ (provider.initialServiceInfo ().serviceUrl (), primaryUrl);
228+
229+ observer.onUpdate (provider.initialServiceInfo ());
230+ provider.initialize ([&observer](const ServiceInfo &serviceInfo) { observer.onUpdate (serviceInfo); });
231+
232+ ASSERT_FALSE (waitUntil (
233+ 80ms, [&observer, &availableSecondaryUrl] { return observer.last () == availableSecondaryUrl; }));
234+ ASSERT_TRUE (waitUntil (
235+ 2s, [&observer, &availableSecondaryUrl] { return observer.last () == availableSecondaryUrl; }));
236+
237+ const auto updates = observer.snapshot ();
238+ ASSERT_EQ (updates.size (), 2u );
239+ ASSERT_EQ (updates[0 ], primaryUrl);
240+ ASSERT_EQ (updates[1 ], availableSecondaryUrl);
246241}
247242
248243TEST (AutoClusterFailoverTest, testSwitchBackToPrimaryAfterRecoveryDelay) {
249- try {
250- ProbeTcpServer primary;
251- const auto primaryUrl = primary.getServiceUrl ();
252- primary.stop ();
253-
254- ProbeTcpServer secondary;
255- const auto secondaryUrl = secondary.getServiceUrl ();
256-
257- ServiceUrlObserver observer;
258- AutoClusterFailover provider =
259- AutoClusterFailover::Builder (ServiceInfo (primaryUrl), {ServiceInfo (secondaryUrl)})
260- .withCheckInterval (20ms)
261- .withFailoverThreshold (4 )
262- .withSwitchBackThreshold (6 )
263- .build ();
264-
265- provider.initialize ([&observer](const ServiceInfo &serviceInfo) { observer.onUpdate (serviceInfo); });
266-
267- ASSERT_TRUE (waitUntil (2s, [&observer, &secondaryUrl] { return observer.last () == secondaryUrl; }));
268-
269- primary.start ();
270-
271- ASSERT_FALSE (waitUntil (80ms, [&observer, &primaryUrl] { return observer.last () == primaryUrl; }));
272- ASSERT_TRUE (waitUntil (2s, [&observer, &primaryUrl] { return observer.last () == primaryUrl; }));
273-
274- const auto updates = observer.snapshot ();
275- ASSERT_EQ (updates.size (), 3u );
276- ASSERT_EQ (updates[0 ], primaryUrl);
277- ASSERT_EQ (updates[1 ], secondaryUrl);
278- ASSERT_EQ (updates[2 ], primaryUrl);
279- } catch (const ASIO_SYSTEM_ERROR &e) {
280- GTEST_SKIP () << " Cannot bind local probe server in this environment: " << e.what ();
281- }
244+ ProbeTcpServer primary;
245+ const auto primaryUrl = primary.getServiceUrl ();
246+ primary.stop ();
247+
248+ ProbeTcpServer secondary;
249+ const auto secondaryUrl = secondary.getServiceUrl ();
250+
251+ ServiceUrlObserver observer;
252+ AutoClusterFailover provider =
253+ AutoClusterFailover::Builder (ServiceInfo (primaryUrl), {ServiceInfo (secondaryUrl)})
254+ .withCheckInterval (20ms)
255+ .withFailoverThreshold (4 )
256+ .withSwitchBackThreshold (6 )
257+ .build ();
258+
259+ observer.onUpdate (provider.initialServiceInfo ());
260+ provider.initialize ([&observer](const ServiceInfo &serviceInfo) { observer.onUpdate (serviceInfo); });
261+
262+ ASSERT_TRUE (waitUntil (2s, [&observer, &secondaryUrl] { return observer.last () == secondaryUrl; }));
263+
264+ primary.start ();
265+
266+ ASSERT_FALSE (waitUntil (80ms, [&observer, &primaryUrl] { return observer.last () == primaryUrl; }));
267+ ASSERT_TRUE (waitUntil (2s, [&observer, &primaryUrl] { return observer.last () == primaryUrl; }));
268+
269+ const auto updates = observer.snapshot ();
270+ ASSERT_EQ (updates.size (), 3u );
271+ ASSERT_EQ (updates[0 ], primaryUrl);
272+ ASSERT_EQ (updates[1 ], secondaryUrl);
273+ ASSERT_EQ (updates[2 ], primaryUrl);
282274}
283275
284276TEST (ServiceInfoProviderTest, testSwitchCluster) {
0 commit comments