Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix data/greeting race condition (w/testcase). #7

Closed
wants to merge 1 commit into from

2 participants

Niall Smart James Halliday
Niall Smart

The greeting event will not be seen by a client if the first data event on the stream arrives before the client starts listening.

This bug is easily reproducible (for example, set a timeout before listening to 'greeting'). Patch includes testcase.

James Halliday
Owner

Sorry for sitting on this so long! Merged in 2.2.1.

James Halliday substack closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 30, 2012
  1. Niall Smart
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 4 deletions.
  1. +15 −4 lib/server/proto.js
  2. +30 −0 test/client.js
19 lib/server/proto.js
View
@@ -11,18 +11,29 @@ function Client (stream) {
self.stream = stream;
self.queue = [
function (err, code, lines) {
- if (err) self.emit('error', err)
- else self.emit('greeting', code, lines)
+ if (err) self.emit('error', err);
+ else {
+ self.greeting = [code, lines];
+ self.emit('greeting', code, lines)
+ }
}
];
-
+
var p = parser(stream, function (err, code, lines) {
if (self.queue.length) {
var cb = self.queue.shift();
if (cb) cb(err, code, lines);
}
});
-
+
+ self.on('newListener', function(event, listener) {
+
+ if (event === 'greeting' && this.greeting) {
+ listener(this.greeting[0], this.greeting[1])
+ }
+
+ });
+
return self;
}
30 test/client.js
View
@@ -0,0 +1,30 @@
+var test = require('tap').test;
+var smtp = require('../');
+
+var EventEmitter = require('events').EventEmitter;
+
+test('data/greeting race', {timeout: 1000}, function(t) {
+
+ t.plan(3);
+
+ stream = new EventEmitter();
+
+ smtp.connect({stream: stream}, function(mail) {
+
+ var listeners = stream.listeners('data');
+
+ t.equals(listeners.length, 1, "data listener is bound");
+
+ stream.emit('data', new Buffer('220 localhost ESMTP\r\n'));
+
+ setTimeout(function() {
+ mail.on('greeting', function(code, text) {
+ t.equals(code, 220, "smtp code");
+ t.equals(code, 220, "localhost ESMTP");
+ t.end();
+ })
+ }, 250);
+
+ });
+
+});
Something went wrong with that request. Please try again.