-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
508 lines (469 loc) · 14.1 KB
/
Copy pathpool_test.go
File metadata and controls
508 lines (469 loc) · 14.1 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright 2026 Ashton Kinslow. SPDX-License-Identifier: Apache-2.0
package mqttv5
import (
"context"
"net"
"net/url"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/ashtonian/mqttv5/transport"
"github.com/ashtonian/mqttv5/wire"
)
// publishRecorder is a fake-broker handler shared across N concurrent
// per-connection handlers. Each handler logs (clientID, topic) for
// every inbound PUBLISH so tests can assert routing distribution.
type publishRecorder struct {
mu sync.Mutex
publishes []recordedPublish
}
type recordedPublish struct {
clientID string
topic string
}
func (r *publishRecorder) record(clientID, topic string) {
r.mu.Lock()
r.publishes = append(r.publishes, recordedPublish{clientID, topic})
r.mu.Unlock()
}
func (r *publishRecorder) snapshot() []recordedPublish {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]recordedPublish, len(r.publishes))
copy(out, r.publishes)
return out
}
// recordingHandler reads CONNECT (capturing the ClientID), replies
// CONNACK, then loops recording PUBLISHes and auto-acking QoS 1.
func recordingHandler(t *testing.T, rec *publishRecorder) func(*fakeBroker, net.Conn) {
t.Helper()
return func(fb *fakeBroker, c net.Conn) {
defer c.Close()
dec := wire.NewDecoder(c)
pkt, err := dec.ReadPacket()
if err != nil {
return
}
conn, ok := pkt.(*wire.Connect)
if !ok {
t.Errorf("got %s, want CONNECT", pkt.Type())
pkt.Release()
return
}
// conn.ClientID aliases the frame buffer; clone it so the
// captured value survives pkt.Release and subsequent frame
// pool reuse.
clientID := strings.Clone(conn.ClientID)
pkt.Release()
if _, err := wire.WriteConnack(c, wire.ConnackOpts{ReasonCode: wire.ReasonSuccess}); err != nil {
return
}
for {
select {
case <-fb.Done():
return
default:
}
pkt, err := dec.ReadPacket()
if err != nil {
return
}
switch pkt.Type() {
case wire.PUBLISH:
pub := pkt.(*wire.Publish)
// pub.Topic aliases the frame buffer; clone before
// retaining or it dangles after pkt.Release.
rec.record(clientID, strings.Clone(pub.Topic))
if pub.QoS == 1 {
_, _ = wire.WritePuback(c, wire.PubRespOpts{
PacketID: pub.PacketID,
ReasonCode: wire.ReasonSuccess,
})
}
case wire.PINGREQ:
_, _ = wire.WritePingresp(c)
case wire.DISCONNECT:
pkt.Release()
return
}
pkt.Release()
}
}
}
// waitForPoolReady blocks until pool members have all reached
// Connected = true, or the timeout expires.
func waitForPoolReady(t *testing.T, cli *Client, want int, timeout time.Duration) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if cli.pubPool == nil {
time.Sleep(20 * time.Millisecond)
continue
}
ready := 0
for _, m := range cli.pubPool.members {
if m.Connected() {
ready++
}
}
if ready >= want {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("pool not ready within %v", timeout)
}
// TestPoolRouting_HashByTopic_SameTopicSameMember verifies that with
// PoolRoutingHashByTopic, repeated publishes to the same topic all
// land on the same pool member's ClientID.
func TestPoolRouting_HashByTopic_SameTopicSameMember(t *testing.T) {
t.Parallel()
rec := &publishRecorder{}
fb := newFakeBroker(t, recordingHandler(t, rec))
cli, err := New(
WithBroker(fb.URL()),
WithClientID("hashtest"),
WithPublisherPool(3),
WithPublisherPoolRouting(PoolRoutingHashByTopic),
)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := cli.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
defer cli.Disconnect(context.Background())
waitForPoolReady(t, cli, 3, 3*time.Second)
// Publish 5× to topic A, 5× to topic B. With hash-by-topic each
// topic must collapse to a single member.
for range 5 {
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "alpha", Payload: []byte("x"), QoS: 1,
}); err != nil {
t.Fatalf("Publish alpha: %v", err)
}
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "beta", Payload: []byte("x"), QoS: 1,
}); err != nil {
t.Fatalf("Publish beta: %v", err)
}
}
// Give the broker side time to record all publishes.
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if len(rec.snapshot()) >= 10 {
break
}
time.Sleep(20 * time.Millisecond)
}
pubs := rec.snapshot()
if len(pubs) != 10 {
t.Fatalf("recorded %d publishes, want 10: %+v", len(pubs), pubs)
}
alphaClients := map[string]int{}
betaClients := map[string]int{}
for _, p := range pubs {
switch p.topic {
case "alpha":
alphaClients[p.clientID]++
case "beta":
betaClients[p.clientID]++
}
}
if len(alphaClients) != 1 {
t.Fatalf("topic alpha hit %d distinct ClientIDs (want 1): %v", len(alphaClients), alphaClients)
}
if len(betaClients) != 1 {
t.Fatalf("topic beta hit %d distinct ClientIDs (want 1): %v", len(betaClients), betaClients)
}
}
// TestPoolRouting_RoundRobin_SpreadsAcrossMembers verifies that the
// default policy distributes consecutive publishes (to the same topic)
// across multiple pool members.
func TestPoolRouting_RoundRobin_SpreadsAcrossMembers(t *testing.T) {
t.Parallel()
rec := &publishRecorder{}
fb := newFakeBroker(t, recordingHandler(t, rec))
cli, err := New(
WithBroker(fb.URL()),
WithClientID("rrtest"),
WithPublisherPool(3),
// PoolRoutingRoundRobin is the default — no WithPublisherPoolRouting needed.
)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := cli.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
defer cli.Disconnect(context.Background())
waitForPoolReady(t, cli, 3, 3*time.Second)
// Publish 9× to a single topic. With round-robin and 3 members
// every member should receive at least one publish.
for range 9 {
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "same/topic", Payload: []byte("x"), QoS: 1,
}); err != nil {
t.Fatalf("Publish: %v", err)
}
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if len(rec.snapshot()) >= 9 {
break
}
time.Sleep(20 * time.Millisecond)
}
pubs := rec.snapshot()
if len(pubs) != 9 {
t.Fatalf("recorded %d publishes, want 9", len(pubs))
}
clients := map[string]int{}
for _, p := range pubs {
clients[p.clientID]++
}
// Round-robin across 3 members of 9 publishes: every pool
// member must have received at least one. (We exclude the
// parent ClientID because the parent only takes the fallback
// path when all members are down, which isn't this test.)
pubMembers := 0
for id := range clients {
if id != "rrtest" {
pubMembers++
}
}
if pubMembers < 3 {
t.Fatalf("round-robin reached %d of 3 pool members (want all 3): %v", pubMembers, clients)
}
}
// TestPoolMemberInheritsConnectProperties verifies that the v5 CONNECT
// property options on the parent Config propagate to every pool
// member's CONNECT. The test snapshots the per-member CONNECT and
// asserts each property is present.
func TestPoolMemberInheritsConnectProperties(t *testing.T) {
const memberCount = 2
// snapshot extracts the v5 fields synchronously while the frame
// is still alive — Properties is a view over the frame buffer
// and becomes invalid after Release.
type connectSnapshot struct {
clientID string
maxPacketSize uint32
hasMaxPacketSize bool
topicAliasMax uint16
hasTopicAliasMax bool
rri byte
hasRRI bool
rpi byte
hasRPI bool
userProperty map[string]string
}
var snapsMu sync.Mutex
snaps := []connectSnapshot{}
fb := newFakeBroker(t, func(fb *fakeBroker, c net.Conn) {
defer c.Close()
dec := wire.NewDecoder(c)
pkt, err := dec.ReadPacket()
if err != nil {
return
}
conn, ok := pkt.(*wire.Connect)
if !ok {
pkt.Release()
return
}
// ClientID and user-property strings alias the frame buffer;
// clone before Release or they dangle once the frame returns
// to the pool and a subsequent CONNECT overwrites it.
snap := connectSnapshot{clientID: strings.Clone(conn.ClientID), userProperty: map[string]string{}}
snap.maxPacketSize, snap.hasMaxPacketSize = conn.Properties.Uint32(wire.PropMaximumPacketSize)
snap.topicAliasMax, snap.hasTopicAliasMax = conn.Properties.Uint16(wire.PropTopicAliasMaximum)
snap.rri, snap.hasRRI = conn.Properties.Byte(wire.PropRequestResponseInfo)
snap.rpi, snap.hasRPI = conn.Properties.Byte(wire.PropRequestProblemInfo)
for k, v := range conn.Properties.UserProperties() {
snap.userProperty[strings.Clone(k)] = strings.Clone(v)
}
conn.Release()
snapsMu.Lock()
snaps = append(snaps, snap)
snapsMu.Unlock()
_, _ = wire.WriteConnack(c, wire.ConnackOpts{ReasonCode: wire.ReasonSuccess})
<-fb.Done()
})
cli, err := New(
WithBroker(fb.URL()),
WithClientID("inherit-test"),
WithPublisherPool(memberCount),
WithMaximumPacketSize(1024*1024),
WithInboundTopicAliasMaximum(16),
WithRequestResponseInformation(true),
WithRequestProblemInformation(true),
WithConnectUserProperty("env", "test"),
)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := cli.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
defer cli.Disconnect(context.Background())
want := memberCount + 1
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
snapsMu.Lock()
got := len(snaps)
snapsMu.Unlock()
if got >= want {
break
}
time.Sleep(20 * time.Millisecond)
}
snapsMu.Lock()
defer snapsMu.Unlock()
if len(snaps) < want {
t.Fatalf("captured %d CONNECTs, want %d", len(snaps), want)
}
for i, s := range snaps {
if !s.hasMaxPacketSize || s.maxPacketSize != 1024*1024 {
t.Errorf("CONNECT[%d] (%q) MaximumPacketSize = (%d, %v), want (1048576, true)",
i, s.clientID, s.maxPacketSize, s.hasMaxPacketSize)
}
if !s.hasTopicAliasMax || s.topicAliasMax != 16 {
t.Errorf("CONNECT[%d] (%q) TopicAliasMaximum = (%d, %v), want (16, true)",
i, s.clientID, s.topicAliasMax, s.hasTopicAliasMax)
}
if !s.hasRRI || s.rri != 1 {
t.Errorf("CONNECT[%d] (%q) RequestResponseInformation = (%d, %v), want (1, true)",
i, s.clientID, s.rri, s.hasRRI)
}
if !s.hasRPI || s.rpi != 1 {
t.Errorf("CONNECT[%d] (%q) RequestProblemInformation = (%d, %v), want (1, true)",
i, s.clientID, s.rpi, s.hasRPI)
}
if s.userProperty["env"] != "test" {
t.Errorf("CONNECT[%d] (%q) missing env=test user property", i, s.clientID)
}
}
}
// TestPoolFallbackIncrementsStats verifies that a publish falling back
// from the pool (all members unhealthy) to the main connection
// increments Stats.PoolFallbacks when WithStats is enabled.
//
// The broker rejects every pool-member CONNECT with a non-success
// CONNACK so the pool members fail to connect and stay disconnected.
// The parent connects normally and handles the fallback publish.
func TestPoolFallbackIncrementsStats(t *testing.T) {
fb := newFakeBroker(t, func(fb *fakeBroker, c net.Conn) {
defer c.Close()
dec := wire.NewDecoder(c)
pkt, err := dec.ReadPacket()
if err != nil {
return
}
conn, ok := pkt.(*wire.Connect)
if !ok {
pkt.Release()
return
}
isPool := strings.Contains(conn.ClientID, "-pub-")
conn.Release()
if isPool {
// Refuse the pool member with NotAuthorized — the client
// reports ErrConnectRefused, m.started stays false, the
// member never enters the supervisor.
_, _ = wire.WriteConnack(c, wire.ConnackOpts{ReasonCode: wire.ReasonNotAuthorized})
return
}
// Parent: accept normally and PUBACK every QoS 1.
_, _ = wire.WriteConnack(c, wire.ConnackOpts{ReasonCode: wire.ReasonSuccess})
for {
pkt, err := dec.ReadPacket()
if err != nil {
return
}
pub, ok := pkt.(*wire.Publish)
if !ok {
pkt.Release()
continue
}
id := pub.PacketID
pkt.Release()
if id != 0 {
_, _ = wire.WritePuback(c, wire.PubRespOpts{PacketID: id, ReasonCode: wire.ReasonSuccess})
}
}
})
cli, err := New(
WithBroker(fb.URL()),
WithClientID("fallback-stats"),
WithStats(),
WithPublisherPool(2),
)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := cli.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
defer cli.Disconnect(context.Background())
// Pool members were refused, so they're unconnected. The publish
// path must fall back to the main connection.
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "fallback/test", QoS: 1, Payload: []byte("x"),
}); err != nil {
t.Fatalf("Publish: %v", err)
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if cli.Stats().PoolFallbacks >= 1 {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("PoolFallbacks = %d, want >= 1", cli.Stats().PoolFallbacks)
}
// TestPoolMemberInheritsDialFunc verifies that a parent-configured
// DialFunc is propagated to pool members. Without this, a user on
// the WebSocket transport (or any non-built-in scheme) sees pool
// members fail to dial with "unsupported scheme".
func TestPoolMemberInheritsDialFunc(t *testing.T) {
fb := newFakeBroker(t, func(fb *fakeBroker, c net.Conn) {
defer c.Close()
dec := wire.NewDecoder(c)
pkt, _ := dec.ReadPacket()
if pkt != nil {
pkt.Release()
}
_, _ = wire.WriteConnack(c, wire.ConnackOpts{ReasonCode: wire.ReasonSuccess})
<-fb.Done()
})
var dialCount atomic.Int32
dialFn := func(ctx context.Context, u *url.URL) (transport.Conn, error) {
dialCount.Add(1)
// Defer to the built-in transport so the rest of the handshake
// proceeds normally.
return transport.Dial(ctx, u.String(), transport.DialOpts{})
}
cli, err := New(
WithBroker(fb.URL()),
WithClientID("dialfunc-prop-test"),
WithDialFunc(dialFn),
WithPublisherPool(2),
)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := cli.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
defer cli.Disconnect(context.Background())
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if dialCount.Load() >= 3 { // 1 parent + 2 members
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("DialFunc invoked %d times, want >= 3 (parent + 2 members)", dialCount.Load())
}