Skip to content

Commit

Permalink
test: add a delay in payloadLogFile test to avoid flaky failures (#3646)
Browse files Browse the repository at this point in the history
This test has been flaky in CI with node v20 and v21. I don't know
why the node version could matter here.

Refs: #3313 (comment)
  • Loading branch information
trentm authored Sep 27, 2023
1 parent e5e014f commit 10934ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dev-utils/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.7"
services:
nodejs-agent:
image: node:12
image: node:20
entrypoint: /bin/bash
volumes:
- ..:/agent
47 changes: 29 additions & 18 deletions test/apm-client/http-apm-client/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,44 +532,55 @@ test('agentName', function (t) {
});

test('payloadLogFile', function (t) {
t.plan(6);

const receivedObjects = [];
const filename = path.join(os.tmpdir(), Date.now() + '.ndjson');
const filename = path.join(
os.tmpdir(),
`test-payloadLogFile-${Date.now()}.ndjson`,
);
let requests = 0;

let client;
const server = APMServer(function (req, res) {
const request = ++requests;

req = processIntakeReq(req);
const pipe = processIntakeReq(req);

req.on('data', function (obj) {
pipe.on('data', function (obj) {
receivedObjects.push(obj);
});

req.on('end', function () {
pipe.on('end', function () {
res.end();

if (request === 2) {
client.destroy();
server.close();
t.equal(receivedObjects.length, 5, 'should have received 5 objects');

const file = fs.createReadStream(filename).pipe(ndjson.parse());

file.on('data', function (obj) {
const expected = receivedObjects.shift();
const n = 5 - receivedObjects.length;
t.deepEqual(
obj,
expected,
`expected line ${n} in the log file to match item no ${n} received by the server`,
);
});
// With the current `payloadLogFile` implementation, there is no
// guarantee that the file writes are complete and sync'd to disk
// by the time the client intake request has completed, or even the
// server has received that request end. Therefore, to avoid flaky
// test failures, we lamely wait a bit to give that time to complete.
setTimeout(() => {
const file = fs.createReadStream(filename).pipe(ndjson.parse());
file.on('data', function (obj) {
const expected = receivedObjects.shift();
const n = 5 - receivedObjects.length;
t.deepEqual(
obj,
expected,
`expected line ${n} in the log file to match item no ${n} received by the server`,
);
});
file.on('end', function () {
t.end();
});
}, 300);
}
});
}).client(
});
server.client(
{ payloadLogFile: filename, apmServerVersion: '8.0.0' },
function (client_) {
client = client_;
Expand Down

0 comments on commit 10934ff

Please sign in to comment.