8000 fix tests - they have to be async! · yanxi123-com/log4js-node@9da158f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9da158f

Browse files
committed
fix tests - they have to be async!
1 parent cd3971c commit 9da158f

File tree

2 files changed

+82
-23
lines changed

2 files changed

+82
-23
lines changed

test/connect-logger-test.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"use strict";
33
var vows = require('vows')
44
, assert = require('assert')
5+
, util = require('util')
6+
, EE = require('events').EventEmitter
57
, levels = require('../lib/levels');
68

79
function MockLogger() {
@@ -37,15 +39,19 @@ function MockRequest(remoteAddr, method, originalUrl, headers) {
3739
}
3840

3941
function MockResponse() {
40-
41-
this.end = function(chunk, encoding) {
42+
var r = this;
43+
this.end = function(chunk, encoding) {
44+
setImmediate(function(){ r.emit('finish') });
4245
};
4346

4447
this.writeHead = function(code, headers) {
48+
this.statusCode = code;
49+
this._headers = headers;
4550
};
46-
4751
}
4852

53+
util.inherits(MockResponse, EE);
54+
4955
function request(cl, method, url, code, reqHeaders, resHeaders) {
5056
var req = new MockRequest('my.remote.addr', method, url, reqHeaders);
5157
var res = new MockResponse();
@@ -81,8 +87,11 @@ vows.describe('log4js connect logger').addBatch({
8187
topic: function(clm) {
8288
var ml = new MockLogger();
8389
var cl = clm.connectLogger(ml);
90+
var cb = this.callback;
8491
request(cl, 'GET', 'http://url', 200);
85-
return ml.messages;
92+
setTimeout(function() {
93+
cb(null, ml.messages);
94+
},10);
8695
},
8796

8897
'check message': function(messages) {
@@ -114,11 +123,13 @@ vows.describe('log4js connect logger').addBatch({
114123
'log events with non-default level and custom format' : {
115124
topic: function(clm) {
116125
var ml = new MockLogger();
126+
var cb = this.callback;
117127
ml.level = levels.INFO;
118128
var cl = clm.connectLogger(ml, { level: levels.INFO, format: ':method :url' } );
119129
request(cl, 'GET', 'http://url', 200);
120-
return ml.messages;
121-
},
130+
setTimeout(function() {
131+
cb(null, ml.messages);
132+
},10); },
122133

123134
'check message': function(messages) {
124135
assert.isArray(messages);
@@ -131,10 +142,13 @@ vows.describe('log4js connect logger').addBatch({
131142
'logger with options as string': {
132143
topic: function(clm) {
133144
var ml = new MockLogger();
145+
var cb = this.callback;
134146
ml.level = levels.INFO;
135147
var cl = clm.connectLogger(ml, ':method :url');
136148
request(cl, 'POST', 'http://meh', 200);
137-
return ml.messages;
149+
setTimeout(function() {
150+
cb(null, ml.messages);
151+
},10);
138152
},
139153
'should use the passed in format': function(messages) {
140154
assert.equal(messages[0].message, 'POST http://meh');
@@ -144,14 +158,17 @@ vows.describe('log4js connect logger').addBatch({
144158
'auto log levels': {
145159
topic: function(clm) {
146160
var ml = new MockLogger();
161+
var cb = this.callback;
147162
ml.level = levels.INFO;
148163
var cl = clm.connectLogger(ml, { level: 'auto', format: ':method :url' });
149164
request(cl, 'GET', 'http://meh', 200);
150165
request(cl, 'GET', 'http://meh', 201);
151166
request(cl, 'GET', 'http://meh', 302);
152167
request(cl, 'GET', 'http://meh', 404);
153168
request(cl, 'GET', 'http://meh', 500);
154-
return ml.messages;
169+
setTimeout(function() {
170+
cb(null, ml.messages);
171+
},10);
155172
},
156173

157174
'should use INFO for 2xx': function(messages) {
@@ -175,10 +192,13 @@ vows.describe('log4js connect logger').addBatch({
175192
'format using a function': {
176193
topic: function(clm) {
177194
var ml = new MockLogger();
195+
var cb = this.callback;
178196
ml.level = levels.INFO;
179197
var cl = clm.connectLogger(ml, function(req, res, formatFn) { return "I was called"; });
180198
request(cl, 'GET', 'http://blah', 200);
181-
return ml.messages;
199+
setTimeout(function() {
200+
cb(null, ml.messages);
201+
},10);
182202
},
183203

184204
'should call the format function': function(messages) {
@@ -189,14 +209,17 @@ vows.describe('log4js connect logger').addBatch({
189209
'format that includes request headers': {
190210
topic: function(clm) {
191211
var ml = new MockLogger();
212+
var cb = this.callback;
192213
ml.level = levels.INFO;
193214
var cl = clm.connectLogger(ml, ':req[Content-Type]');
194215
request(
195216
cl,
196217
'GET', 'http://blah', 200,
197218
{ 'Content-Type': 'application/json' }
198219
);
199-
return ml.messages;
220+
setTimeout(function() {
221+
cb(null, ml.messages);
222+
},10);
200223
},
201224
'should output the request header': function(messages) {
202225
assert.equal(messages[0].message, 'application/json');
@@ -206,6 +229,7 @@ vows.describe('log4js connect logger').addBatch({
206229
'format that includes response headers': {
207230
topic: function(clm) {
208231
var ml = new MockLogger();
232+
var cb = this.callback;
209233
ml.level = levels.INFO;
210234
var cl = clm.connectLogger(ml, ':res[Content-Type]');
211235
request(
@@ -214,7 +238,9 @@ vows.describe('log4js connect logger').addBatch({
214238
null,
215239
{ 'Content-Type': 'application/cheese' }
216240
);
217-
return ml.messages;
241+
setTimeout(function() {
242+
cb(null, ml.messages);
243+
},10);
218244
},
219245

220246
'should output the response header': function(messages) {

test/nolog-test.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
var vows = require('vows')
33
, assert = require('assert')
4+
, util = require('util')
5+
, EE = require('events').EventEmitter
46
, levels = require('../lib/levels');
57

68
function MockLogger() {
@@ -31,13 +33,14 @@ function MockRequest(remoteAddr, method, originalUrl) {
3133
}
3234

3335
function MockResponse(statusCode) {
34-
36+
var r = this;
3537
this.statusCode = statusCode;
3638

3739
this.end = function(chunk, encoding) {
38-
40+
setImmediate(function(){ r.emit('finish') });
3941
};
4042
}
43+
util.inherits(MockResponse, EE);
4144

4245
vows.describe('log4js connect logger').addBatch({
4346
'getConnectLoggerModule': {
@@ -61,9 +64,12 @@ vows.describe('log4js connect logger').addBatch({
6164
topic: function(d){
6265
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
6366
var res = new MockResponse(200);
67+
var cb = this.callback;
6468
d.cl(req, res, function() { });
6569
res.end('chunk', 'encoding');
66-
return d.ml.messages;
70+
setTimeout(function() {
71+
cb(null, d.ml.messages);
72+
},10);
6773
},
6874
'check message': function(messages){
6975
assert.isArray(messages);
@@ -103,9 +109,12 @@ vows.describe('log4js connect logger').addBatch({
103109
topic: function(d){
104110
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
105111
var res = new MockResponse(200);
112+
var cb = this.callback;
106113
d.cl(req, res, function() { });
107114
res.end('chunk', 'encoding');
108-
return d.ml.messages;
115+
setTimeout(function() {
F438 116+
cb(null, d.ml.messages)
117+
}, 10);
109118
},
110119
'check message': function(messages){
111120
assert.isArray(messages);
@@ -123,9 +132,12 @@ vows.describe('log4js connect logger').addBatch({
123132
topic: function(d) {
124133
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
125134
var res = new MockResponse(200);
135+
var cb = this.callback;
126136
d.cl(req, res, function() { });
127137
res.end('chunk', 'encoding');
128-
return d.ml.messages;
138+
setTimeout(function() {
139+
cb(null, d.ml.messages)
140+
}, 10);
129141
},
130142
'check message': function(messages) {
131143
assert.isArray(messages);
@@ -136,9 +148,12 @@ vows.describe('log4js connect logger').addBatch({
136148
topic: function(d) {
137149
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
138150
var res = new MockResponse(200);
151+
var cb = this.callback;
139152
d.cl(req, res, function() { });
140153
res.end('chunk', 'encoding');
141-
return d.ml.messages;
154+
setTimeout(function() {
155+
cb(null, d.ml.messages)
156+
}, 10);
142157
},
143158
'check message': function(messages) {
144159
assert.isArray(messages);
@@ -157,9 +172,12 @@ vows.describe('log4js connect logger').addBatch({
157172
topic: function(d){
158173
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
159174
var res = new MockResponse(200);
175+
var cb = this.callback;
160176
d.cl(req, res, function() { });
161177
res.end('chunk', 'encoding');
162-
return d.ml.messages;
178+
setTimeout(function() {
179+
cb(null, d.ml.messages)
180+
}, 10);
163181
},
164182
'check message': function(messages){
165183
assert.isArray(messages);
@@ -177,9 +195,12 @@ vows.describe('log4js connect logger').addBatch({
177195
topic: function(d) {
178196
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
179197
var res = new MockResponse(200);
198+
var cb = this.callback;
180199
d.cl(req, res, function() { });
181200
res.end('chunk', 'encoding');
182-
return d.ml.messages;
201+
setTimeout(function() {
202+
cb(null, d.ml.messages)
203+
}, 10);
183204
},
184205
'check message': function(messages) {
185206
assert.isArray(messages);
@@ -191,9 +212,12 @@ vows.describe('log4js connect logger').addBatch({
191212
topic: function(d) {
192213
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
193214
var res = new MockResponse(200);
215+
var cb = this.callback;
194216
d.cl(req, res, function() { });
195217
res.end('chunk', 'encoding');
196-
return d.ml.messages;
218+
setTimeout(function() {
219+
cb(null, d.ml.messages)
220+
}, 10);
197221
},
198222
'check message': function(messages) {
199223
assert.isArray(messages);
@@ -212,9 +236,12 @@ vows.describe('log4js connect logger').addBatch({
212236
topic: function(d){
213237
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
214238
var res = new MockResponse(200);
239+
var cb = this.callback;
215240
d.cl(req, res, function() { });
216241
res.end('chunk', 'encoding');
217-
return d.ml.messages;
242+
setTimeout(function() {
243+
cb(null, d.ml.messages)
244+
}, 10);
218245
},
219246
'check message': function(messages){
220247
assert.isArray(messages);
@@ -232,9 +259,12 @@ vows.describe('log4js connect logger').addBatch({
232259
topic: function(d) {
233260
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
234261
var res = new MockResponse(200);
262+
var cb = this.callback;
235263
d.cl(req, res, function() { });
236264
res.end('chunk', 'encoding');
237-
return d.ml.messages;
265+
setTimeout(function() {
266+
cb(null, d.ml.messages)
267+
}, 10);
238268
},
239269
'check message': function(messages) {
240270
assert.isArray(messages);
@@ -246,9 +276,12 @@ vows.describe('log4js connect logger').addBatch({
246276
topic: function(d) {
247277
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
248278
var res = new MockResponse(200);
279+
var cb = this.callback;
249280
d.cl(req, res, function() { });
250281
res.end('chunk', 'encoding');
251-
return d.ml.messages;
282+
setTimeout(function() {
283+
cb(null, d.ml.messages)
284+
}, 10);
252285
},
253286
'check message': function(messages) {
254287
assert.isArray(messages);

0 commit comments

Comments
 (0)
0