mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 13:22:56 +01:00
add subscription test using the damus relay.
This commit is contained in:
parent
304b0a0477
commit
e45370cf47
45
subscription_test.go
Normal file
45
subscription_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// test if we can connect to wss://relay.damus.io and fetch a couple of random events
|
||||
func TestSubscribe(t *testing.T) {
|
||||
rl := mustRelayConnect("wss://relay.damus.io")
|
||||
defer rl.Close("")
|
||||
|
||||
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Limit: 2}})
|
||||
if err != nil {
|
||||
t.Errorf("subscription failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
timeout := time.After(5 * time.Second)
|
||||
events := 0
|
||||
|
||||
for {
|
||||
select {
|
||||
case event := <-sub.Events:
|
||||
if event == nil {
|
||||
t.Errorf("event is nil: %v", event)
|
||||
}
|
||||
events++
|
||||
case <-sub.EndOfStoredEvents:
|
||||
goto end
|
||||
case <-rl.ConnectionContext.Done():
|
||||
t.Errorf("connection closed: %v", rl.ConnectionContext.Err())
|
||||
goto end
|
||||
case <-timeout:
|
||||
t.Errorf("timeout")
|
||||
goto end
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if events != 2 {
|
||||
t.Errorf("expected 2 events, got %d", events)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user