diff --git a/dev-utils/docker-compose.yml b/dev-utils/docker-compose.yml index 9fb6401548..0dae84aac3 100644 --- a/dev-utils/docker-compose.yml +++ b/dev-utils/docker-compose.yml @@ -1,7 +1,7 @@ version: "3.7" services: nodejs-agent: - image: node:12 + image: node:20 entrypoint: /bin/bash volumes: - ..:/agent diff --git a/test/apm-client/http-apm-client/config.test.js b/test/apm-client/http-apm-client/config.test.js index ee45a35b4e..b5d8b43b17 100644 --- a/test/apm-client/http-apm-client/config.test.js +++ b/test/apm-client/http-apm-client/config.test.js @@ -532,23 +532,24 @@ 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) { @@ -556,20 +557,30 @@ test('payloadLogFile', function (t) { 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_;