Skip to content

Commit 27300a9

Browse files
committed
refactor: rename interfaces and update related mock implementations for clarity
1 parent 30e1247 commit 27300a9

File tree

4 files changed

+128
-127
lines changed

4 files changed

+128
-127
lines changed

pkg/gofr/config/remote_config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package config
22

3-
// RemoteConfigurable represents any component that can be updated at runtime.
4-
type RemoteConfigurable interface {
3+
// Subscriber represents any component that can be updated at runtime.
4+
type Subscriber interface {
55
UpdateConfig(config map[string]any)
66
}
77

8-
// RemoteConfiguration represents a runtime config provider.
9-
type RemoteConfiguration interface {
10-
Register(c RemoteConfigurable)
8+
// Provider represents a runtime config provider.
9+
type Provider interface {
10+
Register(c Subscriber)
1111
Start()
1212
}

pkg/gofr/config/remote_config_test.go

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

pkg/gofr/logging/remotelogger/dynamic_level_logger.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (f *httpLogFilter) Log(args ...any) {
7676
}
7777

7878
func (f *httpLogFilter) handleHTTPLog(httpLog *service.Log, args []any) {
79-
// Log initialization message if not already logged
79+
// Log initialization message if not already logged.
8080
f.mu.Lock()
8181
notLoggedYet := !f.initLogged
8282

@@ -104,7 +104,7 @@ func (f *httpLogFilter) handleHTTPLog(httpLog *service.Log, args []any) {
104104
f.mu.Unlock()
105105
f.Logger.Log(args...)
106106

107-
// Subsequent successful hits - log at DEBUG level with consistent format
107+
// Subsequent successful hits - log at DEBUG level with consistent format.
108108
case isSuccessful:
109109
msg := httpDebugMsg{
110110
CorrelationID: httpLog.CorrelationID,
@@ -115,7 +115,7 @@ func (f *httpLogFilter) handleHTTPLog(httpLog *service.Log, args []any) {
115115
}
116116
f.Logger.Debug(msg)
117117

118-
// Error responses - pass through to original logger
118+
// Error responses - pass through to original logger.
119119
default:
120120
f.Logger.Log(args...)
121121
}
@@ -314,18 +314,18 @@ type httpRemoteConfig struct {
314314
url string
315315
interval time.Duration
316316
logger logging.Logger
317-
clients []config.RemoteConfigurable
317+
clients []config.Subscriber
318318
}
319319

320-
func NewHTTPRemoteConfig(url string, interval time.Duration, logger logging.Logger) config.RemoteConfiguration {
320+
func NewHTTPRemoteConfig(url string, interval time.Duration, logger logging.Logger) config.Provider {
321321
return &httpRemoteConfig{
322322
url: url,
323323
interval: interval,
324324
logger: logger,
325325
}
326326
}
327327

328-
func (h *httpRemoteConfig) Register(c config.RemoteConfigurable) {
328+
func (h *httpRemoteConfig) Register(c config.Subscriber) {
329329
h.clients = append(h.clients, c)
330330
}
331331

@@ -365,12 +365,12 @@ func (h *httpRemoteConfig) Start() {
365365

366366
func fetchRemoteConfig(remoteService service.HTTP) (map[string]any, error) {
367367
if newLogLevelStr, err := fetchLogLevelStr(remoteService); err != nil {
368-
return map[string]any{}, err
368+
return nil, err
369369
} else if newLogLevelStr != "" {
370370
return map[string]any{
371371
"logLevel": newLogLevelStr,
372372
}, nil
373373
}
374374

375-
return map[string]any{}, nil
375+
return nil, nil
376376
}

0 commit comments

Comments
 (0)