Skip to content

Commit

Permalink
Issue #63: Removed useless SenderBase. Failing testProcessors().
Browse files Browse the repository at this point in the history
- Generic refactoring to clean up import/export formats.
  • Loading branch information
fgm committed Sep 6, 2018
1 parent e1528d8 commit 4968aee
Show file tree
Hide file tree
Showing 101 changed files with 1,055 additions and 958 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

### 0.1.10 / 0.1.9

* MongoDbSender can now take an existing collection instead of just a name
* MongodbSender can now take an existing collection instead of just a name
* Allow logging client request headers
* New TrivialStrategy for simple configurations
* New documentation site on [https://fgm.github.io/filog](https://fgm.github.io/filog)
Expand Down Expand Up @@ -97,7 +97,7 @@

* New Syslog sender using modern-syslog
* Fixed server-side double escaping in message strings
* Improved collection handling in MongoDbSender
* Improved collection handling in MongodbSender

### 0.1.1

Expand Down
75 changes: 37 additions & 38 deletions __tests__/unit/contextSourcingTest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
DETAILS_KEY,
HOST_KEY,
IContext,
KEY_DETAILS,
KEY_HOST,
IContext, IDetails,
ITimestamps,
ITimestampsHash,
SOURCE_KEY,
TS_KEY,
KEY_SOURCE,
KEY_TS,
} from "../../src/IContext";
import { ClientLogger } from "../../src/Loggers/ClientLogger";
import {ILogger} from "../../src/Loggers/ILogger";
Expand Down Expand Up @@ -35,35 +34,35 @@ function testContextSourcing(): void {
const side = "client";

// Const objects are not immutable: catch details being overwritten.
const details = { a: "A" };
const expectedDetails = { ...details };
const details: IDetails = { a: "A" };
const expectedDetails: IDetails = { ...details };

const t1: number = +new Date();
cl.log(level, message, details);
const t2: number = +new Date();

const expected = {
context: {
[DETAILS_KEY]: expectedDetails,
// HOST_KEY: not on client contexts.
[SOURCE_KEY]: side,
// TS_KEY: Cannot test content just with toMatchObject.
[KEY_DETAILS]: expectedDetails,
// KEY_HOST: not on client contexts.
[KEY_SOURCE]: side,
// KEY_TS: Cannot test content just with toMatchObject.
},
level,
message,
};
expect(t2).toBeGreaterThanOrEqual(t1);
const result = sender.result;
expect(typeof result).toBe("object");
expect(result).not.toHaveProperty(HOST_KEY);
expect(result).not.toHaveProperty(KEY_HOST);
expect(result).toMatchObject(expected);

const actualContext = result.context;
// No side properties without processors.
expect(actualContext).not.toHaveProperty(side);

expect(actualContext).toHaveProperty(TS_KEY);
const ts: ITimestampsHash = actualContext[TS_KEY];
expect(actualContext).toHaveProperty(KEY_TS);
const ts: ITimestamps = actualContext[KEY_TS];
expect(typeof ts).toBe("object");
expect(ts).toHaveProperty(side);
expect(ts[side]).toHaveProperty("log");
Expand All @@ -82,19 +81,19 @@ function testContextSourcing(): void {
const side = "server";

// Const objects are not immutable: catch details being overwritten.
const details = { a: "A" };
const expectedDetails = { ...details };
const details: IDetails = { a: "A" };
const expectedDetails: IDetails = { ...details };

const t1: number = +new Date();
sl.log(level, message, details);
const t2: number = +new Date();

const expected = {
context: {
[DETAILS_KEY]: expectedDetails,
[HOST_KEY]: host,
[SOURCE_KEY]: side,
// TS_KEY: Cannot test content just with toMatchObject.
[KEY_DETAILS]: expectedDetails,
[KEY_HOST]: host,
[KEY_SOURCE]: side,
// KEY_TS: Cannot test content just with toMatchObject.
},
level,
message,
Expand All @@ -105,8 +104,8 @@ function testContextSourcing(): void {
expect(result).toMatchObject(expected);

const actualContext = result.context;
expect(actualContext).toHaveProperty(TS_KEY);
const ts: ITimestampsHash = actualContext[TS_KEY];
expect(actualContext).toHaveProperty(KEY_TS);
const ts: ITimestamps = actualContext[KEY_TS];
expect(typeof ts).toBe("object");
expect(ts).toHaveProperty(side);
expect(ts[side]).toHaveProperty("log");
Expand All @@ -124,8 +123,8 @@ function testContextSourcing(): void {
const host = hostname();

// Const objects are not immutable: catch details being overwritten.
const details = { a: "A" };
const expectedDetails = { ...details };
const details: IDetails = { a: "A" };
const expectedDetails: IDetails = { ...details };

const clientContext: IContext = {
processorObject: { foo: "bar" },
Expand All @@ -139,16 +138,16 @@ function testContextSourcing(): void {
const t1: number = +new Date() - 3;

const initialContext = {
[DETAILS_KEY]: expectedDetails,
// HOST_KEY: not on client contexts.
[TS_KEY]: {
[KEY_DETAILS]: expectedDetails,
// KEY_HOST: not on client contexts.
[KEY_TS]: {
[side]: {
log: t1 + 1,
send: t1 + 2,
} as ITimestamps,
},
// No "server" yet.
} as ITimestampsHash,
[SOURCE_KEY]: side,
} as ITimestamps,
[KEY_SOURCE]: side,
[side]: clientContext,
};

Expand All @@ -157,10 +156,10 @@ function testContextSourcing(): void {

const expected = {
context: {
[DETAILS_KEY]: expectedDetails,
[HOST_KEY]: host,
[SOURCE_KEY]: side,
// TS_KEY: Cannot test content just with toMatchObject.
[KEY_DETAILS]: expectedDetails,
[KEY_HOST]: host,
[KEY_SOURCE]: side,
// KEY_TS: Cannot test content just with toMatchObject.
[side]: expectedClientContext,
// No "server" content without a server processor.
},
Expand All @@ -178,9 +177,9 @@ function testContextSourcing(): void {
// No side property without a processor.
expect(actualContext).not.toHaveProperty("server");

expect(actualContext).toHaveProperty(TS_KEY);
const ts: ITimestampsHash = actualContext[TS_KEY];
expect(typeof actualContext[TS_KEY]).toBe("object");
expect(actualContext).toHaveProperty(KEY_TS);
const ts: ITimestamps = actualContext[KEY_TS];
expect(typeof actualContext[KEY_TS]).toBe("object");
expect(typeof ts).toBe("object");
expect(ts).toHaveProperty(side);
expect(ts).toHaveProperty("server");
Expand Down
61 changes: 32 additions & 29 deletions __tests__/unit/logContextTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DETAILS_KEY, IContext, SOURCE_KEY, TS_KEY } from "../../src/IContext";
import {KEY_DETAILS, IContext, KEY_SOURCE, KEY_TS } from "../../src/IContext";
import {Logger} from "../../src/Loggers/Logger";
import {ServerLogger} from "../../src/Loggers/ServerLogger";
import * as LogLevel from "../../src/LogLevel";
Expand Down Expand Up @@ -35,12 +35,12 @@ function testMessageContext() {
*
* - log { message_details: { a: 1} }.
*/
test(`should add the message argument to ${DETAILS_KEY}`, () => {
test(`should add the message argument to ${KEY_DETAILS}`, () => {
const testSender: TestSender = new TestSender();
const logger = new Logger(newLogStrategy(testSender));
logger.log(LogLevel.DEBUG, "some message", referenceContext());

const actualDetails = testSender.result.context[DETAILS_KEY];
const actualDetails = testSender.result.context[KEY_DETAILS];
const expected = "A";
// Message details is set
expect(actualDetails).toHaveProperty("a", expected);
Expand Down Expand Up @@ -68,27 +68,27 @@ function testMessageContext() {
* - log { message_details: { a: "A", message_details: { foo: "bar" } } },
* unlike the message_details merging it did until 0.1.18 included.
*/
test(`should not merge contents of existing ${DETAILS_KEY} context key`, () => {
test(`should not merge contents of existing ${KEY_DETAILS} context key`, () => {
const sender = new TestSender();
const logger = new Logger(newLogStrategy(sender));
const originalContext = Object.assign({ [DETAILS_KEY]: { foo: "bar" } }, referenceContext());
const originalContext = { [KEY_DETAILS]: { foo: "bar" }, ...referenceContext() };
logger.log(LogLevel.DEBUG, "some message", originalContext);

const actual = sender.result.context;
expect(actual).not.toHaveProperty("a");
expect(actual).not.toHaveProperty("foo");
expect(actual).toHaveProperty(DETAILS_KEY);
expect(actual).toHaveProperty(KEY_DETAILS);

// Original top-level keys should still be in top [KEY_DETAILS].
const actualDetails = actual[DETAILS_KEY];
const actualDetails = actual[KEY_DETAILS];
expect(actualDetails).toHaveProperty("a", "A");
expect(actualDetails).toHaveProperty(DETAILS_KEY);
expect(actualDetails).toHaveProperty(KEY_DETAILS);
expect(actualDetails).not.toHaveProperty("foo");

// Key nested in original message_detail should remain in place.
const actualNested = actualDetails[DETAILS_KEY];
const actualNested = actualDetails[KEY_DETAILS];
expect(actualNested).not.toHaveProperty("a", "A");
expect(actualNested).not.toHaveProperty(DETAILS_KEY);
expect(actualNested).not.toHaveProperty(KEY_DETAILS);
expect(actualNested).toHaveProperty("foo", "bar");
});

Expand All @@ -98,28 +98,28 @@ function testMessageContext() {
* - log { message_details: { a: "A", message_details: { a: "A" } } },
* unlike the message_details merging it did until 0.1.18 included.
*/
test(`should not merge existing ${DETAILS_KEY} context key itself`, () => {
test(`should not merge existing ${KEY_DETAILS} context key itself`, () => {
const sender = new TestSender();
const logger = new Logger(newLogStrategy(sender));

const originalContext = Object.assign({ [DETAILS_KEY]: { a: "A" } }, referenceContext());
const originalContext = { [KEY_DETAILS]: { a: "A" }, ...referenceContext() };
logger.log(LogLevel.DEBUG, "some message", originalContext);

// Message_details should only contain a nested [DETAILS_KEY].
// Message_details should only contain a nested [KEY_DETAILS].
const actual = sender.result.context;
const keys = Object.keys(actual).sort();
expect(keys.length).toBe(3);
expect(keys).toEqual([DETAILS_KEY, SOURCE_KEY, TS_KEY]);
expect(actual).toHaveProperty(DETAILS_KEY);
expect(keys).toEqual([KEY_DETAILS, KEY_SOURCE, KEY_TS]);
expect(actual).toHaveProperty(KEY_DETAILS);

// Original top-level keys should still be in top [KEY_DETAILS].
const actualDetails = actual[DETAILS_KEY];
const actualDetails = actual[KEY_DETAILS];
expect(Object.keys(actualDetails).length).toBe(2);
expect(actualDetails).toHaveProperty("a", "A");
expect(actualDetails).toHaveProperty(DETAILS_KEY);
expect(actualDetails).toHaveProperty(KEY_DETAILS);

// Key nested in original message_detail should remain in place.
const actualNested = actualDetails[DETAILS_KEY];
const actualNested = actualDetails[KEY_DETAILS];
expect(Object.keys(actualNested).length).toBe(1);
expect(actualNested).toHaveProperty("a", "A");
});
Expand All @@ -129,15 +129,15 @@ function testMessageContext() {
*
* - log { message_details: { a: "A", message_details: { a: "B" } } }.
*/
test(`should not merge keys within ${DETAILS_KEY}`, () => {
test(`should not merge keys within ${KEY_DETAILS}`, () => {
const sender = new TestSender();
const logger = new Logger(newLogStrategy(sender));
const originalContext = Object.assign({ [DETAILS_KEY]: { a: "B" } }, referenceContext());
const originalContext = { [KEY_DETAILS]: { a: "B" }, ...referenceContext() };
logger.log(LogLevel.DEBUG, "some message", originalContext);

// [KEY_DETAILS] should contain the newly added value for key "a", not the
// one present in the initial [DETAILS_KEY].
const actualDetails: { a?: any } = sender.result.context[DETAILS_KEY];
// one present in the initial [KEY_DETAILS].
const actualDetails: { a?: any } = sender.result.context[KEY_DETAILS];
const expected = "A";
// Message details are set.
expect(actualDetails).toHaveProperty("a", expected);
Expand Down Expand Up @@ -256,7 +256,10 @@ function testObjectifyContext() {
let expected: string | Foo = "object";
expect(actual).toBe(expected);

actual = initial.constructor.name;
interface IConstructor extends Function {
name: string;
}
actual = (initial.constructor as IConstructor).name;
expected = "Foo";
expect(actual).toBe(expected);

Expand Down Expand Up @@ -310,7 +313,7 @@ function testProcessors() {

const Adder = class extends ProcessorBase implements IProcessor {
public process(context) {
return Object.assign({ added: "value" }, context);
return { added: "value", ...context };
}
};

Expand Down Expand Up @@ -342,7 +345,7 @@ function testProcessors() {
const TimeWarp = class extends ProcessorBase {
// Let's do the time warp again.
public process(context: IContext): IContext {
context[TS_KEY] = {
context[KEY_TS] = {
test: { log: +new Date("1978-11-19 05:00:00") },
};
context.hostname = "remote";
Expand Down Expand Up @@ -410,8 +413,8 @@ function testProcessors() {
expect(this.sender.logs.length).toBe(1);
const [, , context] = this.sender.logs.pop();
expect(context).toHaveProperty("hostname", "local");
expect(context).toHaveProperty(`${TS_KEY}.${this.logger.side}.log`);
const lag = ts - context[TS_KEY][this.logger.side].log;
expect(context).toHaveProperty(`${KEY_TS}.${this.logger.side}.log`);
const lag = ts - context[KEY_TS][this.logger.side].log;
expect(lag).toBeGreaterThanOrEqual(0);
// No sane machine should take more than 100 msec to return from log() with
// such a fast sending configuration.
Expand All @@ -426,8 +429,8 @@ function testProcessors() {
expect(this.sender.logs.length).toBe(1);
const [, , context] = this.sender.logs.pop();
expect(context).toHaveProperty("hostname", "remote");
expect(context).toHaveProperty(`${TS_KEY}.${this.logger.side}.log`);
const lag = ts - context[TS_KEY][this.logger.side].log;
expect(context).toHaveProperty(`${KEY_TS}.${this.logger.side}.log`);
const lag = ts - context[KEY_TS][this.logger.side].log;
expect(lag).toBeGreaterThanOrEqual(0);
// No sane machine should take more than 100 msec to return from log() with
// such a fast sending configuration. The TimeWarp processor attempts to
Expand Down
Loading

0 comments on commit 4968aee

Please sign in to comment.