@@ -93,7 +93,7 @@ func eventually(t *testing.T, f func() bool) {
9393 assert .Eventually (t , f , 5 * time .Second , 10 * time .Millisecond )
9494}
9595
96- func newInstanceUid (t * testing.T ) types.InstanceUid {
96+ func genNewInstanceUid (t * testing.T ) types.InstanceUid {
9797 uid , err := uuid .NewV7 ()
9898 require .NoError (t , err )
9999 b , err := uid .MarshalBinary ()
@@ -103,7 +103,7 @@ func newInstanceUid(t *testing.T) types.InstanceUid {
103103
104104func prepareSettings (t * testing.T , settings * types.StartSettings , c OpAMPClient ) {
105105 // Autogenerate instance id.
106- settings .InstanceUid = newInstanceUid (t )
106+ settings .InstanceUid = genNewInstanceUid (t )
107107
108108 // Make sure correct URL scheme is used, based on the type of the OpAMP client.
109109 u , err := url .Parse (settings .OpAMPServerURL )
@@ -630,27 +630,24 @@ func TestAgentIdentification(t *testing.T) {
630630 testClients (t , func (t * testing.T , client OpAMPClient ) {
631631 // Start a server.
632632 srv := internal .StartMockServer (t )
633- newInstanceUid := newInstanceUid (t )
633+ newInstanceUid := genNewInstanceUid (t )
634634 var rcvAgentInstanceUid atomic.Value
635- var sentInvalidId atomic.Bool
636635 srv .OnMessage = func (msg * protobufs.AgentToServer ) * protobufs.ServerToAgent {
637- rcvAgentInstanceUid .Store (msg .InstanceUid )
638- if sentInvalidId .Load () {
636+ if msg .Flags & uint64 (protobufs .AgentToServerFlags_AgentToServerFlags_RequestInstanceUid ) == 1 {
637+ newInstanceUid = genNewInstanceUid (t )
638+ rcvAgentInstanceUid .Store (newInstanceUid [:])
639639 return & protobufs.ServerToAgent {
640640 InstanceUid : msg .InstanceUid ,
641641 AgentIdentification : & protobufs.AgentIdentification {
642- // If we sent the invalid one first, send a valid one now
642+ // If the RequestInstanceUid flag was set, populate this field.
643643 NewInstanceUid : newInstanceUid [:],
644644 },
645645 }
646646 }
647- sentInvalidId .Store (true )
647+ rcvAgentInstanceUid .Store (msg .InstanceUid )
648+ // Start by sending just the old instance ID.
648649 return & protobufs.ServerToAgent {
649650 InstanceUid : msg .InstanceUid ,
650- AgentIdentification : & protobufs.AgentIdentification {
651- // Start by sending an invalid id forcing an error.
652- NewInstanceUid : nil ,
653- },
654651 }
655652 }
656653
@@ -689,8 +686,8 @@ func TestAgentIdentification(t *testing.T) {
689686 },
690687 )
691688
692- // Send a dummy message again to get the _new_ id
693- _ = client .SetAgentDescription ( createAgentDescr () )
689+ // Set the flags to request a new ID.
690+ client .SetFlags ( protobufs . AgentToServerFlags_AgentToServerFlags_RequestInstanceUid )
694691
695692 // When it was sent, the new instance uid should have been used, which should
696693 // have been observed by the Server
@@ -2122,3 +2119,104 @@ func TestSetCustomCapabilities(t *testing.T) {
21222119 assert .NoError (t , err )
21232120 })
21242121}
2122+
2123+ // TestSetFlags tests the ability for the client to change the set of flags it sends.
2124+ func TestSetFlags (t * testing.T ) {
2125+ testClients (t , func (t * testing.T , client OpAMPClient ) {
2126+
2127+ // Start a Server.
2128+ srv := internal .StartMockServer (t )
2129+ var rcvCustomFlags atomic.Value
2130+ var flags protobufs.AgentToServerFlags
2131+
2132+ srv .OnMessage = func (msg * protobufs.AgentToServer ) * protobufs.ServerToAgent {
2133+ if msg .Flags != 0 {
2134+ rcvCustomFlags .Store (msg .Flags )
2135+ }
2136+ return nil
2137+ }
2138+
2139+ settings := types.StartSettings {}
2140+ settings .OpAMPServerURL = "ws://" + srv .Endpoint
2141+ prepareClient (t , & settings , client )
2142+
2143+ assert .NoError (t , client .Start (context .Background (), settings ))
2144+
2145+ // The zero value of AgentToServerFlags is ready to use
2146+ client .SetFlags (flags )
2147+
2148+ // Update flags to send
2149+ flags |= protobufs .AgentToServerFlags_AgentToServerFlags_RequestInstanceUid
2150+ client .SetFlags (flags )
2151+
2152+ // Verify new flags were delivered to the server
2153+ eventually (
2154+ t ,
2155+ func () bool {
2156+ msg , ok := rcvCustomFlags .Load ().(uint64 )
2157+ if ! ok || msg == 0 {
2158+ return false
2159+ }
2160+ return uint64 (flags ) == msg
2161+ },
2162+ )
2163+
2164+ // Shutdown the Server.
2165+ srv .Close ()
2166+
2167+ // Shutdown the client.
2168+ err := client .Stop (context .Background ())
2169+ assert .NoError (t , err )
2170+ })
2171+ }
2172+
2173+ // TestSetFlags tests the ability for the client to set its flags before starting up.
2174+ func TestSetFlagsBeforeStart (t * testing.T ) {
2175+ testClients (t , func (t * testing.T , client OpAMPClient ) {
2176+ // Start a Server.
2177+ flags := protobufs .AgentToServerFlags_AgentToServerFlags_RequestInstanceUid
2178+ srv := internal .StartMockServer (t )
2179+ var rcvCustomFlags atomic.Value
2180+ var isFirstMessage atomic.Bool
2181+ isFirstMessage .Store (true )
2182+
2183+ // Make sure we only record flags from the very first message.
2184+ srv .OnMessage = func (msg * protobufs.AgentToServer ) * protobufs.ServerToAgent {
2185+ if isFirstMessage .Load () {
2186+ rcvCustomFlags .Store (msg .Flags )
2187+ }
2188+ isFirstMessage .Store (false )
2189+ return nil
2190+ }
2191+
2192+ settings := types.StartSettings {}
2193+ settings .OpAMPServerURL = "ws://" + srv .Endpoint
2194+ prepareClient (t , & settings , client )
2195+
2196+ // Set up the flags _before_ calling Start to verify that they're
2197+ // handled correctly in PrepareFirstMessage.
2198+ client .SetFlags (flags )
2199+
2200+ // Start the client.
2201+ assert .NoError (t , client .Start (context .Background (), settings ))
2202+
2203+ // Verify the flags were delivered to the server during the first message.
2204+ eventually (
2205+ t ,
2206+ func () bool {
2207+ msg , ok := rcvCustomFlags .Load ().(uint64 )
2208+ if ! ok || msg == 0 {
2209+ return false
2210+ }
2211+ return uint64 (flags ) == msg
2212+ },
2213+ )
2214+
2215+ // Shutdown the Server.
2216+ srv .Close ()
2217+
2218+ // Shutdown the client.
2219+ err := client .Stop (context .Background ())
2220+ assert .NoError (t , err )
2221+ })
2222+ }
0 commit comments