Skip to content

Commit

Permalink
add detail log when reconnecting and add unit test for mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dewisuryani committed Sep 3, 2021
1 parent e9d7528 commit 46ec71f
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 1 deletion.
190 changes: 190 additions & 0 deletions common/mapper/mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package mapper_test

import (
"encoding/json"
"errors"
"testing"

"bou.ke/monkey"
"github.com/stretchr/testify/assert"

"github.com/dewisuryani/go-force/common/mapper"
)

type Foo struct {
A float64 `json:"a"`
B string `json:"b"`
}

func TestStructToMapString(t *testing.T) {
v := &Foo{
A: 42,
B: "foo",
}

t.Run("marshal error", func(t *testing.T) {
errObj := errors.New("foo")
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, errObj
})
defer patch1.Unpatch()

_, err := mapper.StructToMapString(v)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("success", func(t *testing.T) {
result, err := mapper.StructToMapString(v)
assert.NoError(t, err)
assert.Equal(t, result["a"], v.A)
assert.Equal(t, result["b"], v.B)
})
}

func TestStructToType(t *testing.T) {
v := &Foo{
A: 42,
B: "foo",
}
d := `[{"A":42, "B":"foo"}]`

t.Run("marshal error", func(t *testing.T) {
errObj := errors.New("foo")
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, errObj
})
defer patch1.Unpatch()

err := mapper.StructToType(v, d)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("unmarshal error", func(t *testing.T) {
err := mapper.StructToType(v, d)
assert.Error(t, err)
})
}

func TestMapStringToStruct(t *testing.T) {
d := &Foo{}
v := map[string]interface{}{
"A": 42,
"B": "foo",
}

errObj := errors.New("foo")

t.Run("marshal error", func(t *testing.T) {
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, errObj
})
defer patch1.Unpatch()

err := mapper.MapStringToStruct(v, d)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("unmarshal error", func(t *testing.T) {
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error {
return errObj
})
defer patch1.Unpatch()

err := mapper.MapStringToStruct(v, d)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("success", func(t *testing.T) {
err := mapper.MapStringToStruct(v, d)
assert.NoError(t, err)
})
}

func TestStructToArrMapString(t *testing.T) {
v := &Foo{
A: 42,
B: "foo",
}

t.Run("marshal error", func(t *testing.T) {
errObj := errors.New("foo")
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, errObj
})
defer patch1.Unpatch()

_, err := mapper.StructToArrMapString(v)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("success", func(t *testing.T) {
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error {
return nil
})
defer patch1.Unpatch()

_, err := mapper.StructToArrMapString(v)
assert.NoError(t, err)
})
}

func TestArrMapStringToStruct(t *testing.T) {
d := &Foo{}
v := []map[string]interface{}{
{
"A": 42,
"B": "foo",
},
}

errObj := errors.New("foo")

t.Run("marshal error", func(t *testing.T) {
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, errObj
})
defer patch1.Unpatch()

err := mapper.ArrMapStringToStruct(v, d)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("unmarshal error", func(t *testing.T) {
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error {
return errObj
})
defer patch1.Unpatch()

err := mapper.ArrMapStringToStruct(v, d)
assert.Error(t, err)
assert.Equal(t, err, errObj)
})

t.Run("success", func(t *testing.T) {
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error {
return nil
})
defer patch1.Unpatch()

err := mapper.ArrMapStringToStruct(v, d)
assert.NoError(t, err)
})
}

func TestArrStringToMapString(t *testing.T) {
source := []string{"abc", "def"}

mockResult := map[string]string{
source[0]: source[0],
source[1]: source[1],
}

result := mapper.ArrStringToMapString(source)
assert.Equal(t, mockResult, result)
}
6 changes: 5 additions & 1 deletion force/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ func (forceAPI *ForceApi) Stream(ctx context.Context) error {
case <-ctx.Done():
return nil
default:
logrus.Info("reconnecting")
connBytes, err := forceAPI.stream.connect(ctx)
if err != nil {
logrus.WithFields(logrus.Fields{
Expand All @@ -257,6 +256,11 @@ func (forceAPI *ForceApi) Stream(ctx context.Context) error {

var connectData []map[string]interface{}
json.Unmarshal(connBytes, &connectData)
logrus.WithFields(logrus.Fields{
"resp": connectData,
"clientId": forceAPI.stream.ClientID,
}).Info("reconnecting")

err = checkClientError(connectData)
if err != nil {
logrus.WithFields(logrus.Fields{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/dewisuryani/go-force
go 1.15

require (
bou.ke/monkey v1.0.2
github.com/sirupsen/logrus v1.8.1
github.com/sony/gobreaker v0.4.1 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/net v0.0.0-20210716203947-853a461950ff
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -10,6 +12,7 @@ github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJ
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20210716203947-853a461950ff h1:j2EK/QoxYNBsXI4R7fQkkRUk8y6wnOBI+6hgPdP/6Ds=
golang.org/x/net v0.0.0-20210716203947-853a461950ff/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down

0 comments on commit 46ec71f

Please sign in to comment.