Skip to content

Commit

Permalink
fix: backgroundTasksFinished ensure all tasks finished (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Jan 17, 2020
1 parent 98fd7e4 commit 51ef091
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ module.exports = {
this._backgroundTasks = [];
return Promise.all(tasks).then(() => {
debug('finished %d background tasks', tasks.length);
if (this._backgroundTasks.length) {
debug('new background tasks created: %s', this._backgroundTasks.length);
return this.backgroundTasksFinished();
}
});
},

Expand Down
25 changes: 24 additions & 1 deletion test/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,34 @@ describe('test/bootstrap.test.js', () => {
.expect({ counter: 0 });
});

it('should all reset', function* () {
it('should wait for background task 4 finished', function* () {
yield app.httpRequest()
.get('/counter')
.expect(200)
.expect({ counter: 10 });
yield app.httpRequest()
.get('/counter/plusplus')
.expect(200)
.expect({ counter: 10 });
});

it('should wait for background task 5 finished', function* () {
yield app.httpRequest()
.get('/counter')
.expect(200)
.expect({ counter: 12 });
app.mockContext({ superMan: true });
yield app.httpRequest()
.get('/counter/plusplus')
.expect(200)
.expect({ counter: 12 });
});

it('should all reset', function* () {
yield app.httpRequest()
.get('/counter')
.expect(200)
.expect({ counter: 32 });
});

it('should always return promise instance', () => {
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/app/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,26 @@ module.exports = app => {
});
this.body = { counter };
});

app.get('/counter/plusplus', function* () {
this.runInBackground(function* (ctx) {
// mock io delay
yield sleep(10);
if (ctx.superMan) {
counter += 10;
} else {
counter++;
}
ctx.runInBackground(function* (ctx) {
// mock io delay
yield sleep(10);
if (ctx.superMan) {
counter += 10;
} else {
counter++;
}
});
});
this.body = { counter };
});
};

0 comments on commit 51ef091

Please sign in to comment.