forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
366 additions
and
3 deletions.
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
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,101 @@ | ||
package rpcx | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tal-tech/go-zero/core/logx" | ||
"github.com/tal-tech/go-zero/rpcx/mock" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/grpc/test/bufconn" | ||
) | ||
|
||
func init() { | ||
logx.Disable() | ||
} | ||
|
||
func dialer() func(context.Context, string) (net.Conn, error) { | ||
listener := bufconn.Listen(1024 * 1024) | ||
server := grpc.NewServer() | ||
mock.RegisterDepositServiceServer(server, &mock.DepositServer{}) | ||
|
||
go func() { | ||
if err := server.Serve(listener); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
return func(context.Context, string) (net.Conn, error) { | ||
return listener.Dial() | ||
} | ||
} | ||
|
||
func TestDepositServer_Deposit(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
amount float32 | ||
res *mock.DepositResponse | ||
errCode codes.Code | ||
errMsg string | ||
}{ | ||
{ | ||
"invalid request with negative amount", | ||
-1.11, | ||
nil, | ||
codes.InvalidArgument, | ||
fmt.Sprintf("cannot deposit %v", -1.11), | ||
}, | ||
{ | ||
"valid request with non negative amount", | ||
0.00, | ||
&mock.DepositResponse{Ok: true}, | ||
codes.OK, | ||
"", | ||
}, | ||
} | ||
|
||
directClient := MustNewClient(RpcClientConf{ | ||
Endpoints: []string{"foo"}, | ||
App: "foo", | ||
Token: "bar", | ||
Timeout: 1000, | ||
}, WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithContextDialer(dialer()))) | ||
targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()), | ||
WithDialOption(grpc.WithContextDialer(dialer()))) | ||
assert.Nil(t, err) | ||
clients := []Client{ | ||
directClient, | ||
targetClient, | ||
} | ||
for _, tt := range tests { | ||
for _, client := range clients { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cli := mock.NewDepositServiceClient(client.Conn()) | ||
request := &mock.DepositRequest{Amount: tt.amount} | ||
response, err := cli.Deposit(context.Background(), request) | ||
if response != nil { | ||
assert.True(t, len(response.String()) > 0) | ||
if response.GetOk() != tt.res.GetOk() { | ||
t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk()) | ||
} | ||
} | ||
if err != nil { | ||
if e, ok := status.FromError(err); ok { | ||
if e.Code() != tt.errCode { | ||
t.Error("error code: expected", codes.InvalidArgument, "received", e.Code()) | ||
} | ||
if e.Message() != tt.errMsg { | ||
t.Error("error message: expected", tt.errMsg, "received", e.Message()) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
syntax = "proto3"; | ||
|
||
package mock; | ||
|
||
message DepositRequest { | ||
float amount = 1; | ||
} | ||
|
||
message DepositResponse { | ||
bool ok = 1; | ||
} | ||
|
||
service DepositService { | ||
rpc Deposit(DepositRequest) returns (DepositResponse); | ||
} |
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,19 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type DepositServer struct { | ||
} | ||
|
||
func (*DepositServer) Deposit(ctx context.Context, req *DepositRequest) (*DepositResponse, error) { | ||
if req.GetAmount() < 0 { | ||
return nil, status.Errorf(codes.InvalidArgument, "cannot deposit %v", req.GetAmount()) | ||
} | ||
|
||
return &DepositResponse{Ok: true}, nil | ||
} |
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
Oops, something went wrong.