Skip to content

Commit

Permalink
tests: improve reliability of events verification
Browse files Browse the repository at this point in the history
Add a fsync operation inside the fake device gateway used for testing,
and add a delay before reading the generated events file, and also
before resetting the events list. The delay is required because the
sending of events inside aklite is not done synchronously. A ReportQueue
flush is forced on LiteClient destruction, but we keep an instance
active during the test procedure.

Signed-off-by: Andre Detsch <[email protected]>
  • Loading branch information
detsch committed Jul 17, 2024
1 parent da88112 commit bed41ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/device-gateway_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def _dump_event(self):
cur_events.append(e)
event_ids.add(e["id"])
json.dump(cur_events, f)
os.fsync(f)

def _reset_events(self):
if os.path.exists(self.server.events_file):
Expand Down
9 changes: 8 additions & 1 deletion tests/fixtures/liteclient/devicegatewaymock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ class DeviceGatewayMock {
std::string getTlsUri() const { return url_; }
const std::string& getPort() const { return port_; }
Json::Value getReqHeaders() const { return Utils::parseJSONFile(req_headers_file_); }
Json::Value getEvents() const { return Utils::parseJSONFile(events_file_); }
Json::Value getEvents() const {
// Allow some time for the ReportQueue to be flushed and the events to arrive at the Device Gateway mock.
// The flush operation is forced in the LiteClient destructor, but we inspect the sent events while keeping an
// LiteClient instance active.
std::this_thread::sleep_for(std::chrono::milliseconds(500));
return Utils::parseJSONFile(events_file_);
}
bool resetEvents(std::shared_ptr<HttpClient> http_client) const {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
const HttpResponse r = http_client->post(url_ + "/events/reset", Json::nullValue);
return r.isOk();
}
Expand Down

0 comments on commit bed41ae

Please sign in to comment.