Allow starting connection only once
This commit is contained in:
parent
abafae8cd3
commit
7944be712f
|
|
@ -27,4 +27,70 @@ describe("Connection", () => {
|
|||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it("cannot start a running connection", async (done) => {
|
||||
let options: ISignalROptions = {
|
||||
httpClient: <IHttpClient>{
|
||||
get(url: string): Promise<string> {
|
||||
if (url.indexOf("/negotiate") >= 0) {
|
||||
connection.start()
|
||||
.then(() => {
|
||||
fail();
|
||||
done();
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
expect(error.message).toBe("Cannot start a connection that is not in the 'Initial' state.");
|
||||
done();
|
||||
});
|
||||
|
||||
return Promise.reject("error");
|
||||
}
|
||||
return Promise.resolve("");
|
||||
}
|
||||
}
|
||||
} as ISignalROptions;
|
||||
|
||||
let connection = new Connection("http://tempuri.org", undefined, options);
|
||||
|
||||
try {
|
||||
await connection.start();
|
||||
}
|
||||
catch (e) {
|
||||
// This exception is thrown after the actual verification is completed.
|
||||
// The connection is not setup to be running so just ignore the error.
|
||||
}
|
||||
});
|
||||
|
||||
it("cannot start a stopped connection", async (done) => {
|
||||
let options: ISignalROptions = {
|
||||
httpClient: <IHttpClient>{
|
||||
get(url: string): Promise<string> {
|
||||
if (url.indexOf("/negotiate") >= 0) {
|
||||
return Promise.reject("error");
|
||||
}
|
||||
return Promise.resolve("");
|
||||
}
|
||||
}
|
||||
} as ISignalROptions;
|
||||
|
||||
let connection = new Connection("http://tempuri.org", undefined, options);
|
||||
|
||||
try {
|
||||
// start will fail and transition the connection to the Disconnected state
|
||||
await connection.start();
|
||||
}
|
||||
catch (e) {
|
||||
// The connection is not setup to be running so just ignore the error.
|
||||
}
|
||||
|
||||
try {
|
||||
await connection.start();
|
||||
fail();
|
||||
done();
|
||||
}
|
||||
catch (e) {
|
||||
expect(e.message).toBe("Cannot start a connection that is not in the 'Initial' state.");
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ import { IHttpClient, HttpClient } from "./HttpClient"
|
|||
import { ISignalROptions } from "./ISignalROptions"
|
||||
|
||||
enum ConnectionState {
|
||||
Disconnected,
|
||||
Initial,
|
||||
Connecting,
|
||||
Connected
|
||||
Connected,
|
||||
Disconnected
|
||||
}
|
||||
|
||||
export class Connection implements IConnection {
|
||||
|
|
@ -22,14 +23,16 @@ export class Connection implements IConnection {
|
|||
this.url = url;
|
||||
this.queryString = queryString;
|
||||
this.httpClient = options.httpClient || new HttpClient();
|
||||
this.connectionState = ConnectionState.Disconnected;
|
||||
this.connectionState = ConnectionState.Initial;
|
||||
}
|
||||
|
||||
async start(transportName: string = "webSockets"): Promise<void> {
|
||||
if (this.connectionState != ConnectionState.Disconnected) {
|
||||
throw new Error("Cannot start a connection that is not in the 'Disconnected' state");
|
||||
if (this.connectionState != ConnectionState.Initial) {
|
||||
throw new Error("Cannot start a connection that is not in the 'Initial' state.");
|
||||
}
|
||||
|
||||
this.connectionState = ConnectionState.Connecting;
|
||||
|
||||
this.transport = this.createTransport(transportName);
|
||||
this.transport.onDataReceived = this.onDataReceived;
|
||||
this.transport.onClosed = e => this.stopConnection(e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue