-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_test.go
More file actions
116 lines (103 loc) · 2.96 KB
/
Copy pathstats_test.go
File metadata and controls
116 lines (103 loc) · 2.96 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
// Copyright 2026 Ashton Kinslow. SPDX-License-Identifier: Apache-2.0
package mqttv5
import (
"context"
"net"
"testing"
"time"
"github.com/ashtonian/mqttv5/wire"
)
// pubackBroker accepts CONNECT and PUBACKs every QoS>0 PUBLISH.
func pubackBroker(t *testing.T) *fakeBroker {
t.Helper()
return 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})
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,
})
}
}
})
}
func TestStatsCountersTickOnPublish(t *testing.T) {
fb := pubackBroker(t)
cli, err := New(WithBroker(fb.URL()), WithStats())
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())
if got := cli.Stats().Connects; got != 1 {
t.Errorf("Stats.Connects after Connect = %d, want 1", got)
}
for i := 0; i < 3; i++ {
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "stats/test", QoS: 1, Payload: []byte("x"),
}); err != nil {
t.Fatalf("Publish[%d]: %v", i, err)
}
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
s := cli.Stats()
if s.PublishesSent >= 3 && s.PublishesAcked >= 3 {
return
}
time.Sleep(20 * time.Millisecond)
}
s := cli.Stats()
t.Fatalf("after 3 Publishes: PublishesSent=%d PublishesAcked=%d (want both >= 3)",
s.PublishesSent, s.PublishesAcked)
}
// TestStatsDisabledReturnsZero verifies that WithStats() is required
// to populate counters. Without it, every Stats() field stays at the
// zero value even after activity that would otherwise tick counters.
// (PublishesInflight and SubscriptionsActive are point-in-time gauges
// and stay zero only because we haven't subscribed or pre-loaded any
// outbound entries.)
func TestStatsDisabledReturnsZero(t *testing.T) {
fb := pubackBroker(t)
cli, err := New(WithBroker(fb.URL())) // no WithStats
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())
for i := 0; i < 3; i++ {
if err := cli.Publish(context.Background(), wire.PublishOpts{
Topic: "stats/disabled", QoS: 1, Payload: []byte("x"),
}); err != nil {
t.Fatalf("Publish[%d]: %v", i, err)
}
}
// Give the broker a beat to ack so the publish path completes.
time.Sleep(100 * time.Millisecond)
s := cli.Stats()
if s.Connects != 0 || s.PublishesSent != 0 || s.PublishesAcked != 0 {
t.Errorf("Stats without WithStats should be zero, got %+v", s)
}
}