Skip to content

Commit

Permalink
Merge pull request #1151 from sorattorafa/fix-flaky-tests-serializers
Browse files Browse the repository at this point in the history
fix (test): remove flaky tests
  • Loading branch information
icebob authored Nov 9, 2022
2 parents 6a854d8 + 768377b commit 1dc3661
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<a name="Unreleased"></a>
# [Unreleased](https://github.com/moleculerjs/moleculer/compare/v0.14.25...master)

## Changes
- fix serializers datetime flaky test

--------------------------------------------------
<a name="0.14.25"></a>
# [0.14.25](https://github.com/moleculerjs/moleculer/compare/v0.14.24...v0.14.25) (2022-10-29)
Expand Down
129 changes: 127 additions & 2 deletions test/unit/serializers/cbor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,108 @@ describe("Test CborSerializer", () => {
let serializer = new CborSerializer();
serializer.init();

it("should serialize the event packet", () => {
const now = new Date();
it("should serialize the event packet (with UTC)", () => {
const now = new Date().toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(171);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 000Z with UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(171);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 001Z with UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(171);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 000Z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z");
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(146);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 001Z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z");
const obj = {
ver: "4",
sender: "node-100",
Expand All @@ -40,6 +140,31 @@ describe("Test CborSerializer", () => {
expect(res).toEqual(obj);
});

it("should serialize the event packet with flaky date time string", () => {
const now = new Date("2022-11-06T22:59:47.000Z");
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(146);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should deserialize maps to objects by default", () => {
const input = new Map([
["foo", "bar"],
Expand Down
107 changes: 105 additions & 2 deletions test/unit/serializers/msgpack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe("Test MsgPackSerializer", () => {
let serializer = new MsgPackSerializer();
serializer.init();

it("should serialize the event packet", () => {
const now = new Date();
it("should serialize the event packet (with UTC String)", () => {
const now = new Date().toUTCString();
const obj = {
ver: "4",
sender: "node-100",
Expand All @@ -32,6 +32,109 @@ describe("Test MsgPackSerializer", () => {
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});
it("should serialize the event packet (date time ends 000Z using UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};

const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 001Z and using UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};

const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 000Z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z");
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};

const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(140);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date time ends with 001Z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z");
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};

const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(144);

Expand Down
104 changes: 102 additions & 2 deletions test/unit/serializers/notepack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,108 @@ describe("Test NotePackSerializer", () => {
let serializer = new NotePackSerializer();
serializer.init();

it("should serialize the event packet", () => {
const now = new Date();
it("should serialize the event packet with UTC date string", () => {
const now = new Date().toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date ends with 000z using UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date ends with 001z using UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z").toUTCString();
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(164);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date ends with 000z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.000Z");
const obj = {
ver: "4",
sender: "node-100",
id: "8b3c7371-7f0a-4aa2-b734-70ede29e1bbb",
event: "user.created",
data: {
a: 5,
b: "Test",
c: now
},
broadcast: true,
meta: {},
level: 1,
needAck: false
};
const s = serializer.serialize(cloneDeep(obj), P.PACKET_EVENT);
expect(s.length).toBe(140);

const res = serializer.deserialize(s, P.PACKET_EVENT);
expect(res).not.toBe(obj);
expect(res).toEqual(obj);
});

it("should serialize the event packet (date ends with 001z without UTC)", () => {
const now = new Date("2022-11-06T22:59:47.001Z");
const obj = {
ver: "4",
sender: "node-100",
Expand Down

0 comments on commit 1dc3661

Please sign in to comment.