From e45370cf4771c97d4fe512215c12fca21f568c05 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 7 Apr 2023 11:32:14 -0300 Subject: [PATCH] add subscription test using the damus relay. --- subscription_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 subscription_test.go diff --git a/subscription_test.go b/subscription_test.go new file mode 100644 index 0000000..082d73a --- /dev/null +++ b/subscription_test.go @@ -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) + } +}