| 13 | ) |
| 14 | |
| 15 | func TestGrpc(t *testing.T) { |
| 16 | |
| 17 | tt := testutils.T{T: t} |
| 18 | |
| 19 | // A successful call should return the message, a nil error, and the status code should evaluate to codes.OK |
| 20 | resp, err := Client.Echo(context.Background(), &EchoRequest{Text: "hello"}) |
| 21 | tt.Assert(err == nil) |
| 22 | tt.Assert(resp.Reply == "echoing: hello") |
| 23 | tt.Assert(status.Code(err) == codes.OK) |
| 24 | |
| 25 | // A sentinel error should be detectable across grpc boundaries |
| 26 | // A failed call that does not have a status specified should evaluate to codes.Unknown |
| 27 | _, err = Client.Echo(context.Background(), &EchoRequest{Text: "noecho"}) |
| 28 | tt.Assert(err != nil) |
| 29 | tt.Assert(errors.Is(err, ErrCantEcho)) |
| 30 | tt.Assert(status.Code(err) == codes.Unknown) |
| 31 | |
| 32 | // A wrapped error should be unwrappable after crossing grpc boundaries |
| 33 | _, err = Client.Echo(context.Background(), &EchoRequest{Text: "really_long_message"}) |
| 34 | tt.Assert(err != nil) |
| 35 | tt.Assert(err.Error() == "really_long_message is too long: text is too long") |
| 36 | tt.Assert(errors.Is(err, ErrTooLong)) |
| 37 | tt.Assert(errors.UnwrapAll(err).Error() == "text is too long") |
| 38 | |
| 39 | // A failed call with a specified status should evaluate correctly after crossing a grpc boundary |
| 40 | _, err = Client.Echo(context.Background(), &EchoRequest{Text: "reverse"}) |
| 41 | tt.Assert(err != nil) |
| 42 | tt.Assert(err.Error() == "reverse is not implemented") |
| 43 | tt.Assert(status.Code(err) == codes.Unimplemented) |
| 44 | |
| 45 | // Sentinel error and status code in the same response |
| 46 | // Printing the error out with detail should include the grpc status |
| 47 | _, err = Client.Echo(context.Background(), &EchoRequest{Text: "internal"}) |
| 48 | tt.Assert(err != nil) |
| 49 | tt.Assert(err.Error() == "there was a problem: internal error!") |
| 50 | tt.Assert(status.Code(err) == codes.Internal) |
| 51 | tt.Assert(errors.Is(err, ErrInternal)) |
| 52 | spv := fmt.Sprintf("%+v", err) |
| 53 | t.Logf("spv:\n%s", spv) |
| 54 | tt.Assert(strings.Contains(spv, "gRPC code: Internal")) |
| 55 | } |