forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_test.go
57 lines (45 loc) · 1.23 KB
/
socket_test.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
48
49
50
51
52
53
54
55
56
57
package provider
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/coder/websocket"
"github.com/stretchr/testify/require"
)
func TestSocketProvider(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
require.NoError(t, err)
defer c.Close(websocket.StatusNormalClosure, "")
uuids := []string{"foo", "bar"}
for i := 0; ; i++ {
json := fmt.Sprintf(`{"data":{"uuid":"%s","tuples":[[1682319567986,%d]]}}`, uuids[i%2], i%2)
if err := c.Write(ctx, websocket.MessageText, []byte(json)); err != nil {
require.NoError(t, err)
}
select {
case <-time.Tick(time.Millisecond):
case <-ctx.Done():
return
}
}
}))
defer srv.Close()
addr := "ws://" + srv.Listener.Addr().String()
p, err := NewSocketProviderFromConfig(map[string]any{
"uri": addr,
"jq": `.data | select(.uuid=="bar") .tuples[0][1]`,
})
require.NoError(t, err)
<-p.(*Socket).val.Done()
g, err := p.(IntProvider).IntGetter()
require.NoError(t, err)
i, err := g()
require.NoError(t, err)
require.Equal(t, int64(1), i)
}