From b879cb8faa0876c8e567f9d55263b10732cca388 Mon Sep 17 00:00:00 2001 From: MohitKatare-protean Date: Tue, 1 Apr 2025 15:26:38 +0530 Subject: [PATCH] updated code as per the review comment 1. changed struct to httpConfig 2. Removed initLogger func var 3. Removed mocked registryClient from registry test code --- cmd/adapter/main.go | 7 +++---- cmd/adapter/main_test.go | 10 +++++----- core/module/client/registry_test.go | 24 +++++++----------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/cmd/adapter/main.go b/cmd/adapter/main.go index a581a59..1c88c60 100644 --- a/cmd/adapter/main.go +++ b/cmd/adapter/main.go @@ -25,10 +25,10 @@ type Config struct { Log log.Config `yaml:"log"` PluginManager *plugin.ManagerConfig `yaml:"pluginManager"` Modules []module.Config `yaml:"modules"` - HTTP timeouts `yaml:"http"` + HTTP httpConfig `yaml:"http"` } -type timeouts struct { +type httpConfig struct { Port string `yaml:"port"` Timeouts timeoutConfig `yaml:"timeout"` } @@ -103,7 +103,6 @@ func newServer(ctx context.Context, mgr handler.PluginManager, cfg *Config) (htt var newManagerFunc = plugin.NewManager var newServerFunc = newServer -var initLoggerFunc = log.InitLogger // run encapsulates the application logic. func run(ctx context.Context, configPath string) error { @@ -114,7 +113,7 @@ func run(ctx context.Context, configPath string) error { return fmt.Errorf("failed to initialize config: %w", err) } log.Infof(ctx, "Initializing logger with config: %+v", cfg.Log) - if err := initLoggerFunc(cfg.Log); err != nil { + if err := log.InitLogger(cfg.Log); err != nil { return fmt.Errorf("failed to initialize logger: %w", err) } diff --git a/cmd/adapter/main_test.go b/cmd/adapter/main_test.go index b1b15d1..8bfd24e 100644 --- a/cmd/adapter/main_test.go +++ b/cmd/adapter/main_test.go @@ -364,7 +364,7 @@ func TestNewServerSuccess(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := &Config{ Modules: tt.modules, - HTTP: timeouts{ + HTTP: httpConfig{ Port: "8080", Timeouts: timeoutConfig{ Read: 5, @@ -409,7 +409,7 @@ func TestNewServerFailure(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := &Config{ Modules: tt.modules, - HTTP: timeouts{ + HTTP: httpConfig{ Port: "8080", Timeouts: timeoutConfig{ Read: 5, @@ -441,7 +441,7 @@ func TestValidateConfigSuccess(t *testing.T) { name: "Valid Config", cfg: Config{ AppName: "TestApp", - HTTP: timeouts{ + HTTP: httpConfig{ Port: "8080", }, }, @@ -469,7 +469,7 @@ func TestValidateConfigFailure(t *testing.T) { name: "Missing AppName", cfg: Config{ AppName: "", - HTTP: timeouts{ + HTTP: httpConfig{ Port: "8080", }, }, @@ -479,7 +479,7 @@ func TestValidateConfigFailure(t *testing.T) { name: "Missing Port", cfg: Config{ AppName: "TestApp", - HTTP: timeouts{ + HTTP: httpConfig{ Port: "", }, }, diff --git a/core/module/client/registry_test.go b/core/module/client/registry_test.go index 608c328..ac05c14 100644 --- a/core/module/client/registry_test.go +++ b/core/module/client/registry_test.go @@ -13,16 +13,6 @@ import ( "github.com/stretchr/testify/require" ) -// MockRegistryClient is a mock implementation of the RegistryClient. -type MockRegistryClient struct { - SubscribeFunc func(ctx context.Context, subscription *model.Subscription) error -} - -// Subscribe calls the mock Subscribe function. -func (m *MockRegistryClient) Subscribe(ctx context.Context, subscription *model.Subscription) error { - return m.SubscribeFunc(ctx, subscription) -} - // TestSubscribeSuccess verifies that the Subscribe function succeeds when the server responds with HTTP 200. func TestSubscribeSuccess(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -84,11 +74,11 @@ func TestSubscribeFailure(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - mockClient := &MockRegistryClient{ - SubscribeFunc: func(ctx context.Context, subscription *model.Subscription) error { - return tt.mockError - }, - } + client := NewRegisteryClient(&Config{ + RetryMax: 1, + RetryWaitMin: 1 * time.Millisecond, + RetryWaitMax: 2 * time.Millisecond, + }) subscription := &model.Subscription{ KeyID: "test-key", @@ -103,7 +93,7 @@ func TestSubscribeFailure(t *testing.T) { subscription = &model.Subscription{} // Example of an invalid object } - err := mockClient.Subscribe(context.Background(), subscription) + err := client.Subscribe(context.Background(), subscription) require.Error(t, err) // Directly checking for an error since all cases should fail }) } @@ -217,7 +207,7 @@ func TestLookupFailure(t *testing.T) { config := &Config{ RegisteryURL: server.URL, - RetryMax: 0, // Prevent excessive retries + RetryMax: 0, RetryWaitMin: 1 * time.Millisecond, RetryWaitMax: 2 * time.Millisecond, }