forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventPluginManager-spec.ts
More file actions
184 lines (149 loc) · 6.13 KB
/
EventPluginManager-spec.ts
File metadata and controls
184 lines (149 loc) · 6.13 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
174
175
176
177
178
179
180
181
182
183
184
import { Configuration } from '../configuration/Configuration';
import { ContextData } from 'ContextData';
import { ExceptionlessClient } from '../ExceptionlessClient';
import { EventPluginManager } from 'EventPluginManager';
import { EventPluginContext } from 'EventPluginContext';
describe('EventPluginManager', () => {
it('should add items to the event.', (done) => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(context.event.source).toBeUndefined();
expect(context.event.geo).toBeUndefined();
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => {
setTimeout(() => {
context.event.source = 'plugin 1';
if (next) {
next();
}
}, 100);
});
client.config.addPlugin('2', 2, (context:EventPluginContext, next?:() => void) => {
context.event.geo = '43.5775,88.4472';
if (next) {
next();
}
});
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(context.cancelled).toBeUndefined();
expect(context.event.source).toBe('plugin 1');
expect(context.event.geo).toBe('43.5775,88.4472');
done();
});
}, 5000);
it('setting cancel should stop plugin execution.', (done) => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => {
context.cancelled = true;
if (next) {
next();
}
});
client.config.addPlugin('2', 2, () => {
// Fail this test as this plugin should not be called.
expect(false).toBe(true);
});
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(context.cancelled).toBe(true);
done();
});
}, 5000);
it('throwing error should stop plugin execution.', (done) => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, () => {
throw new Error('Random Error');
});
client.config.addPlugin('2', 2, () => {
// Fail this test as this plugin should not be called.
expect(false).toBe(true);
});
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(context.cancelled).toBe(true);
done();
});
}, 5000);
it('throwing async error should stop plugin execution.', (done) => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => {
// NOTE: Currently you have to catch it and set cancelled.
setTimeout(() => {
try {
throw new Error('Random Error');
} catch (e) {
context.cancelled = true;
}
if (next) {
next();
}
}, 500);
});
client.config.addPlugin('2', 2, () => {
// Fail this test as this plugin should not be called.
expect(false).toBe(true);
});
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(context.cancelled).toBe(true);
done();
});
}, 5000);
it('should cancel via timeout.', (done) => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => {
setTimeout(done, 100)
});
client.config.addPlugin('2', 2, () => {
// Fail this test as this plugin should not be called.
expect(false).toBe(true);
});
EventPluginManager.run(context, (context?:EventPluginContext) => {
// Fail this test as this callback should not be called.
expect(false).toBe(true);
});
}, 500);
it('should ensure config plugins are not wrapped.', () => {
var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
var context = new EventPluginContext(client, {}, new ContextData());
expect(client.config.plugins).not.toBe(null);
while(client.config.plugins.length > 0) {
client.config.removePlugin(client.config.plugins[0]);
}
client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => {
if (next) {
next();
}
});
expect(client.config.plugins[0].name).toBe('1');
expect(client.config.plugins.length).toBe(1);
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(client.config.plugins[0].name).toBe('1');
});
expect(client.config.plugins.length).toBe(1);
EventPluginManager.run(context, (context?:EventPluginContext) => {
expect(client.config.plugins[0].name).toBe('1');
});
expect(client.config.plugins.length).toBe(1);
});
});