-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_internal_test.go
More file actions
309 lines (282 loc) · 8.8 KB
/
Copy pathzz_internal_test.go
File metadata and controls
309 lines (282 loc) · 8.8 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
// This file deliberately avoids `import "C"`. Go's test toolchain
// refuses to compile `_test.go` files that pull in cgo (see "use of
// cgo in test … not supported"). Every helper exercised here uses
// pure Go types in its signature, even though the package itself is a
// c-shared cgo build. Anything whose signature carries a C.* type
// (errJSON, okJSON, driverFromHandle, all //export wrappers,
// unmarshalCString) can only be reached from C-side integration
// tests.
import (
"sync"
"testing"
)
// ---------------------------------------------------------------------
// Handle table: storeHandle / loadHandle / deleteHandle
//
// These three power every PilotXxx wrapper's argument lookup. They are
// also the only spot where the package holds onto Go heap objects that
// the C side references by uint64 token — getting them wrong leaks
// memory or hands out dangling handles. Worth testing exhaustively.
// ---------------------------------------------------------------------
func TestStoreHandleAssignsUniqueMonotonicIDs(t *testing.T) {
const N = 50
ids := make([]uint64, N)
for i := 0; i < N; i++ {
ids[i] = storeHandle(i)
}
for i := 1; i < N; i++ {
if ids[i] <= ids[i-1] {
t.Fatalf("expected monotonic IDs; ids[%d]=%d ids[%d]=%d",
i-1, ids[i-1], i, ids[i])
}
}
for _, id := range ids {
deleteHandle(id)
}
}
func TestStoreHandleNeverReturnsZero(t *testing.T) {
// init() seeds next=1, so the very first store can never hand
// out 0. Zero is reserved as "no handle" in the C ABI.
id := storeHandle("first")
defer deleteHandle(id)
if id == 0 {
t.Fatal("storeHandle returned the reserved zero id")
}
}
func TestStoreHandlePreservesIdentity(t *testing.T) {
type marker struct{ name string }
want := &marker{name: "round-trip"}
id := storeHandle(want)
defer deleteHandle(id)
got, ok := loadHandle(id)
if !ok {
t.Fatalf("loadHandle returned !ok for id %d", id)
}
gotMarker, ok := got.(*marker)
if !ok {
t.Fatalf("loaded value is %T, want *marker", got)
}
if gotMarker != want {
t.Fatalf("identity mismatch: got %p want %p", gotMarker, want)
}
if gotMarker.name != "round-trip" {
t.Fatalf("value mutated through table; got %q", gotMarker.name)
}
}
func TestStoreHandleAllowsNil(t *testing.T) {
// Storing nil is legal — the table maps to interface{} and a
// nil entry must still be retrievable (the C side may store
// "no driver yet" sentinels).
id := storeHandle(nil)
defer deleteHandle(id)
v, ok := loadHandle(id)
if !ok {
t.Fatal("loadHandle returned !ok for a nil-valued handle")
}
if v != nil {
t.Fatalf("expected nil value; got %v", v)
}
}
func TestLoadHandleMissingReturnsFalse(t *testing.T) {
if _, ok := loadHandle(0); ok {
t.Fatal("loadHandle(0) reported ok; zero is never assigned")
}
if _, ok := loadHandle(1 << 60); ok {
t.Fatal("loadHandle of a never-stored id should return false")
}
}
func TestDeleteHandleRemovesEntry(t *testing.T) {
id := storeHandle("sentinel")
if _, ok := loadHandle(id); !ok {
t.Fatalf("precondition: handle %d should exist", id)
}
deleteHandle(id)
if _, ok := loadHandle(id); ok {
t.Fatalf("loadHandle(%d) still ok after deleteHandle", id)
}
}
func TestDeleteHandleIsIdempotent(t *testing.T) {
// The C side is allowed to drop a handle twice (e.g. PilotClose
// followed by an explicit FreeString during shutdown). The
// second delete must be a silent no-op, not a panic.
id := storeHandle("twice")
deleteHandle(id)
deleteHandle(id) // would panic if delete weren't idempotent
}
func TestDeleteHandleUnknownIDDoesNotPanic(t *testing.T) {
deleteHandle(1 << 62) // never assigned; must be a no-op
}
func TestHandleTableMultipleConcurrentStores(t *testing.T) {
// Race-detector hook: hammer the table from many goroutines and
// confirm the sync.RWMutex pairing in store/load/delete holds up.
// Run with `go test -race` to make this meaningful.
const writers = 8
const perWriter = 200
var wg sync.WaitGroup
wg.Add(writers)
collected := make(chan uint64, writers*perWriter)
for w := 0; w < writers; w++ {
go func(seed int) {
defer wg.Done()
for i := 0; i < perWriter; i++ {
id := storeHandle(seed*1000 + i)
v, ok := loadHandle(id)
if !ok || v.(int) != seed*1000+i {
t.Errorf("round-trip mismatch for id %d", id)
return
}
collected <- id
}
}(w)
}
wg.Wait()
close(collected)
seen := make(map[uint64]struct{}, writers*perWriter)
for id := range collected {
if _, dup := seen[id]; dup {
t.Fatalf("duplicate id %d returned from storeHandle", id)
}
seen[id] = struct{}{}
deleteHandle(id)
}
}
func TestHandleTableMixedStoreLoadDelete(t *testing.T) {
// Interleave stores, loads, and deletes from independent
// goroutines so the race detector can hunt for missing locks
// on any of the three operations.
const goroutines = 16
const ops = 100
var wg sync.WaitGroup
wg.Add(goroutines)
for g := 0; g < goroutines; g++ {
go func(seed int) {
defer wg.Done()
local := make([]uint64, 0, ops)
for i := 0; i < ops; i++ {
id := storeHandle(seed*ops + i)
local = append(local, id)
if v, ok := loadHandle(id); !ok || v.(int) != seed*ops+i {
t.Errorf("g=%d round-trip mismatch id=%d", seed, id)
return
}
}
// Delete every handle we own; concurrent goroutines
// must not see any cross-contamination.
for _, id := range local {
deleteHandle(id)
}
}(g)
}
wg.Wait()
}
func TestHandleTableDistinctIDsForIdenticalValues(t *testing.T) {
// Two stores of the same Go value must produce distinct
// handles — the C side relies on token identity, not value
// equality, to distinguish driver instances from each other.
a := storeHandle("same-value")
b := storeHandle("same-value")
defer deleteHandle(a)
defer deleteHandle(b)
if a == b {
t.Fatalf("storeHandle returned the same id %d for two stores", a)
}
}
// ---------------------------------------------------------------------
// embeddedConfig.defaults()
//
// PilotEmbeddedStart relies on this to fill blanks before booting the
// daemon; getting it wrong silently wires a different rendezvous than
// the caller expects.
// ---------------------------------------------------------------------
func TestEmbeddedConfigDefaultsFillsBlanks(t *testing.T) {
var c embeddedConfig
c.defaults()
if c.RegistryAddr == "" {
t.Fatal("RegistryAddr default not applied")
}
if c.BeaconAddr == "" {
t.Fatal("BeaconAddr default not applied")
}
if c.KeepaliveSec <= 0 {
t.Fatalf("KeepaliveSec default not applied: %d", c.KeepaliveSec)
}
if c.Version == "" {
t.Fatal("Version default not applied")
}
}
func TestEmbeddedConfigDefaultsKeepsExplicitValues(t *testing.T) {
c := embeddedConfig{
RegistryAddr: "registry.example:9999",
BeaconAddr: "beacon.example:8888",
KeepaliveSec: 17,
Version: "custom",
}
c.defaults()
if c.RegistryAddr != "registry.example:9999" {
t.Fatalf("registry addr overwritten: %s", c.RegistryAddr)
}
if c.BeaconAddr != "beacon.example:8888" {
t.Fatalf("beacon addr overwritten: %s", c.BeaconAddr)
}
if c.KeepaliveSec != 17 {
t.Fatalf("keepalive overwritten: %d", c.KeepaliveSec)
}
if c.Version != "custom" {
t.Fatalf("version overwritten: %s", c.Version)
}
}
func TestEmbeddedConfigDefaultsNegativeKeepaliveClamped(t *testing.T) {
// The guard is `<= 0`, so negatives must be clamped to the
// default — otherwise daemon.Config would receive a negative
// duration and silently skew keepalive timers.
c := embeddedConfig{KeepaliveSec: -5}
c.defaults()
if c.KeepaliveSec <= 0 {
t.Fatalf("expected positive keepalive after defaults; got %d", c.KeepaliveSec)
}
}
func TestEmbeddedConfigDefaultsZeroKeepaliveReplaced(t *testing.T) {
c := embeddedConfig{KeepaliveSec: 0}
c.defaults()
if c.KeepaliveSec == 0 {
t.Fatal("zero KeepaliveSec was not replaced with default")
}
}
func TestEmbeddedConfigDefaultsPreservesDataAndSocketPaths(t *testing.T) {
// defaults() must never touch fields the caller cares about
// for filesystem location.
c := embeddedConfig{
DataDir: "/var/lib/pilot",
SocketPath: "/tmp/pilot.sock",
}
c.defaults()
if c.DataDir != "/var/lib/pilot" {
t.Fatalf("DataDir overwritten: %s", c.DataDir)
}
if c.SocketPath != "/tmp/pilot.sock" {
t.Fatalf("SocketPath overwritten: %s", c.SocketPath)
}
}
func TestEmbeddedConfigDefaultsRegistryAddrFormat(t *testing.T) {
// Sanity check: the default registry/beacon must be a
// host:port-shaped string so daemon.New won't reject them. We
// don't validate the host is reachable.
var c embeddedConfig
c.defaults()
if !containsColon(c.RegistryAddr) {
t.Fatalf("default RegistryAddr %q missing :port", c.RegistryAddr)
}
if !containsColon(c.BeaconAddr) {
t.Fatalf("default BeaconAddr %q missing :port", c.BeaconAddr)
}
}
func containsColon(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] == ':' {
return true
}
}
return false
}