-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathoneerror-per-visit-test.mjs
More file actions
209 lines (177 loc) · 6.32 KB
/
oneerror-per-visit-test.mjs
File metadata and controls
209 lines (177 loc) · 6.32 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import qunit from 'qunit';
import { merge } from 'lodash-es';
import { appScenarios } from './scenarios.mjs';
import buildFastboot from './helpers/build-fastboot.mjs';
const { module: Qmodule, test } = qunit;
appScenarios
.map('oneerror-per-visit', (project) => {
merge(project.files, {
app: {
'router.js': `import EmberRouter from '@ember/routing/router';
import config from 'classic-app-template/config/environment';
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}
Router.map(function () {
this.route('slow', {
path: '/slow/:timeout/:type',
});
});`,
'instance-initializers': {
'setup-onerror.js': `import { setOnerror } from '@ember/-internals/error-handling';
export function initialize(owner) {
let isFastBoot = typeof 'FastBoot' !== 'undefined';
let fastbootRequestPath;
if (isFastBoot) {
let fastbootService = owner.lookup('service:fastboot');
fastbootRequestPath = fastbootService.request.path;
} // normally only done in prod builds, but this makes the demo easier
console.log('setting up error handler ' + fastbootRequestPath);
setOnerror(function (error) {
if (isFastBoot) {
error.fastbootRequestPath = fastbootRequestPath;
throw error;
}
});
}
export default {
initialize,
};
`,
},
routes: {
'application.js': `import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { getOnerror } from '@ember/-internals/error-handling';
export default class ApplicationRoute extends Route {
@action
error(err) {
const onerror = getOnerror();
if (onerror) {
onerror(err);
}
}
}
`,
'slow.js': `import Route from '@ember/routing/route';
export default class SlowRoute extends Route {
model({ timeout, type }) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (type === 'reject') {
let error = new Error('slow route rejected after '.concat(timeout));
error.code = 'from-slow';
reject(error);
} else {
resolve('slow route rejected after '.concat(timeout));
}
}, timeout);
});
}
}`,
},
},
});
})
.forEachScenario((scenario) => {
Qmodule(scenario.name, function (hooks) {
let app; // PreparedApp
hooks.before(async () => {
app = await scenario.prepare();
});
test('errors can be properly attributed with buildSandboxPerVisit=true', async function (assert) {
let fastboot = await buildFastboot(app);
let first = fastboot.visit('/slow/100/reject', {
buildSandboxPerVisit: true,
request: { url: '/slow/100/reject', headers: {} },
});
let second = fastboot.visit('/slow/50/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/50/resolve', headers: {} },
});
let third = fastboot.visit('/slow/25/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/25/resolve', headers: {} },
});
await Promise.all([second, third]);
await first.then(
() => {
throw new Error('Visit should not resolve!');
},
(error) => {
assert.equal(error.code, 'from-slow');
assert.equal(
error.fastbootRequestPath,
'/slow/100/reject',
'fastbootRequestPath does not match'
);
}
);
});
test('it eagerly builds sandbox when queue is empty', async function (assert) {
let fastboot = await buildFastboot(app, {
maxSandboxQueueSize: 2,
});
let first = fastboot.visit('/slow/50/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/50/resolve', headers: {} },
});
let second = fastboot.visit('/slow/50/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/50/resolve', headers: {} },
});
let third = fastboot.visit('/slow/25/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/25/resolve', headers: {} },
});
let result = await first;
let analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: true,
});
result = await second;
analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: true,
});
result = await third;
analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: false,
});
});
test('it leverages sandbox from queue when present', async function (assert) {
let fastboot = await buildFastboot(app, {
maxSandboxQueueSize: 3,
});
let first = fastboot.visit('/slow/50/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/50/resolve', headers: {} },
});
let second = fastboot.visit('/slow/50/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/50/resolve', headers: {} },
});
let third = fastboot.visit('/slow/25/resolve', {
buildSandboxPerVisit: true,
request: { url: '/slow/25/resolve', headers: {} },
});
let result = await first;
let analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: true,
});
result = await second;
analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: true,
});
result = await third;
analytics = result.analytics;
assert.deepEqual(analytics, {
usedPrebuiltSandbox: true,
});
});
});
});