@@ -4,45 +4,45 @@ import (
44 "sync"
55 "testing"
66 "time"
7+
8+ "github.com/stretchr/testify/assert"
79)
810
9- // mockRemoteConfigurable is a mock that records updates for verification.
10- type mockRemoteConfigurable struct {
11+ type mockConfigSubscriber struct {
1112 mu sync.Mutex
1213 updatedCount int
1314 lastConfig map [string ]any
1415}
1516
16- func (m * mockRemoteConfigurable ) UpdateConfig (cfg map [string ]any ) {
17+ func (m * mockConfigSubscriber ) UpdateConfig (cfg map [string ]any ) {
1718 m .mu .Lock ()
1819 defer m .mu .Unlock ()
1920
2021 m .updatedCount ++
2122 m .lastConfig = cfg
2223}
2324
24- // mockRemoteConfiguration simulates a runtime configuration manager.
25- type mockRemoteConfiguration struct {
25+ type mockConfigProvider struct {
2626 mu sync.Mutex
27- registered []RemoteConfigurable
27+ registered []Subscriber
2828 started bool
2929 startTrigger chan map [string ]any
3030}
3131
32- func newMockRemoteConfiguration () * mockRemoteConfiguration {
33- return & mockRemoteConfiguration {
32+ func newMockConfigProvider () * mockConfigProvider {
33+ return & mockConfigProvider {
3434 startTrigger : make (chan map [string ]any , 1 ),
3535 }
3636}
3737
38- func (m * mockRemoteConfiguration ) Register (c RemoteConfigurable ) {
38+ func (m * mockConfigProvider ) Register (c Subscriber ) {
3939 m .mu .Lock ()
4040 defer m .mu .Unlock ()
4141
4242 m .registered = append (m .registered , c )
4343}
4444
45- func (m * mockRemoteConfiguration ) Start () {
45+ func (m * mockConfigProvider ) Start () {
4646 m .mu .Lock ()
4747 m .started = true
4848 m .mu .Unlock ()
@@ -60,68 +60,56 @@ func (m *mockRemoteConfiguration) Start() {
6060 }()
6161}
6262
63- func (m * mockRemoteConfiguration ) pushConfig (cfg map [string ]any ) {
63+ func (m * mockConfigProvider ) pushConfig (cfg map [string ]any ) {
6464 m .startTrigger <- cfg
6565}
6666
67- func TestRemoteConfiguration_RegisterAndStart (t * testing.T ) {
68- rc := newMockRemoteConfiguration ()
69- c1 := & mockRemoteConfigurable {}
70- c2 := & mockRemoteConfigurable {}
67+ func TestConfigProvider_RegisterAndStart (t * testing.T ) {
68+ rc := newMockConfigProvider ()
69+ c1 := & mockConfigSubscriber {}
70+ c2 := & mockConfigSubscriber {}
7171
7272 rc .Register (c1 )
7373 rc .Register (c2 )
7474
7575 rc .Start ()
7676
77- // Simulate runtime config update
78- cfg := map [string ]any {"log_level" : "DEBUG" }
79- rc .pushConfig (cfg )
80-
81- // Wait briefly for goroutine to deliver update
77+ rc .pushConfig (map [string ]any {"log_level" : "DEBUG" })
8278 time .Sleep (50 * time .Millisecond )
8379
84- if c1 .updatedCount != 1 || c2 .updatedCount != 1 {
85- t .Fatalf ("expected both configurables to be updated once, got c1=%d, c2=%d" , c1 .updatedCount , c2 .updatedCount )
86- }
87-
88- if c1 .lastConfig ["log_level" ] != "DEBUG" {
89- t .Errorf ("expected c1 to receive correct config value, got %+v" , c1 .lastConfig )
90- }
91-
92- if ! rc .started {
93- t .Errorf ("expected configuration Start() to set started=true" )
94- }
80+ assert .Equal (t , 1 , c1 .updatedCount , "c1 update count" )
81+ assert .Equal (t , 1 , c2 .updatedCount , "c2 update count" )
82+ assert .Equal (t , "DEBUG" , c1 .lastConfig ["log_level" ], "c1 config value" )
83+ assert .True (t , rc .started , "provider started" )
9584}
9685
97- func TestRemoteConfiguration_NoRegisteredComponents (_ * testing.T ) {
98- rc := newMockRemoteConfiguration ()
86+ func TestConfigProvider_NoRegisteredComponents (_ * testing.T ) {
87+ rc := newMockConfigProvider ()
9988 rc .Start ()
10089
101- cfg := map [string ]any {"feature" : "enabled" }
102- rc .pushConfig (cfg )
103-
104- // Should not panic even if no components are registered.
90+ rc .pushConfig (map [string ]any {"feature" : "enabled" })
10591 time .Sleep (20 * time .Millisecond )
10692}
10793
108- func TestRemoteConfigurable_UpdateConfigCalledMultipleTimes (t * testing.T ) {
109- rc := newMockRemoteConfiguration ()
110- c := & mockRemoteConfigurable {}
94+ func TestConfigSubscriber_UpdateConfigMultipleTimes (t * testing.T ) {
95+ rc := newMockConfigProvider ()
96+ c := & mockConfigSubscriber {}
97+
11198 rc .Register (c )
11299 rc .Start ()
113100
114- for i := 0 ; i < 3 ; i ++ {
101+ expectedLast := 0
102+
103+ for i := range 3 {
115104 rc .pushConfig (map [string ]any {"version" : i })
105+ expectedLast = i
116106 }
117107
118108 time .Sleep (100 * time .Millisecond )
119109
120- if c .updatedCount != 3 {
121- t .Fatalf ("expected 3 updates, got %d" , c .updatedCount )
122- }
110+ assert .Equal (t , 3 , c .updatedCount , "update count" )
123111
124- if c .lastConfig ["version" ] != 2 {
125- t . Errorf ( "expected last config version 2, got %v" , c . lastConfig [ "version" ] )
126- }
112+ version , ok := c .lastConfig ["version" ].( int )
113+ assert . True ( t , ok )
114+ assert . Equal ( t , expectedLast , version , "last version" )
127115}
0 commit comments