Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves reliability of a few wiring/no_fixture tests #2406

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 61 additions & 77 deletions user/tests/wiring/no_fixture/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,137 +300,121 @@ test(SYSTEM_07_system_describe_is_not_overflowed_when_factory_module_present_but
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
}

namespace {
int sLastEvent = 0;
int sLastParam = 0;
bool checkLastParamCloudDisconnected() {
return sLastParam == cloud_status_disconnected;
}
bool checkLastParamCloudConnected() {
return sLastParam == cloud_status_connected;
}
} // anonymous

test(SYSTEM_08_system_event_subscription) {
SCOPE_GUARD({
System.off(all_events);
});
int lastEvent = 0;
auto subscription = System.on(cloud_status, [&lastEvent](system_event_t ev) {
lastEvent = ev;
// Disconnect to reset test
auto subscription = System.on(cloud_status, [&sLastEvent, &sLastParam](system_event_t ev, int data) {
sLastEvent = ev;
sLastParam = data;
});
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(lastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

System.off(subscription);
assertTrue(Particle.disconnected);
lastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(lastEvent, 0);
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);

subscription = System.on(cloud_status, [&lastEvent](system_event_t ev) {
lastEvent = ev;
// Disconnect to reset test
subscription = System.on(cloud_status, [&sLastEvent, &sLastParam](system_event_t ev, int data) {
sLastEvent = ev;
sLastParam = data;
});
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(lastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

System.off(cloud_status);
assertTrue(Particle.disconnected);
lastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(lastEvent, 0);
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);
}

namespace {
int sLastEvent = 0;
} // anonymous

test(SYSTEM_09_system_event_subscription_funcptr_or_non_capturing_lambda) {
SCOPE_GUARD({
System.off(all_events);
});
auto handler = [](system_event_t ev, int data, void*) {
sLastEvent = ev;
sLastParam = data;
};
// Disconnect to reset test
assertTrue((bool)System.on(cloud_status, handler));
assertTrue(Particle.connected());
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

// System.off(event)
System.off(cloud_status);
assertTrue(Particle.disconnected);
sLastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, 0);
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);

// Disconnect to reset test
assertTrue((bool)System.on(cloud_status, handler));
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

// System.off(event, handler)
System.off(cloud_status, handler);
assertTrue(Particle.disconnected);
sLastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, 0);
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);

// Disconnect to reset test
assertTrue((bool)System.on(cloud_status, handler));
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

// System.off(handler)
System.off(handler);
assertTrue(Particle.disconnected);
sLastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, 0);
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);

// Disconnect to reset test
assertTrue((bool)System.on(cloud_status, handler));
sLastParam = cloud_status_disconnecting;
Particle.disconnect();
assertTrue(waitFor(Particle.disconnected, 5000));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, (int)cloud_status);
assertTrue(waitFor(checkLastParamCloudDisconnected, 5000));
assertTrue(Particle.disconnected);

// System.off(event)
System.off(cloud_status);
assertTrue(Particle.disconnected);
sLastEvent = 0;
assertEqual(sLastParam, (int)cloud_status_disconnected);
Particle.connect();
assertTrue(waitFor(Particle.connected, HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME));
// Events are delivered on application thread when threading is enabled, make sure to process queue
delay(100);
Particle.process();
assertEqual(sLastEvent, 0);
}
assertFalse(waitFor(checkLastParamCloudConnected, 1000)); // should not fire cloud_status_connected
assertEqual(sLastParam, (int)cloud_status_disconnected);
}
25 changes: 19 additions & 6 deletions user/tests/wiring/no_fixture/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ test(TIME_19_LocalTimeIsCloseToNtpTime) {

assertTrue((bool)client);

Particle.connect();
waitFor(Particle.connected, 60000);
assertTrue(Particle.connected());
Particle.syncTime();
waitFor(Particle.syncTimeDone, 60000);
Expand All @@ -363,18 +365,29 @@ test(TIME_19_LocalTimeIsCloseToNtpTime) {
if (!r) {
break;
}
delay(i * 1000);
delay(i * 3000);
}
// assertEqual(SYSTEM_ERROR_NONE, r);
// Assertion failed: (0=0) == (r=-160), file tests/integration/wiring/no_fixture/time.cpp, line 368.
// (TIMEOUT, "Timeout error", -160)
// [sc-97562]: this test doesn't fail that often when run in a loop by itself, but when it does fail
// as the only test that failed for 4 hours of testing, it's quite annoying. So let's only
// assert a failure if there is a valid response from the server, and it's not within
// 20 seconds. This was increased from 10s due to several failures that were 15-17s.
if (r != SYSTEM_ERROR_NONE) {
out->printlnf("NTP server was unresponsive: %d, bailing!", r);
return;
}
assertEqual(0, r);

struct timeval tv = {};
assertEqual(0, hal_rtc_get_time(&tv, nullptr));
uint64_t now = tv.tv_sec * 1000000ULL + tv.tv_usec;

out->printlnf("Local time: %u", (unsigned)now);
out->printlnf("NTP time: %u", (unsigned)ntpTime);
out->printlnf("Local time: %u: %s", (unsigned)(now/1000000ULL), Time.format((unsigned)tv.tv_sec + (tv.tv_usec/1000000ULL), TIME_FORMAT_DEFAULT).c_str());
out->printlnf(" NTP time: %u: %s", (unsigned)(ntpTime/1000000ULL), Time.format((unsigned)(ntpTime / 1000000ULL), TIME_FORMAT_DEFAULT).c_str());

// Within 10 seconds
const int64_t diff = std::chrono::microseconds(10s).count();
// Within 20 seconds
const int64_t diff = std::chrono::microseconds(20s).count();
out->printlnf("Within %0.2f seconds of each other.", (float)(std::abs((int64_t)now - (int64_t)ntpTime))/1000000.0f);
assertLessOrEqual(std::abs((int64_t)now - (int64_t)ntpTime), diff);
}