Skip to content

Commit

Permalink
add one more test function
Browse files Browse the repository at this point in the history
  • Loading branch information
Umang01-hash committed Jun 28, 2024
1 parent 089b4e6 commit 929a83c
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions pkg/gofr/logging/remotelogger/dynamicLevelLogger_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package remotelogger

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"gofr.dev/pkg/gofr/logging"
"gofr.dev/pkg/gofr/service"
"gofr.dev/pkg/gofr/testutil"
Expand Down Expand Up @@ -53,16 +50,29 @@ func TestRemoteLogger_UpdateLevelError(t *testing.T) {
assert.Equal(t, logging.INFO, rl.currentLevel)
}

func Test_fetchAndUpdateLogLevel_ErrorCases(t *testing.T) {
func Test_fetchAndUpdateLogLevel_InvalidResponse(t *testing.T) {
logger := logging.NewMockLogger(logging.INFO)

ctrl := gomock.NewController(t)
mockMetrics := service.NewMockMetrics(ctrl)
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")

body := `{ "data": { "serviceName": "test-service","logLevel":"TEST" } }`

_, _ = w.Write([]byte(body))
}))
defer mockServer.Close()

remoteService := service.NewHTTPService(mockServer.URL, logger, nil)

level, err := fetchAndUpdateLogLevel(remoteService, logging.DEBUG)

mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_http_service_response", gomock.Any(), "path", gomock.Any(),
"method", http.MethodGet, "status", fmt.Sprintf("%v", http.StatusInternalServerError))
assert.Equal(t, logging.DEBUG, level, "Test_fetchAndUpdateLogLevel_InvalidResponse, Failed.\n")

assert.Nil(t, err)
}

remoteService := service.NewHTTPService("http://", logger, mockMetrics)
func Test_fetchAndUpdateLogLevel_InvalidLogLevel(t *testing.T) {
logger := logging.NewMockLogger(logging.INFO)

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand All @@ -81,22 +91,11 @@ func Test_fetchAndUpdateLogLevel_ErrorCases(t *testing.T) {

remoteService2 := service.NewHTTPService(mockServer.URL, logger, nil)

tests := []struct {
desc string
remoteService service.HTTP
currentLogLevel logging.Level
}{
{"invalid URL for remote service", remoteService, logging.INFO},
{"invalid response from remote service", remoteService2, logging.DEBUG},
}

for i, tc := range tests {
level, err := fetchAndUpdateLogLevel(tc.remoteService, tc.currentLogLevel)
level, err := fetchAndUpdateLogLevel(remoteService2, logging.DEBUG)

assert.Equal(t, tc.currentLogLevel, level, "TEST[%d], Failed.\n%s", i, tc.desc)
assert.Equal(t, logging.DEBUG, level, "Test_fetchAndUpdateLogLevel_InvalidResponse, Failed.\n")

assert.NotNil(t, err)
}
assert.NotNil(t, err)
}

func TestDynamicLoggerSuccess(t *testing.T) {
Expand Down

0 comments on commit 929a83c

Please sign in to comment.