Skip to content

Commit

Permalink
fix naive implementation of fake expect function
Browse files Browse the repository at this point in the history
not sure what it did before
  • Loading branch information
paed01 committed Nov 16, 2023
1 parent 13352c0 commit dfcb64c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0dep/pino-applicationinsights",
"version": "0.1.1",
"version": "0.1.2",
"description": "Pino applicationinsights transport",
"type": "module",
"module": "./src/index.js",
Expand Down
11 changes: 9 additions & 2 deletions src/fake-applicationinsights.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ export class FakeApplicationInsights {

this._scope
.post(this._endpointPathname, (body) => {
const match = collected.length <= count;
if (!match) return false;

deflated = this.deflateSync(body);
collected = collected.concat(this.parseLines(deflated));
return collected.length <= count;

return true;
})
.times(count)
.reply(function reply(uri) {
resolve(collected.map((l) => new CollectData(this.req.method, uri, this.req.headers, l)));
if (collected.length >= count) {
resolve(collected.map((l) => new CollectData(this.req.method, uri, this.req.headers, l)));
}
return [ 200 ];
});
});
Expand Down
184 changes: 148 additions & 36 deletions test/src/fake-applicationinsights-test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,182 @@
import { randomUUID } from 'node:crypto';
import { pino } from 'pino';
import nock from 'nock';

import compose from '../../src/index.js';
import { FakeApplicationInsights } from '../../src/fake-applicationinsights.js';

describe('fake applicationinsights', () => {
const connectionString = `InstrumentationKey=${randomUUID()};IngestionEndpoint=https://ingestion.local;LiveEndpoint=https://livemonitor.local/`;

/** @type {FakeApplicationInsights} */
let fakeAI;
before(() => {
fakeAI = new FakeApplicationInsights(connectionString);
});
after(() => {
afterEach(() => {
fakeAI.reset();
});

it('log with track event catches event record', async () => {
const transport = compose({
track(chunk) {
const { time, properties } = chunk;
this.trackEvent({ name: 'my event', time, properties, measurements: { logins: 1 } });
},
connectionString,
config: { maxBatchSize: 1, disableStatsbeat: true },
describe('catches tracking', () => {
it('expect exception and traces', async () => {
const transport = compose({
connectionString,
config: { maxBatchSize: 1, disableStatsbeat: true },
});
const logger = pino(transport);

const expectException = fakeAI.expectExceptionData();
const expectMessage1 = fakeAI.expectMessageData();
const expectMessage2 = fakeAI.expectMessageData();

logger.error(new Error('bar'), 'foo');
logger.info('baz');

const msgs = await Promise.all([ expectMessage1, expectException, expectMessage2 ]);

expect(msgs[0].body.data.baseType, '# 0').to.equal('MessageData');
expect(msgs[0].body.data.baseData.message, '# 1').to.equal('foo');
expect(msgs[1].body.data.baseType, '# 1').to.equal('ExceptionData');
expect(msgs[2].body.data.baseType, '# 2').to.equal('MessageData');
expect(msgs[2].body.data.baseData.message, '# 1').to.equal('baz');
});
const logger = pino(transport);
});

describe('expectEventData', () => {
it('log with track event catches event record', async () => {
const transport = compose({
track(chunk) {
const { time, properties } = chunk;
this.trackEvent({ name: 'my event', time, properties, measurements: { logins: 1 } });
},
connectionString,
config: { maxBatchSize: 1, disableStatsbeat: true },
});
const logger = pino(transport);

const expectMessage = fakeAI.expectEventData();
const expectMessage = fakeAI.expectEventData();

logger.info({ bar: 'baz' }, 'foo');
logger.info({ bar: 'baz' }, 'foo');

const msg = await expectMessage;
const msg = await expectMessage;

expect(msg.body.data.baseData).to.deep.include({
properties: { bar: 'baz' },
measurements: { logins: 1 },
name: 'my event',
expect(msg.body.data.baseData).to.deep.include({
properties: { bar: 'baz' },
measurements: { logins: 1 },
name: 'my event',
});

transport.destroy();
});

transport.destroy();
it('log with track event catches first event record', async () => {
const transport = compose({
track(chunk) {
const { time, properties } = chunk;
this.trackEvent({ name: 'my event', time, properties, measurements: { logins: 1 } });
},
connectionString,
config: { maxBatchSize: 2, disableStatsbeat: true },
});
const logger = pino(transport);

const expectMessage = fakeAI.expectEventData();

logger.info({ bar: 'baz' }, 'foo');
logger.warn({ bar: 'baz' }, 'warning');

const msg = await expectMessage;

expect(msg.body.data.baseData).to.deep.include({
properties: { bar: 'baz' },
measurements: { logins: 1 },
name: 'my event',
});

transport.destroy();
});
});

it('log with track event catches first event record', async () => {
const transport = compose({
track(chunk) {
const { time, properties } = chunk;
this.trackEvent({ name: 'my event', time, properties, measurements: { logins: 1 } });
},
connectionString,
config: { maxBatchSize: 2, disableStatsbeat: true },
describe('expect(count)', () => {
it('defaults to 1 and is resolved when one call has completed', async () => {
const transport = compose({
connectionString,
config: { maxBatchSize: 1, disableStatsbeat: true },
});

const logger = pino(transport);

const tracked = fakeAI.expect();

logger.info('foo 0');

const msgs = await tracked;

expect(msgs).to.have.length(1);
});
const logger = pino(transport);

const expectMessage = fakeAI.expectEventData();
it('maxBatchSize=3 resolves when count is passed', async () => {
const transport = compose({
connectionString,
config: { maxBatchSize: 3, disableStatsbeat: true },
});

const logger = pino(transport);

logger.info({ bar: 'baz' }, 'foo');
logger.warn({ bar: 'baz' }, 'warning');
const tracked = fakeAI.expect(8);

const msg = await expectMessage;
const ingestion = new Promise((resolve) => {
let ingestCount = 1;
nock('https://ingestion.local')
.post('/v2.1/track')
.times(ingestCount)
.reply(() => {
if (!--ingestCount) resolve();
return [ 200 ];
});
});

expect(msg.body.data.baseData).to.deep.include({
properties: { bar: 'baz' },
measurements: { logins: 1 },
name: 'my event',
for (let i = 0; i < 12; i++) {
logger.info(`foo ${i}`);
}

const msgs = await tracked;

expect(msgs).to.have.length(9);

await ingestion;
});

transport.destroy();
it('maxBatchSize=1 resolves when exact count is reached', async () => {
const transport = compose({
connectionString,
config: { maxBatchSize: 1, disableStatsbeat: true },
});

const logger = pino(transport);

const tracked = fakeAI.expect(8);

const ingested = new Promise((resolve) => {
let ingestCount = 4;
nock('https://ingestion.local')
.post('/v2.1/track')
.times(ingestCount)
.reply(() => {
if (!--ingestCount) resolve();
return [ 200 ];
});
});

for (let i = 0; i < 12; i++) {
logger.info(`foo ${i}`);
}

const msgs = await tracked;

expect(msgs).to.have.length(8);

await ingested;
});
});
});
4 changes: 2 additions & 2 deletions test/typescript/log-transport-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('log transport', () => {
before(() => {
fakeAI = new FakeApplicationInsights(connectionString);
});
after(() => {
afterEach(() => {
fakeAI.reset();
});

Expand Down Expand Up @@ -113,7 +113,7 @@ describe('log transport', () => {
expect(client.getStatsbeat().isEnabled()).to.be.false;
});

it('fake application insights expect telemetry', async () => {
it('fake application insights expect telemetry type', async () => {
const expectTelemetry = fakeAI.expectTelemetryType('MetricData');

const transport = compose({
Expand Down

0 comments on commit dfcb64c

Please sign in to comment.