forked from smartcontractkit/wasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_server_mock.go
47 lines (40 loc) · 954 Bytes
/
ws_server_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package wasp
import (
"context"
"net/http"
"time"
"nhooyr.io/websocket/wsjson"
"nhooyr.io/websocket"
)
/* This is a naive WS mock server to check the tool performance */
type MockWSServer struct {
// Logf controls where logs are sent.
Logf func(f string, v ...interface{})
Sleep time.Duration
}
func (s MockWSServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
s.Logf("%v", err)
return
}
// nolint
defer c.Close(websocket.StatusInternalError, "")
for {
//nolint
err = constantAnswer(s.Sleep, c)
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
return
}
if err != nil {
s.Logf("failed to constantAnswer with %v: %v", r.RemoteAddr, err)
return
}
}
}
func constantAnswer(sleep time.Duration, c *websocket.Conn) error {
time.Sleep(sleep)
return wsjson.Write(context.Background(), c, map[string]string{
"answer": "epico!",
})
}