Skip to content

Commit 7abddfc

Browse files
committed
fixes.
1 parent 6ccf3b3 commit 7abddfc

File tree

8 files changed

+54
-20
lines changed

8 files changed

+54
-20
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ env:
88
# may also want to run `go mod edit -go=1.13` to fix go.mod as well
99
- GIMME_GO_VERSION=1.13.x GIMME_OS=linux GIMME_ARCH=amd64
1010

11+
# For unit testing purposes, redis uses the default configuration and is available on localhost.
1112
services:
1213
- redis-server
1314

cmd/optimizely/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func loadConfig(v *viper.Viper) *config.AgentConfig {
8787
conf.Server.Interceptors = interceptors
8888
}
8989

90-
// Check if JSON string was set using OPTIMIZELY_CLIENT_USERPROFILESERVICE
90+
// Check if JSON string was set using OPTIMIZELY_CLIENT_USERPROFILESERVICE environment variable
9191
if userProfileService := v.GetStringMap("client.userprofileservice"); userProfileService != nil {
9292
conf.Client.UserProfileService = userProfileService
9393
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/rakyll/statik v0.1.7
1919
github.com/rs/zerolog v1.18.1-0.20200514152719-663cbb4c8469
2020
github.com/spf13/viper v1.4.0
21-
github.com/stretchr/testify v1.5.1
21+
github.com/stretchr/testify v1.7.0
2222
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
2323
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
2424
gopkg.in/yaml.v2 v2.4.0

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
168168
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
169169
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
170170
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
171-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
172171
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
172+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
173+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
173174
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
174175
github.com/twmb/murmur3 v1.0.0 h1:MLMwMEQRKsu94uJnoveYjjHmcLwI3HNcWXP4LJuNe3I=
175176
github.com/twmb/murmur3 v1.0.0/go.mod h1:5Y5m8Y8WIyucaICVP+Aep5C8ydggjEuRQHDq1icoOYo=
@@ -271,4 +272,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
271272
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
272273
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
273274
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
275+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
276+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
274277
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/handlers/webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (tc *TestCache) UpdateConfigs(_ string) {
5959
tc.updateConfigsCalled = true
6060
}
6161

62-
// SetUserProfileService sets maps userProfileService against the given sdkKey
62+
// SetUserProfileService sets userProfileService to be used for the given sdkKey
6363
func (tc *TestCache) SetUserProfileService(sdkKey, userProfileService string) {
6464
}
6565

pkg/optimizely/cache.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (c *OptlyCache) UpdateConfigs(sdkKey string) {
119119
}
120120
}
121121

122-
// SetUserProfileService sets maps userProfileService against the given sdkKey
122+
// SetUserProfileService sets userProfileService to be used for the given sdkKey
123123
func (c *OptlyCache) SetUserProfileService(sdkKey, userProfileService string) {
124124
c.userProfileServiceMap.SetIfAbsent(sdkKey, userProfileService)
125125
}
@@ -222,17 +222,18 @@ func defaultLoader(
222222
}
223223
}
224224

225+
// Returns the registered userProfileService against the sdkKey
225226
func getUserProfileService(sdkKey string, userProfileServiceMap cmap.ConcurrentMap, conf config.ClientConfig) decision.UserProfileService {
226227

227-
createAndReturnUPSWithName := func(upsName string) decision.UserProfileService {
228+
intializeUPSWithName := func(upsName string) decision.UserProfileService {
228229
if clientConfigUPSMap, ok := conf.UserProfileService["services"].(map[string]interface{}); ok {
229-
if defaultUserProfileServiceMap, ok := clientConfigUPSMap[upsName].(map[string]interface{}); ok {
230+
if userProfileServiceConfig, ok := clientConfigUPSMap[upsName].(map[string]interface{}); ok {
230231
// Check if any such user profile service was added using `Add` method
231232
if creator, ok := userprofileservice.Creators[upsName]; ok {
232233
if upsInstance := creator(); upsInstance != nil {
233234
success := true
234235
// Trying to map userProfileService from client config to struct
235-
if upsConfig, err := json.Marshal(defaultUserProfileServiceMap); err != nil {
236+
if upsConfig, err := json.Marshal(userProfileServiceConfig); err != nil {
236237
log.Warn().Err(err).Msgf(`Error marshaling user profile service config: "%s"`, upsName)
237238
success = false
238239
} else if err := json.Unmarshal(upsConfig, upsInstance); err != nil {
@@ -250,16 +251,16 @@ func getUserProfileService(sdkKey string, userProfileServiceMap cmap.ConcurrentM
250251
return nil
251252
}
252253

253-
// Check if ups type was provided in the request headers
254+
// Check if ups name was provided in the request headers
254255
if ups, ok := userProfileServiceMap.Get(sdkKey); ok {
255256
if upsNameStr, ok := ups.(string); ok && upsNameStr != "" {
256-
return createAndReturnUPSWithName(upsNameStr)
257+
return intializeUPSWithName(upsNameStr)
257258
}
258259
}
259260

260261
// Check if any default user profile service was provided and if it exists in client config
261262
if upsNameStr, ok := conf.UserProfileService["default"].(string); ok && upsNameStr != "" {
262-
return createAndReturnUPSWithName(upsNameStr)
263+
return intializeUPSWithName(upsNameStr)
263264
}
264265
return nil
265266
}

pkg/optimizely/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
type Cache interface {
2626
GetClient(sdkKey string) (*OptlyClient, error)
2727
UpdateConfigs(sdkKey string)
28+
// SetUserProfileService sets userProfileService to be used for the given sdkKey
2829
SetUserProfileService(sdkKey, userProfileService string)
2930
}
3031

plugins/userprofileservice/services/redis_ups_test.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ type RedisUPSTestSuite struct {
3232
func (r *RedisUPSTestSuite) SetupTest() {
3333
// To check if lifo is used by default
3434
r.ups = RedisUserProfileService{
35-
Address: "100",
36-
Password: "10",
37-
Database: 1,
35+
Address: "localhost:6379",
36+
Password: "", // no password set
37+
Database: 0, // use default DB
3838
}
3939
}
4040

41-
func (r *RedisUPSTestSuite) TestFirstSaveConfiguresClient() {
41+
func (r *RedisUPSTestSuite) TestInitClient() {
42+
r.ups.initClient()
43+
pong, err := r.ups.Client.Ping(ctx).Result()
44+
r.Equal("PONG", pong)
45+
r.Nil(err)
46+
}
47+
48+
func (r *RedisUPSTestSuite) TestFirstSaveOrLookupConfiguresClient() {
4249
r.Nil(r.ups.Client)
4350

4451
profile := decision.UserProfile{
@@ -50,17 +57,38 @@ func (r *RedisUPSTestSuite) TestFirstSaveConfiguresClient() {
5057
// Should initialize redis client on first save call
5158
r.ups.Save(profile)
5259
r.NotNil(r.ups.Client)
60+
61+
r.ups.Client = nil
62+
// Should initialize redis client on first save call
63+
r.ups.Lookup("")
64+
r.NotNil(r.ups.Client)
5365
}
5466

55-
func (r *RedisUPSTestSuite) TestLookupNilClient() {
56-
r.Nil(r.ups.Client)
67+
func (r *RedisUPSTestSuite) TestLookupEmptyProfileID() {
68+
expectedProfile := decision.UserProfile{
69+
ID: "",
70+
ExperimentBucketMap: map[decision.UserDecisionKey]string{},
71+
}
72+
r.Equal(expectedProfile, r.ups.Lookup(""))
73+
}
5774

58-
expected := decision.UserProfile{
75+
func (r *RedisUPSTestSuite) TestLookupNotSavedProfileID() {
76+
expectedProfile := decision.UserProfile{
5977
ID: "",
6078
ExperimentBucketMap: map[decision.UserDecisionKey]string{},
6179
}
62-
actual := r.ups.Lookup("1")
63-
r.Equal(expected, actual)
80+
r.Equal(expectedProfile, r.ups.Lookup("123"))
81+
}
82+
83+
func (r *RedisUPSTestSuite) TestSaveAndLookup() {
84+
expectedProfile := decision.UserProfile{
85+
ID: "1",
86+
ExperimentBucketMap: map[decision.UserDecisionKey]string{
87+
decision.NewUserDecisionKey("1"): "1",
88+
},
89+
}
90+
r.ups.Save(expectedProfile)
91+
r.Equal(expectedProfile, r.ups.Lookup("1"))
6492
}
6593

6694
func TestRedisUPSTestSuite(t *testing.T) {

0 commit comments

Comments
 (0)