-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathscope.spec.js
More file actions
173 lines (135 loc) · 3.86 KB
/
Copy pathscope.spec.js
File metadata and controls
173 lines (135 loc) · 3.86 KB
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
'use strict'
const assert = require('node:assert/strict')
const { describe, it, beforeEach } = require('mocha')
const sinon = require('sinon')
const { Span } = require('opentracing')
require('./setup/core')
const Scope = require('../src/scope')
describe('Scope', () => {
let scope
let span
beforeEach(() => {
scope = new Scope()
span = new Span()
})
describe('active()', () => {
it('should return null by default', () => {
assert.strictEqual(scope.active(), null)
})
})
describe('activate()', () => {
it('should return the value returned by the callback', () => {
assert.strictEqual(scope.activate(span, () => 'test'), 'test')
})
it('should preserve the surrounding scope', () => {
assert.strictEqual(scope.active(), null)
scope.activate(span, () => {})
assert.strictEqual(scope.active(), null)
})
it('should support an invalid callback', () => {
scope.activate(span, 'invalid')
})
it('should activate the span on the current scope', () => {
assert.strictEqual(scope.active(), null)
scope.activate(span, () => {
assert.strictEqual(scope.active(), span)
})
assert.strictEqual(scope.active(), null)
})
it('should persist through setTimeout', done => {
scope.activate(span, () => {
setTimeout(() => {
assert.strictEqual(scope.active(), span)
done()
}, 0)
})
})
it('should persist through setImmediate', done => {
scope.activate(span, () => {
setImmediate(() => {
assert.strictEqual(scope.active(), span)
done()
}, 0)
})
})
it('should persist through setInterval', done => {
scope.activate(span, () => {
let shouldReturn = false
const timer = setInterval(() => {
assert.strictEqual(scope.active(), span)
if (shouldReturn) {
clearInterval(timer)
return done()
}
shouldReturn = true
}, 0)
})
})
it('should persist through process.nextTick', done => {
scope.activate(span, () => {
process.nextTick(() => {
assert.strictEqual(scope.active(), span)
done()
}, 0)
})
})
it('should persist through promises', () => {
const promise = Promise.resolve()
return scope.activate(span, () => {
return promise.then(() => {
assert.strictEqual(scope.active(), span)
})
})
})
it('should handle concurrency', done => {
scope.activate(span, () => {
setImmediate(() => {
assert.strictEqual(scope.active(), span)
done()
})
})
scope.activate(span, () => {})
})
it('should handle errors', () => {
const error = new Error('boom')
sinon.spy(span, 'setTag')
try {
scope.activate(span, () => {
throw error
})
} catch (e) {
sinon.assert.calledWith(span.setTag, 'error', e)
}
})
})
describe('bind()', () => {
describe('with a function', () => {
it('should bind the function to the active span', () => {
let fn = () => {
assert.strictEqual(scope.active(), span)
}
scope.activate(span, () => {
fn = scope.bind(fn)
})
fn()
})
it('should bind the function to the provided span', () => {
let fn = () => {
assert.strictEqual(scope.active(), span)
}
fn = scope.bind(fn, span)
fn()
})
it('should keep the return value', () => {
let fn = () => 'test'
fn = scope.bind(fn)
assert.strictEqual(fn(), 'test')
})
})
describe('with an unsupported target', () => {
it('should return the target', () => {
assert.strictEqual(scope.bind('test', span), 'test')
})
})
})
})