|
| 1 | +/** |
| 2 | + * Copyright 2019 Google Inc. All Rights Reserved. |
| 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 | +import * as mongooseTypes from 'mongoose'; |
| 19 | + |
| 20 | +import * as traceTestModule from '../trace'; |
| 21 | +import {describeInterop} from '../utils'; |
| 22 | + |
| 23 | +describeInterop<typeof mongooseTypes>('mongoose', (fixture) => { |
| 24 | + let mongoose: typeof mongooseTypes; |
| 25 | + // Simple will be treated as a class constructor. |
| 26 | + // tslint:disable-next-line:variable-name |
| 27 | + let Simple: mongooseTypes.Model<mongooseTypes.Document>; |
| 28 | + |
| 29 | + /** |
| 30 | + * Common logic used in multiple tests -- inserts an object into the database. |
| 31 | + * @param doc |
| 32 | + */ |
| 33 | + async function insertTestData(doc: {f1: string, f2: boolean, f3: number}) { |
| 34 | + const data = new Simple(doc); |
| 35 | + const tracer = traceTestModule.get(); |
| 36 | + await tracer.runInRootSpan({name: 'insert-test-data'}, async (span) => { |
| 37 | + assert.ok(tracer.isRealSpan(span)); |
| 38 | + await data.save(); |
| 39 | + span.endSpan(); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + before(async () => { |
| 44 | + traceTestModule.setCLSForTest(); |
| 45 | + traceTestModule.setPluginLoaderForTest(); |
| 46 | + traceTestModule.start(); |
| 47 | + mongoose = fixture.require(); |
| 48 | + await mongoose.connect('mongodb://localhost:27017/testdb'); |
| 49 | + |
| 50 | + const {Schema} = mongoose; |
| 51 | + const simpleSchema = new Schema({f1: String, f2: Boolean, f3: Number}); |
| 52 | + Simple = mongoose.model('Simple', simpleSchema); |
| 53 | + }); |
| 54 | + |
| 55 | + after(async () => { |
| 56 | + traceTestModule.setCLSForTest(traceTestModule.TestCLS); |
| 57 | + traceTestModule.setPluginLoaderForTest(traceTestModule.TestPluginLoader); |
| 58 | + await mongoose.connection.db.dropDatabase(); |
| 59 | + await mongoose.disconnect(); |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + traceTestModule.clearTraceData(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Traces creates with async/await', async () => { |
| 67 | + await insertTestData({f1: 'val', f2: false, f3: 1729}); |
| 68 | + const trace = traceTestModule.getOneTrace( |
| 69 | + trace => trace.spans.some(span => span.name === 'insert-test-data')); |
| 70 | + assert.strictEqual(trace.spans.length, 2); |
| 71 | + assert.strictEqual(trace.spans[1].name, 'mongo-insert'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Traces queries with async/await', async () => { |
| 75 | + await insertTestData({f1: 'sim', f2: false, f3: 1729}); |
| 76 | + const tracer = traceTestModule.get(); |
| 77 | + await tracer.runInRootSpan({name: 'query-test-data'}, async (span) => { |
| 78 | + assert.ok(tracer.isRealSpan(span)); |
| 79 | + await Simple.findOne({f1: 'sim'}); |
| 80 | + span.endSpan(); |
| 81 | + }); |
| 82 | + const trace = traceTestModule.getOneTrace( |
| 83 | + trace => trace.spans.some(span => span.name === 'query-test-data')); |
| 84 | + assert.strictEqual(trace.spans.length, 2); |
| 85 | + assert.strictEqual(trace.spans[1].name, 'mongo-cursor'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments