Skip to content

Commit 2f79805

Browse files
committed
test: fix livesync tool tests comments
1 parent 26f0926 commit 2f79805

File tree

5 files changed

+187
-183
lines changed

5 files changed

+187
-183
lines changed

lib/definitions/livesync.d.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ interface IAndroidNativeScriptDeviceLiveSyncService extends INativeScriptDeviceL
395395
finalizeSync(liveSyncInfo: ILiveSyncResultInfo, projectData: IProjectData): Promise<IAndroidLivesyncSyncOperationResult>;
396396
}
397397

398+
interface ILiveSyncSocket extends INetSocket {
399+
uid: string,
400+
writeAsync(data: Buffer): Promise<Boolean>
401+
}
402+
398403
interface IAndroidLivesyncTool {
399404
/**
400405
* The protocol version the current app(adnroid runtime) is using.
@@ -430,21 +435,19 @@ interface IAndroidLivesyncTool {
430435
* @param filePath - The full path to the file.
431436
* @returns {Promise<boolean>}
432437
*/
433-
removeFile(filePath: string): Promise<boolean>;
438+
removeFile(filePath: string): Promise<void>;
434439
/**
435440
* Removes files
436441
* @param filePaths - Array of files that will be removed.
437442
* @returns {Promise<boolean[]>}
438443
*/
439-
removeFiles(filePaths: string[]): Promise<boolean[]>;
444+
removeFiles(filePaths: string[]): Promise<void[]>;
440445
/**
441446
* Sends doSyncOperation that will be handled by the runtime.
442-
* @param doRefresh - Indicates if the application should be restarted. Defaults to true.
443-
* @param operationId - The identifier of the operation
444-
* @param timeout - The timeout in milliseconds
447+
* @param options
445448
* @returns {Promise<void>}
446449
*/
447-
sendDoSyncOperation(doRefresh: boolean, timeout?: number, operationId?: string): Promise<IAndroidLivesyncSyncOperationResult>;
450+
sendDoSyncOperation(options?: IDoSyncOperationOptions): Promise<IAndroidLivesyncSyncOperationResult>;
448451
/**
449452
* Generates new operation identifier.
450453
*/
@@ -467,6 +470,17 @@ interface IAndroidLivesyncTool {
467470
hasConnection(): boolean;
468471
}
469472

473+
/**
474+
* doRefresh - Indicates if the application should be refreshed. Defaults to true.
475+
* operationId - The identifier of the operation
476+
* timeout - The timeout in milliseconds
477+
*/
478+
interface IDoSyncOperationOptions {
479+
doRefresh?: boolean,
480+
timeout?: number,
481+
operationId?: string
482+
}
483+
470484
interface IAndroidLivesyncToolConfiguration {
471485
/**
472486
* The application identifier.

lib/services/livesync/android-device-livesync-sockets-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
5959

6060
if (liveSyncInfo.modifiedFilesData.length) {
6161
const canExecuteFastSync = !liveSyncInfo.isFullSync && this.canExecuteFastSyncForPaths(liveSyncInfo, liveSyncInfo.modifiedFilesData, projectData, this.device.deviceInfo.platform);
62-
const doSyncPromise = this.livesyncTool.sendDoSyncOperation(canExecuteFastSync, null, operationId);
62+
const doSyncPromise = this.livesyncTool.sendDoSyncOperation({ doRefresh: canExecuteFastSync, operationId});
6363

6464
const syncInterval: NodeJS.Timer = setInterval(() => {
6565
if (this.livesyncTool.isOperationInProgress(operationId)) {

lib/services/livesync/android-livesync-tool.ts

+49-57
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
1919
public static OPERATION_END_NO_REFRESH_REPORT_CODE = 3;
2020
public static DO_REFRESH = 1;
2121
public static SKIP_REFRESH = 0;
22+
public static APP_IDENTIFIER_MISSING_ERROR = 'You need to provide "appIdentifier" as a configuration property!';
23+
public static APP_PLATFORMS_PATH_MISSING_ERROR = 'You need to provide "appPlatformsPath" as a configuration property!';
24+
public static SOCKET_CONNECTION_ALREADY_EXISTS_ERROR = "Socket connection already exists.";
25+
public static SOCKET_CONNECTION_TIMED_OUT_ERROR = "Socket connection timed out.";
26+
public static NO_SOCKET_CONNECTION_AVAILABLE_ERROR = "No socket connection available.";
2227
public protocolVersion: string;
2328
private operationPromises: IDictionary<any>;
2429
private socketError: string | Error;
25-
private socketConnection: INetSocket;
30+
private socketConnection: ILiveSyncSocket;
2631
private configuration: IAndroidLivesyncToolConfiguration;
2732
private pendingConnectionData: {
2833
connectionTimer?: NodeJS.Timer,
@@ -46,15 +51,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
4651

4752
public async connect(configuration: IAndroidLivesyncToolConfiguration): Promise<void> {
4853
if (!configuration.appIdentifier) {
49-
this.$errors.fail(`You need to provide "appIdentifier" as a configuration property!`);
54+
this.$errors.fail(AndroidLivesyncTool.APP_IDENTIFIER_MISSING_ERROR);
5055
}
5156

5257
if (!configuration.appPlatformsPath) {
53-
this.$errors.fail(`You need to provide "appPlatformsPath" as a configuration property!`);
58+
this.$errors.fail(AndroidLivesyncTool.APP_PLATFORMS_PATH_MISSING_ERROR);
5459
}
5560

5661
if (this.socketConnection) {
57-
this.$errors.fail("Socket connection already exists.");
62+
this.$errors.fail(AndroidLivesyncTool.SOCKET_CONNECTION_ALREADY_EXISTS_ERROR);
5863
}
5964

6065
if (!configuration.localHostAddress) {
@@ -98,9 +103,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
98103
return this.sendFiles(list);
99104
}
100105

101-
public removeFile(filePath: string): Promise<boolean> {
102-
return new Promise((resolve: Function, reject: Function) => {
103-
this.verifyActiveConnection(reject);
106+
public async removeFile(filePath: string): Promise<void> {
107+
this.verifyActiveConnection();
104108
const filePathData = this.getFilePathData(filePath);
105109
const headerBuffer = Buffer.alloc(PROTOCOL_OPERATION_LENGTH_SIZE +
106110
SIZE_BYTE_LENGTH +
@@ -114,11 +118,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
114118
headerBuffer.write(filePathData.relativeFilePath, offset, filePathData.filePathLengthBytes);
115119
const hash = crypto.createHash("md5").update(headerBuffer).digest();
116120

117-
this.socketConnection.write(headerBuffer);
118-
this.socketConnection.write(hash, () => {
119-
resolve(true);
120-
});
121-
});
121+
await this.writeToSocket(headerBuffer);
122+
await this.writeToSocket(hash);
122123
}
123124

124125
public removeFiles(files: string[]) {
@@ -133,10 +134,14 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
133134
return !!this.operationPromises[operationId];
134135
}
135136

136-
public sendDoSyncOperation(doRefresh = true , timeout?: number, operationId?: string): Promise<IAndroidLivesyncSyncOperationResult> {
137+
public sendDoSyncOperation(options?: IDoSyncOperationOptions): Promise<IAndroidLivesyncSyncOperationResult> {
138+
options = _.assign({ doRefresh: true, timeout: SYNC_OPERATION_TIMEOUT }, options);
139+
const { doRefresh , timeout, operationId } = options;
137140
const id = operationId || this.generateOperationIdentifier();
138-
const operationPromise: Promise<IAndroidLivesyncSyncOperationResult> = new Promise((resolve: Function, reject: Function) => {
139-
this.verifyActiveConnection(reject);
141+
const operationPromise: Promise<IAndroidLivesyncSyncOperationResult> = new Promise((resolve, reject) => {
142+
if (!this.verifyActiveConnection(reject)) {
143+
return;
144+
}
140145
const message = `${AndroidLivesyncTool.DO_SYNC_OPERATION}${id}`;
141146
const headerBuffer = Buffer.alloc(Buffer.byteLength(message) + DO_REFRESH_LENGTH);
142147
const socketId = this.socketConnection.uid;
@@ -145,11 +150,10 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
145150

146151
headerBuffer.writeUInt8(doRefreshCode, offset);
147152
const hash = crypto.createHash("md5").update(headerBuffer).digest();
153+
this.writeToSocket(headerBuffer).then(() => {
154+
this.writeToSocket(hash).catch(reject);
155+
}).catch(reject);
148156

149-
this.socketConnection.write(headerBuffer);
150-
this.socketConnection.write(hash);
151-
152-
timeout = timeout || SYNC_OPERATION_TIMEOUT;
153157
const timeoutId = setTimeout(() => {
154158
if (this.isOperationInProgress(id)) {
155159
this.handleSocketError(socketId, "Sync operation is taking too long");
@@ -186,10 +190,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
186190
return !!this.socketConnection;
187191
}
188192

189-
private sendFileHeader(filePath: string): Promise<void> {
190-
return new Promise((resolve, reject) => {
191-
let error;
192-
this.verifyActiveConnection(reject);
193+
private async sendFileHeader(filePath: string): Promise<void> {
194+
this.verifyActiveConnection();
193195
const filePathData = this.getFilePathData(filePath);
194196
const stats = this.$fs.getFsStats(filePathData.filePath);
195197
const fileContentLengthBytes = stats.size;
@@ -203,13 +205,9 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
203205
fileContentLengthSize);
204206

205207
if (filePathData.filePathLengthSize > 255) {
206-
error = this.getErrorWithMessage("File name size is longer that 255 digits.");
208+
this.$errors.failWithoutHelp("File name size is longer that 255 digits.");
207209
} else if (fileContentLengthSize > 255) {
208-
error = this.getErrorWithMessage("File name size is longer that 255 digits.");
209-
}
210-
211-
if (error) {
212-
reject(error);
210+
this.$errors.failWithoutHelp("File name size is longer that 255 digits.");
213211
}
214212

215213
let offset = 0;
@@ -221,56 +219,42 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
221219
headerBuffer.write(fileContentLengthString, offset, fileContentLengthSize);
222220
const hash = crypto.createHash("md5").update(headerBuffer).digest();
223221

224-
this.socketConnection.write(headerBuffer);
225-
this.socketConnection.write(hash);
226-
resolve();
227-
});
222+
await this.writeToSocket(headerBuffer);
223+
await this.writeToSocket(hash);
228224
}
229225

230226
private sendFileContent(filePath: string): Promise<boolean> {
231227
return new Promise((resolve, reject) => {
232-
this.verifyActiveConnection(reject);
228+
if (!this.verifyActiveConnection(reject)) {
229+
return;
230+
}
231+
233232
const fileStream = this.$fs.createReadStream(filePath);
234233
const fileHash = crypto.createHash("md5");
235234

236235
fileStream
237-
.on("data", (chunk: string | Buffer) => {
236+
.on("data", (chunk: Buffer) => {
238237
fileHash.update(chunk);
239-
if (this.socketConnection) {
240-
this.socketConnection.write(chunk);
241-
} else {
242-
const error = this.checkConnectionStatus();
243-
//TODO Destroy method added in node 8.0.0.
244-
//when we deprecate node 6.x uncomment the line below
245-
//fileStream.destroy(error);
246-
reject(error);
247-
}
238+
this.writeToSocket(chunk).catch(reject);
248239
})
249240
.on("end", () => {
250-
if (this.socketConnection) {
251-
this.socketConnection.write(fileHash.digest(), () => {
252-
resolve(true);
253-
});
254-
} else {
255-
const error = this.checkConnectionStatus();
256-
reject(error);
257-
}
241+
this.writeToSocket(fileHash.digest()).then(() => resolve()).catch(reject);
258242
})
259243
.on("error", (error: Error) => {
260244
reject(error);
261245
});
262246
});
263247
}
264248

265-
private createSocket(port: number): INetSocket {
249+
private createSocket(port: number): ILiveSyncSocket {
266250
const socket = this.$injector.resolve("LiveSyncSocket");
267251
socket.connect(port, this.configuration.localHostAddress);
268252
return socket;
269253
}
270254

271255
private checkConnectionStatus() {
272256
if (this.socketConnection === null) {
273-
const defaultError = this.getErrorWithMessage("No socket connection available.");
257+
const defaultError = this.getErrorWithMessage(AndroidLivesyncTool.NO_SOCKET_CONNECTION_AVAILABLE_ERROR);
274258
const error = this.socketError || defaultError;
275259

276260
return error;
@@ -286,9 +270,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
286270
if (error && !rejectHandler) {
287271
this.$errors.failWithoutHelp(error.toString());
288272
}
273+
274+
return true;
289275
}
290276

291-
private handleConnection({ socket, data }: { socket: INetSocket, data: NodeBuffer | string }) {
277+
private handleConnection({ socket, data }: { socket: ILiveSyncSocket, data: NodeBuffer | string }) {
292278
this.socketConnection = socket;
293279
this.socketConnection.uid = this.generateOperationIdentifier();
294280

@@ -313,7 +299,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
313299
});
314300
}
315301

316-
private connectEventuallyUntilTimeout(factory: () => INetSocket, timeout: number): Promise<{socket: INetSocket, data: NodeBuffer | string}> {
302+
private connectEventuallyUntilTimeout(factory: () => ILiveSyncSocket, timeout: number): Promise<{socket: ILiveSyncSocket, data: NodeBuffer | string}> {
317303
return new Promise((resolve, reject) => {
318304
let lastKnownError: Error | string,
319305
isConnected = false;
@@ -325,7 +311,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
325311
clearTimeout(this.pendingConnectionData.socketTimer);
326312
}
327313

328-
reject(lastKnownError || new Error("Socket connection timed out."));
314+
reject(lastKnownError || new Error(AndroidLivesyncTool.SOCKET_CONNECTION_TIMED_OUT_ERROR));
329315
this.pendingConnectionData = null;
330316
}
331317
}, timeout);
@@ -478,5 +464,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
478464
clearTimeout(operationPromise.timeoutId);
479465
});
480466
}
467+
468+
private async writeToSocket(data: Buffer): Promise<Boolean> {
469+
this.verifyActiveConnection();
470+
const result = await this.socketConnection.writeAsync(data);
471+
return result;
472+
}
481473
}
482474
$injector.register("androidLivesyncTool", AndroidLivesyncTool);
+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import * as net from "net";
22

3-
class LiveSyncSocket extends net.Socket {
3+
export class LiveSyncSocket extends net.Socket implements ILiveSyncSocket {
44
public uid: string;
5+
public writeAsync (data: Buffer): Promise<Boolean> {
6+
return new Promise((resolve, reject) => {
7+
8+
const result: Boolean = this.write(data, () => resolve(result));
9+
});
10+
}
511
}
612

713
$injector.register("LiveSyncSocket", LiveSyncSocket, false);

0 commit comments

Comments
 (0)