Skip to content

Commit 84138dd

Browse files
author
Malte-Maurice Dreyer
committed
Make PromiseConnection extend EventEmitter
The following events are re-emitted from the underlying Connection instance: - error - drain - connect - end - enqueue
1 parent 2ac0546 commit 84138dd

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

promise.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ function createConnection (opts) {
3333
function PromiseConnection (connection, promiseImpl) {
3434
this.connection = connection;
3535
this.Promise = promiseImpl;
36+
37+
inheritEvents(connection, this, ['error', 'drain', 'connect', 'end', 'enqueue']);
3638
}
39+
util.inherits(PromiseConnection, EventEmitter);
3740

3841
PromiseConnection.prototype.release = function () {
3942
this.connection.release();

test/integration/test-promise-wrappers.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ assert.equal(mainExport, createConnection);
1818

1919
var doneCalled = false;
2020
var exceptionCaught = false;
21+
var doneEventsConnect = false;
2122

2223
var doneCalledPool = false;
2324
var exceptionCaughtPool = false;
@@ -103,6 +104,38 @@ function testObjParams() {
103104
});
104105
}
105106

107+
function testEventsConnect() {
108+
var connPromise = createConnection(config).then(function(conn) {
109+
var events = 0;
110+
111+
conn
112+
.once('error', function(connection) {
113+
++events;
114+
})
115+
.once('drain', function(connection) {
116+
++events;
117+
})
118+
.once('connect', function() {
119+
++events;
120+
})
121+
.once('enqueue', function() {
122+
++events;
123+
})
124+
.once('end', function() {
125+
++events;
126+
127+
doneEventsConnect = events === 5;
128+
});
129+
130+
conn.connection.emit('error');
131+
conn.connection.emit('drain');
132+
conn.connection.emit('connect');
133+
conn.connection.emit('enqueue');
134+
conn.connection.emit('end');
135+
conn.end();
136+
});
137+
}
138+
106139
function testBasicPool() {
107140
var pool = createPool(config);
108141
pool
@@ -193,6 +226,7 @@ function testEventsPool() {
193226
testBasic();
194227
testErrors();
195228
testObjParams();
229+
testEventsConnect();
196230
testBasicPool();
197231
testErrorsPool();
198232
testObjParamsPool();
@@ -204,6 +238,7 @@ process.on('exit', function() {
204238
}
205239
assert.equal(doneCalled, true);
206240
assert.equal(exceptionCaught, true);
241+
assert.equal(doneEventsConnect, true);
207242
assert.equal(doneCalledPool, true);
208243
assert.equal(exceptionCaughtPool, true);
209244
assert.equal(doneEventsPool, true);

0 commit comments

Comments
 (0)