Skip to content

Commit 1b0dc9a

Browse files
author
Dave Johnston
committed
(MAINT) Fix linter issues
1 parent c7673ef commit 1b0dc9a

File tree

6 files changed

+51
-49
lines changed

6 files changed

+51
-49
lines changed

client/client.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
6565
sdkKey: sdkKey,
6666
config: config,
6767
authenticated: make(chan struct{}),
68-
initialized: make(chan bool),
68+
initialized: make(chan bool),
6969
}
7070
ctx, client.cancelFunc = context.WithCancel(context.Background())
7171

@@ -94,14 +94,16 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
9494
return client, nil
9595
}
9696

97+
// IsInitialized determines if the client is ready to be used. This is true if it has both authenticated
98+
// and successfully retrived flags. If it takes longer than 30 seconds the call will timeout and return an error.
9799
func (c *CfClient) IsInitialized() (bool, error) {
98100
select {
99101
case <-c.initialized:
100102
return true, nil
101-
case <-time.After(30*time.Second):
103+
case <-time.After(30 * time.Second):
102104
break
103105
}
104-
return false, fmt.Errorf("Timeout waiting to initialize\n")
106+
return false, fmt.Errorf("timeout waiting to initialize")
105107
}
106108

107109
func (c *CfClient) retrieve(ctx context.Context) {
@@ -126,7 +128,7 @@ func (c *CfClient) retrieve(ctx context.Context) {
126128
}
127129
}()
128130
wg.Wait()
129-
c.initialized<-true
131+
c.initialized <- true
130132
c.config.Logger.Info("Sync run finished")
131133
}
132134

@@ -165,7 +167,6 @@ func (c *CfClient) authenticate(ctx context.Context) {
165167
c.mux.RLock()
166168
defer c.mux.RUnlock()
167169

168-
169170
// dont check err just retry
170171
httpClient, err := rest.NewClientWithResponses(c.config.url, rest.WithHTTPClient(c.config.httpClient))
171172
if err != nil {
@@ -497,4 +498,4 @@ func checkPreRequisite(client *CfClient, featureConfig *evaluation.FeatureConfig
497498
}
498499

499500
return result
500-
}
501+
}

client/client_test.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ package client_test
22

33
import (
44
"encoding/json"
5+
"net/http"
6+
"os"
7+
"testing"
8+
59
"github.com/drone/ff-golang-server-sdk/client"
610
"github.com/drone/ff-golang-server-sdk/dto"
711
"github.com/drone/ff-golang-server-sdk/evaluation"
812
"github.com/drone/ff-golang-server-sdk/rest"
913
"github.com/jarcoal/httpmock"
1014
"github.com/stretchr/testify/assert"
11-
"net/http"
12-
"testing"
13-
"os"
1415
)
1516

1617
const (
1718
sdkKey = "27bed8d2-2610-462b-90eb-d80fd594b623"
18-
URL = "http://localhost/api/1.0"
19+
URL = "http://localhost/api/1.0"
20+
//nolint
1921
AuthToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0IjoiMTA0MjM5NzYtODQ1MS00NmZjLTg2NzctYmNiZDM3MTA3M2JhIiwiZW52aXJvbm1lbnQiOiI3ZWQxMDI1ZC1hOWIxLTQxMjktYTg4Zi1lMjdlZjM2MDk4MmQiLCJwcm9qZWN0SWRlbnRpZmllciI6IiIsImVudmlyb25tZW50SWRlbnRpZmllciI6IlByZVByb2R1Y3Rpb24iLCJhY2NvdW50SUQiOiIiLCJvcmdhbml6YXRpb24iOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAifQ.z6EYSDVWwwAY6OTc2PnjSub43R6lOSJywlEObi6PDqQ"
20-
2122
)
2223

2324
// TestMain runs before the other tests
@@ -61,13 +62,14 @@ func TestCfClient_BoolVariation(t *testing.T) {
6162
{"Test Default True Flag when Pre-Req is True returns true", args{"TestTrueOnWithPreReqTrue", target, true}, true, false},
6263
}
6364
for _, tt := range tests {
64-
t.Run(tt.name, func(t *testing.T) {
65-
flag, err := client.BoolVariation(tt.args.key, tt.args.target, tt.args.defaultValue)
66-
if (err != nil) != tt.wantErr {
67-
t.Errorf("BoolVariation() error = %v, wantErr %v", err, tt.wantErr)
65+
test := tt
66+
t.Run(test.name, func(t *testing.T) {
67+
flag, err := client.BoolVariation(test.args.key, test.args.target, test.args.defaultValue)
68+
if (err != nil) != test.wantErr {
69+
t.Errorf("BoolVariation() error = %v, wantErr %v", err, test.wantErr)
6870
return
6971
}
70-
assert.Equal(t, tt.want, flag, "%s didn't get expected value", tt.name)
72+
assert.Equal(t, test.want, flag, "%s didn't get expected value", test.name)
7173
})
7274
}
7375
}
@@ -89,7 +91,6 @@ func TestCfClient_StringVariation(t *testing.T) {
8991
args args
9092
want string
9193
wantErr bool
92-
9394
}{
9495
{"Test Invalid Flag Name returns default value", args{"MadeUpIDontExist", target, "foo"}, "foo", false},
9596
{"Test Default String Flag with when On returns A", args{"TestStringAOn", target, "foo"}, "A", false},
@@ -98,13 +99,14 @@ func TestCfClient_StringVariation(t *testing.T) {
9899
{"Test Default String Flag when Pre-Req is True returns A", args{"TestStringAOnWithPreReqTrue", target, "foo"}, "A", false},
99100
}
100101
for _, tt := range tests {
101-
t.Run(tt.name, func(t *testing.T) {
102-
flag, err := client.StringVariation(tt.args.key, tt.args.target, tt.args.defaultValue)
103-
if (err != nil) != tt.wantErr {
104-
t.Errorf("BoolVariation() error = %v, wantErr %v", err, tt.wantErr)
102+
test := tt
103+
t.Run(test.name, func(t *testing.T) {
104+
flag, err := client.StringVariation(test.args.key, test.args.target, test.args.defaultValue)
105+
if (err != nil) != test.wantErr {
106+
t.Errorf("BoolVariation() error = %v, wantErr %v", err, test.wantErr)
105107
return
106108
}
107-
assert.Equal(t, tt.want, flag, "%s didn't get expected value", tt.name)
109+
assert.Equal(t, test.want, flag, "%s didn't get expected value", test.name)
108110
})
109111
}
110112
}
@@ -126,7 +128,6 @@ func MakeNewClientAndTarget() (*client.CfClient, *evaluation.Target, error) {
126128
return client, target, nil
127129
}
128130

129-
130131
// newClient creates a new client with some default options
131132
func newClient(httpClient *http.Client) (*client.CfClient, error) {
132133
return client.NewCfClient(sdkKey,
@@ -149,7 +150,7 @@ func target() *evaluation.Target {
149150

150151
var ValidAuthResponse = func(req *http.Request) (*http.Response, error) {
151152
return httpmock.NewJsonResponse(200, rest.AuthenticationResponse{
152-
AuthToken: AuthToken})
153+
AuthToken: AuthToken})
153154
}
154155

155156
var TargetSegmentsResponse = func(req *http.Request) (*http.Response, error) {
@@ -197,4 +198,4 @@ var FeatureConfigsResponse = func(req *http.Request) (*http.Response, error) {
197198
FeatureConfigResponse = append(FeatureConfigResponse, MakeStringFeatureConfigs("TestStringAOnWithPreReqTrue", "Alpha", "Bravo", "on", MakeBoolPreRequisite("PreReq1", "true"))...)
198199

199200
return httpmock.NewJsonResponse(200, FeatureConfigResponse)
200-
}
201+
}

client/config.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type config struct {
1616
Cache cache.Cache
1717
Store storage.Storage
1818
Logger logger.Logger
19-
httpClient *http.Client
19+
httpClient *http.Client
2020
enableStream bool
21-
enableStore bool
21+
enableStore bool
2222
}
2323

2424
func newDefaultConfig() *config {
@@ -29,7 +29,6 @@ func newDefaultConfig() *config {
2929
defaultCache, _ := cache.NewLruCache(10000, defaultLogger) // size of cache
3030
defaultStore := storage.NewFileStore("defaultProject", storage.GetHarnessDir(), defaultLogger)
3131

32-
3332
retryClient := retryablehttp.NewClient()
3433
retryClient.RetryMax = 10
3534

@@ -41,6 +40,6 @@ func newDefaultConfig() *config {
4140
Logger: defaultLogger,
4241
httpClient: retryClient.StandardClient(),
4342
enableStream: true,
44-
enableStore: true,
43+
enableStore: true,
4544
}
4645
}

client/helpers_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package client_test
22

33
import (
44
"fmt"
5+
"net/http"
6+
57
"github.com/drone/ff-golang-server-sdk/rest"
68
"github.com/jarcoal/httpmock"
7-
"net/http"
89
)
910

1011
func MakeBoolFeatureConfigs(name, defaultVariation, offVariation, state string, preReqs ...rest.Prerequisite) []rest.FeatureConfig {
1112
var featureConfig []rest.FeatureConfig
1213
featureConfig = append(featureConfig, MakeBoolFeatureConfig(name, defaultVariation, offVariation, state, preReqs))
1314

1415
// If there are any PreReqs then we need to store them as flags as well.
15-
for _, x := range preReqs{
16+
for _, x := range preReqs {
1617
featureConfig = append(featureConfig, MakeBoolFeatureConfig(x.Feature, "true", "false", x.Variations[0], nil))
1718
}
1819

@@ -24,24 +25,24 @@ func MakeBoolFeatureConfig(name, defaultVariation, offVariation, state string, p
2425
DefaultServe: rest.Serve{
2526
Variation: &defaultVariation,
2627
},
27-
Environment: "PreProduction",
28-
Feature: name,
29-
Kind: "boolean",
28+
Environment: "PreProduction",
29+
Feature: name,
30+
Kind: "boolean",
3031
OffVariation: offVariation,
31-
State: rest.FeatureState(state),
32+
State: rest.FeatureState(state),
3233
Variations: []rest.Variation{
3334
{Identifier: "true", Name: strPtr("True"), Value: "true"},
3435
{Identifier: "false", Name: strPtr("False"), Value: "false"},
3536
},
3637
Prerequisites: &preReqs,
37-
Version: intPtr(1),
38+
Version: intPtr(1),
3839
}
3940
}
4041

4142
func MakeBoolPreRequisite(name string, state string) rest.Prerequisite {
4243
return rest.Prerequisite{
43-
Feature: name,
44-
Variations: []string{state},
44+
Feature: name,
45+
Variations: []string{state},
4546
}
4647
}
4748

@@ -50,14 +51,13 @@ func MakeStringFeatureConfigs(name, defaultVariation, offVariation, state string
5051
featureConfig = append(featureConfig, MakeStringFeatureConfig(name, defaultVariation, offVariation, state, preReqs))
5152

5253
// If there are any PreReqs then we need to store them as flags as well.
53-
for _, x := range preReqs{
54+
for _, x := range preReqs {
5455
featureConfig = append(featureConfig, MakeBoolFeatureConfig(x.Feature, "true", "false", x.Variations[0], nil))
5556
}
5657

5758
return featureConfig
5859
}
5960

60-
6161
/*
6262
{
6363
"defaultServe": {
@@ -85,24 +85,24 @@ func MakeStringFeatureConfigs(name, defaultVariation, offVariation, state string
8585
],
8686
"version": 1
8787
}
88-
*/
88+
*/
8989

9090
func MakeStringFeatureConfig(name, defaultVariation, offVariation, state string, preReqs []rest.Prerequisite) rest.FeatureConfig {
9191
return rest.FeatureConfig{
9292
DefaultServe: rest.Serve{
9393
Variation: &defaultVariation,
9494
},
95-
Environment: "PreProduction",
96-
Feature: name,
97-
Kind: "string",
95+
Environment: "PreProduction",
96+
Feature: name,
97+
Kind: "string",
9898
OffVariation: offVariation,
99-
State: rest.FeatureState(state),
99+
State: rest.FeatureState(state),
100100
Variations: []rest.Variation{
101101
{Identifier: "Alpha", Name: strPtr("Alpha"), Value: "A"},
102102
{Identifier: "Bravo", Name: strPtr("Bravo"), Value: "B"},
103103
},
104104
Prerequisites: &preReqs,
105-
Version: intPtr(1),
105+
Version: intPtr(1),
106106
}
107107
}
108108

@@ -114,6 +114,6 @@ func strPtr(value string) *string {
114114
return &value
115115
}
116116

117-
func jsonError(err error) (*http.Response, error) {
117+
func jsonError(err error) (*http.Response, error) {
118118
return httpmock.NewJsonResponse(500, fmt.Errorf(`{"error" : "%s"}`, err))
119119
}

client/options.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package client
22

33
import (
4+
"net/http"
5+
46
"github.com/drone/ff-golang-server-sdk/cache"
57
"github.com/drone/ff-golang-server-sdk/logger"
68
"github.com/drone/ff-golang-server-sdk/storage"
7-
"net/http"
89
)
910

1011
// ConfigOption is used as return value for advanced client configuration
@@ -66,7 +67,6 @@ func WithStoreEnabled(val bool) ConfigOption {
6667
}
6768
}
6869

69-
7070
// WithHTTPClient set http client for use in interactions with ff server
7171
func WithHTTPClient(client *http.Client) ConfigOption {
7272
return func(config *config) {

evaluation/feature.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evaluation
22

33
import (
44
"encoding/json"
5+
56
"github.com/drone/ff-golang-server-sdk/types"
67

78
"reflect"

0 commit comments

Comments
 (0)