Skip to content

Commit

Permalink
Fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Sep 11, 2017
1 parent 0a0cbf5 commit 18895de
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
13 changes: 12 additions & 1 deletion examples/hot.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ module.exports = {
name: "test",
actions: {
hello() {
return "Hello Moleculer!!!";
return "Hello Moleculer!";
}
},
events: {
"test.event"(c) {
this.logger.info("Event", c);
}
},
created() {
this.logger.info(">>> Service created!");
},

started() {
this.logger.info(">>> Service started!");
},

stopped() {
this.logger.info(">>> Service stopped!");
}
};
10 changes: 9 additions & 1 deletion src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class ServiceBroker {
// Promise constructor
this.Promise = Promise;

// Broker started flag
this._started = false;

// Class factories
this.ServiceFactory = this.options.ServiceFactory || require("./service");
this.ContextFactory = this.options.ContextFactory || require("./context");
Expand Down Expand Up @@ -289,6 +292,7 @@ class ServiceBroker {
})
.then(() => {
this.logger.info("Broker started.");
this._started = true;
});
}

Expand Down Expand Up @@ -507,7 +511,6 @@ class ServiceBroker {

return this.destroyService(service)
.then(() => this.loadService(service.__filename))
.then(svc => svc.started.call(svc).then(() => svc))
.then(svc => {
this.logger.info(`Service '${svc.name}' is reloaded.`);
return svc;
Expand All @@ -530,6 +533,11 @@ class ServiceBroker {

let service = new this.ServiceFactory(this, s);

if (this._started) {
// If broker started, should call the started lifecycle event
service.started.call(service).catch(err => this.logger.error("Unable to start service!", err));
}

this.servicesChanged(true);

return service;
Expand Down
43 changes: 43 additions & 0 deletions test/integration/service.lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,46 @@ describe("Test Service handlers", () => {
});
});
});

describe("Test Service handlers after broker.call", () => {

let createdHandler = jest.fn();
let startedHandler = jest.fn();
let stoppedHandler = jest.fn();
let eventHandler = jest.fn();

let broker = new ServiceBroker({ nodeID: "node-1" });

beforeAll(() => broker.start());

it("load service dynamically", () => {
broker.createService({
name: "posts",

created: createdHandler,
started: startedHandler,
stopped: stoppedHandler,

events: {
"user.*": eventHandler
}
});
});

it("should called created & started handler", () => {
expect(createdHandler).toHaveBeenCalledTimes(1);
expect(startedHandler).toHaveBeenCalledTimes(1);
});

it("should called event handler", () => {
broker.broadcastLocal("user.created", { id: 1, name: "John" });
expect(eventHandler).toHaveBeenCalledTimes(1);
expect(eventHandler).toHaveBeenCalledWith({ id: 1, name: "John" }, "node-1", "user.created");
});

it("should called stop handler", () => {
return broker.stop().then(() => {
expect(stoppedHandler).toHaveBeenCalledTimes(1);
});
});
});
10 changes: 3 additions & 7 deletions test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2405,18 +2405,17 @@ describe("Test hot-reload feature", () => {
hotReload: true
});

let started = jest.fn(() => Promise.resolve());
let svc = broker.createService({
name: "test",
started
name: "test"
});
svc.__filename = "./hello.service.js";

broker.destroyService = jest.fn(() => Promise.resolve(svc));
broker.loadService = jest.fn(() => Promise.resolve(svc));

beforeAll(() => broker.start());

it("should call hot reload methods", () => {
started.mockClear();
return broker.hotReloadService(svc).catch(protectReject).then(() => {

expect(utils.clearRequireCache).toHaveBeenCalledTimes(1);
Expand All @@ -2427,9 +2426,6 @@ describe("Test hot-reload feature", () => {

expect(broker.loadService).toHaveBeenCalledTimes(1);
expect(broker.loadService).toHaveBeenCalledWith("./hello.service.js");

expect(started).toHaveBeenCalledTimes(1);

});
});
});
Expand Down

0 comments on commit 18895de

Please sign in to comment.