-
Notifications
You must be signed in to change notification settings - Fork 35
/
operation.ts
343 lines (296 loc) 路 10.2 KB
/
operation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*!
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import {describe, it} from 'mocha';
import * as sinon from 'sinon';
import {Service} from '../src';
import {Operation} from '../src/operation';
import {
Metadata,
ServiceObject,
ServiceObjectConfig,
} from '../src/service-object';
import {util} from '../src/util';
// tslint:disable-next-line no-any
const asAny = (o: {}) => o as any;
describe('Operation', () => {
const FAKE_SERVICE = {} as Service;
const OPERATION_ID = '/a/b/c/d';
const sandbox = sinon.createSandbox();
let operation: Operation;
beforeEach(() => {
operation = new Operation({parent: FAKE_SERVICE, id: OPERATION_ID});
});
afterEach(() => {
sandbox.restore();
});
describe('instantiation', () => {
const parent = {};
it('should extend ServiceObject and EventEmitter', () => {
const svcObj = ServiceObject;
assert(operation instanceof Operation);
assert(operation instanceof svcObj);
assert(operation.on);
});
it('should pass ServiceObject the correct config', () => {
assert.strictEqual(operation.baseUrl, '');
assert.strictEqual(operation.parent, FAKE_SERVICE);
assert.strictEqual(operation.id, OPERATION_ID);
assert.deepStrictEqual(asAny(operation).methods, {
exists: true,
get: true,
getMetadata: {
reqOpts: {
name: OPERATION_ID,
},
},
});
});
it('should allow overriding baseUrl', () => {
const baseUrl = 'baseUrl';
const operation = new Operation({baseUrl, parent} as ServiceObjectConfig);
assert.strictEqual(operation.baseUrl, baseUrl);
});
it('should localize listener variables', () => {
assert.strictEqual(operation.completeListeners, 0);
assert.strictEqual(operation.hasActiveListeners, false);
});
it('should call listenForEvents_', () => {
// tslint:disable-next-line no-any
const stub = sandbox.stub(Operation.prototype as any, 'listenForEvents_');
const op = new Operation({parent} as ServiceObjectConfig);
assert.ok(stub.called);
});
});
describe('promise', () => {
beforeEach(() => {
asAny(operation).startPolling_ = () => Promise.resolve();
});
it('should reject the promise if an error occurs', () => {
const error = new Error('err');
setImmediate(() => {
operation.emit('error', error);
});
return operation.promise().then(
() => {
throw new Error('Promise should have been rejected.');
},
(err: Error) => {
assert.strictEqual(err, error);
}
);
});
it('should resolve the promise on complete', () => {
const metadata = {};
setImmediate(() => {
operation.emit('complete', metadata);
});
return operation.promise().then(data => {
assert.deepStrictEqual(data, [metadata]);
});
});
});
describe('listenForEvents_', () => {
beforeEach(() => {
asAny(operation).startPolling_ = util.noop;
});
it('should start polling when complete listener is bound', done => {
asAny(operation).startPolling_ = () => done();
operation.on('complete', util.noop);
});
it('should track the number of listeners', () => {
assert.strictEqual(operation.completeListeners, 0);
operation.on('complete', util.noop);
assert.strictEqual(operation.completeListeners, 1);
operation.removeListener('complete', util.noop);
assert.strictEqual(operation.completeListeners, 0);
});
it('should only run a single pulling loop', () => {
let startPollingCallCount = 0;
asAny(operation).startPolling_ = () => startPollingCallCount++;
operation.on('complete', util.noop);
operation.on('complete', util.noop);
assert.strictEqual(startPollingCallCount, 1);
});
it('should close when no more message listeners are bound', () => {
operation.on('complete', util.noop);
operation.on('complete', util.noop);
assert.strictEqual(operation.hasActiveListeners, true);
operation.removeListener('complete', util.noop);
assert.strictEqual(operation.hasActiveListeners, true);
operation.removeListener('complete', util.noop);
assert.strictEqual(operation.hasActiveListeners, false);
});
});
describe('poll_', () => {
it('should call getMetdata', done => {
asAny(operation).getMetadata = () => done();
asAny(operation).poll_(assert.ifError);
});
describe('could not get metadata', () => {
it('should callback with an error', done => {
const error = new Error('Error.');
sandbox.stub(operation, 'getMetadata').callsArgWith(0, error);
asAny(operation).poll_((err: Error) => {
assert.strictEqual(err, error);
done();
});
});
it('should callback with the operation error', done => {
const apiResponse = {
error: {},
} as Metadata;
sandbox
.stub(operation, 'getMetadata')
.callsArgWith(0, null, apiResponse);
asAny(operation).poll_((err: Error) => {
assert.strictEqual(err, apiResponse.error);
done();
});
});
});
describe('operation incomplete', () => {
const apiResponse = {done: false};
beforeEach(() => {
sandbox
.stub(operation, 'getMetadata')
.callsArgWith(0, null, apiResponse);
});
it('should callback with no arguments', done => {
asAny(operation).poll_((err: Error, resp: {}) => {
assert.strictEqual(resp, undefined);
done();
});
});
});
describe('operation complete', () => {
const apiResponse = {done: true};
beforeEach(() => {
sandbox
.stub(operation, 'getMetadata')
.callsArgWith(0, null, apiResponse);
});
it('should emit complete with metadata', done => {
asAny(operation).poll_((err: Error, resp: {}) => {
assert.strictEqual(resp, apiResponse);
done();
});
});
});
});
describe('startPolling_', () => {
beforeEach(() => {
sandbox.stub(asAny(Operation).prototype, 'listenForEvents_');
operation.hasActiveListeners = true;
});
it('should not call getMetadata if no listeners', done => {
operation.hasActiveListeners = false;
sandbox.stub(operation, 'getMetadata').callsFake(done); // if called, test will fail.
asAny(operation).startPolling_();
done();
});
it('should call getMetadata if listeners are registered', done => {
operation.hasActiveListeners = true;
sandbox.stub(operation, 'getMetadata').callsFake(() => done());
asAny(operation).startPolling_();
});
describe('API error', () => {
const error = new Error('Error.');
beforeEach(() => {
sandbox.stub(operation, 'getMetadata').callsArgWith(0, error);
});
it('should emit the error', done => {
operation.on('error', (err: Error) => {
assert.strictEqual(err, error);
done();
});
asAny(operation).startPolling_();
});
});
describe('operation pending', () => {
const apiResponse = {done: false};
beforeEach(() => {
sandbox
.stub(operation, 'getMetadata')
.callsArgWith(0, null, apiResponse);
});
it('should call startPolling_ after 500 ms by default', done => {
const startPolling_ = asAny(operation).startPolling_;
let startPollingCalled = false;
sandbox.stub(global, 'setTimeout').callsFake((fn, timeoutMs) => {
fn(); // should call startPolling_
assert.strictEqual(timeoutMs, 500);
return asAny({});
});
asAny(operation).startPolling_ = function() {
if (!startPollingCalled) {
// Call #1.
startPollingCalled = true;
startPolling_.apply(this, arguments);
return;
}
// This is from the setTimeout call.
assert.strictEqual(this, operation);
done();
};
asAny(operation).startPolling_();
});
it('should call startPolling_ after 2000 ms if configured to do so', done => {
// Mock a long running operation with a 2000 ms timeout:
const op = new Operation({
parent: FAKE_SERVICE,
id: OPERATION_ID,
pollIntervalMs: 2000,
});
op.hasActiveListeners = true;
sandbox.stub(op, 'getMetadata').callsArgWith(0, null, apiResponse);
const startPolling_ = asAny(op).startPolling_;
let startPollingCalled = false;
sandbox.stub(global, 'setTimeout').callsFake((fn, timeoutMs) => {
fn(); // should call startPolling_
assert.strictEqual(timeoutMs, 2000);
return asAny({});
});
asAny(op).startPolling_ = function() {
if (!startPollingCalled) {
// Call #1.
startPollingCalled = true;
startPolling_.apply(this, arguments);
return;
}
// This is from the setTimeout call.
assert.strictEqual(this, op);
done();
};
asAny(op).startPolling_();
});
});
describe('operation complete', () => {
const apiResponse = {done: true};
beforeEach(() => {
sandbox
.stub(operation, 'getMetadata')
.callsArgWith(0, null, apiResponse);
});
it('should emit complete with metadata', async () => {
operation.on('complete', (metadata: {}) => {
assert.strictEqual(metadata, apiResponse);
});
await asAny(operation).startPolling_();
});
});
});
});