diff --git a/eose_test.go b/eose_test.go new file mode 100644 index 0000000..2815a8e --- /dev/null +++ b/eose_test.go @@ -0,0 +1,52 @@ +package nostr + +import ( + "context" + "testing" + "time" +) + +func TestEOSEMadness(t *testing.T) { + rl := mustRelayConnect(RELAY) + defer rl.Close() + + sub, err := rl.Subscribe(context.Background(), Filters{ + {Kinds: []int{KindTextNote}, Limit: 2}, + }) + if err != nil { + t.Errorf("subscription failed: %v", err) + return + } + + timeout := time.After(3 * time.Second) + n := 0 + e := 0 + + for { + select { + case event := <-sub.Events: + if event == nil { + t.Fatalf("event is nil: %v", event) + } + n++ + case <-sub.EndOfStoredEvents: + e++ + if e > 1 { + t.Fatalf("eose infinite loop") + } + continue + case <-rl.Context().Done(): + t.Fatalf("connection closed: %v", rl.Context().Err()) + case <-timeout: + goto end + } + } + +end: + if e != 1 { + t.Fatalf("didn't get an eose") + } + if n < 2 { + t.Fatalf("didn't get events") + } +} diff --git a/subscription.go b/subscription.go index 930bec9..ec858d3 100644 --- a/subscription.go +++ b/subscription.go @@ -106,15 +106,16 @@ func (sub *Subscription) dispatchEose() { if sub.eosed.CompareAndSwap(false, true) { go func() { sub.storedwg.Wait() - close(sub.EndOfStoredEvents) + sub.EndOfStoredEvents <- struct{}{} }() } } func (sub *Subscription) dispatchClosed(reason string) { if sub.closed.CompareAndSwap(false, true) { - sub.ClosedReason <- reason - close(sub.ClosedReason) + go func() { + sub.ClosedReason <- reason + }() } }