-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes_test.go
More file actions
221 lines (201 loc) · 5.53 KB
/
Copy pathtypes_test.go
File metadata and controls
221 lines (201 loc) · 5.53 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package p2p
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestP2PConfig_Validation(t *testing.T) {
tests := []struct {
name string
config Config
validate func(t *testing.T, config Config)
}{
{
name: "valid basic config",
config: Config{
ProcessName: "test-node",
ListenAddresses: []string{testLocalhost},
Port: 4001,
},
validate: func(t *testing.T, config Config) {
assert.Equal(t, "test-node", config.ProcessName)
assert.Equal(t, 4001, config.Port)
assert.Len(t, config.ListenAddresses, 1)
},
},
{
name: "config with private DHT",
config: Config{
ProcessName: "private-node",
ListenAddresses: []string{"0.0.0.0"},
Port: 5001,
UsePrivateDHT: true,
SharedKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
DHTProtocolID: "/custom/dht/1.0.0",
BootstrapAddresses: []string{"/ip4/10.0.0.1/tcp/4001/p2p/12D3KooWTest"},
},
validate: func(t *testing.T, config Config) {
assert.True(t, config.UsePrivateDHT)
assert.Equal(t, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", config.SharedKey)
assert.Equal(t, "/custom/dht/1.0.0", config.DHTProtocolID)
assert.Len(t, config.BootstrapAddresses, 1)
},
},
{
name: "config with advertise addresses",
config: Config{
ProcessName: "public-node",
ListenAddresses: []string{"0.0.0.0"},
AdvertiseAddresses: []string{"1.2.3.4", "example.com:4002"},
Port: 4001,
Advertise: true,
},
validate: func(t *testing.T, config Config) {
assert.True(t, config.Advertise)
assert.Len(t, config.AdvertiseAddresses, 2)
assert.Contains(t, config.AdvertiseAddresses, "1.2.3.4")
assert.Contains(t, config.AdvertiseAddresses, "example.com:4002")
},
},
{
name: "config with static peers",
config: Config{
ProcessName: "connected-node",
ListenAddresses: []string{testLocalhost},
Port: 4001,
StaticPeers: []string{
"/ip4/192.168.1.10/tcp/4001/p2p/12D3KooWTest1",
"/ip4/192.168.1.11/tcp/4001/p2p/12D3KooWTest2",
},
OptimiseRetries: true,
},
validate: func(t *testing.T, config Config) {
assert.True(t, config.OptimiseRetries)
assert.Len(t, config.StaticPeers, 2)
},
},
{
name: "empty config fields",
config: Config{
ProcessName: "",
ListenAddresses: []string{},
Port: 0,
},
validate: func(t *testing.T, config Config) {
assert.Empty(t, config.ProcessName)
assert.Empty(t, config.ListenAddresses)
assert.Zero(t, config.Port)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.validate(t, tt.config)
})
}
}
func TestP2PNode_AtomicOperations(t *testing.T) {
node := &Node{
bytesSent: 0,
bytesReceived: 0,
lastSend: 0,
lastRecv: 0,
}
// Test concurrent atomic operations
var wg sync.WaitGroup
iterations := 1000
goroutines := 10
// Test bytesSent increments
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
atomic.AddUint64(&node.bytesSent, 1)
}
}()
}
wg.Wait()
assert.Equal(t, uint64(goroutines*iterations), atomic.LoadUint64(&node.bytesSent))
// Test bytesReceived increments
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
atomic.AddUint64(&node.bytesReceived, 10)
}
}()
}
wg.Wait()
assert.Equal(t, uint64(goroutines*iterations*10), atomic.LoadUint64(&node.bytesReceived))
// Test timestamp updates
now := time.Now().Unix()
atomic.StoreInt64(&node.lastSend, now)
atomic.StoreInt64(&node.lastRecv, now+1)
assert.Equal(t, now, atomic.LoadInt64(&node.lastSend))
assert.Equal(t, now+1, atomic.LoadInt64(&node.lastRecv))
}
func TestP2PNode_ThreadSafeMaps(t *testing.T) {
node := &Node{
peerHeights: sync.Map{},
peerConnTimes: sync.Map{},
}
// Test concurrent map operations
var wg sync.WaitGroup
peers := 100
testPeerIDs := []string{
"12D3KooWGRYZDHBembyGJQqQ6WgLqJWYNjnECJwGBnCg8vbCeo8F",
"12D3KooWLRYZDHBembyGJQqQ6WgLqJWYNjnECJwGBnCg8vbCeo8G",
"12D3KooWMRYZDHBembyGJQqQ6WgLqJWYNjnECJwGBnCg8vbCeo8H",
}
// Test peerHeights concurrent access
wg.Add(peers * 2)
for i := 0; i < peers; i++ {
peerID := testPeerIDs[i%len(testPeerIDs)]
// Writer goroutine
go func(id string, height int32) {
defer wg.Done()
node.peerHeights.Store(id, height)
}(peerID, int32(i))
// Reader goroutine
go func(id string) {
defer wg.Done()
node.peerHeights.Load(id)
}(peerID)
}
wg.Wait()
// Verify some values were stored
count := 0
node.peerHeights.Range(func(_, _ interface{}) bool {
count++
return true
})
assert.Positive(t, count)
// Test peerConnTimes concurrent access
wg.Add(peers)
for i := 0; i < peers; i++ {
go func(i int) {
defer wg.Done()
peerID := testPeerIDs[i%len(testPeerIDs)]
node.peerConnTimes.Store(peerID, time.Now())
node.peerConnTimes.Load(peerID)
}(i)
}
wg.Wait()
}
func TestHandler_FunctionType(t *testing.T) {
// Test that Handler type can be properly instantiated
var handler Handler = func(_ context.Context, _ []byte, _ string) {
// Sample handler implementation
}
assert.NotNil(t, handler)
}
func TestConstants(t *testing.T) {
// Verify constants are defined correctly
assert.Equal(t, "[Node] error creating DHT: %w", errorCreatingDhtMessage)
assert.Equal(t, "/ip4/%s/tcp/%d", multiAddrIPTemplate)
}