|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +import { HubConnection } from "../src/HubConnection"; |
| 5 | +import { IConnection } from "../src/IConnection"; |
| 6 | +import { IHubProtocol } from "../src/IHubProtocol"; |
| 7 | +import { ILogger } from "../src/ILogger"; |
| 8 | +import { JsonHubProtocol } from "../src/JsonHubProtocol"; |
| 9 | +import { NullLogger } from "../src/Loggers"; |
| 10 | +import { Subject } from "../src/Subject"; |
| 11 | +import { VerifyLogger } from "./Common"; |
| 12 | +import { TestConnection } from "./TestConnection"; |
| 13 | +import { delayUntil, registerUnhandledRejectionHandler } from "./Utils"; |
| 14 | + |
| 15 | +registerUnhandledRejectionHandler(); |
| 16 | + |
| 17 | +function createHubConnection(connection: IConnection, logger?: ILogger | null, protocol?: IHubProtocol | null) { |
| 18 | + return HubConnection.create(connection, logger || NullLogger.instance, protocol || new JsonHubProtocol()); |
| 19 | +} |
| 20 | + |
| 21 | +describe("Message size", () => { |
| 22 | + it("send invocation", async () => { |
| 23 | + await VerifyLogger.run(async (logger) => { |
| 24 | + const connection = new TestConnection(); |
| 25 | + |
| 26 | + const hubConnection = createHubConnection(connection, logger); |
| 27 | + try { |
| 28 | + // We don't actually care to wait for the send. |
| 29 | + // tslint:disable-next-line:no-floating-promises |
| 30 | + hubConnection.send("target", 1) |
| 31 | + .catch((_) => { }); // Suppress exception and unhandled promise rejection warning. |
| 32 | + |
| 33 | + // Verify the message is sent |
| 34 | + expect(connection.sentData.length).toBe(1); |
| 35 | + expect((connection.sentData[0] as string).length).toEqual(59); |
| 36 | + } finally { |
| 37 | + // Close the connection |
| 38 | + await hubConnection.stop(); |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("invoke invocation", async () => { |
| 44 | + await VerifyLogger.run(async (logger) => { |
| 45 | + const connection = new TestConnection(); |
| 46 | + |
| 47 | + const hubConnection = createHubConnection(connection, logger); |
| 48 | + try { |
| 49 | + // We don't actually care to wait for the invoke. |
| 50 | + // tslint:disable-next-line:no-floating-promises |
| 51 | + hubConnection.invoke("target", 1) |
| 52 | + .catch((_) => { }); // Suppress exception and unhandled promise rejection warning. |
| 53 | + |
| 54 | + // Verify the message is sent |
| 55 | + expect(connection.sentData.length).toBe(1); |
| 56 | + expect((connection.sentData[0] as string).length).toEqual(63); |
| 57 | + } finally { |
| 58 | + // Close the connection |
| 59 | + await hubConnection.stop(); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("stream invocation", async () => { |
| 65 | + await VerifyLogger.run(async (logger) => { |
| 66 | + const connection = new TestConnection(); |
| 67 | + |
| 68 | + const hubConnection = createHubConnection(connection, logger); |
| 69 | + try { |
| 70 | + hubConnection.stream("target", 1); |
| 71 | + |
| 72 | + // Verify the message is sent |
| 73 | + expect(connection.sentData.length).toBe(1); |
| 74 | + expect((connection.sentData[0] as string).length).toEqual(63); |
| 75 | + } finally { |
| 76 | + // Close the connection |
| 77 | + await hubConnection.stop(); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("upload invocation", async () => { |
| 83 | + await VerifyLogger.run(async (logger) => { |
| 84 | + const connection = new TestConnection(); |
| 85 | + |
| 86 | + const hubConnection = createHubConnection(connection, logger); |
| 87 | + try { |
| 88 | + // We don't actually care to wait for the invoke. |
| 89 | + // tslint:disable-next-line:no-floating-promises |
| 90 | + hubConnection.invoke("target", 1, new Subject()) |
| 91 | + .catch((_) => { }); // Suppress exception and unhandled promise rejection warning. |
| 92 | + |
| 93 | + // Verify the message is sent |
| 94 | + expect(connection.sentData.length).toBe(1); |
| 95 | + expect((connection.sentData[0] as string).length).toEqual(81); |
| 96 | + } finally { |
| 97 | + // Close the connection |
| 98 | + await hubConnection.stop(); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it("upload stream invocation", async () => { |
| 104 | + await VerifyLogger.run(async (logger) => { |
| 105 | + const connection = new TestConnection(); |
| 106 | + |
| 107 | + const hubConnection = createHubConnection(connection, logger); |
| 108 | + try { |
| 109 | + hubConnection.stream("target", 1, new Subject()); |
| 110 | + |
| 111 | + // Verify the message is sent |
| 112 | + expect(connection.sentData.length).toBe(1); |
| 113 | + expect((connection.sentData[0] as string).length).toEqual(81); |
| 114 | + } finally { |
| 115 | + // Close the connection |
| 116 | + await hubConnection.stop(); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("completion message", async () => { |
| 122 | + await VerifyLogger.run(async (logger) => { |
| 123 | + const connection = new TestConnection(); |
| 124 | + |
| 125 | + const hubConnection = createHubConnection(connection, logger); |
| 126 | + try { |
| 127 | + const subject = new Subject(); |
| 128 | + hubConnection.stream("target", 1, subject); |
| 129 | + subject.complete(); |
| 130 | + |
| 131 | + await delayUntil(1000, () => connection.sentData.length === 2); |
| 132 | + |
| 133 | + // Verify the message is sent |
| 134 | + expect(connection.sentData.length).toBe(2); |
| 135 | + expect((connection.sentData[1] as string).length).toEqual(29); |
| 136 | + } finally { |
| 137 | + // Close the connection |
| 138 | + await hubConnection.stop(); |
| 139 | + } |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it("cancel message", async () => { |
| 144 | + await VerifyLogger.run(async (logger) => { |
| 145 | + const connection = new TestConnection(); |
| 146 | + |
| 147 | + const hubConnection = createHubConnection(connection, logger); |
| 148 | + try { |
| 149 | + hubConnection.stream("target", 1).subscribe({ |
| 150 | + complete: () => {}, |
| 151 | + error: () => {}, |
| 152 | + next: () => {}, |
| 153 | + }).dispose(); |
| 154 | + |
| 155 | + await delayUntil(1000, () => connection.sentData.length === 2); |
| 156 | + |
| 157 | + // Verify the message is sent |
| 158 | + expect(connection.sentData.length).toBe(2); |
| 159 | + expect((connection.sentData[1] as string).length).toEqual(29); |
| 160 | + } finally { |
| 161 | + // Close the connection |
| 162 | + await hubConnection.stop(); |
| 163 | + } |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments