-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathutil_test.go
More file actions
135 lines (115 loc) · 2.94 KB
/
Copy pathutil_test.go
File metadata and controls
135 lines (115 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package relayer
import (
"context"
"testing"
"github.com/fiatjaf/eventstore"
"github.com/nbd-wtf/go-nostr"
)
func startTestRelay(t *testing.T, tr *testRelay) *Server {
t.Helper()
srv, _ := NewServer(tr)
started := make(chan bool)
go srv.Start("127.0.0.1", 0, started)
<-started
return srv
}
type testRelay struct {
name string
storage eventstore.Store
init func() error
onShutdown func(context.Context)
acceptEvent func(*nostr.Event) (bool, string)
}
func (tr *testRelay) Name() string { return tr.name }
func (tr *testRelay) Storage(context.Context) eventstore.Store { return tr.storage }
func (tr *testRelay) Init() error {
if fn := tr.init; fn != nil {
return fn()
}
return nil
}
func (tr *testRelay) OnShutdown(ctx context.Context) {
if fn := tr.onShutdown; fn != nil {
fn(ctx)
}
}
func (tr *testRelay) AcceptEvent(ctx context.Context, e *nostr.Event) (bool, string) {
if fn := tr.acceptEvent; fn != nil {
return fn(e)
}
return true, ""
}
type testStorage struct {
init func() error
close func()
queryEvents func(context.Context, nostr.Filter) (chan *nostr.Event, error)
deleteEvent func(context.Context, *nostr.Event) error
saveEvent func(context.Context, *nostr.Event) error
countEvents func(context.Context, *nostr.Filter) (int64, error)
replaceEvent func(context.Context, *nostr.Event) error
}
func (st *testStorage) Init() error {
if fn := st.init; fn != nil {
return fn()
}
return nil
}
func (st *testStorage) Close() {
if fn := st.close; fn != nil {
fn()
}
}
func (st *testStorage) QueryEvents(ctx context.Context, f nostr.Filter) (chan *nostr.Event, error) {
if fn := st.queryEvents; fn != nil {
return fn(ctx, f)
}
return nil, nil
}
func (st *testStorage) DeleteEvent(ctx context.Context, evt *nostr.Event) error {
if fn := st.deleteEvent; fn != nil {
return fn(ctx, evt)
}
return nil
}
func (st *testStorage) SaveEvent(ctx context.Context, e *nostr.Event) error {
if fn := st.saveEvent; fn != nil {
return fn(ctx, e)
}
return nil
}
func (st *testStorage) CountEvents(ctx context.Context, f *nostr.Filter) (int64, error) {
if fn := st.countEvents; fn != nil {
return fn(ctx, f)
}
return 0, nil
}
func (st *testStorage) ReplaceEvent(ctx context.Context, e *nostr.Event) error {
if fn := st.replaceEvent; fn != nil {
return fn(ctx, e)
}
return nil
}
func clearListeners() {
serversMutex.RLock()
defer serversMutex.RUnlock()
for srv := range servers {
srv.listenersMu.Lock()
srv.listeners = make(map[*WebSocket]map[string]*Listener)
srv.listenersMu.Unlock()
}
}
type testAdvancedStorage struct {
testStorage
beforeSave func(context.Context, *nostr.Event)
afterSave func(*nostr.Event)
}
func (s *testAdvancedStorage) BeforeSave(ctx context.Context, evt *nostr.Event) {
if s.beforeSave != nil {
s.beforeSave(ctx, evt)
}
}
func (s *testAdvancedStorage) AfterSave(evt *nostr.Event) {
if s.afterSave != nil {
s.afterSave(evt)
}
}