Skip to content

Commit

Permalink
fix: apply gofmt and added Nunos suggested test for client data
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Jul 29, 2024
1 parent f78b2c5 commit 3459bb7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 30 deletions.
1 change: 0 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func NewClient(options ...ConfigOption) (*Client, error) {
httpClient: uc.options.httpClient,
customHeaders: uc.options.customHeaders,
disableMetrics: uc.options.disableMetrics,
started: uc.options.started,
},
metricsChannels{
errorChannels: errChannels,
Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type configOption struct {
storage Storage
httpClient *http.Client
customHeaders http.Header
started *time.Time
}

// ConfigOption represents a option for configuring the client.
Expand Down
1 change: 1 addition & 0 deletions internal/strategies/gradual_rollout_random_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build norace
// +build norace

package strategies
Expand Down
1 change: 1 addition & 0 deletions internal/strategies/gradual_rollout_session_id_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build norace
// +build norace

package strategies
Expand Down
1 change: 1 addition & 0 deletions internal/strategies/gradual_rollout_user_id_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build norace
// +build norace

package strategies
Expand Down
63 changes: 47 additions & 16 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import (
"github.com/stretchr/testify/mock"
)

func WithStarted(startedAt time.Time) ConfigOption {
return func(o *configOption) {
o.started = &startedAt
}
}

func TestMetrics_RegisterInstance(t *testing.T) {
assert := assert.New(t)
defer gock.OffAll()
Expand Down Expand Up @@ -436,7 +430,7 @@ func TestMetrics_ErrorCountShouldDecreaseIfSuccessful(t *testing.T) {
assert.Nil(err, "Client should close without a problem")
}

func TestMetrics_ClientDataIncludesNewMetadata(t *testing.T) {
func clientDataMatcher() func(req *http.Request, ereq *gock.Request) (bool, error) {
defaultStrategies := []string{
"default",
"applicationHostname",
Expand All @@ -447,30 +441,67 @@ func TestMetrics_ClientDataIncludesNewMetadata(t *testing.T) {
"userWithId",
"flexibleRollout",
}
assert := assert.New(t)
started := time.Now()
defer gock.OffAll()
gock.New(mockerServer).
Post("/client/register").
JSON(ClientData{
return func(req *http.Request, ereq *gock.Request) (bool, error) {
var data ClientData
err := json.NewDecoder(req.Body).Decode(&data)
if err != nil {
return false, err
}

if data.Started.IsZero() {
return false, nil
}

expectedData := ClientData{
AppName: mockAppName,
InstanceID: mockInstanceId,
SDKVersion: fmt.Sprintf("%s:%s", clientName, clientVersion),
Strategies: defaultStrategies,
Started: started,
Interval: 0,
PlatformVersion: runtime.Version(),
PlatformName: "go",
YggdrasilVersion: nil,
SpecVersion: specVersion,
}).
}

return data.AppName == expectedData.AppName &&
data.InstanceID == expectedData.InstanceID &&
data.SDKVersion == expectedData.SDKVersion &&
compareStringSlices(data.Strategies, expectedData.Strategies) &&
data.Interval == expectedData.Interval &&
data.PlatformVersion == expectedData.PlatformVersion &&
data.PlatformName == expectedData.PlatformName &&
data.YggdrasilVersion == expectedData.YggdrasilVersion &&
data.SpecVersion == expectedData.SpecVersion, nil
}
}

func compareStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func TestMetrics_ClientDataIncludesNewMetadata(t *testing.T) {
assert := assert.New(t)
defer gock.OffAll()

gock.New(mockerServer).
Post("/client/register").
AddMatcher(clientDataMatcher()).
Reply(200)

client, err := NewClient(
WithUrl(mockerServer),
WithMetricsInterval(50*time.Millisecond),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithStarted(started),
)

assert.Nil(err, "Client should open without a problem")
Expand Down
23 changes: 11 additions & 12 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,19 @@ func TestRepository_ParseAPIResponse(t *testing.T) {
assert.Equal(0, len(response.Segments))
}


func TestRepository_backs_off_on_http_statuses(t *testing.T) {
a := assert.New(t)
testCases := []struct {
statusCode int
errorCount float64
}{
{ 401, 10},
{ 403, 10},
{ 404, 10},
{ 429, 1},
{ 500, 1},
{ 502, 1},
{ 503, 1},
{401, 10},
{403, 10},
{404, 10},
{429, 1},
{500, 1},
{502, 1},
{503, 1},
}
defer gock.Off()
for _, tc := range testCases {
Expand All @@ -155,7 +154,7 @@ func TestRepository_backs_off_on_http_statuses(t *testing.T) {
WithAppName(mockAppName),
WithDisableMetrics(true),
WithInstanceId(mockInstanceId),
WithRefreshInterval(time.Millisecond * 15),
WithRefreshInterval(time.Millisecond*15),
)
a.Nil(err)
time.Sleep(20 * time.Millisecond)
Expand All @@ -168,8 +167,8 @@ func TestRepository_back_offs_are_gradually_reduced_on_success(t *testing.T) {
a := assert.New(t)
defer gock.Off()
gock.New(mockerServer).
Get("/client/features").
Times(4).
Get("/client/features").
Times(4).
Reply(429)
gock.New(mockerServer).
Get("/client/features").
Expand All @@ -180,7 +179,7 @@ func TestRepository_back_offs_are_gradually_reduced_on_success(t *testing.T) {
WithAppName(mockAppName),
WithDisableMetrics(true),
WithInstanceId(mockInstanceId),
WithRefreshInterval(time.Millisecond * 10),
WithRefreshInterval(time.Millisecond*10),
)
a.Nil(err)
client.WaitForReady()
Expand Down

0 comments on commit 3459bb7

Please sign in to comment.