Skip to content

Commit 665e6ec

Browse files
committed
[TS Client] Test message size
1 parent 16be9a2 commit 665e6ec

File tree

4 files changed

+200
-18
lines changed

4 files changed

+200
-18
lines changed

src/SignalR/clients/ts/signalr/src/HubConnection.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,22 @@ export class HubConnection {
814814
const invocationId = this.invocationId;
815815
this.invocationId++;
816816

817-
return {
818-
arguments: args,
819-
invocationId: invocationId.toString(),
820-
streamIds,
821-
target: methodName,
822-
type: MessageType.Invocation,
823-
};
817+
if (streamIds.length !== 0) {
818+
return {
819+
arguments: args,
820+
invocationId: invocationId.toString(),
821+
streamIds,
822+
target: methodName,
823+
type: MessageType.Invocation,
824+
};
825+
} else {
826+
return {
827+
arguments: args,
828+
invocationId: invocationId.toString(),
829+
target: methodName,
830+
type: MessageType.Invocation,
831+
};
832+
}
824833
}
825834
}
826835

@@ -889,13 +898,22 @@ export class HubConnection {
889898
const invocationId = this.invocationId;
890899
this.invocationId++;
891900

892-
return {
893-
arguments: args,
894-
invocationId: invocationId.toString(),
895-
streamIds,
896-
target: methodName,
897-
type: MessageType.StreamInvocation,
898-
};
901+
if (streamIds.length !== 0) {
902+
return {
903+
arguments: args,
904+
invocationId: invocationId.toString(),
905+
streamIds,
906+
target: methodName,
907+
type: MessageType.StreamInvocation,
908+
};
909+
} else {
910+
return {
911+
arguments: args,
912+
invocationId: invocationId.toString(),
913+
target: methodName,
914+
type: MessageType.StreamInvocation,
915+
};
916+
}
899917
}
900918

901919
private createCancelInvocation(id: string): CancelInvocationMessage {

src/SignalR/clients/ts/signalr/src/IHubProtocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface InvocationMessage extends HubInvocationMessage {
6565
/** The target method arguments. */
6666
readonly arguments: any[];
6767
/** The target methods stream IDs. */
68-
readonly streamIds: string[];
68+
readonly streamIds?: string[];
6969
}
7070

7171
/** A hub message representing a streaming invocation. */
@@ -80,7 +80,7 @@ export interface StreamInvocationMessage extends HubInvocationMessage {
8080
/** The target method arguments. */
8181
readonly arguments: any[];
8282
/** The target methods stream IDs. */
83-
readonly streamIds: string[];
83+
readonly streamIds?: string[];
8484
}
8585

8686
/** A hub message representing a single item produced as part of a result stream. */

src/SignalR/clients/ts/signalr/tests/HubConnection.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ describe("HubConnection", () => {
226226
42,
227227
],
228228
invocationId: connection.lastInvocationId,
229-
streamIds: [],
230229
target: "testMethod",
231230
type: MessageType.Invocation,
232231
});
@@ -977,7 +976,6 @@ describe("HubConnection", () => {
977976
42,
978977
],
979978
invocationId: connection.lastInvocationId,
980-
streamIds: [],
981979
target: "testStream",
982980
type: MessageType.StreamInvocation,
983981
});
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)