Skip to content

Commit

Permalink
remove Promise results in findNextActionEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Sep 12, 2017
1 parent 0ccbef2 commit ac72fed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ class ServiceBroker {
*
* @param {String} actionName
* @param {Object} opts
* @returns {Endpoint|Promise}
* @returns {Endpoint|Error}
*
* @performance-critical
* @memberof ServiceBroker
Expand All @@ -728,7 +728,7 @@ class ServiceBroker {
const endpoint = this.registry.getActionEndpointByNodeId(actionName, opts.nodeID);
if (!endpoint) {
this.logger.warn(`Service '${actionName}' is not found on '${opts.nodeID}' node.`);
return Promise.reject(new E.ServiceNotFoundError(actionName, opts.nodeID));
return new E.ServiceNotFoundError(actionName, opts.nodeID);
}
return endpoint;

Expand All @@ -737,15 +737,15 @@ class ServiceBroker {
const epList = this.registry.getActionEndpoints(actionName);
if (!epList) {
this.logger.warn(`Service '${actionName}' is not registered.`);
return Promise.reject(new E.ServiceNotFoundError(actionName));
return new E.ServiceNotFoundError(actionName);
}

// Get the next available endpoint
const endpoint = epList.next();
if (!endpoint) {
const errMsg = `Service '${actionName}' is not available.`;
this.logger.warn(errMsg);
return Promise.reject(new E.ServiceNotAvailable(actionName));
return new E.ServiceNotAvailable(actionName);
}
return endpoint;
}
Expand All @@ -765,8 +765,8 @@ class ServiceBroker {
*/
call(actionName, params, opts = {}) {
const endpoint = this.findNextActionEndpoint(actionName, opts);
if (utils.isPromise(endpoint))
return endpoint;
if (endpoint instanceof Error)
return Promise.reject(endpoint);

// Load opts with default values
if (opts.timeout == null)
Expand Down
33 changes: 15 additions & 18 deletions test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,31 +1152,28 @@ describe("Test broker.findNextActionEndpoint", () => {
});

it("should reject if no action", () => {
return broker.findNextActionEndpoint("posts.noaction").then(protectReject).catch(err => {
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotFoundError);
expect(err.message).toBe("Service 'posts.noaction' is not found.");
expect(err.data).toEqual({ action: "posts.noaction", nodeID: undefined });
});
const err = broker.findNextActionEndpoint("posts.noaction");
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotFoundError);
expect(err.message).toBe("Service 'posts.noaction' is not found.");
expect(err.data).toEqual({ action: "posts.noaction", nodeID: undefined });
});

it("should reject if no handler", () => {
broker.registry.unregisterAction({ id: broker.nodeID }, "posts.noHandler");
return broker.findNextActionEndpoint("posts.noHandler", {}).then(protectReject).catch(err => {
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotAvailable);
expect(err.message).toBe("Service 'posts.noHandler' is not available.");
expect(err.data).toEqual({ action: "posts.noHandler", nodeID: undefined });
});
const err = broker.findNextActionEndpoint("posts.noHandler", {});
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotAvailable);
expect(err.message).toBe("Service 'posts.noHandler' is not available.");
expect(err.data).toEqual({ action: "posts.noHandler", nodeID: undefined });
});

it("should reject if no action on node", () => {
return broker.findNextActionEndpoint("posts.noHandler", { nodeID: "node-123"}).then(protectReject).catch(err => {
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotFoundError);
expect(err.message).toBe("Service 'posts.noHandler' is not found on 'node-123' node.");
expect(err.data).toEqual({ action: "posts.noHandler", nodeID: "node-123" });
});
const err = broker.findNextActionEndpoint("posts.noHandler", { nodeID: "node-123"});
expect(err).toBeDefined();
expect(err).toBeInstanceOf(ServiceNotFoundError);
expect(err.message).toBe("Service 'posts.noHandler' is not found on 'node-123' node.");
expect(err.data).toEqual({ action: "posts.noHandler", nodeID: "node-123" });
});

it("should find the endpoint with nodeID", () => {
Expand Down

0 comments on commit ac72fed

Please sign in to comment.