Skip to content

Commit

Permalink
create new Context in request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Apr 11, 2017
1 parent be2ccbc commit 2b3ee83
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
5 changes: 3 additions & 2 deletions examples/post.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ module.exports = function() {
},

count(ctx) {
throw new CustomError("Hibás adatok!", 410, ctx.params);
//throw new CustomError("Hibás adatok!", 410, ctx.params);
let count = posts.filter(post => post.author == ctx.params.id).length;
return Promise.delay(1500).then(() => count);
//return Promise.delay(1500).then(() => count);
return count;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/sandbox/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Promise.resolve()
.then(delay(1000))

.then(() => {
b1.call("posts.get", { id: 1 }).then(res => {
b1.call("posts.get", { id: 1 }, { meta: { user: "John" }}).then(res => {
console.log("[server-1] Success!", res);
}).catch(err => {
console.error("[server-1] Error!", err);
Expand Down
14 changes: 12 additions & 2 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"use strict";

const Promise = require("bluebird");
const { RequestTimeoutError } = require("./errors");
const Context = require("./context");
const P = require("./packets");

// Prefix for logger
Expand Down Expand Up @@ -198,7 +198,17 @@ class Transit {
*/
_requestHandler(payload) {
this.logger.info(`Request '${payload.action}' from '${payload.sender}'. Params:`, payload.params);
return this.broker.call(payload.action, payload.params, {}) // empty {} opts to avoid deoptimizing
const ctx = new Context({
broker: this.broker,
id: payload.id,
parent: {
id: payload.parentID,
},
level: payload.level + 1,
metrics: payload.metrics,
meta: payload.meta
});
return this.broker.call(payload.action, payload.params, { parentCtx: ctx })
.then(res => this.sendResponse(payload.sender, payload.id, res, null))
.catch(err => this.sendResponse(payload.sender, payload.id, null, err));
}
Expand Down
4 changes: 0 additions & 4 deletions test/integration/serializer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,6 @@ describe("Test MsgPack serializer", () => {
});

it("should serialize the request packet", () => {
const params = {
a: 5,
b: "Test"
};
const packet = new P.PacketRequest(broker.transit, "test-2", ctx);
const s = packet.serialize();
expect(Buffer.byteLength(s, "binary")).toBe(138);
Expand Down
8 changes: 5 additions & 3 deletions test/unit/transit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ describe("Test Transit.messageHandler", () => {
let msg = { sender: "remote", action: "posts.find", id: "123", params: JSON.stringify({ limit: 5 }), meta: "{}" };
return transit.messageHandler("REQ", JSON.stringify(msg)).then(() => {
expect(broker.call).toHaveBeenCalledTimes(1);
expect(broker.call).toHaveBeenCalledWith(msg.action, { limit: 5 }, {});
expect(broker.call).toHaveBeenCalledWith(msg.action, { limit: 5 }, { parentCtx: jasmine.any(Context) });
// TODO: check context props

expect(transit.sendResponse).toHaveBeenCalledTimes(1);
expect(transit.sendResponse).toHaveBeenCalledWith("remote", "123", [1, 5, 8], null);
Expand All @@ -195,7 +196,8 @@ describe("Test Transit.messageHandler", () => {
let msg = { sender: "remote", action: "posts.create", id: "123", params: JSON.stringify({ title: "Hello" }), meta: "{}" };
return transit.messageHandler("REQ", JSON.stringify(msg)).then(() => {
expect(broker.call).toHaveBeenCalledTimes(1);
expect(broker.call).toHaveBeenCalledWith(msg.action, { title: "Hello" }, {});
expect(broker.call).toHaveBeenCalledWith(msg.action, { title: "Hello" }, { parentCtx: jasmine.any(Context) });
// TODO: check context props

expect(transit.sendResponse).toHaveBeenCalledTimes(1);
expect(transit.sendResponse).toHaveBeenCalledWith("remote", "123", null, jasmine.any(ValidationError));
Expand Down Expand Up @@ -344,7 +346,7 @@ describe("Test Transit.request", () => {
return transit.request(ctx).then(req => {
expect(transit.pendingRequests.size).toBe(1);
expect(transit.publish).toHaveBeenCalledTimes(1);

const packet = transit.publish.mock.calls[0][0];
expect(packet).toBeInstanceOf(P.PacketRequest);
expect(packet.payload.id).toBe("12345");
Expand Down

0 comments on commit 2b3ee83

Please sign in to comment.