-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
48 lines (39 loc) · 1.27 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package grpc
import (
"context"
"os"
"github.com/jinxprotocol/v4-chain/protocol/mocks"
"google.golang.org/grpc"
)
const (
SocketPath = "/tmp/daemons.sock"
UserReadWriteOnlyPermissions = os.FileMode(0600)
DefaultPermissions = os.FileMode(0x180)
)
var (
Ctx = context.TODO()
TcpConn = &grpc.ClientConn{}
GrpcConn = (*grpc.ClientConn)(nil)
)
// GenerateMockGrpcClientWithOptionalGrpcConnectionErrors generates a mock gRPC client that mocks both Tcp and Grpc
// connections and optionally returns the specified errors on Grpc connections.
func GenerateMockGrpcClientWithOptionalGrpcConnectionErrors(
connectionErr error,
closeErr error,
closeConnectionIsCalled bool,
) *mocks.GrpcClient {
mockGrpcClient := &mocks.GrpcClient{}
// Conditionally set up Grpc connection to return the given connection and close errors.
mockGrpcClient.On("NewGrpcConnection", Ctx, SocketPath).
Return(GrpcConn, connectionErr)
if closeErr != nil || closeConnectionIsCalled {
mockGrpcClient.On("CloseConnection", GrpcConn).
Return(closeErr)
}
// Setup Tcp connections to return without error.
mockGrpcClient.On("NewTcpConnection", Ctx, TcpEndpoint).
Return(TcpConn, nil)
mockGrpcClient.On("CloseConnection", TcpConn).
Return(nil)
return mockGrpcClient
}