mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 23:16:53 +00:00
websocket: add SendMessageReturnResponse latency reporter (#1031)
* add websocket sync request latency reporter * add globalReporter similar to request package * gofmt * connection level reporter and test * fix SetupNewConnection
This commit is contained in:
@@ -778,6 +778,18 @@ func TestSendMessageWithResponse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type reporter struct {
|
||||
name string
|
||||
msg []byte
|
||||
t time.Duration
|
||||
}
|
||||
|
||||
func (r *reporter) Latency(name string, message []byte, t time.Duration) {
|
||||
r.name = name
|
||||
r.msg = message
|
||||
r.t = t
|
||||
}
|
||||
|
||||
// readMessages helper func
|
||||
func readMessages(t *testing.T, wc *WebsocketConnection) {
|
||||
t.Helper()
|
||||
@@ -1324,3 +1336,50 @@ func TestWebsocketConnectionShutdown(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLatency logic test
|
||||
func TestLatency(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := &reporter{}
|
||||
exch := "Kraken"
|
||||
wc := &WebsocketConnection{
|
||||
ExchangeName: exch,
|
||||
Verbose: true,
|
||||
URL: "wss://ws.kraken.com",
|
||||
ResponseMaxLimit: time.Second * 5,
|
||||
Match: NewMatch(),
|
||||
Reporter: r,
|
||||
}
|
||||
if wc.ProxyURL != "" && !useProxyTests {
|
||||
t.Skip("Proxy testing not enabled, skipping")
|
||||
}
|
||||
|
||||
err := wc.Dial(&dialer, http.Header{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go readMessages(t, wc)
|
||||
|
||||
request := testRequest{
|
||||
Event: "subscribe",
|
||||
Pairs: []string{currency.NewPairWithDelimiter("XBT", "USD", "/").String()},
|
||||
Subscription: testRequestData{
|
||||
Name: "ticker",
|
||||
},
|
||||
RequestID: wc.GenerateMessageID(false),
|
||||
}
|
||||
|
||||
_, err = wc.SendMessageReturnResponse(request.RequestID, request)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if r.t == 0 {
|
||||
t.Error("expected a nonzero duration, got zero")
|
||||
}
|
||||
|
||||
if r.name != exch {
|
||||
t.Errorf("expected %v, got %v", exch, r.name)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user