Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti committed Sep 18, 2023
1 parent 7117453 commit 29ff22d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 89 deletions.
1 change: 1 addition & 0 deletions infra/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ accum=$(yq '.sdk.apikey = env(SPLITD_APIKEY) | .link.address = env(SPLITD_LINK_A
[ ! -z ${SPLITD_LINK_READ_TIMEOUT_MS+x} ] && accum=$(echo "${accum}" | yq '.link.readTimeoutMS = env(SPLITD_LINK_READ_TIMEOUT_MS)')
[ ! -z ${SPLITD_LINK_WRITE_TIMEOUT_MS+x} ] && accum=$(echo "${accum}" | yq '.link.writeTimeoutMS = env(SPLITD_LINK_WRITE_TIMEOUT_MS)')
[ ! -z ${SPLITD_LINK_ACCEPT_TIMEOUT_MS+x} ] && accum=$(echo "${accum}" | yq '.link.acceptTimeoutMS = env(SPLITD_LINK_ACCEPT_TIMEOUT_MS)')
[ ! -z ${SPLITD_LINK_BUFFER_SIZE+x} ] && accum=$(echo "${accum}" | yq '.link.bufferSize = env(SPLITD_LINK_BUFFER_SIZE)')

# logger configs
[ ! -z ${SPLITD_LOG_LEVEL+x} ] && accum=$(echo "${accum}" | yq '.logging.level = env(SPLITD_LOG_LEVEL)')
Expand Down
2 changes: 2 additions & 0 deletions infra/test/test_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function testAllVars {
export SPLITD_LINK_READ_TIMEOUT_MS=2
export SPLITD_LINK_WRITE_TIMEOUT_MS=3
export SPLITD_LINK_ACCEPT_TIMEOUT_MS=4
export SPLITD_LINK_BUFFER_SIZE=5
export SPLITD_LOG_LEVEL="WARNING"
export SPLITD_LOG_OUTPUT="/dev/stderr"

Expand Down Expand Up @@ -92,6 +93,7 @@ function testAllVars {
assert_eq "2" $(echo "$conf_json" | jq '.Link.ReadTimeoutMS') "incorrect read timeout"
assert_eq "3" $(echo "$conf_json" | jq '.Link.WriteTimeoutMS') "incorrect write timeout"
assert_eq "4" $(echo "$conf_json" | jq '.Link.AcceptTimeoutMS') "incorrect accept timeout"
assert_eq "5" $(echo "$conf_json" | jq '.Link.BufferSize') "incorrect buffer size"

# ---

Expand Down
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 = "10d08c6"
const CommitSHA = "7117453"
21 changes: 20 additions & 1 deletion splitio/link/protocol/v1/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/splitio/splitd/splitio"
"github.com/splitio/splitd/splitio/common/lang"
"github.com/splitio/splitd/splitio/link/protocol"
v1 "github.com/splitio/splitd/splitio/link/protocol/v1"
"github.com/splitio/splitd/splitio/sdk"
Expand Down Expand Up @@ -86,6 +87,21 @@ func NewTreatmentResp(ok bool, treatment string, ilData *v1.ListenerExtraData) *
}
}

func NewTreatmentWithConfigResp(ok bool, treatment string, ilData *v1.ListenerExtraData, cfg string) *v1.ResponseWrapper[v1.TreatmentPayload] {
res := v1.ResultOk
if !ok {
res = v1.ResultInternalError
}
return &v1.ResponseWrapper[v1.TreatmentPayload]{
Status: res,
Payload: v1.TreatmentPayload{
Treatment: treatment,
ListenerData: ilData,
Config: lang.Ref(cfg),
},
}
}

func NewTreatmentsResp(ok bool, data []sdk.EvaluationResult) *v1.ResponseWrapper[v1.TreatmentsPayload] {
res := v1.ResultOk
if !ok {
Expand All @@ -94,7 +110,10 @@ func NewTreatmentsResp(ok bool, data []sdk.EvaluationResult) *v1.ResponseWrapper

payload := make([]v1.TreatmentPayload, 0, len(data))
for _, r := range data {
p := v1.TreatmentPayload{Treatment: r.Treatment}
p := v1.TreatmentPayload{
Treatment: r.Treatment,
Config: r.Config,
}
if r.Impression != nil {
p.ListenerData = &v1.ListenerExtraData{
Label: r.Impression.Label,
Expand Down
90 changes: 90 additions & 0 deletions splitio/link/service/v1/clientmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,96 @@ func TestRegisterAndTreatmentsHappyPath(t *testing.T) {
assert.Nil(t, err)
}

func TestRegisterAndTreatmentWithConfigHappyPath(t *testing.T) {
rawConnMock := &transferMocks.RawConnMock{}
rawConnMock.On("ReceiveMessage").Return([]byte("registrationMessage"), nil).Once()
rawConnMock.On("SendMessage", []byte("successRegistration")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("treatmentWithConfigMessage"), nil).Once()
rawConnMock.On("SendMessage", []byte("successPayload")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte(nil), io.EOF).Once()

serializerMock := &serializerMocks.SerializerMock{}
serializerMock.On("Parse", []byte("registrationMessage"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.RPC) = v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCRegister,
Args: []interface{}{"someID", "some_sdk-1.2.3", uint64(0)},
}
}).Once()
serializerMock.On("Serialize", proto1Mocks.NewRegisterResp(true)).Return([]byte("successRegistration"), nil).Once()
serializerMock.On("Parse", []byte("treatmentWithConfigMessage"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.RPC) = v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCTreatmentWithConfig,
Args: []interface{}{"key", nil, "someFeature", map[string]interface{}(nil)},
}
}).Once()
serializerMock.On("Serialize", proto1Mocks.NewTreatmentWithConfigResp(true, "on", nil, `{"a": "some"}`)).Return([]byte("successPayload"), nil).Once()

sdkMock := &sdkMocks.SDKMock{}
sdkMock.
On("Treatment",
&types.ClientConfig{Metadata: types.ClientMetadata{ID: "someID", SdkVersion: "some_sdk-1.2.3"}},
"key", (*string)(nil), "someFeature", map[string]interface{}(nil)).
Return(&sdk.EvaluationResult{Treatment: "on", Config: lang.Ref(`{"a": "some"}`)}, nil).Once()

logger := logging.NewLogger(nil)
cm := NewClientManager(rawConnMock, logger, sdkMock, serializerMock)
err := cm.handleClientInteractions()
assert.Nil(t, err)
}

func TestRegisterAndTreatmentsWithConfigHappyPath(t *testing.T) {
rawConnMock := &transferMocks.RawConnMock{}
rawConnMock.On("ReceiveMessage").Return([]byte("registrationMessage"), nil).Once()
rawConnMock.On("SendMessage", []byte("successRegistration")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte("treatmentsWithConfigMessage"), nil).Once()
rawConnMock.On("SendMessage", []byte("successPayload")).Return(nil).Once()
rawConnMock.On("ReceiveMessage").Return([]byte(nil), io.EOF).Once()

var strCfg = "what"

serializerMock := &serializerMocks.SerializerMock{}
serializerMock.On("Parse", []byte("registrationMessage"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.RPC) = v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCRegister,
Args: []interface{}{"someID", "some_sdk-1.2.3", uint64(0)},
}
}).Once()
serializerMock.On("Serialize", proto1Mocks.NewRegisterResp(true)).Return([]byte("successRegistration"), nil).Once()
serializerMock.On("Parse", []byte("treatmentsWithConfigMessage"), mock.Anything).Return(nil).Run(func(args mock.Arguments) {
*args.Get(1).(*v1.RPC) = v1.RPC{
RPCBase: protocol.RPCBase{Version: protocol.V1},
OpCode: v1.OCTreatmentsWithConfig,
Args: []interface{}{"key", nil, []interface{}{"feat1", "feat2", "feat3"}, map[string]interface{}(nil)},
}
}).Once()
serializerMock.On("Serialize", proto1Mocks.NewTreatmentsResp(true, []sdk.EvaluationResult{
{Treatment: "on"}, {Treatment: "off"}, {Treatment: "control", Config: &strCfg},
})).Return([]byte("successPayload"), nil).Once()

sdkMock := &sdkMocks.SDKMock{}
sdkMock.
On(
"Treatments",
&types.ClientConfig{Metadata: types.ClientMetadata{ID: "someID", SdkVersion: "some_sdk-1.2.3"}},
"key",
(*string)(nil),
[]string{"feat1", "feat2", "feat3"},
map[string]interface{}(nil),
).Return(map[string]sdk.EvaluationResult{
"feat1": {Treatment: "on"},
"feat2": {Treatment: "off"},
"feat3": {Treatment: "control", Config: &strCfg},
}, nil).Once()

logger := logging.NewLogger(nil)
cm := NewClientManager(rawConnMock, logger, sdkMock, serializerMock)
err := cm.handleClientInteractions()
assert.Nil(t, err)
}

func TestRegisterWithImpsAndTreatmentHappyPath(t *testing.T) {
rawConnMock := &transferMocks.RawConnMock{}
rawConnMock.On("ReceiveMessage").Return([]byte("registrationMessage"), nil).Once()
Expand Down
87 changes: 0 additions & 87 deletions splitio/link/transfer/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,90 +150,3 @@ func newConnWrapper(c net.Conn, f FramingWrapperFactory, o *Options) RawConn {

var _ RawConn = (*PacketBasedConnection)(nil)
var _ RawConn = (*StreamBasedConnection)(nil)

// ---------
/*
type Impl struct {
conn net.Conn
readBuffer []byte
readTimeout time.Duration
writeTimeout time.Duration
}
func newConnWrapper(c net.Conn, f framing.Interface, o *Options) RawConn {
if f != nil {
c = &FramingRawConnWrapper{f: f, c: c}
}
return &Impl{
conn: c,
readBuffer: make([]byte, o.BufferSize),
readTimeout: o.ReadTimeout,
writeTimeout: o.WriteTimeout,
}
}
// ReceiveMessage implements ClientConnection
func (c *Impl) ReceiveMessage() ([]byte, error) {
if err := c.conn.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil {
return nil, fmt.Errorf("error setting read timeout: %w", err)
}
n, err := c.conn.Read(c.readBuffer)
if err != nil {
if err == io.EOF {
return nil, err
}
return nil, fmt.Errorf("error reading from socket: %w", err)
}
if n == len(c.readBuffer) {
return nil, ErrBufferTooSmall
}
return c.readBuffer[:n], nil
}
// SendMessage implements ClientConnection
func (c *Impl) SendMessage(data []byte) error {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout)); err != nil {
return fmt.Errorf("error setting read timeout: %w", err)
}
sent, err := c.conn.Write(data)
if err != nil {
return fmt.Errorf("error when sending message to client: %w", err)
}
if sent != len(data) {
return ErrSentDataMismatch
}
return nil
}
// Shutdown implements ClientConnection
func (u *Impl) Shutdown() error {
err := u.conn.Close()
if err != nil {
return fmt.Errorf("error closing connection")
}
return nil
}
type FramingRawConnWrapper struct {
f framing.Interface
c net.Conn
}
func (w *FramingRawConnWrapper) Close() error { return w.c.Close() }
func (w *FramingRawConnWrapper) LocalAddr() net.Addr { return w.c.LocalAddr() }
func (w *FramingRawConnWrapper) RemoteAddr() net.Addr { return w.c.RemoteAddr() }
func (w *FramingRawConnWrapper) SetDeadline(t time.Time) error { return w.c.SetDeadline(t) }
func (w *FramingRawConnWrapper) SetReadDeadline(t time.Time) error { return w.c.SetReadDeadline(t) }
func (w *FramingRawConnWrapper) SetWriteDeadline(t time.Time) error { return w.c.SetWriteDeadline(t) }
func (w *FramingRawConnWrapper) Read(b []byte) (n int, err error) { return w.f.ReadFrame(b) }
func (w *FramingRawConnWrapper) Write(b []byte) (n int, err error) { return w.f.WriteFrame(b) }
var _ net.Conn = (*FramingRawConnWrapper)(nil)
var _ RawConn = (*Impl)(nil)
*/

0 comments on commit 29ff22d

Please sign in to comment.