-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub_test.go
55 lines (52 loc) · 1.67 KB
/
hub_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
package main
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestSetPeerOnline(t *testing.T) {
startTest(t)
redisDouble.HSet("peer:A", "fp", "A", "name", "foo", "kind", "lay",
"user", "j", "verified", "1", "online", "0")
c := &Conn{User: "j", FP: "A"}
c.SetOnline(true)
require.Equal(t, "1", redisDouble.HGet("peer:A", "online"))
c.SetOnline(false)
require.Equal(t, "0", redisDouble.HGet("peer:A", "online"))
}
func TestPeersNotifications(t *testing.T) {
startTest(t)
redisDouble.SetAdd("userset:j", "A", "B")
redisDouble.HSet("user:j", "email", "j@example.com")
redisDouble.HSet("peer:A", "fp", "A", "name", "foo", "kind", "lay",
"user", "j", "verified", "1")
redisDouble.HSet("peer:B", "fp", "B", "name", "bar", "kind", "lay",
"user", "j", "verified", "0")
redisDouble.SAdd("userset:j", "A", "B")
server := httptest.NewServer(http.HandlerFunc(rcHandler))
defer server.Close()
os.Setenv("REVENUECAT_URL", server.URL)
wsA, _, err := openWS("ws://127.0.0.1:17777/ws?fp=A&name=foo&kind=lay&uid=j")
defer wsA.Close()
wsB, _, err := openWS("ws://127.0.0.1:17777/ws?fp=B&name=bar&kind=lay&uid=j")
require.Nil(t, err)
defer wsB.Close()
if err := wsA.SetReadDeadline(time.Now().Add(time.Second / 100)); err != nil {
t.Fatalf("SetReadDeadline: %v", err)
}
var i map[string]interface{}
err = wsA.ReadJSON(&i)
require.Nil(t, err)
_, found := i["peers"]
require.True(t, found, "No peers in: %v", i)
if err := wsB.SetReadDeadline(time.Now().Add(time.Second / 100)); err != nil {
t.Fatalf("SetReadDeadline: %v", err)
}
var s StatusMessage
err = wsB.ReadJSON(&s)
require.Nil(t, err)
require.Equal(t, 401, s.Code)
}