Skip to content

Commit

Permalink
Websocket: Improve GenerateMessageID test
Browse files Browse the repository at this point in the history
Testing just the last id doesn't feel very robust
  • Loading branch information
gbjk committed Feb 4, 2024
1 parent 0d21cf6 commit 67be848
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion exchanges/stream/websocket_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (w *WebsocketConnection) parseBinaryResponse(resp []byte) ([]byte, error) {
return standardMessage, reader.Close()
}

// GenerateMessageID Creates a messageID to checkout
// GenerateMessageID Creates a random message ID
func (w *WebsocketConnection) GenerateMessageID(highPrec bool) int64 {
var min int64 = 1e8
var max int64 = 2e8
Expand Down
13 changes: 6 additions & 7 deletions exchanges/stream/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,12 @@ func TestCanUseAuthenticatedWebsocketForWrapper(t *testing.T) {
func TestGenerateMessageID(t *testing.T) {
t.Parallel()
wc := WebsocketConnection{}
var id int64
for i := 0; i < 10; i++ {
newID := wc.GenerateMessageID(true)
if id == newID {
t.Fatal("ID generation is not unique")
}
id = newID
const spins = 1000
ids := make([]int64, spins)
for i := 0; i < spins; i++ {
id := wc.GenerateMessageID(true)
assert.NotContains(t, ids, id, "GenerateMessageID must not generate the same ID twice")
ids[i] = id
}
}

Expand Down

0 comments on commit 67be848

Please sign in to comment.