Skip to content

Commit 8200345

Browse files
tests: connection refactor.
1 parent 6fd7154 commit 8200345

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

lib/common/connection.js

+18-25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
'use strict';
2323

24+
var events = require('events');
2425
var fs = require('fs');
2526
var GAPIToken = require('gapitoken');
27+
var nodeutil = require('util');
2628
var req = require('request');
2729

2830
/** @type {module:common/util} */
@@ -91,14 +93,15 @@ module.exports.Token = Token;
9193
* var conn = new Connection({ scopes: SCOPES });
9294
*/
9395
function Connection(opts) {
96+
events.EventEmitter.call(this);
97+
9498
this.opts = opts || {};
9599

96100
this.credentials = null;
97101
this.scopes = opts.scopes || [];
98102
this.token = null; // existing access token, if exists
99103

100104
this.isConnecting = false;
101-
this.waitQueue = [];
102105

103106
if (opts.credentials) {
104107
if (opts.credentials.client_email && opts.credentials.private_key) {
@@ -110,18 +113,18 @@ function Connection(opts) {
110113
}
111114
}
112115

116+
nodeutil.inherits(Connection, events.EventEmitter);
117+
113118
/**
114119
* Retrieve a token to authorize requests.
115120
*
116121
* @todo Connect should be context aware, it should not require an email and
117122
* key, if it's running on Google Compute Engine.
118123
*
119-
* @param {function} callback - The callback function.
120-
*
121124
* @example
122-
* conn.connect(function(err) {});
125+
* conn.connect();
123126
*/
124-
Connection.prototype.connect = function(callback) {
127+
Connection.prototype.connect = function() {
125128
var that = this;
126129
this.isConnecting = true;
127130
// retrieves an access token
@@ -130,7 +133,7 @@ Connection.prototype.connect = function(callback) {
130133
that.token = token;
131134
}
132135
that.isConnecting = false;
133-
callback(err);
136+
that.emit('connected');
134137
});
135138
};
136139

@@ -246,30 +249,20 @@ Connection.prototype.createAuthorizedReq = function(reqOpts, callback) {
246249
reqOpts.headers['User-Agent'] = USER_AGENT;
247250
}
248251

249-
if (this.isConnected()) {
250-
return callback(null, this.authorizeReq(reqOpts));
252+
function onConnected() {
253+
callback(null, that.authorizeReq(reqOpts));
251254
}
252-
if (this.isConnecting) {
253-
this.waitQueue = this.waitQueue || [];
254-
this.waitQueue.push({ req: reqOpts, cb: callback });
255+
256+
if (this.isConnected()) {
257+
onConnected();
255258
return;
256259
}
257-
this.connect(function(err) {
258-
that.waitQueue.push({ req: reqOpts, cb: callback });
259-
that.waitQueue.forEach(function(v) {
260-
if (!v.cb) {
261-
return;
262-
}
263260

264-
if (err) {
265-
v.cb(err);
266-
return;
267-
}
261+
this.on('connected', onConnected);
268262

269-
v.cb(null, that.authorizeReq(v.req));
270-
});
271-
that.waitQueue = [];
272-
});
263+
if (!this.isConnecting) {
264+
this.connect();
265+
}
273266
};
274267

275268
/**

test/common/connection.js

+27-36
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ describe('Connection', function() {
3232
conn = new connection.Connection({
3333
keyFilename: path.join(__dirname, '../testdata/privateKeyFile.json')
3434
});
35+
conn.requester = function(opts, callback) {
36+
callback(null);
37+
};
3538
});
3639

3740
it('should use a private key json file', function(done) {
@@ -62,70 +65,58 @@ describe('Connection', function() {
6265
});
6366
});
6467

65-
6668
describe('Token', function() {
6769
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
6870
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));
6971

7072
it('should fetch a new token if token expires', function(done) {
71-
var c = new connection.Connection({
72-
email: 'x@provider',
73-
privateKey: '/some/path',
74-
scopes: ['scope1', 'scope2']
75-
});
76-
c.token = tokenExpired;
77-
c.fetchToken = function() {
73+
conn.token = tokenExpired;
74+
conn.fetchToken = function() {
7875
done();
7976
};
80-
c.requester = function(opts, callback) {
81-
callback(null);
82-
};
83-
c.req({ uri: 'https://someuri' }, function() {});
77+
conn.req({ uri: 'https://someuri' }, function() {});
8478
});
8579

8680
it('should make other requests wait while connecting', function(done) {
8781
var numTokenFetches = 0;
88-
var c = new connection.Connection({
89-
email: 'x@provider',
90-
privateKey: '/some/path',
91-
scopes: ['scope1', 'scope2']
92-
});
93-
c.fetchToken = function(cb) {
82+
var requestedUris = [];
83+
conn.fetchToken = function(cb) {
9484
numTokenFetches++;
9585
setImmediate(function() {
9686
cb(null, tokenNeverExpires);
9787
});
9888
};
99-
c.requester = function(opts, callback) {
89+
conn.requester = function(opts, callback) {
90+
requestedUris.push(opts.uri);
10091
callback(null);
10192
};
102-
10393
async.parallel([
104-
function(done) { c.req({ uri: 'https://someuri' }, done); },
105-
function(done) { c.req({ uri: 'https://someuri' }, done); },
106-
function(done) { c.req({ uri: 'https://someuri' }, done); }
94+
function(next) {
95+
assert.strictEqual(conn.isConnecting, false);
96+
conn.req({ uri: '1' }, next);
97+
},
98+
function(next) {
99+
assert.strictEqual(conn.isConnecting, true);
100+
conn.req({ uri: '2' }, next);
101+
},
102+
function(next) {
103+
conn.req({ uri: '3' }, next);
104+
}
107105
], function(err) {
108-
assert.equal(err, null);
106+
assert.ifError(err);
109107
assert.equal(numTokenFetches, 1);
110-
assert.equal(c.token, tokenNeverExpires);
108+
assert.equal(conn.token, tokenNeverExpires);
109+
assert.deepEqual(requestedUris, ['1', '2', '3']);
111110
done();
112111
});
113112
});
114113

115114
it('should fetch a new token if token is invalid', function(done) {
116-
var c = new connection.Connection({
117-
email: 'x@provider',
118-
privateKey: '/some/path',
119-
scopes: ['scope1', 'scope2']
120-
});
121-
c.token = new connection.Token();
122-
c.fetchToken = function() {
115+
conn.token = new connection.Token();
116+
conn.fetchToken = function() {
123117
done();
124118
};
125-
c.requester = function(opts, callback) {
126-
callback(null);
127-
};
128-
c.req({ uri: 'https://someuri' }, function() {});
119+
conn.req({ uri: 'https://someuri' }, function() {});
129120
});
130121
});
131122
});

0 commit comments

Comments
 (0)