/
basic-protocol.ts
369 lines (331 loc) · 13.4 KB
/
basic-protocol.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"use strict";
import { expect } from "chai";
import { X_OK } from "constants";
import * as fs from "fs";
import { createLineBreakpointData, createSectionBreakpointData, SourceMessage } from "../src/messages";
import { BreakpointType as BT, SteppingType as ST } from "./somns-support";
import { ControllerWithInitialBreakpoints, expectSourceCoordinate, expectStack, HandleStoppedAndGetStackTrace, PING_PONG_URI, SOM, TestConnection, getCmd } from "./test-setup";
let connectionPossible = false;
function onlyWithConnection(fn) {
return function() {
if (connectionPossible) {
return fn();
} else {
this.skip();
}
};
}
describe("Basic Project Setup", () => {
describe("SOMns is testable", () => {
it("SOMns executable should be in SOM_BASEPATH", (done) => {
fs.access(SOM, X_OK, (err) => {
expect(err).to.be.null;
done();
});
});
it("should be possible to connect", done => {
const conn = new TestConnection();
conn.fullyConnected.then(_ => {
conn.close(done);
connectionPossible = true;
});
conn.fullyConnected.catch(reason => {
done(reason);
});
});
});
});
describe("Basic Protocol", function() {
let conn: TestConnection;
const closeConnectionAfterSuite = (done) => {
conn.fullyConnected.then(_ => { conn.close(done); });
conn.fullyConnected.catch(reason => done(reason));
};
describe("source message", () => {
let sourceP: Promise<SourceMessage>;
before("Start SOMns and Connect", () => {
conn = new TestConnection();
const ctrl = new ControllerWithInitialBreakpoints([], conn);
let firstSourceCaptured = false;
sourceP = new Promise<SourceMessage>((resolve, reject) => {
conn.fullyConnected.catch(reject);
ctrl.onReceivedSource = (msg: SourceMessage) => {
if (firstSourceCaptured) { return; };
firstSourceCaptured = true;
resolve(msg);
};
});
});
after(closeConnectionAfterSuite);
it("should have sources", onlyWithConnection(() => {
return sourceP.then(sourceMsg => {
const source = sourceMsg.source;
expect(source.mimeType).to.equal("application/x-newspeak-som-ns");
expect(source.name).to.equal("Platform.ns");
expect(source).to.have.property("sourceText");
expect(source).to.have.property("uri");
});
}));
it("should have source sections", onlyWithConnection(() => {
return sourceP.then(sourceMsg => {
const s = sourceMsg.source;
for (let ss of s.sections) {
expectSourceCoordinate(ss);
return;
}
});
}));
it("should have methods", onlyWithConnection(() => {
return sourceP.then(sourceMsg => {
const s = sourceMsg.source;
for (let method of s.methods) {
expect(method).to.have.property("name");
expect(method).to.have.property("definition");
const def = method.definition[0];
expectSourceCoordinate(def);
expectSourceCoordinate(method.sourceSection);
return;
}
});
}));
});
const breakpointTests = {
"setting a line breakpoint":
[{
test: "accept line breakpoint, and halt on expected line",
breakpoint: createLineBreakpointData(PING_PONG_URI, 92, true),
stackLength: 6,
topMethod: "PingPong>>#benchmark",
line: 92
}],
"setting a source section sender breakpoint":
[{
test: "accept send breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 23, 14, 3, BT.MSG_SENDER, true),
stackLength: 2,
topMethod: "Ping>>#ping",
line: 23
}],
"setting a source section asynchronous method before execution breakpoint":
[{
test: "accept async method before execution breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 22, 9, 203, BT.ASYNC_MSG_BEFORE_EXEC, true),
stackLength: 1,
topMethod: "Ping>>#ping",
line: 22
}],
"setting a source section asynchronous message after execution breakpoint":
[{
test: "accept async message after execution breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 69, 9, 87, BT.ASYNC_MSG_AFTER_EXEC, true),
stackLength: 1,
topMethod: "Pong>>#ping:",
line: 69
}],
"setting a source section receiver breakpoint":
[{
test: "accept send breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 23, 14, 3, BT.MSG_RECEIVER, true),
stackLength: 1,
topMethod: "Pong>>#ping:",
line: 69
}],
"setting a source section promise resolver breakpoint":
[{
test: "for normal resolution, accept promise resolver breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 95, 16, 3, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Ping>>#start",
line: 16
},
{
test: "for null resolution, accept promise resolver breakpoint for null resolution, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 23, 14, 3, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Pong>>#ping:",
line: 69
},
{
test: "for chained resolution, accept promise resolver breakpoint for chained resolution, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 26, 19, 3, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Ping>>#validate:",
line: 33
},
{
test: "on unresolved explicit promise, accept promise resolver breakpoint on unresolved explicit promise, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 92, 29, 17, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Pong>>#stop",
line: 80
},
{
test: "on resolved explicit promise, accept promise resolution breakpoint on resolved explicit promise, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 41, 21, 17, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Thing>>#println",
line: 71
},
{
test: "on resolved explicit promise, accept promise resolver breakpoint on resolved explicit promise, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 41, 21, 17, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Ping>>#validNumber:",
line: 49
},
{
test: "on whenResolved, accept promise resolver breakpoint on whenResolved, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 27, 17, 32, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Ping>>#λping@27@31:",
line: 27
},
{
test: "onError, accept promise resolver breakpoint onError, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 78, 18, 50, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Pong>>#λstop@78@27:",
line: 78
},
{
test: "whenResolvedOnError, accept promise resolver breakpoint whenResolvedOnError, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 34, 17, 91, BT.PROMISE_RESOLVER, true),
stackLength: 1,
topMethod: "Ping>>#λvalidate@34@77:",
line: 34
}],
"setting a source section promise resolution breakpoint":
[{
test: "for normal resolution, accept promise resolution breakpoint, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 95, 16, 3, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Thing>>#println",
line: 71
},
{
test: "for chained resolution, accept promise resolution breakpoint for chained resolution, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 26, 19, 3, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Ping>>#λping@27@31:",
line: 27
},
{
test: "on unresolved explicit promise, accept promise resolution breakpoint on unresolved explicit promise, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 92, 29, 17, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "PingPong>>#λbenchmark@97@44:",
line: 98
},
{
test: "on whenResolved, accept promise resolution breakpoint on whenResolved, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 27, 17, 32, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Thing>>#println",
line: 71
},
{
test: "onError, accept promise resolution breakpoint onError, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 78, 18, 50, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Thing>>#println",
line: 71
},
{
test: "whenResolvedOnError, accept promise resolution breakpoint on whenResolvedOnError, and halt on expected source section",
breakpoint: createSectionBreakpointData(PING_PONG_URI, 34, 17, 91, BT.PROMISE_RESOLUTION, true),
stackLength: 1,
topMethod: "Thing>>#println",
line: 71
}]
};
for (const suiteName in breakpointTests) {
const suite = breakpointTests[suiteName];
describe(suiteName, () => {
suite.forEach(testDesc => {
describe("should", () => {
let ctrl: HandleStoppedAndGetStackTrace;
before("Start SOMns and Connect", () => {
conn = new TestConnection();
ctrl = new HandleStoppedAndGetStackTrace([testDesc.breakpoint], conn, conn.fullyConnected);
});
after(closeConnectionAfterSuite);
afterEach(function() {
if (this.currentTest.state === "failed") {
console.log("Failed test: " + suiteName);
console.log("To reproduce the issue:")
console.log("\tcmd: " + getCmd());
console.log("\tbp: " + JSON.stringify(testDesc.breakpoint));
}
});
it(testDesc.test, onlyWithConnection(() => {
return ctrl.stackPs[0].then(msg => {
expectStack(msg.stackFrames, testDesc.stackLength, testDesc.topMethod, testDesc.line);
});
}));
});
});
});
}
describe("single stepping", () => {
// Capture suspended events
let ctrl: HandleStoppedAndGetStackTrace;
before("Start SOMns and Connect", () => {
const breakpoint = createSectionBreakpointData(PING_PONG_URI, 23, 14, 3,
BT.MSG_SENDER, true);
conn = new TestConnection();
ctrl = new HandleStoppedAndGetStackTrace([breakpoint], conn, conn.fullyConnected, 4);
});
after(closeConnectionAfterSuite);
it("should stop initially at breakpoint", onlyWithConnection(() => {
return ctrl.stackPs[0].then(msg => {
expectStack(msg.stackFrames, 2, "Ping>>#ping", 23);
});
}));
it("should single stepping", onlyWithConnection(() => {
return new Promise((resolve, _reject) => {
ctrl.stackPs[0].then(_ => {
conn.fullyConnected.then(_ => {
conn.sendDebuggerAction(ST.STEP_INTO, ctrl.stoppedActivities[0]);
});
const p = ctrl.stackPs[1].then(msgAfterStep => {
expectStack(msgAfterStep.stackFrames, 2, "Ping>>#ping", 24);
});
resolve(p);
});
});
}));
it("should be possible to dynamically activate line breakpoints",
onlyWithConnection(() => {
return Promise.all([
ctrl.stackPs[1].then(_ => {
conn.fullyConnected.then(_ => {
// set another breakpoint, after stepping, and with connection
const lbp = createLineBreakpointData(PING_PONG_URI, 22, true);
conn.updateBreakpoint(lbp);
conn.sendDebuggerAction(ST.RESUME, ctrl.stoppedActivities[1]);
});
}),
ctrl.stackPs[2].then(msgLineBP => {
expectStack(msgLineBP.stackFrames, 1, "Ping>>#ping", 22);
})]);
}));
it("should be possible to disable a line breakpoint",
onlyWithConnection(() => {
return new Promise((resolve, _reject) => {
ctrl.stackPs[2].then(_ => {
conn.fullyConnected.then(_ => {
const lbp23 = createLineBreakpointData(PING_PONG_URI, 23, true);
conn.updateBreakpoint(lbp23);
const lbp22 = createLineBreakpointData(PING_PONG_URI, 22, false);
conn.updateBreakpoint(lbp22);
conn.sendDebuggerAction(ST.RESUME, ctrl.stoppedActivities[2]);
const p = ctrl.stackPs[3].then(msgLineBP => {
expectStack(msgLineBP.stackFrames, 1, "Ping>>#ping", 23);
});
resolve(p);
});
});
});
}));
});
});