-
Notifications
You must be signed in to change notification settings - Fork 758
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
WebSockets #1251
Open
tabaktoni
wants to merge
31
commits into
develop
Choose a base branch
from
feat/ws
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,347
−5,202
Open
WebSockets #1251
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9f14d77
feat: ws init setup
tabaktoni efe7ce3
feat: init ws and test
tabaktoni 3dcb47e
feat: subscribe, unsubscribe, waitForDisconnection, test flow for new…
tabaktoni f217192
feat: subscribeNewHeads done and tested
tabaktoni 21e3f82
feat: test standard endpoint, update wait methods with statuses
tabaktoni 923fa25
feat: transaction status and pending transactions
tabaktoni 16c261a
fix: missing file
tabaktoni fc9e9b9
feat: onUnsubscribe, make bulk test works
tabaktoni 9da3ab0
feat: beta types-js release and import, on methods data type definiti…
tabaktoni 4bcc486
feat: docs, extended tests, events map, types
tabaktoni 38a99bc
feat: manage subscriptions by map
tabaktoni a14a66e
test: onUnsubscribe, docs, cleanup
tabaktoni 3db410a
fix: cleanup
tabaktoni bb95fec
Merge branch 'develop' into feat/ws
tabaktoni c27f80e
fix: tipo and test
tabaktoni 6dbe82e
Update __tests__/WebSocketChannel.test.ts
tabaktoni 9daddc7
Update src/channel/ws_0_8.ts
tabaktoni b084c59
Update src/channel/ws_0_8.ts
tabaktoni 45448b0
Merge branch 'develop' into feat/ws
tabaktoni 4e195c3
fix: a SUBSCRIPTION_RESULT, bump types beta.2, debug tests
tabaktoni 1ebf37d
Merge branch 'temp/ws' into feat/ws
tabaktoni 7ecc217
Merge branch 'develop' into feat/ws
tabaktoni e28ccec
chore: starknet-types to starknet-types-08
tabaktoni 6b35a2d
Merge branch 'temp/ws' into feat/ws
tabaktoni 61b88e0
docs: websocket channel docs init
tabaktoni 10c2e87
docs: websocket channel docs init
tabaktoni d394574
fix: block_id and txStatus event name
tabaktoni ebb0d8a
feat: add types-beta-4, add T check on socket methods
tabaktoni 1a9a1fd
Merge branch 'develop' into feat/ws
tabaktoni 8fcf1a5
test: stabilize websocket tests (#1272)
penovicp ecf6290
feat: onReorg
tabaktoni 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,171 @@ | ||
import { WebSocket } from 'isows'; | ||
|
||
import { Provider, WSSubscriptions, WebSocketChannel } from '../src'; | ||
import { StarknetChainId } from '../src/constants'; | ||
import { getTestAccount, getTestProvider } from './config/fixtures'; | ||
|
||
describe('websocket specific endpoints - pathfinder test', () => { | ||
// account provider | ||
const provider = new Provider(getTestProvider()); | ||
const account = getTestAccount(provider); | ||
|
||
// websocket | ||
const webSocketChannel = new WebSocketChannel({ | ||
nodeUrl: 'wss://toni.spaceshard.io/rpc/v0_8', | ||
}); | ||
|
||
beforeAll(async () => { | ||
expect(webSocketChannel.isConnected()).toBe(false); | ||
await webSocketChannel.waitForConnection(); | ||
expect(webSocketChannel.isConnected()).toBe(true); | ||
}); | ||
|
||
test('Test WS Error and edge cases', async () => { | ||
webSocketChannel.disconnect(); | ||
|
||
// should fail as disconnected | ||
await expect(webSocketChannel.subscribeNewHeads()).rejects.toThrow(); | ||
|
||
// should reconnect | ||
webSocketChannel.reconnect(); | ||
await webSocketChannel.waitForConnection(); | ||
|
||
// should succeed after reconnection | ||
await expect(webSocketChannel.subscribeNewHeads()).resolves.toEqual(expect.any(Number)); | ||
|
||
// should fails because already subscribed | ||
tabaktoni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await expect(webSocketChannel.subscribeNewHeads()).resolves.toBe(false); | ||
}); | ||
|
||
test('onUnsubscribe with unsubscribeNewHeads', async () => { | ||
const mockOnUnsubscribe = jest.fn().mockImplementation((subId: number) => { | ||
expect(subId).toEqual(expect.any(Number)); | ||
}); | ||
webSocketChannel.onUnsubscribe = mockOnUnsubscribe; | ||
|
||
await webSocketChannel.subscribeNewHeads(); | ||
await expect(webSocketChannel.unsubscribeNewHeads()).resolves.toBe(true); | ||
await expect(webSocketChannel.unsubscribeNewHeads()).rejects.toThrow(); | ||
|
||
expect(mockOnUnsubscribe).toHaveBeenCalled(); | ||
expect(webSocketChannel.subscriptions.has(WSSubscriptions.NEW_HEADS)).toBeFalsy(); | ||
}); | ||
|
||
test('Test subscribeNewHeads', async () => { | ||
await webSocketChannel.subscribeNewHeads(); | ||
|
||
let i = 0; | ||
webSocketChannel.onsNewHeads = async function (data) { | ||
expect(this).toBeInstanceOf(WebSocketChannel); | ||
i += 1; | ||
ivpavici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO : Add data format validation | ||
expect(data.result).toBeDefined(); | ||
if (i === 6) { | ||
const status = await webSocketChannel.unsubscribeNewHeads(); | ||
expect(status).toBe(true); | ||
} | ||
}; | ||
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.NEW_HEADS); | ||
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId); | ||
expect(subscriptionId).toBe(expectedId); | ||
expect(webSocketChannel.subscriptions.get(WSSubscriptions.NEW_HEADS)).toBe(undefined); | ||
}); | ||
|
||
test('Test subscribeEvents', async () => { | ||
await webSocketChannel.subscribeEvents(); | ||
|
||
let i = 0; | ||
webSocketChannel.onEvents = async (data) => { | ||
i += 1; | ||
// TODO : Add data format validation | ||
expect(data.result).toBeDefined(); | ||
if (i === 2) { | ||
const status = await webSocketChannel.unsubscribeEvents(); | ||
expect(status).toBe(true); | ||
} | ||
}; | ||
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.EVENTS); | ||
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId); | ||
expect(subscriptionId).toBe(expectedId); | ||
expect(webSocketChannel.subscriptions.get(WSSubscriptions.EVENTS)).toBe(undefined); | ||
}); | ||
|
||
test('Test subscribePendingTransaction', async () => { | ||
await webSocketChannel.subscribePendingTransaction(true); | ||
|
||
let i = 0; | ||
webSocketChannel.onPendingTransaction = async (data) => { | ||
i += 1; | ||
// TODO : Add data format validation | ||
expect(data.result).toBeDefined(); | ||
if (i === 5) { | ||
const status = await webSocketChannel.unsubscribePendingTransaction(); | ||
expect(status).toBe(true); | ||
} | ||
}; | ||
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.PENDING_TRANSACTION); | ||
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId); | ||
expect(subscriptionId).toBe(expectedId); | ||
expect(webSocketChannel.subscriptions.get(WSSubscriptions.PENDING_TRANSACTION)).toBe(undefined); | ||
}); | ||
|
||
test('Test subscribeTransactionStatus', async () => { | ||
const { transaction_hash } = await account.execute({ | ||
contractAddress: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d', | ||
entrypoint: 'transfer', | ||
calldata: [account.address, '10', '0'], | ||
}); | ||
|
||
await webSocketChannel.subscribeTransactionStatus(transaction_hash); | ||
|
||
let i = 0; | ||
webSocketChannel.onTransactionStatus = async (data) => { | ||
i += 1; | ||
// TODO : Add data format validation | ||
expect(data.result).toBeDefined(); | ||
if (i === 2) { | ||
const status = await webSocketChannel.unsubscribeTransactionStatus(); | ||
expect(status).toBe(true); | ||
} | ||
}; | ||
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS); | ||
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId); | ||
expect(subscriptionId).toEqual(expectedId); | ||
expect(webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS)).toBe(undefined); | ||
}); | ||
|
||
test('disconnect', async () => { | ||
expect(webSocketChannel.isConnected()).toBe(true); | ||
webSocketChannel.disconnect(); | ||
await expect(webSocketChannel.waitForDisconnection()).resolves.toBe(WebSocket.CLOSED); | ||
}); | ||
|
||
afterAll(async () => { | ||
webSocketChannel.disconnect(); | ||
const status = await webSocketChannel.waitForDisconnection(); | ||
expect(status).toBe(WebSocket.CLOSED); | ||
}); | ||
}); | ||
|
||
describe('websocket regular endpoints - pathfinder test', () => { | ||
const webSocketChannel = new WebSocketChannel({ | ||
nodeUrl: 'wss://toni.spaceshard.io/rpc/v0_8', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flagging here as well for later, also maybe to extract into constant |
||
}); | ||
|
||
beforeAll(async () => { | ||
expect(webSocketChannel.isConnected()).toBe(false); | ||
const status = await webSocketChannel.waitForConnection(); | ||
expect(status).toBe(WebSocket.OPEN); | ||
}); | ||
|
||
test('regular rpc endpoint', async () => { | ||
const response = await webSocketChannel.sendReceive('starknet_chainId'); | ||
expect(response).toBe(StarknetChainId.SN_SEPOLIA); | ||
}); | ||
|
||
afterAll(async () => { | ||
webSocketChannel.disconnect(); | ||
const status = await webSocketChannel.waitForDisconnection(); | ||
expect(status).toBe(WebSocket.CLOSED); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
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.
flagging this to not forget to change later :D