Skip to content

Commit d2624b5

Browse files
fstasiAlberto Iannaccone
authored and
Alberto Iannaccone
committed
serial-service tests
1 parent 4e92f26 commit d2624b5

File tree

6 files changed

+281
-440
lines changed

6 files changed

+281
-440
lines changed

Diff for: arduino-ide-extension/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
"test:watch": "mocha --watch --watch-files lib \"./lib/test/**/*.test.js\""
2020
},
2121
"dependencies": {
22-
"arduino-serial-plotter-webapp": "0.0.15",
2322
"@grpc/grpc-js": "^1.3.7",
2423
"@theia/application-package": "1.19.0",
2524
"@theia/core": "1.19.0",
2625
"@theia/editor": "1.19.0",
27-
"@theia/editor-preview": "1.19.0",
26+
"@theia/editor-preview": "1.19.0",
2827
"@theia/filesystem": "1.19.0",
2928
"@theia/git": "1.19.0",
3029
"@theia/keymaps": "1.19.0",
@@ -53,10 +52,10 @@
5352
"@types/ps-tree": "^1.1.0",
5453
"@types/react-select": "^3.0.0",
5554
"@types/react-tabs": "^2.3.2",
56-
"@types/sinon": "^7.5.2",
5755
"@types/temp": "^0.8.34",
5856
"@types/which": "^1.3.1",
5957
"ajv": "^6.5.3",
58+
"arduino-serial-plotter-webapp": "0.0.15",
6059
"async-mutex": "^0.3.0",
6160
"atob": "^2.1.2",
6261
"auth0-js": "^9.14.0",
@@ -97,6 +96,8 @@
9796
"@types/chai-string": "^1.4.2",
9897
"@types/mocha": "^5.2.7",
9998
"@types/react-window": "^1.8.5",
99+
"@types/sinon": "^10.0.6",
100+
"@types/sinon-chai": "^3.2.6",
100101
"chai": "^4.2.0",
101102
"chai-string": "^1.5.0",
102103
"decompress": "^4.2.0",
@@ -109,7 +110,8 @@
109110
"moment": "^2.24.0",
110111
"protoc": "^1.0.4",
111112
"shelljs": "^0.8.3",
112-
"sinon": "^9.0.1",
113+
"sinon": "^12.0.1",
114+
"sinon-chai": "^3.7.0",
113115
"typemoq": "^2.1.0",
114116
"uuid": "^3.2.1",
115117
"yargs": "^11.1.0"

Diff for: arduino-ide-extension/src/browser/serial/serial-connection-manager.ts

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ export class SerialConnectionManager {
169169
this.messageService.error(
170170
`Please select a board and a port to open the serial connection.`
171171
);
172-
return;
173172
}
174173

175174
if (!this.webSocket && this.wsPort) {

Diff for: arduino-ide-extension/src/node/serial/serial-service-impl.ts

+44-25
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ namespace ErrorWithCode {
6363

6464
@injectable()
6565
export class SerialServiceImpl implements SerialService {
66-
@named(SerialServiceName)
67-
@inject(ILogger)
68-
protected readonly logger: ILogger;
69-
70-
@inject(MonitorClientProvider)
71-
protected readonly serialClientProvider: MonitorClientProvider;
72-
73-
@inject(WebSocketService)
74-
protected readonly webSocketService: WebSocketService;
75-
7666
protected theiaFEClient?: SerialServiceClient;
7767
protected serialConfig?: SerialConfig;
7868

@@ -88,6 +78,18 @@ export class SerialServiceImpl implements SerialService {
8878

8979
uploadInProgress = false;
9080

81+
constructor(
82+
@inject(ILogger)
83+
@named(SerialServiceName)
84+
protected readonly logger: ILogger,
85+
86+
@inject(MonitorClientProvider)
87+
protected readonly serialClientProvider: MonitorClientProvider,
88+
89+
@inject(WebSocketService)
90+
protected readonly webSocketService: WebSocketService
91+
) {}
92+
9193
async isSerialPortOpen(): Promise<boolean> {
9294
return !!this.serialConnection;
9395
}
@@ -115,7 +117,6 @@ export class SerialServiceImpl implements SerialService {
115117
public async connectSerialIfRequired(): Promise<void> {
116118
if (this.uploadInProgress) return;
117119
const clients = await this.clientsAttached();
118-
this.logger.info(`WS clients: ${clients}`);
119120
clients > 0 ? await this.connect() : await this.disconnect();
120121
}
121122

@@ -144,7 +145,7 @@ export class SerialServiceImpl implements SerialService {
144145
this.webSocketService.sendMessage(JSON.stringify(msg));
145146
}
146147

147-
async connect(): Promise<Status> {
148+
private async connect(): Promise<Status> {
148149
if (!this.serialConfig) {
149150
return Status.CONFIG_MISSING;
150151
}
@@ -155,8 +156,6 @@ export class SerialServiceImpl implements SerialService {
155156
)} on port ${Port.toString(this.serialConfig.port)}...`
156157
);
157158

158-
// check if the board/port is available
159-
160159
if (this.serialConnection) {
161160
return Status.ALREADY_CONNECTED;
162161
}
@@ -187,7 +186,6 @@ export class SerialServiceImpl implements SerialService {
187186
// Log the original, unexpected error.
188187
this.logger.error(error);
189188
}
190-
// });
191189
}).bind(this)
192190
);
193191

@@ -259,9 +257,19 @@ export class SerialServiceImpl implements SerialService {
259257
}
260258
req.setConfig(monitorConfig);
261259

262-
return new Promise<Status>((resolve) => {
263-
if (this.serialConnection) {
264-
this.serialConnection.duplex.write(req, () => {
260+
if (!this.serialConnection) {
261+
return await this.disconnect();
262+
}
263+
264+
const writeTimeout = new Promise<Status>((resolve) => {
265+
setTimeout(async () => {
266+
resolve(Status.NOT_CONNECTED);
267+
}, 1000);
268+
});
269+
270+
const writePromise = (serialConnection: any) => {
271+
return new Promise<Status>((resolve) => {
272+
serialConnection.duplex.write(req, () => {
265273
const boardName = this.serialConfig?.board
266274
? Board.toString(this.serialConfig.board, {
267275
useFqbn: false,
@@ -276,14 +284,23 @@ export class SerialServiceImpl implements SerialService {
276284
);
277285
resolve(Status.OK);
278286
});
279-
return;
280-
}
281-
this.disconnect().then(() => resolve(Status.NOT_CONNECTED));
282-
});
287+
});
288+
};
289+
290+
const status = await Promise.race([
291+
writeTimeout,
292+
writePromise(this.serialConnection),
293+
]);
294+
295+
if (status === Status.NOT_CONNECTED) {
296+
this.disconnect();
297+
}
298+
299+
return status;
283300
}
284301

285302
public async disconnect(reason?: SerialError): Promise<Status> {
286-
return new Promise<Status>((resolve, reject) => {
303+
return new Promise<Status>((resolve) => {
287304
try {
288305
if (this.onMessageReceived) {
289306
this.onMessageReceived.dispose();
@@ -299,12 +316,14 @@ export class SerialServiceImpl implements SerialService {
299316
reason &&
300317
reason.code === SerialError.ErrorCodes.CLIENT_CANCEL
301318
) {
302-
return Status.OK;
319+
resolve(Status.OK);
320+
return;
303321
}
304322
this.logger.info('>>> Disposing serial connection...');
305323
if (!this.serialConnection) {
306324
this.logger.warn('<<< Not connected. Nothing to dispose.');
307-
return Status.NOT_CONNECTED;
325+
resolve(Status.NOT_CONNECTED);
326+
return;
308327
}
309328
const { duplex, config } = this.serialConnection;
310329

0 commit comments

Comments
 (0)