-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[common] Add unit test for convertions #1395
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
246ded3
[common] Add unit test for convertions
3vilhamster dc73a11
simplify the change
3vilhamster 8caf34b
fmt and copyright
3vilhamster 3acf602
revert IsUseThriftDecoding
3vilhamster 5501cc5
fix the test
3vilhamster d727131
address review comments
3vilhamster a215fbc
Empty-Commit
3vilhamster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2017-2021 Uber Technologies Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
s "go.uber.org/cadence/.gen/go/shared" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPtrOf(t *testing.T) { | ||
assert.Equal(t, "a", *PtrOf("a")) | ||
assert.Equal(t, 1, *PtrOf(1)) | ||
assert.Equal(t, int32(1), *PtrOf(int32(1))) | ||
assert.Equal(t, int64(1), *PtrOf(int64(1))) | ||
assert.Equal(t, float64(1.1), *PtrOf(float64(1.1))) | ||
assert.Equal(t, true, *PtrOf(true)) | ||
} | ||
|
||
func TestPtrHelpers(t *testing.T) { | ||
assert.Equal(t, int32(1), *Int32Ptr(1)) | ||
assert.Equal(t, int64(1), *Int64Ptr(1)) | ||
assert.Equal(t, 1.1, *Float64Ptr(1.1)) | ||
assert.Equal(t, true, *BoolPtr(true)) | ||
assert.Equal(t, "a", *StringPtr("a")) | ||
assert.Equal(t, s.TaskList{Name: PtrOf("a")}, *TaskListPtr(s.TaskList{Name: PtrOf("a")})) | ||
assert.Equal(t, s.DecisionTypeScheduleActivityTask, *DecisionTypePtr(s.DecisionTypeScheduleActivityTask)) | ||
assert.Equal(t, s.EventTypeWorkflowExecutionStarted, *EventTypePtr(s.EventTypeWorkflowExecutionStarted)) | ||
assert.Equal(t, s.QueryTaskCompletedTypeCompleted, *QueryTaskCompletedTypePtr(s.QueryTaskCompletedTypeCompleted)) | ||
assert.Equal(t, s.TaskListKindNormal, *TaskListKindPtr(s.TaskListKindNormal)) | ||
assert.Equal(t, s.QueryResultTypeFailed, *QueryResultTypePtr(s.QueryResultTypeFailed)) | ||
} | ||
|
||
func TestCeilHelpers(t *testing.T) { | ||
assert.Equal(t, int32(2), Int32Ceil(1.1)) | ||
assert.Equal(t, int64(2), Int64Ceil(1.1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,19 +27,13 @@ | |
"github.com/apache/thrift/lib/go/thrift" | ||
) | ||
|
||
// TSerialize is used to serialize thrift TStruct to []byte | ||
func TSerialize(ctx context.Context, t thrift.TStruct) (b []byte, err error) { | ||
return thrift.NewTSerializer().Write(ctx, t) | ||
} | ||
|
||
// TListSerialize is used to serialize list of thrift TStruct to []byte | ||
func TListSerialize(ts []thrift.TStruct) (b []byte, err error) { | ||
func TListSerialize(ts []thrift.TStruct) ([]byte, error) { | ||
if ts == nil { | ||
return | ||
return nil, nil | ||
} | ||
|
||
t := thrift.NewTSerializer() | ||
t.Transport.Reset() | ||
|
||
// NOTE: we don't write any markers as thrift by design being a streaming protocol doesn't | ||
// recommend writing length. | ||
|
@@ -48,26 +42,11 @@ | |
ctx := context.Background() | ||
for _, v := range ts { | ||
if e := v.Write(ctx, t.Protocol); e != nil { | ||
err = thrift.PrependError("error writing TStruct: ", e) | ||
return | ||
return nil, thrift.PrependError("error writing TStruct: ", e) | ||
} | ||
} | ||
|
||
if err = t.Protocol.Flush(ctx); err != nil { | ||
return | ||
} | ||
|
||
if err = t.Transport.Flush(ctx); err != nil { | ||
return | ||
} | ||
|
||
b = t.Transport.Bytes() | ||
return | ||
} | ||
|
||
// TDeserialize is used to deserialize []byte to thrift TStruct | ||
func TDeserialize(ctx context.Context, t thrift.TStruct, b []byte) (err error) { | ||
return thrift.NewTDeserializer().Read(ctx, t, b) | ||
return t.Transport.Bytes(), t.Protocol.Flush(ctx) | ||
} | ||
|
||
// TListDeserialize is used to deserialize []byte to list of thrift TStruct | ||
|
@@ -92,15 +71,13 @@ | |
|
||
// IsUseThriftEncoding checks if the objects passed in are all encoded using thrift. | ||
func IsUseThriftEncoding(objs []interface{}) bool { | ||
// NOTE: our criteria to use which encoder is simple if all the types are serializable using thrift then we use | ||
// thrift encoder. For everything else we default to gob. | ||
|
||
if len(objs) == 0 { | ||
return false | ||
} | ||
|
||
for i := 0; i < len(objs); i++ { | ||
if !IsThriftType(objs[i]) { | ||
// NOTE: our criteria to use which encoder is simple if all the types are serializable using thrift then we use | ||
// thrift encoder. For everything else we default to gob. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for _, obj := range objs { | ||
if !IsThriftType(obj) { | ||
return false | ||
} | ||
} | ||
|
@@ -109,15 +86,13 @@ | |
|
||
// IsUseThriftDecoding checks if the objects passed in are all de-serializable using thrift. | ||
func IsUseThriftDecoding(objs []interface{}) bool { | ||
// NOTE: our criteria to use which encoder is simple if all the types are de-serializable using thrift then we use | ||
// thrift decoder. For everything else we default to gob. | ||
|
||
if len(objs) == 0 { | ||
return false | ||
} | ||
|
||
for i := 0; i < len(objs); i++ { | ||
rVal := reflect.ValueOf(objs[i]) | ||
// NOTE: our criteria to use which encoder is simple if all the types are de-serializable using thrift then we use | ||
// thrift decoder. For everything else we default to gob. | ||
for _, obj := range objs { | ||
rVal := reflect.ValueOf(obj) | ||
if rVal.Kind() != reflect.Ptr || !IsThriftType(reflect.Indirect(rVal).Interface()) { | ||
return false | ||
} | ||
|
@@ -133,6 +108,7 @@ | |
if reflect.ValueOf(v).Kind() != reflect.Ptr { | ||
return false | ||
} | ||
t := reflect.TypeOf((*thrift.TStruct)(nil)).Elem() | ||
return reflect.TypeOf(v).Implements(t) | ||
return reflect.TypeOf(v).Implements(tStructType) | ||
} | ||
|
||
var tStructType = reflect.TypeOf((*thrift.TStruct)(nil)).Elem() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright (c) 2017-2021 Uber Technologies Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/apache/thrift/lib/go/thrift" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTListSerialize(t *testing.T) { | ||
t.Run("nil", func(t *testing.T) { | ||
data, err := TListSerialize(nil) | ||
assert.NoError(t, err) | ||
assert.Nil(t, data) | ||
}) | ||
t.Run("normal", func(t *testing.T) { | ||
ts := []thrift.TStruct{ | ||
&mockThriftStruct{Field1: "value1", Field2: 1}, | ||
&mockThriftStruct{Field1: "value2", Field2: 2}, | ||
} | ||
|
||
_, err := TListSerialize(ts) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestTListDeserialize(t *testing.T) { | ||
ts := []thrift.TStruct{ | ||
&mockThriftStruct{}, | ||
&mockThriftStruct{}, | ||
} | ||
|
||
data, err := TListSerialize(ts) | ||
assert.NoError(t, err) | ||
|
||
err = TListDeserialize(ts, data) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestIsUseThriftEncoding(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
input []interface{} | ||
expected bool | ||
}{ | ||
{ | ||
name: "nil", | ||
input: nil, | ||
expected: false, | ||
}, | ||
{ | ||
name: "success", | ||
input: []interface{}{ | ||
&mockThriftStruct{}, | ||
&mockThriftStruct{}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "fail", | ||
input: []interface{}{ | ||
&mockThriftStruct{}, | ||
PtrOf("string"), | ||
}, | ||
expected: false, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert.Equal(t, tc.expected, IsUseThriftEncoding(tc.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsUseThriftDecoding(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
input []interface{} | ||
expected bool | ||
}{ | ||
{ | ||
name: "nil", | ||
input: nil, | ||
expected: false, | ||
}, | ||
{ | ||
name: "success", | ||
input: []interface{}{ | ||
PtrOf(&mockThriftStruct{}), | ||
PtrOf(&mockThriftStruct{}), | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "fail", | ||
input: []interface{}{ | ||
PtrOf(&mockThriftStruct{}), | ||
PtrOf("string"), | ||
}, | ||
expected: false, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert.Equal(t, tc.expected, IsUseThriftDecoding(tc.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsThriftType(t *testing.T) { | ||
assert.True(t, IsThriftType(&mockThriftStruct{})) | ||
|
||
assert.False(t, IsThriftType(mockThriftStruct{})) | ||
} | ||
|
||
type mockThriftStruct struct { | ||
Field1 string | ||
Field2 int | ||
} | ||
|
||
func (m *mockThriftStruct) Read(ctx context.Context, iprot thrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
func (m *mockThriftStruct) Write(ctx context.Context, oprot thrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
func (m *mockThriftStruct) String() string { | ||
return "" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this (and not resetting) is a rather significant set of changes - is there something that implies it's correct? e.g. newer thrift/tchannel docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked into thrift.NewTSerializer() and it uses TMemoryBuffer that has a comment of Flush is Noop for it, since it uses plain buffer.
Also, Protocol.Flush is just a wrapper around Transport.Flush().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always initialize one buffer with
Why it should be reset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it be reset: I agree it doesn't look correct, but the only thing that matters is if it is correct. If that hasn't been verified, it needs to be, but hasn't been mentioned anywhere so I'm not sure if it has been. By default we kinda have to assume the current code exists for some reason, unless it's clearly wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From reading the source code: yea looks fine.
I do think this deserves a comment though, as under normal circumstances this reads like:
which means to get the bytes before flushing, which is generally nonsense. It's only valid for the specific in-memory implementation that's being used right now, not for the interface in general.
If we have a way to make sure that's always correct, that'd be worth doing, otherwise tbh I think I prefer the explicit branching :\ it'll catch errors on upgrades that might change this, where right now it'll probably just silently drop data.