-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track current Sarus state in state machine #404
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
738b105
Track current connection state internally
justuswilhelm 5f908d1
Add state property
justuswilhelm 3be37fc
Set Sarus state property in connect()/etc. methods
justuswilhelm 63690e7
Remove "created" state
justuswilhelm 9578a8b
Add state transition cycle test cases
justuswilhelm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// File Dependencies | ||
import Sarus from "../../src/index"; | ||
import { WS } from "jest-websocket-mock"; | ||
import { delay } from "../helpers/delay"; | ||
|
||
const url: string = "ws://localhost:1234"; | ||
const sarusConfig = { | ||
url, | ||
retryConnectionDelay: 1, | ||
}; | ||
|
||
describe("state machine", () => { | ||
it("cycles through a closed connection correctly", async () => { | ||
let server: WS = new WS(url); | ||
|
||
// In the beginning, the state is "connecting" | ||
const sarus: Sarus = new Sarus(sarusConfig); | ||
expect(sarus.state).toBe("connecting"); | ||
|
||
// We wait until we are connected, and see a "connected" state | ||
await server.connected; | ||
expect(sarus.state).toBe("connected"); | ||
|
||
// When the connection drops, the state will be "closed" | ||
server.close(); | ||
await server.closed; | ||
expect(sarus.state).toBe("closed"); | ||
|
||
// Restart server | ||
server = new WS(url); | ||
|
||
// We wait a while, and the status is "connecting" again | ||
await delay(1); | ||
expect(sarus.state).toBe("connecting"); | ||
|
||
// When we connect in our mock server, we are "connected" again | ||
await server.connected; | ||
expect(sarus.state).toBe("connected"); | ||
|
||
// Cleanup | ||
server.close(); | ||
}); | ||
|
||
it("cycles through disconnect() correctly", async () => { | ||
let server: WS = new WS(url); | ||
|
||
// Same initial state transition as above | ||
const sarus: Sarus = new Sarus(sarusConfig); | ||
expect(sarus.state).toBe("connecting"); | ||
await server.connected; | ||
expect(sarus.state).toBe("connected"); | ||
|
||
// The user can disconnect and the state will be "disconnected" | ||
sarus.disconnect(); | ||
expect(sarus.state).toBe("disconnected"); | ||
await server.closed; | ||
|
||
// The user can now reconnect, and the state will be "connecting", and then | ||
// "connected" again | ||
sarus.connect(); | ||
expect(sarus.state).toBe("connecting"); | ||
await server.connected; | ||
// XXX for some reason the test will fail without waiting 10 ms here | ||
await delay(10); | ||
expect(sarus.state).toBe("connected"); | ||
server.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a function in Hub's repo called
delayUntil
which will delay until a condition is met:https://github.com/anephenix/hub/blob/master/helpers/delay.js
We could potentially import that function into the helpers/delay.ts file and use this code instead of delaying for 10 miliseconds:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it would be better to not have to hardcode a duration here. Unfortunately I do not understand the websocket mock well enough. It was my understanding that awaiting server.connected should already cover the required waiting. I don't see how awaiting any more than that is changing the sarus.state (e.g. onopen being called).
Some leaky abstraction perhaps? If you have any ideas, please let me know.