@@ -370,3 +370,151 @@ func (t *clientTestServer) sendResponse(messageID string, responseErr error,
370370 }
371371 }
372372}
373+
374+ func TestClient_SetConfig (t * testing.T ) {
375+ lggr := logger .Test (t )
376+ capabilityID := "[email protected] " 377+
378+ // Create broker and dispatcher like other tests
379+ broker := newTestAsyncMessageBroker (t , 100 )
380+ peerID := NewP2PPeerID (t )
381+ dispatcher := broker .NewDispatcherForNode (peerID )
382+ client := executable .NewClient (capabilityID , "execute" , dispatcher , lggr )
383+
384+ // Create valid test data
385+ validCapInfo := commoncap.CapabilityInfo {
386+ ID : capabilityID ,
387+ CapabilityType : commoncap .CapabilityTypeAction ,
388+ Description : "Test capability" ,
389+ }
390+
391+ validDonInfo := commoncap.DON {
392+ ID : 1 ,
393+ Members : []p2ptypes.PeerID {NewP2PPeerID (t )},
394+ F : 0 ,
395+ }
396+
397+ validTimeout := 30 * time .Second
398+
399+ t .Run ("successful config set" , func (t * testing.T ) {
400+ transmissionConfig := & transmission.TransmissionConfig {
401+ Schedule : transmission .Schedule_OneAtATime ,
402+ DeltaStage : 10 * time .Millisecond ,
403+ }
404+
405+ err := client .SetConfig (validCapInfo , validDonInfo , validTimeout , transmissionConfig )
406+ require .NoError (t , err )
407+
408+ // Verify config was set
409+ info , err := client .Info (context .Background ())
410+ require .NoError (t , err )
411+ assert .Equal (t , validCapInfo .ID , info .ID )
412+ })
413+
414+ t .Run ("mismatched capability ID" , func (t * testing.T ) {
415+ invalidCapInfo := commoncap.CapabilityInfo {
416+ 417+ CapabilityType : commoncap .CapabilityTypeAction ,
418+ }
419+
420+ err := client .SetConfig (invalidCapInfo , validDonInfo , validTimeout , nil )
421+ require .Error (t , err )
422+ assert .Contains (t , err .Error (), "capability info provided does not match the client's capabilityID" )
423+ assert .
Contains (
t ,
err .
Error (),
"[email protected] != [email protected] " )
424+ })
425+
426+ t .Run ("empty DON members" , func (t * testing.T ) {
427+ invalidDonInfo := commoncap.DON {
428+ ID : 1 ,
429+ Members : []p2ptypes.PeerID {},
430+ F : 0 ,
431+ }
432+
433+ err := client .SetConfig (validCapInfo , invalidDonInfo , validTimeout , nil )
434+ require .Error (t , err )
435+ assert .Contains (t , err .Error (), "empty localDonInfo provided" )
436+ })
437+
438+ t .Run ("successful config update" , func (t * testing.T ) {
439+ // Set initial config
440+ initialTimeout := 10 * time .Second
441+ err := client .SetConfig (validCapInfo , validDonInfo , initialTimeout , nil )
442+ require .NoError (t , err )
443+
444+ // Replace with new config
445+ newTimeout := 60 * time .Second
446+ newDonInfo := commoncap.DON {
447+ ID : 2 ,
448+ Members : []p2ptypes.PeerID {NewP2PPeerID (t ), NewP2PPeerID (t )},
449+ F : 1 ,
450+ }
451+
452+ err = client .SetConfig (validCapInfo , newDonInfo , newTimeout , nil )
453+ require .NoError (t , err )
454+
455+ // Verify the config was completely replaced
456+ info , err := client .Info (context .Background ())
457+ require .NoError (t , err )
458+ assert .Equal (t , validCapInfo .ID , info .ID )
459+ })
460+ }
461+
462+ func TestClient_SetConfig_StartClose (t * testing.T ) {
463+ ctx := testutils .Context (t )
464+ lggr := logger .Test (t )
465+ capabilityID := "[email protected] " 466+
467+ // Create broker and dispatcher like other tests
468+ broker := newTestAsyncMessageBroker (t , 100 )
469+ peerID := NewP2PPeerID (t )
470+ dispatcher := broker .NewDispatcherForNode (peerID )
471+ client := executable .NewClient (capabilityID , "execute" , dispatcher , lggr )
472+
473+ validCapInfo := commoncap.CapabilityInfo {
474+ ID : capabilityID ,
475+ CapabilityType : commoncap .CapabilityTypeAction ,
476+ Description : "Test capability" ,
477+ }
478+
479+ validDonInfo := commoncap.DON {
480+ ID : 1 ,
481+ Members : []p2ptypes.PeerID {NewP2PPeerID (t )},
482+ F : 0 ,
483+ }
484+
485+ validTimeout := 30 * time .Second
486+
487+ t .Run ("start fails without config" , func (t * testing.T ) {
488+ clientWithoutConfig := executable .NewClient (capabilityID , "execute" , dispatcher , lggr )
489+ err := clientWithoutConfig .Start (ctx )
490+ require .Error (t , err )
491+ assert .Contains (t , err .Error (), "config not set - call SetConfig() before Start()" )
492+ })
493+
494+ t .Run ("start succeeds after config set" , func (t * testing.T ) {
495+ require .NoError (t , client .SetConfig (validCapInfo , validDonInfo , validTimeout , nil ))
496+ require .NoError (t , client .Start (ctx ))
497+ require .NoError (t , client .Close ())
498+ })
499+
500+ t .Run ("config can be updated after start" , func (t * testing.T ) {
501+ // Create a fresh client for this test since services can only be started once
502+ freshClient := executable .NewClient (capabilityID , "execute" , dispatcher , lggr )
503+
504+ // Set initial config and start
505+ require .NoError (t , freshClient .SetConfig (validCapInfo , validDonInfo , validTimeout , nil ))
506+ require .NoError (t , freshClient .Start (ctx ))
507+
508+ // Update config while running
509+ validCapInfo .Description = "new description"
510+ require .NoError (t , freshClient .SetConfig (validCapInfo , validDonInfo , validTimeout , nil ))
511+
512+ // Verify config was updated
513+ info , err := freshClient .Info (ctx )
514+ require .NoError (t , err )
515+ assert .Equal (t , validCapInfo .Description , info .Description )
516+
517+ // Clean up
518+ require .NoError (t , freshClient .Close ())
519+ })
520+ }
0 commit comments