-
Notifications
You must be signed in to change notification settings - Fork 86
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
thrift: deal with TStruct serialization failures #744
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b07b24f
thrift: deal with TStruct serialization failures
644270c
Merge remote-tracking branch 'origin/master' into tristan/another-day…
abhinav e0907eb
Merge branch 'dev' into tristan/another-day-another-error
abhinav 5e73607
thrift: Report write errors without buffering
abhinav 81d4443
channel.go: Fix lint
abhinav ca727e3
server_test: Align NewClient parens
abhinav 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
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,124 @@ | ||
package thrift | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
athrift "github.com/apache/thrift/lib/go/thrift" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/uber/tchannel-go" | ||
"github.com/uber/tchannel-go/testutils" | ||
) | ||
|
||
var errIO = errors.New("IO Error") | ||
|
||
// badTStruct implements TStruct that always fails with the provided error. | ||
type badTStruct struct { | ||
// If specified, runs the specified function before failing the Write. | ||
PreWrite func(athrift.TProtocol) | ||
|
||
Err error | ||
} | ||
|
||
func (t *badTStruct) Write(p athrift.TProtocol) error { | ||
if t.PreWrite != nil { | ||
t.PreWrite(p) | ||
} | ||
return t.Err | ||
} | ||
|
||
func (t *badTStruct) Read(p athrift.TProtocol) error { | ||
return t.Err | ||
} | ||
|
||
// nullTStruct implements TStruct that does nothing at all with no errors. | ||
type nullTStruct struct{} | ||
|
||
func (*nullTStruct) Write(p athrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
func (*nullTStruct) Read(p athrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
// thriftStruction is a TChannel service that implements the following | ||
// methods: | ||
// | ||
// destruct | ||
// Returns a TStruct that fails without writing anything. | ||
// partialDestruct | ||
// Returns a TStruct that fails after writing partial output. | ||
type thriftStruction struct{} | ||
|
||
func (ts *thriftStruction) Handle( | ||
ctx Context, | ||
methodName string, | ||
protocol athrift.TProtocol, | ||
) (success bool, resp athrift.TStruct, err error) { | ||
var preWrite func(athrift.TProtocol) | ||
if methodName == "partialDestruct" { | ||
preWrite = func(p athrift.TProtocol) { | ||
p.WriteStructBegin("foo") | ||
p.WriteFieldBegin("bar", athrift.STRING, 42) | ||
p.WriteString("baz") | ||
} | ||
} | ||
|
||
// successful call with a TStruct that fails while writing. | ||
return true, &badTStruct{Err: errIO, PreWrite: preWrite}, nil | ||
} | ||
|
||
func (ts *thriftStruction) Service() string { return "destruct" } | ||
|
||
func (ts *thriftStruction) Methods() []string { | ||
return []string{"destruct", "partialDestruct"} | ||
} | ||
|
||
func TestHandleTStructError(t *testing.T) { | ||
serverOpts := testutils.NewOpts(). | ||
AddLogFilter( | ||
"Thrift server error.", 1, | ||
"error", "IO Error", | ||
"method", "destruct::destruct"). | ||
AddLogFilter( | ||
"Thrift server error.", 1, | ||
"error", "IO Error", | ||
"method", "destruct::partialDestruct") | ||
server := testutils.NewTestServer(t, serverOpts) | ||
defer server.CloseAndVerify() | ||
|
||
// Create a thrift server with a handler that returns success with | ||
// TStructs that refuse to do I/O. | ||
tchan := server.Server() | ||
NewServer(tchan).Register(&thriftStruction{}) | ||
|
||
client := NewClient( | ||
server.NewClient(testutils.NewOpts()), | ||
tchan.ServiceName(), | ||
&ClientOptions{HostPort: server.HostPort()}, | ||
) | ||
|
||
t.Run("failing response", func(t *testing.T) { | ||
ctx, cancel := NewContext(time.Second) | ||
defer cancel() | ||
|
||
_, err := client.Call(ctx, "destruct", "destruct", &nullTStruct{}, &nullTStruct{}) | ||
assert.Error(t, err) | ||
assert.IsType(t, tchannel.SystemError{}, err) | ||
assert.Equal(t, tchannel.ErrCodeUnexpected, tchannel.GetSystemErrorCode(err)) | ||
assert.Equal(t, "IO Error", tchannel.GetSystemErrorMessage(err)) | ||
}) | ||
|
||
t.Run("failing response with partial write", func(t *testing.T) { | ||
ctx, cancel := NewContext(time.Second) | ||
defer cancel() | ||
|
||
_, err := client.Call(ctx, "destruct", "partialDestruct", &nullTStruct{}, &nullTStruct{}) | ||
assert.Error(t, err) | ||
assert.IsType(t, tchannel.SystemError{}, err) | ||
assert.Equal(t, tchannel.ErrCodeUnexpected, tchannel.GetSystemErrorCode(err)) | ||
assert.Equal(t, "IO Error", tchannel.GetSystemErrorMessage(err)) | ||
}) | ||
} |
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.
(unrelated to this diff but just thought of this) @twilly just as a safety check, let's verify that the passed in protocol here is a
TBinaryProtocol
so that users don't end up callingWrite
with a custom protocol and expect it to work?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.
Agreed. Created an internal ticket to track this.