-
Notifications
You must be signed in to change notification settings - Fork 30
/
subscribe_test.go
161 lines (144 loc) · 3.45 KB
/
subscribe_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package pocketbase
import (
"net"
"net/http"
"testing"
"time"
"github.com/r--w/pocketbase/migrations"
"github.com/stretchr/testify/assert"
)
func TestCollection_Subscribe(t *testing.T) {
client := NewClient(defaultURL)
defaultBody := map[string]interface{}{
"field": "value_" + time.Now().Format(time.StampMilli),
}
collection := Collection[map[string]any]{client, migrations.PostsPublic}
stream, err := collection.Subscribe()
if err != nil {
t.Error(err)
return
}
defer stream.Unsubscribe()
<-stream.Ready()
ch := stream.Events()
t.Run("subscribe event: create", func(t *testing.T) {
resp, err := collection.Create(defaultBody)
if err != nil {
t.Error(err)
return
}
e := <-ch
assert.Equal(t, "create", e.Action)
assert.Equal(t, resp.ID, e.Record["id"])
})
t.Run("subscribe event: update", func(t *testing.T) {
resp, err := collection.Create(defaultBody)
if err != nil {
t.Error(err)
return
}
<-ch // ignore create event
body := map[string]interface{}{
"field": "value_" + time.Now().Format(time.StampMilli),
}
err = collection.Update(resp.ID, body)
if err != nil {
t.Error(err)
return
}
e := <-ch
assert.Equal(t, "update", e.Action)
assert.Equal(t, body["field"], e.Record["field"])
})
t.Run("subscribe event: delete", func(t *testing.T) {
resp, err := collection.Create(defaultBody)
if err != nil {
t.Error(err)
return
}
<-ch // ignore create event
err = collection.Delete(resp.ID)
if err != nil {
t.Error(err)
return
}
e := <-ch
assert.Equal(t, "delete", e.Action)
assert.Equal(t, resp.ID, e.Record["id"])
})
}
func TestCollection_Unsubscribe(t *testing.T) {
client := NewClient(defaultURL)
defaultBody := map[string]interface{}{
"field": "value_" + time.Now().Format(time.StampMilli),
}
collection := Collection[map[string]any]{client, migrations.PostsPublic}
stream, err := collection.Subscribe()
if err != nil {
t.Error(err)
return
}
<-stream.Ready()
ch := stream.Events()
resp, err := collection.Create(defaultBody)
if err != nil {
t.Error(err)
return
}
e := <-ch
assert.Equal(t, resp.ID, e.Record["id"])
stream.Unsubscribe()
if err := collection.Delete(resp.ID); err != nil {
t.Error(err)
return
}
if _, ok := <-ch; ok {
t.Error("unsubscribe is not working.")
}
}
func TestCollection_RealtimeReconnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping realtime reconnect in short mode")
return
}
client := NewClient(defaultURL)
transport := &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err == nil {
// Simulate pocketbase closing realtime connection after 5m of inactivity
time.AfterFunc(3*time.Second, func() { conn.Close() })
}
return conn, err
},
}
client.client.SetTransport(transport)
defaultBody := map[string]interface{}{
"field": "value_" + time.Now().Format(time.StampMilli),
}
collection := Collection[map[string]any]{client, migrations.PostsPublic}
stream, err := collection.Subscribe()
if err != nil {
t.Error(err)
return
}
defer stream.Unsubscribe()
<-stream.Ready()
var got = false
time.AfterFunc(13*time.Second, func() {
if _, err := collection.Create(defaultBody); err != nil {
t.Error(err)
}
})
time.AfterFunc(15*time.Second, func() {
defer stream.Unsubscribe()
if !got {
panic("stream reconnect is not working")
}
})
for range stream.Events() {
got = true
break
}
assert.Equal(t, true, got)
}