Skip to content

Commit

Permalink
fix get-treatments-with-config on cli client
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti committed Sep 19, 2023
1 parent 29ff22d commit d9aea88
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion splitio/commitsha.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package splitio

const CommitSHA = "7117453"
const CommitSHA = "29ff22d"
7 changes: 6 additions & 1 deletion splitio/link/client/v1/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ func (c *Impl) treatments(key string, bucketingKey string, features []string, at
BucketingKey: bucketingKey,
}
}
results[features[idx]] = types.Result{Treatment: resp.Payload.Results[idx].Treatment, Impression: imp}

res := types.Result{Treatment: resp.Payload.Results[idx].Treatment, Impression: imp}
if withConfig {
res.Config = resp.Payload.Results[idx].Config
}
results[features[idx]] = res
}

return results, nil
Expand Down
85 changes: 81 additions & 4 deletions splitio/link/client/v1/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestClientGetTreatmentNoImpression(t *testing.T) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1})).
serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, false)).
Return([]byte("treatmentMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{
Expand All @@ -49,6 +49,41 @@ func TestClientGetTreatmentNoImpression(t *testing.T) {
assert.Nil(t, res.Impression)
}

func TestClientGetTreatmentWithConfig(t *testing.T) {

logger := logging.NewLogger(nil)

rawConnMock := &transferMocks.RawConnMock{}
rawConnMock.On("SendMessage", []byte("registrationMessage")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("registrationSuccess"), nil).Once()
rawConnMock.On("SendMessage", []byte("treatmentWithConfigMessage")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("treatmentWithConfigResult"), nil).Once()

serializerMock := &serializerMocks.SerializerMock{}
serializerMock.On("Serialize", proto1Mocks.NewRegisterRPC("some", false)).Return([]byte("registrationMessage"), nil).Once()
serializerMock.On("Parse", []byte("registrationSuccess"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, true)).
Return([]byte("treatmentWithConfigMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentWithConfigResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{
Status: v1.ResultOk,
Payload: v1.TreatmentPayload{Treatment: "on", Config: lang.Ref(`{"some": 1}`)},
}
}).Once()
client, err := New("some", logger, rawConnMock, serializerMock, false)
assert.NotNil(t, client)
assert.Nil(t, err)

res, err := client.TreatmentWithConfig("key1", "buck1", "feat1", map[string]interface{}{"a": 1})
assert.Nil(t, err)
assert.Equal(t, lang.Ref(`{"some": 1}`), res.Config)
assert.Equal(t, "on", res.Treatment)
assert.Nil(t, res.Impression)
}

func TestTrack(t *testing.T) {

logger := logging.NewLogger(nil)
Expand Down Expand Up @@ -94,7 +129,7 @@ func TestClientGetTreatmentWithImpression(t *testing.T) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1})).
serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, false)).
Return([]byte("treatmentMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{
Expand Down Expand Up @@ -140,7 +175,7 @@ func TestClientGetTreatmentsNoImpression(t *testing.T) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1})).
serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, false)).
Return([]byte("treatmentsMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentsResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{
Expand All @@ -162,6 +197,48 @@ func TestClientGetTreatmentsNoImpression(t *testing.T) {

}

func TestClientGetTreatmentsWithConfig(t *testing.T) {

logger := logging.NewLogger(nil)

rawConnMock := &transferMocks.RawConnMock{}
rawConnMock.On("SendMessage", []byte("registrationMessage")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("registrationSuccess"), nil).Once()
rawConnMock.On("SendMessage", []byte("treatmentsWithConfigMessage")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("treatmentsWithConfigResult"), nil).Once()

serializerMock := &serializerMocks.SerializerMock{}
serializerMock.On("Serialize", proto1Mocks.NewRegisterRPC("some", false)).Return([]byte("registrationMessage"), nil).Once()
serializerMock.On("Parse", []byte("registrationSuccess"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, true)).
Return([]byte("treatmentsWithConfigMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentsWithConfigResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{
Status: v1.ResultOk,
Payload: v1.TreatmentsPayload{Results: []v1.TreatmentPayload{
{Treatment: "on", Config: lang.Ref(`{"some": 2}`)}, {Treatment: "off"}, {Treatment: "na"}}}}
}).Once()
client, err := New("some", logger, rawConnMock, serializerMock, false)
assert.NotNil(t, client)
assert.Nil(t, err)

res, err := client.TreatmentsWithConfig("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1})
assert.Nil(t, err)
assert.Equal(t, "on", res["a"].Treatment)
assert.Nil(t, res["a"].Impression)
assert.Equal(t, lang.Ref(`{"some": 2}`), res["a"].Config)
assert.Equal(t, "off", res["b"].Treatment)
assert.Nil(t, res["b"].Impression)
assert.Nil(t, res["b"].Config)
assert.Equal(t, "na", res["c"].Treatment)
assert.Nil(t, res["c"].Config)
assert.Nil(t, res["c"].Impression)

}

func TestClientGetTreatmentsWithImpression(t *testing.T) {

logger := logging.NewLogger(nil)
Expand All @@ -178,7 +255,7 @@ func TestClientGetTreatmentsWithImpression(t *testing.T) {
*args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk}
}).Once()

serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1})).
serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, false)).
Return([]byte("treatmentsMessage"), nil).Once()
serializerMock.On("Parse", []byte("treatmentsResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{
Expand Down
17 changes: 13 additions & 4 deletions splitio/link/protocol/v1/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ func NewRegisterRPC(id string, listener bool) *v1.RPC {
}
}

func NewTreatmentRPC(key string, bucketing string, feature string, attrs map[string]interface{}) *v1.RPC {
return &v1.RPC{
func NewTreatmentRPC(key string, bucketing string, feature string, attrs map[string]interface{}, withConfig bool) *v1.RPC {
rpc := &v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCTreatment,
Args: []interface{}{key, bucketing, feature, attrs},
}
if withConfig {
rpc.OpCode = v1.OCTreatmentWithConfig
}
return rpc
}

func NewTreatmentsRPC(key string, bucketing string, features []string, attrs map[string]interface{}) *v1.RPC {
return &v1.RPC{
func NewTreatmentsRPC(key string, bucketing string, features []string, attrs map[string]interface{}, withConfig bool) *v1.RPC {
rpc := &v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCTreatments,
Args: []interface{}{key, bucketing, features, attrs},
}

if withConfig {
rpc.OpCode = v1.OCTreatmentsWithConfig
}
return rpc
}

func NewTrackRPC(key string, trafficType string, eventType string, eventVal *float64, props map[string]interface{}) *v1.RPC {
Expand Down

0 comments on commit d9aea88

Please sign in to comment.