-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace close() with actually sending a value to .EndOfStoredEvents a…
…nd .Closed channels. I thought `close()` would be nice because it would be cheap and not lock the goroutine while waiting for the receiver to acknowledge the thing, but turns out it introduces the serious risk of users putting <- sub.EndOfStoredEvents in the same for { select {} } statement as sub.Events, for example, and they they get into an infinite loop. we had this same problem here inside this same library, and what is fixed in 242af0b by @mattn.
- Loading branch information
Showing
2 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters