2023-04-07 11:32:14 -03:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-25 00:17:57 -03:00
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
2023-04-07 11:32:14 -03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2024-09-09 13:50:56 +03:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-07 11:32:14 -03:00
|
|
|
)
|
|
|
|
|
2023-12-02 14:38:18 -03:00
|
|
|
const RELAY = "wss://nos.lol"
|
2023-06-24 21:02:12 -03:00
|
|
|
|
|
|
|
// test if we can fetch a couple of random events
|
2024-01-01 10:15:06 -03:00
|
|
|
func TestSubscribeBasic(t *testing.T) {
|
2024-09-09 13:50:56 +03:30
|
|
|
rl := mustRelayConnect(t, RELAY)
|
2023-04-11 07:10:05 -03:00
|
|
|
defer rl.Close()
|
2023-04-07 11:32:14 -03:00
|
|
|
|
2023-09-06 21:00:05 -03:00
|
|
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}})
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
2023-04-07 11:32:14 -03:00
|
|
|
|
|
|
|
timeout := time.After(5 * time.Second)
|
2023-06-24 21:02:12 -03:00
|
|
|
n := 0
|
2023-04-07 11:32:14 -03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-sub.Events:
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NotNil(t, event)
|
2023-06-24 21:02:12 -03:00
|
|
|
n++
|
2023-04-07 11:32:14 -03:00
|
|
|
case <-sub.EndOfStoredEvents:
|
|
|
|
goto end
|
2023-05-09 17:08:04 -03:00
|
|
|
case <-rl.Context().Done():
|
2024-01-01 10:15:06 -03:00
|
|
|
t.Fatalf("connection closed: %v", rl.Context().Err())
|
2023-04-07 11:32:14 -03:00
|
|
|
goto end
|
|
|
|
case <-timeout:
|
2024-01-01 10:15:06 -03:00
|
|
|
t.Fatalf("timeout")
|
2023-04-07 11:32:14 -03:00
|
|
|
goto end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.Equal(t, 2, n)
|
2023-06-24 21:02:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test if we can do multiple nested subscriptions
|
|
|
|
func TestNestedSubscriptions(t *testing.T) {
|
2024-09-09 13:50:56 +03:30
|
|
|
rl := mustRelayConnect(t, RELAY)
|
2023-06-24 21:02:12 -03:00
|
|
|
defer rl.Close()
|
|
|
|
|
2023-06-25 00:17:57 -03:00
|
|
|
n := atomic.Uint32{}
|
|
|
|
|
|
|
|
// fetch 2 replies to a note
|
2023-09-06 21:00:05 -03:00
|
|
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}})
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
2023-06-24 21:02:12 -03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-sub.Events:
|
2023-06-25 00:17:57 -03:00
|
|
|
// now fetch author of this
|
2023-10-31 11:00:46 -03:00
|
|
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindProfileMetadata}, Authors: []string{event.PubKey}, Limit: 1}})
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
2023-06-24 21:02:12 -03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sub.Events:
|
2023-06-25 00:17:57 -03:00
|
|
|
// do another subscription here in "sync" mode, just so we're sure things are not blocking
|
|
|
|
rl.QuerySync(context.Background(), Filter{Limit: 1})
|
2023-06-24 21:02:12 -03:00
|
|
|
|
2023-06-25 00:17:57 -03:00
|
|
|
n.Add(1)
|
|
|
|
if n.Load() == 3 {
|
|
|
|
// if we get here it means the test passed
|
|
|
|
return
|
2023-06-24 21:02:12 -03:00
|
|
|
}
|
2023-06-25 00:17:57 -03:00
|
|
|
case <-sub.Context.Done():
|
|
|
|
goto end
|
|
|
|
case <-sub.EndOfStoredEvents:
|
|
|
|
sub.Unsub()
|
2023-06-24 21:02:12 -03:00
|
|
|
}
|
|
|
|
}
|
2023-06-25 00:17:57 -03:00
|
|
|
end:
|
|
|
|
fmt.Println("")
|
|
|
|
case <-sub.EndOfStoredEvents:
|
|
|
|
sub.Unsub()
|
|
|
|
return
|
|
|
|
case <-sub.Context.Done():
|
2024-01-01 10:15:06 -03:00
|
|
|
t.Fatalf("connection closed: %v", rl.Context().Err())
|
2023-06-25 00:17:57 -03:00
|
|
|
return
|
2023-06-24 21:02:12 -03:00
|
|
|
}
|
2023-04-07 11:32:14 -03:00
|
|
|
}
|
|
|
|
}
|