Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu committed Aug 23, 2024
1 parent fe827f5 commit 06080cf
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions client/wsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,77 @@ import (
"github.com/open-telemetry/opamp-go/protobufs"
)

func TestWSSenderReportsHeartbeat(t *testing.T) {
tests := []struct {
name string
clientEnableHeartbeat bool
serverEnableHeartbeat bool
expectHeartbeats bool
}{
{"enable heartbeat", true, true, true},
{"client disable heartbeat", false, true, false},
{"server disable heartbeat", true, false, false},
}

for _, tt := range tests {
srv := internal.StartMockServer(t)

var firstMsg atomic.Bool
var conn atomic.Value
srv.OnWSConnect = func(c *websocket.Conn) {
conn.Store(c)
firstMsg.Store(true)
}
var msgCount atomic.Int64
srv.OnMessage = func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent {
if firstMsg.Load() {
firstMsg.Store(false)
resp := &protobufs.ServerToAgent{
InstanceUid: msg.InstanceUid,
ConnectionSettings: &protobufs.ConnectionSettingsOffers{
Opamp: &protobufs.OpAMPConnectionSettings{
HeartbeatIntervalSeconds: 1,
},
},
}
if !tt.serverEnableHeartbeat {
resp.ConnectionSettings.Opamp.HeartbeatIntervalSeconds = 0
}
return resp
}
msgCount.Add(1)
return nil
}

// Start an OpAMP/WebSocket client.
settings := types.StartSettings{
OpAMPServerURL: "ws://" + srv.Endpoint,
}
if tt.clientEnableHeartbeat {
settings.Capabilities = protobufs.AgentCapabilities_AgentCapabilities_ReportsHeartbeat
}
client := NewWebSocket(nil)
startClient(t, settings, client)

// Wait for connection to be established.
eventually(t, func() bool { return conn.Load() != nil })

if tt.expectHeartbeats {
assert.Eventually(t, func() bool {
return msgCount.Load() >= 2
}, 3*time.Second, 10*time.Millisecond)
} else {
assert.Never(t, func() bool {
return msgCount.Load() >= 2
}, 3*time.Second, 10*time.Millisecond)
}

// Stop the client.
err := client.Stop(context.Background())
assert.NoError(t, err)
}
}

func TestDisconnectWSByServer(t *testing.T) {
// Start a Server.
srv := internal.StartMockServer(t)
Expand Down

0 comments on commit 06080cf

Please sign in to comment.