nested subscription test.
This commit is contained in:
@@ -6,9 +6,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// test if we can connect to wss://relay.damus.io and fetch a couple of random events
|
const RELAY = "wss://relay.damus.io"
|
||||||
|
|
||||||
|
// test if we can fetch a couple of random events
|
||||||
func TestSubscribe(t *testing.T) {
|
func TestSubscribe(t *testing.T) {
|
||||||
rl := mustRelayConnect("wss://relay.damus.io")
|
rl := mustRelayConnect(RELAY)
|
||||||
defer rl.Close()
|
defer rl.Close()
|
||||||
|
|
||||||
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Limit: 2}})
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Limit: 2}})
|
||||||
@@ -18,7 +20,7 @@ func TestSubscribe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
timeout := time.After(5 * time.Second)
|
timeout := time.After(5 * time.Second)
|
||||||
events := 0
|
n := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -26,7 +28,7 @@ func TestSubscribe(t *testing.T) {
|
|||||||
if event == nil {
|
if event == nil {
|
||||||
t.Errorf("event is nil: %v", event)
|
t.Errorf("event is nil: %v", event)
|
||||||
}
|
}
|
||||||
events++
|
n++
|
||||||
case <-sub.EndOfStoredEvents:
|
case <-sub.EndOfStoredEvents:
|
||||||
goto end
|
goto end
|
||||||
case <-rl.Context().Done():
|
case <-rl.Context().Done():
|
||||||
@@ -39,7 +41,62 @@ func TestSubscribe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if events != 2 {
|
if n != 2 {
|
||||||
t.Errorf("expected 2 events, got %d", events)
|
t.Errorf("expected 2 events, got %d", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test if we can do multiple nested subscriptions
|
||||||
|
func TestNestedSubscriptions(t *testing.T) {
|
||||||
|
rl := mustRelayConnect(RELAY)
|
||||||
|
defer rl.Close()
|
||||||
|
|
||||||
|
// fetch any note
|
||||||
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Limit: 1}})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("subscription 1 failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := time.After(5 * time.Second)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-sub.Events:
|
||||||
|
// now fetch author of this event
|
||||||
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{0}, Authors: []string{event.PubKey}, Limit: 1}})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("subscription 2 failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sub.Events:
|
||||||
|
// now mentions of this person
|
||||||
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Tags: TagMap{"p": []string{event.PubKey}}, Limit: 1}})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("subscription 3 failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sub.Events:
|
||||||
|
// if we get here safely we won
|
||||||
|
return
|
||||||
|
case <-timeout:
|
||||||
|
t.Errorf("timeout 3")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-timeout:
|
||||||
|
t.Errorf("timeout 2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-rl.Context().Done():
|
||||||
|
t.Errorf("connection closed: %v", rl.Context().Err())
|
||||||
|
case <-timeout:
|
||||||
|
t.Errorf("timeout 1")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user