forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activitymonitor.spec.ts
186 lines (145 loc) · 5.08 KB
/
activitymonitor.spec.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import expect = require('expect.js');
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
ActivityMonitor
} from '@jupyterlab/coreutils';
class TestObject {
one = new Signal<TestObject, number>(this);
two = new Signal<TestObject, string[]>(this);
}
describe('coreutils', () => {
describe('ActivityMonitor()', () => {
let testObj: TestObject;
let signal: Signal<TestObject, number>;
beforeEach(() => {
testObj = new TestObject();
signal = testObj.one;
});
describe('#constructor()', () => {
it('should accept a signal', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
expect(monitor).to.be.an(ActivityMonitor);
});
it('should accept a timeout', () => {
let monitor = new ActivityMonitor<TestObject, string[]>({
signal: testObj.two, timeout: 100
});
expect(monitor).to.be.an(ActivityMonitor);
});
});
describe('#activityStopped', () => {
it('should be emitted after the signal has fired and a timeout', (done) => {
let called = false;
let monitor = new ActivityMonitor({ signal, timeout: 100 });
monitor.activityStopped.connect((sender, args) => {
expect(sender).to.be(monitor);
expect(args.sender).to.be(testObj);
expect(args.args).to.be(10);
called = true;
});
signal.emit(10);
expect(called).to.be(false);
setTimeout(() => {
expect(called).to.be(true);
done();
}, 100);
});
it('should collapse during activity', (done) => {
let called = false;
let timeout = 300;
let emission = -1;
let monitor = new ActivityMonitor<TestObject, number>({ signal, timeout });
monitor.activityStopped.connect((sender, args) => {
emission = args.args;
called = true;
expect(sender).to.be(monitor);
expect(args.sender).to.be(testObj);
});
let firstEmission = 5;
let secondEmission = 10;
let start = 0; // The start time in ms for each signal emitted.
let thirdPass = () => {
let now = (new Date()).getTime();
let delta = now - start;
if (delta > timeout) {
expect(called).to.be(true);
expect(emission).to.be(secondEmission);
done();
return;
}
throw new Error('Time traveled in the wrong direction!');
};
let secondPass = () => {
let now = (new Date()).getTime();
let delta = now - start;
if (delta > timeout) {
expect(called).to.be(true);
expect(emission).to.be(secondEmission);
done();
return;
}
setTimeout(thirdPass, timeout);
};
let firstPass = () => {
let now = (new Date()).getTime();
let delta = now - start;
if (delta > timeout) {
expect(called).to.be(true);
expect(emission).to.be(firstEmission);
done();
return;
}
signal.emit(secondEmission);
// Restart the clock.
start = (new Date()).getTime();
setTimeout(secondPass, timeout - 100);
};
// The timing of setTimeout is *not* guaranteed and on overburdened
// systems, it can take longer than intended, so each pass must check
// the actual time elapsed if timing is an inherent part of its behavior
// as it is in this test.
signal.emit(firstEmission);
expect(called).to.be(false);
// Start the clock.
start = (new Date()).getTime();
setTimeout(firstPass, timeout - 100);
});
});
describe('#timeout', () => {
it('should default to `1000`', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
expect(monitor.timeout).to.be(1000);
});
it('should be set-able', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
monitor.timeout = 200;
expect(monitor.timeout).to.be(200);
});
});
describe('#isDisposed', () => {
it('should test whether the monitor is disposed', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
expect(monitor.isDisposed).to.be(false);
monitor.dispose();
expect(monitor.isDisposed).to.be(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources used by the monitor', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
monitor.dispose();
expect(monitor.isDisposed).to.be(true);
});
it('should be a no-op if called more than once', () => {
let monitor = new ActivityMonitor<TestObject, number>({ signal });
monitor.dispose();
monitor.dispose();
expect(monitor.isDisposed).to.be(true);
});
});
});
});