Skip to content

Commit aa93505

Browse files
committed
fix failed tests
1 parent fb0cb42 commit aa93505

2 files changed

Lines changed: 86 additions & 87 deletions

File tree

lib/AutoClusterFailover.cc

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterF
7474
} else {
7575
thread_.join();
7676
}
77-
thread_.join();
7877
}
7978

8079
auto primary() const noexcept { return config_.primary; }
@@ -275,6 +274,8 @@ class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterF
275274
return;
276275
}
277276

277+
LOG_DEBUG("Detected secondary " << self->config_.secondary[index].serviceUrl()
278+
<< " availability: " << available);
278279
if (available) {
279280
self->switchTo(&self->config_.secondary[index]);
280281
done();
@@ -293,6 +294,8 @@ class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterF
293294
return;
294295
}
295296

297+
LOG_DEBUG("Detected primary " << self->current().serviceUrl()
298+
<< " availability: " << primaryAvailable);
296299
if (primaryAvailable) {
297300
self->consecutiveFailureCount_ = 0;
298301
done();
@@ -344,6 +347,8 @@ class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterF
344347
return;
345348
}
346349

350+
LOG_DEBUG("Detected secondary " << self->current().serviceUrl()
351+
<< " availability: " << secondaryAvailable);
347352
if (secondaryAvailable) {
348353
self->consecutiveFailureCount_ = 0;
349354
self->checkSwitchBackToPrimaryAsync(std::move(done), std::nullopt);
@@ -355,21 +360,23 @@ class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterF
355360
return;
356361
}
357362

358-
self->probeAvailableAsync(self->config_.primary,
359-
[weakSelf, done = std::move(done)](bool primaryAvailable) mutable {
360-
auto self = weakSelf.lock();
361-
if (!self) {
362-
return;
363-
}
364-
365-
if (primaryAvailable) {
366-
self->switchTo(&self->config_.primary);
367-
done();
368-
return;
369-
}
370-
371-
self->checkSwitchBackToPrimaryAsync(std::move(done), false);
372-
});
363+
self->probeAvailableAsync(
364+
self->config_.primary, [weakSelf, done = std::move(done)](bool primaryAvailable) mutable {
365+
auto self = weakSelf.lock();
366+
if (!self) {
367+
return;
368+
}
369+
370+
LOG_DEBUG("Detected primary after secondary is available "
371+
<< self->config_.primary.serviceUrl() << " availability: " << primaryAvailable);
372+
if (primaryAvailable) {
373+
self->switchTo(&self->config_.primary);
374+
done();
375+
return;
376+
}
377+
378+
self->checkSwitchBackToPrimaryAsync(std::move(done), false);
379+
});
373380
});
374381
}
375382
};

tests/ServiceInfoProviderTest.cc

Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -205,80 +205,72 @@ class TestServiceInfoProvider : public ServiceInfoProvider {
205205
};
206206

207207
TEST(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

248243
TEST(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

284276
TEST(ServiceInfoProviderTest, testSwitchCluster) {

0 commit comments

Comments
 (0)