|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | + |
| 19 | +import {bluebird_3 as BluebirdPromise} from '../../src/plugins/types'; |
| 20 | +import {Trace} from '../../src/trace'; |
| 21 | +import * as traceTestModule from '../trace'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Describes a test case. |
| 25 | + */ |
| 26 | +interface TestCase<T = void> { |
| 27 | + /** |
| 28 | + * Description of a test case; included in string argument to it(). |
| 29 | + */ |
| 30 | + description: string; |
| 31 | + /** |
| 32 | + * Creates and returns a new Promise. |
| 33 | + */ |
| 34 | + makePromise: () => BluebirdPromise<T>; |
| 35 | + /** |
| 36 | + * Given a Promise and a callback, calls the callback some time after the |
| 37 | + * Promise has been resolved or rejected. |
| 38 | + */ |
| 39 | + thenFn: (promise: BluebirdPromise<T>, cb: () => void) => void; |
| 40 | +} |
| 41 | +/** |
| 42 | + * For a given Promise implementation, create two traces: |
| 43 | + * 1. Constructs a new Promise and resolves it. |
| 44 | + * 2. Within a then callback to the above mentioned Promise, construct a child |
| 45 | + * span. |
| 46 | + */ |
| 47 | +const getTracesForPromiseImplementation = |
| 48 | + <T>(makePromise: () => BluebirdPromise<T>, |
| 49 | + thenFn: (promise: BluebirdPromise<T>, cb: () => void) => |
| 50 | + void): Promise<[Trace, Trace]> => { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + const tracer = traceTestModule.get(); |
| 53 | + let p: BluebirdPromise<T>; |
| 54 | + const firstSpan = tracer.runInRootSpan({name: 'first'}, span => { |
| 55 | + p = makePromise(); |
| 56 | + return span; |
| 57 | + }); |
| 58 | + tracer.runInRootSpan({name: 'second'}, secondSpan => { |
| 59 | + // Note to maintainers: Do NOT convert this to async/await, |
| 60 | + // as it changes context propagation behavior. |
| 61 | + thenFn(p, () => { |
| 62 | + tracer.createChildSpan().endSpan(); |
| 63 | + secondSpan.endSpan(); |
| 64 | + firstSpan.endSpan(); |
| 65 | + setImmediate(() => { |
| 66 | + try { |
| 67 | + const trace1 = traceTestModule.getOneTrace( |
| 68 | + trace => trace.spans.some(root => root.name === 'first')); |
| 69 | + const trace2 = traceTestModule.getOneTrace( |
| 70 | + trace => trace.spans.some(root => root.name === 'second')); |
| 71 | + traceTestModule.clearTraceData(); |
| 72 | + resolve([trace1, trace2]); |
| 73 | + } catch (e) { |
| 74 | + traceTestModule.clearTraceData(); |
| 75 | + reject(e); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }; |
| 82 | + |
| 83 | + |
| 84 | +describe('Patch plugin for bluebird', () => { |
| 85 | + // BPromise is a class. |
| 86 | + // tslint:disable-next-line:variable-name |
| 87 | + let BPromise: typeof BluebirdPromise; |
| 88 | + |
| 89 | + before(() => { |
| 90 | + traceTestModule.setCLSForTest(); |
| 91 | + traceTestModule.setPluginLoaderForTest(); |
| 92 | + traceTestModule.start(); |
| 93 | + BPromise = require('./fixtures/bluebird3'); |
| 94 | + }); |
| 95 | + |
| 96 | + after(() => { |
| 97 | + traceTestModule.setCLSForTest(traceTestModule.TestCLS); |
| 98 | + traceTestModule.setPluginLoaderForTest(traceTestModule.TestPluginLoader); |
| 99 | + }); |
| 100 | + |
| 101 | + const testCases = [ |
| 102 | + { |
| 103 | + description: 'immediate resolve + child from then callback', |
| 104 | + makePromise: () => new BPromise(res => res()), |
| 105 | + thenFn: (p, cb) => p.then(cb) |
| 106 | + } as TestCase, |
| 107 | + { |
| 108 | + description: 'deferred resolve + child from then callback', |
| 109 | + makePromise: () => new BPromise(res => setTimeout(res, 0)), |
| 110 | + thenFn: (p, cb) => p.then(cb) |
| 111 | + } as TestCase, |
| 112 | + { |
| 113 | + description: 'bound, deferred resolve + child from then callback', |
| 114 | + makePromise: () => new BPromise<void>(res => setTimeout(res, 0)).bind({}), |
| 115 | + thenFn: (p, cb) => p.then(cb) |
| 116 | + } as TestCase, |
| 117 | + { |
| 118 | + description: 'deferred resolve + child from spread callback', |
| 119 | + makePromise: () => new BPromise(res => setTimeout(() => res([]), 0)), |
| 120 | + thenFn: (p, cb) => p.spread(cb) |
| 121 | + } as TestCase<never[]>, |
| 122 | + { |
| 123 | + description: 'deferred rejection + child from then callback', |
| 124 | + makePromise: () => new BPromise((res, rej) => setTimeout(rej, 0)), |
| 125 | + thenFn: (p, cb) => p.then(null, cb) |
| 126 | + } as TestCase, |
| 127 | + { |
| 128 | + description: 'deferred rejection + child from catch callback', |
| 129 | + makePromise: () => new BPromise((res, rej) => setTimeout(rej, 0)), |
| 130 | + thenFn: (p, cb) => p.catch(cb) |
| 131 | + } as TestCase, |
| 132 | + { |
| 133 | + description: 'deferred rejection + child from error callback', |
| 134 | + makePromise: () => new BPromise( |
| 135 | + (res, rej) => |
| 136 | + setTimeout(() => rej(new BPromise.OperationalError()), 0)), |
| 137 | + thenFn: (p, cb) => p.error(cb) |
| 138 | + } as TestCase, |
| 139 | + { |
| 140 | + description: 'deferred rejection + child from finally callback', |
| 141 | + makePromise: () => new BPromise((res, rej) => setTimeout(rej, 0)), |
| 142 | + thenFn: (p, cb) => p.catch(() => {}).finally(cb) |
| 143 | + } as TestCase, |
| 144 | + { |
| 145 | + description: 'immediate resolve + child after await', |
| 146 | + makePromise: () => new BPromise(res => res()), |
| 147 | + thenFn: async (p, cb) => { |
| 148 | + await p; |
| 149 | + cb(); |
| 150 | + } |
| 151 | + } as TestCase, |
| 152 | + { |
| 153 | + description: 'deferred resolve + child after await', |
| 154 | + makePromise: () => new BPromise(res => setTimeout(res, 0)), |
| 155 | + thenFn: async (p, cb) => { |
| 156 | + await p; |
| 157 | + cb(); |
| 158 | + } |
| 159 | + } as TestCase |
| 160 | + ]; |
| 161 | + |
| 162 | + // tslint:disable-next-line:no-any |
| 163 | + testCases.forEach((testCase: TestCase<any>) => { |
| 164 | + it(`enables context propagation in the same way as native promises for test case: ${ |
| 165 | + testCase.description}`, |
| 166 | + async () => { |
| 167 | + const actual = (await getTracesForPromiseImplementation( |
| 168 | + testCase.makePromise, testCase.thenFn)) |
| 169 | + .map(trace => trace.spans.length) |
| 170 | + .join(', '); |
| 171 | + // In each case, the second trace should have the child span. |
| 172 | + // The format here is "[numSpansInFirstTrace], |
| 173 | + // [numSpansInSecondTrace]". |
| 174 | + assert.strictEqual(actual, '1, 2'); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments