forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_test.go
161 lines (132 loc) · 3.42 KB
/
subscription_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 nostr
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/net/websocket"
)
const RELAY = "wss://nos.lol"
// test if we can fetch a couple of random events
func TestSubscribeBasic(t *testing.T) {
t.Parallel()
rl := mustRelayConnect(t, RELAY)
defer rl.Close()
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}})
require.NoError(t, err)
timeout := time.After(5 * time.Second)
n := 0
for {
select {
case event := <-sub.Events:
require.NotNil(t, event)
n++
case <-sub.EndOfStoredEvents:
goto end
case <-rl.Context().Done():
t.Fatalf("connection closed: %v", rl.Context().Err())
goto end
case <-timeout:
t.Fatalf("timeout")
goto end
}
}
end:
require.Equal(t, 2, n)
}
// test if we can do multiple nested subscriptions
func TestNestedSubscriptions(t *testing.T) {
t.Parallel()
t.Skip("not compatible with the current Nostr version")
rl := mustRelayConnect(t, RELAY)
defer rl.Close()
n := atomic.Uint32{}
// fetch 2 replies to a note
sub, err := rl.Subscribe(context.Background(),
Filters{
{
Kinds: []int{KindTextNote},
Tags: TagMap{}.SetLiterals("e", "0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"),
Limit: 3,
}})
require.NoError(t, err)
for {
select {
case event := <-sub.Events:
// now fetch author of this
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindProfileMetadata}, Authors: []string{event.PubKey}, Limit: 1}})
require.NoError(t, err)
for {
select {
case <-sub.Events:
// do another subscription here in "sync" mode, just so we're sure things are not blocking
rl.QuerySync(context.Background(), Filter{Limit: 1})
n.Add(1)
if n.Load() == 3 {
// if we get here it means the test passed
return
}
case <-sub.Context.Done():
goto end
case <-sub.EndOfStoredEvents:
sub.Unsub()
}
}
end:
fmt.Println("")
case <-sub.EndOfStoredEvents:
sub.Unsub()
return
case <-sub.Context.Done():
t.Fatalf("connection closed: %v", rl.Context().Err())
return
}
}
}
func TestSubscribeWithExternalSignature(t *testing.T) {
t.Parallel()
ch := make(chan struct{})
ws := newWebsocketServer(func(conn *websocket.Conn) {
var req ReqEnvelope
err := websocket.JSON.Receive(conn, &req)
require.NoError(t, err)
t.Logf("received subscription request: %v", req)
var e = EventEnvelope{
SubscriptionID: &req.SubscriptionID,
Events: []*Event{
{
Kind: KindTextNote,
Content: "hello",
},
},
}
err = websocket.JSON.Send(conn, &e)
require.NoError(t, err)
<-ch
})
defer ws.Close()
var didCheck atomic.Bool
rl := mustRelayConnect(t, ws.URL, WithSignatureChecker(func(event *Event) bool {
t.Logf("checking signature for event: %v", event)
didCheck.Store(true)
return true
}))
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 1}})
require.NoError(t, err)
select {
case event := <-sub.Events:
require.NotNil(t, event)
require.Equal(t, KindTextNote, event.Kind)
ok, err := event.CheckSignature() // Must be invalid because we didn't sign it.
require.Error(t, err)
require.False(t, ok)
require.True(t, didCheck.Load())
close(ch)
case <-sub.EndOfStoredEvents:
sub.Unsub()
case <-sub.Context.Done():
t.Fatalf("connection closed: %v", rl.Context().Err())
}
}