@@ -22,6 +22,77 @@ import (
22
22
"github.com/open-telemetry/opamp-go/protobufs"
23
23
)
24
24
25
+ func TestWSSenderReportsHeartbeat (t * testing.T ) {
26
+ tests := []struct {
27
+ name string
28
+ clientEnableHeartbeat bool
29
+ serverEnableHeartbeat bool
30
+ expectHeartbeats bool
31
+ }{
32
+ {"enable heartbeat" , true , true , true },
33
+ {"client disable heartbeat" , false , true , false },
34
+ {"server disable heartbeat" , true , false , false },
35
+ }
36
+
37
+ for _ , tt := range tests {
38
+ srv := internal .StartMockServer (t )
39
+
40
+ var firstMsg atomic.Bool
41
+ var conn atomic.Value
42
+ srv .OnWSConnect = func (c * websocket.Conn ) {
43
+ conn .Store (c )
44
+ firstMsg .Store (true )
45
+ }
46
+ var msgCount atomic.Int64
47
+ srv .OnMessage = func (msg * protobufs.AgentToServer ) * protobufs.ServerToAgent {
48
+ if firstMsg .Load () {
49
+ firstMsg .Store (false )
50
+ resp := & protobufs.ServerToAgent {
51
+ InstanceUid : msg .InstanceUid ,
52
+ ConnectionSettings : & protobufs.ConnectionSettingsOffers {
53
+ Opamp : & protobufs.OpAMPConnectionSettings {
54
+ HeartbeatIntervalSeconds : 1 ,
55
+ },
56
+ },
57
+ }
58
+ if ! tt .serverEnableHeartbeat {
59
+ resp .ConnectionSettings .Opamp .HeartbeatIntervalSeconds = 0
60
+ }
61
+ return resp
62
+ }
63
+ msgCount .Add (1 )
64
+ return nil
65
+ }
66
+
67
+ // Start an OpAMP/WebSocket client.
68
+ settings := types.StartSettings {
69
+ OpAMPServerURL : "ws://" + srv .Endpoint ,
70
+ }
71
+ if tt .clientEnableHeartbeat {
72
+ settings .Capabilities = protobufs .AgentCapabilities_AgentCapabilities_ReportsHeartbeat
73
+ }
74
+ client := NewWebSocket (nil )
75
+ startClient (t , settings , client )
76
+
77
+ // Wait for connection to be established.
78
+ eventually (t , func () bool { return conn .Load () != nil })
79
+
80
+ if tt .expectHeartbeats {
81
+ assert .Eventually (t , func () bool {
82
+ return msgCount .Load () >= 2
83
+ }, 3 * time .Second , 10 * time .Millisecond )
84
+ } else {
85
+ assert .Never (t , func () bool {
86
+ return msgCount .Load () >= 2
87
+ }, 3 * time .Second , 10 * time .Millisecond )
88
+ }
89
+
90
+ // Stop the client.
91
+ err := client .Stop (context .Background ())
92
+ assert .NoError (t , err )
93
+ }
94
+ }
95
+
25
96
func TestDisconnectWSByServer (t * testing.T ) {
26
97
// Start a Server.
27
98
srv := internal .StartMockServer (t )
0 commit comments