microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/extensionMessaging.test.ts
124lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {HostPlatform} from "../../common/hostPlatform"; |
| 5 | import {Log} from "../../common/log/log"; |
| 6 | |
| 7 | import { |
| 8 | ExtensionMessage, |
| 9 | ExtensionMessageSender |
| 10 | } from "../../common/extensionMessaging"; |
| 11 | |
| 12 | import * as assert from "assert"; |
| 13 | import * as net from "net"; |
| 14 | import * as sinon from "sinon"; |
| 15 | import * as Q from "q"; |
| 16 | |
| 17 | suite("extensionMessaging", function() { |
| 18 | suite("commonContext", function() { |
| 19 | teardown(function() { |
| 20 | if (HostPlatform.getExtensionPipePath.hasOwnProperty("restore")) { |
| 21 | (<any>HostPlatform.getExtensionPipePath).restore(); |
| 22 | } |
| 23 | }); |
| 24 | |
| 25 | test("should successfully send a message", function(done: MochaDone) { |
| 26 | let port: number = 8081; |
| 27 | let mockServer: net.Server = net.createServer(function(client: net.Socket): void { |
| 28 | mockServer.close(); |
| 29 | client.on("data", function(data: Buffer) { |
| 30 | let messageData: any = JSON.parse(data.toString("utf8")); |
| 31 | client.end(); |
| 32 | |
| 33 | assert.equal(messageData.message, ExtensionMessage.START_PACKAGER); |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | mockServer.on("error", done); |
| 38 | |
| 39 | mockServer.listen(port, function(): void { |
| 40 | Log.logMessage("Mock server listening on port: " + port); |
| 41 | }); |
| 42 | |
| 43 | sinon.stub(HostPlatform, "getExtensionPipePath", function() { |
| 44 | return port; |
| 45 | }); |
| 46 | |
| 47 | const sender = new ExtensionMessageSender(); |
| 48 | |
| 49 | Q({}) |
| 50 | .then(function() { |
| 51 | return sender.sendMessage(ExtensionMessage.START_PACKAGER); |
| 52 | }).done(() => done(), done); |
| 53 | }); |
| 54 | |
| 55 | test("should successfully send a message with args", function(done: MochaDone) { |
| 56 | let port: number = 8081; |
| 57 | let args = ["android"]; |
| 58 | let mockServer: net.Server = net.createServer(function(client: net.Socket): void { |
| 59 | mockServer.close(); |
| 60 | client.on("data", function(data: Buffer) { |
| 61 | let messageData: any = JSON.parse(data.toString("utf8")); |
| 62 | client.end(); |
| 63 | |
| 64 | assert.equal(messageData.message, ExtensionMessage.PREWARM_BUNDLE_CACHE); |
| 65 | assert.deepEqual(messageData.args, args); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | mockServer.on("error", done); |
| 70 | |
| 71 | mockServer.listen(port, function(): void { |
| 72 | Log.logMessage("Mock server listening on port: " + port); |
| 73 | }); |
| 74 | |
| 75 | sinon.stub(HostPlatform, "getExtensionPipePath", function() { |
| 76 | return port; |
| 77 | }); |
| 78 | |
| 79 | const sender = new ExtensionMessageSender(); |
| 80 | |
| 81 | Q({}) |
| 82 | .then(function() { |
| 83 | return sender.sendMessage(ExtensionMessage.PREWARM_BUNDLE_CACHE, args); |
| 84 | }).done(() => done(), done); |
| 85 | }); |
| 86 | |
| 87 | test("should reject on socket error", function(done: MochaDone) { |
| 88 | let port: number = 8081; |
| 89 | let mockServer: net.Server = net.createServer(function(client: net.Socket): void { |
| 90 | mockServer.close(); |
| 91 | client.on("data", function(data: Buffer) { |
| 92 | // client.emit("error"); |
| 93 | // client.end(); |
| 94 | throw "Blah"; |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | mockServer.on("error", done); |
| 99 | |
| 100 | mockServer.listen(port, function(): void { |
| 101 | Log.logMessage("Mock server listening on port: " + port); |
| 102 | }); |
| 103 | |
| 104 | sinon.stub(HostPlatform, "getExtensionPipePath", function() { |
| 105 | return port; |
| 106 | }); |
| 107 | |
| 108 | const sender = new ExtensionMessageSender(); |
| 109 | |
| 110 | Q({}) |
| 111 | .then(function() { |
| 112 | return sender.sendMessage(ExtensionMessage.PREWARM_BUNDLE_CACHE); |
| 113 | }) |
| 114 | .then(function() { |
| 115 | assert.fail("sendMessage should reject on socket error"); |
| 116 | }) |
| 117 | .catch(function(reason: any) { |
| 118 | let expectedErrorMessage = "An error ocurred while handling message: PREWARM_BUNDLE_CACHE"; |
| 119 | assert.equal(reason.message, expectedErrorMessage); |
| 120 | }) |
| 121 | .done(() => done(), done); |
| 122 | }); |
| 123 | }); |
| 124 | }); |