mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-01 10:39:19 +02:00
update reflect/unsafe.String thing.
This commit is contained in:
parent
7692f1c148
commit
3afa6fc5f6
@ -1,7 +1,6 @@
|
|||||||
package nostr
|
package nostr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@ -17,8 +16,8 @@ var namedMutexPool = make([]sync.Mutex, MAX_LOCKS)
|
|||||||
func memhash(p unsafe.Pointer, h, s uintptr) uintptr
|
func memhash(p unsafe.Pointer, h, s uintptr) uintptr
|
||||||
|
|
||||||
func namedLock(name string) (unlock func()) {
|
func namedLock(name string) (unlock func()) {
|
||||||
ss := (*reflect.StringHeader)(unsafe.Pointer(&name))
|
sptr := unsafe.StringData(name)
|
||||||
idx := uint64(memhash(unsafe.Pointer(ss.Data), 0, uintptr(ss.Len))) % MAX_LOCKS
|
idx := uint64(memhash(unsafe.Pointer(sptr), 0, uintptr(len(name)))) % MAX_LOCKS
|
||||||
namedMutexPool[idx].Lock()
|
namedMutexPool[idx].Lock()
|
||||||
return namedMutexPool[idx].Unlock
|
return namedMutexPool[idx].Unlock
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,13 @@ import (
|
|||||||
const RELAY = "wss://nos.lol"
|
const RELAY = "wss://nos.lol"
|
||||||
|
|
||||||
// test if we can fetch a couple of random events
|
// test if we can fetch a couple of random events
|
||||||
func TestSubscribe(t *testing.T) {
|
func TestSubscribeBasic(t *testing.T) {
|
||||||
rl := mustRelayConnect(RELAY)
|
rl := mustRelayConnect(RELAY)
|
||||||
defer rl.Close()
|
defer rl.Close()
|
||||||
|
|
||||||
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}})
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("subscription failed: %v", err)
|
t.Fatalf("subscription failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,23 +28,23 @@ func TestSubscribe(t *testing.T) {
|
|||||||
select {
|
select {
|
||||||
case event := <-sub.Events:
|
case event := <-sub.Events:
|
||||||
if event == nil {
|
if event == nil {
|
||||||
t.Errorf("event is nil: %v", event)
|
t.Fatalf("event is nil: %v", event)
|
||||||
}
|
}
|
||||||
n++
|
n++
|
||||||
case <-sub.EndOfStoredEvents:
|
case <-sub.EndOfStoredEvents:
|
||||||
goto end
|
goto end
|
||||||
case <-rl.Context().Done():
|
case <-rl.Context().Done():
|
||||||
t.Errorf("connection closed: %v", rl.Context().Err())
|
t.Fatalf("connection closed: %v", rl.Context().Err())
|
||||||
goto end
|
goto end
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
t.Errorf("timeout")
|
t.Fatalf("timeout")
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if n != 2 {
|
if n != 2 {
|
||||||
t.Errorf("expected 2 events, got %d", n)
|
t.Fatalf("expected 2 events, got %d", n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ func TestNestedSubscriptions(t *testing.T) {
|
|||||||
// fetch 2 replies to a note
|
// fetch 2 replies to a note
|
||||||
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}})
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("subscription 1 failed: %v", err)
|
t.Fatalf("subscription 1 failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ func TestNestedSubscriptions(t *testing.T) {
|
|||||||
// now fetch author of this
|
// now fetch author of this
|
||||||
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindProfileMetadata}, Authors: []string{event.PubKey}, Limit: 1}})
|
sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindProfileMetadata}, Authors: []string{event.PubKey}, Limit: 1}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("subscription 2 failed: %v", err)
|
t.Fatalf("subscription 2 failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ func TestNestedSubscriptions(t *testing.T) {
|
|||||||
sub.Unsub()
|
sub.Unsub()
|
||||||
return
|
return
|
||||||
case <-sub.Context.Done():
|
case <-sub.Context.Done():
|
||||||
t.Errorf("connection closed: %v", rl.Context().Err())
|
t.Fatalf("connection closed: %v", rl.Context().Err())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user