-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub_test.go
More file actions
66 lines (56 loc) · 1.38 KB
/
hub_test.go
File metadata and controls
66 lines (56 loc) · 1.38 KB
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
58
59
60
61
62
63
64
65
66
package jdb
import (
"testing"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/sirupsen/logrus"
)
func TestHub(t *testing.T) {
log := logrus.New()
log.Level = logrus.TraceLevel
var hub *Hub
t.Run("create", func(t *testing.T) {
hub = createInMemoryHub(t, log)
})
// Run hub routines on a separate goroutine
go hub.Run()
client := newMockClient(log)
t.Run("register client", func(t *testing.T) {
hub.register <- client
// Wait for hello or timeout
select {
case <-time.After(10 * time.Second):
t.Fatal("server took too long to take action")
case <-client.send:
}
if len(hub.clients.Clients()) < 1 {
t.Fatal("client not registered")
}
})
t.Run("unregister client", func(t *testing.T) {
hub.unregister <- client
// Wait for close or timeout
select {
case <-time.After(10 * time.Second):
t.Fatal("server took too long to take action")
case <-client.send:
}
if len(hub.clients.Clients()) > 0 {
t.Fatal("client not removed")
}
})
}
func createInMemoryHub(t *testing.T, log logrus.FieldLogger) *Hub {
// Open in-memory DB
options := badger.DefaultOptions("").WithInMemory(true).WithLogger(log)
db, err := badger.Open(options)
if err != nil {
t.Fatal("db initialization failed", err.Error())
}
// Create hub with in-mem DB
hub, err := NewHub(db, log)
if err != nil {
t.Fatal("hub initialization failed", err.Error())
}
return hub
}