forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_test.go
171 lines (154 loc) · 4.99 KB
/
rpc_test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package api
import (
"context"
"encoding/json"
"reflect"
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
"github.com/celestiaorg/celestia-node/api/rpc"
"github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/celestiaorg/celestia-node/nodebuilder"
dasMock "github.com/celestiaorg/celestia-node/nodebuilder/das/mocks"
fraudMock "github.com/celestiaorg/celestia-node/nodebuilder/fraud/mocks"
headerMock "github.com/celestiaorg/celestia-node/nodebuilder/header/mocks"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
shareMock "github.com/celestiaorg/celestia-node/nodebuilder/share/mocks"
stateMock "github.com/celestiaorg/celestia-node/nodebuilder/state/mocks"
"github.com/celestiaorg/celestia-node/state"
)
func TestRPCCallsUnderlyingNode(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
nd, server := setupNodeWithModifiedRPC(t)
url := nd.RPCServer.ListenAddr()
// we need to run this a few times to prevent the race where the server is not yet started
var (
rpcClient *client.Client
err error
)
for i := 0; i < 3; i++ {
time.Sleep(time.Second * 1)
rpcClient, err = client.NewClient(ctx, "http://"+url)
if err == nil {
t.Cleanup(rpcClient.Close)
break
}
}
require.NotNil(t, rpcClient)
require.NoError(t, err)
expectedBalance := &state.Balance{
Amount: sdk.NewInt(100),
Denom: "utia",
}
server.State.EXPECT().Balance(gomock.Any()).Return(expectedBalance, nil).Times(1)
balance, err := rpcClient.State.Balance(ctx)
require.NoError(t, err)
require.Equal(t, expectedBalance, balance)
}
func TestModulesImplementFullAPI(t *testing.T) {
api := reflect.TypeOf(new(client.API)).Elem()
client := reflect.TypeOf(new(client.Client)).Elem()
for i := 0; i < client.NumField(); i++ {
module := client.Field(i)
for j := 0; j < module.Type.NumField(); j++ {
impl := module.Type.Field(j)
method, _ := api.MethodByName(impl.Name)
// closers is the only thing on the Client struct that doesn't exist in the API
if impl.Name != "closers" {
require.Equal(t, method.Type, impl.Type, "method %s does not match", impl.Name)
}
}
}
}
func TestAllReturnValuesAreMarshalable(t *testing.T) {
ra := reflect.TypeOf(new(client.API)).Elem()
for i := 0; i < ra.NumMethod(); i++ {
m := ra.Method(i)
for j := 0; j < m.Type.NumOut(); j++ {
implementsMarshaler(t, m.Type.Out(j))
}
}
}
func implementsMarshaler(t *testing.T, typ reflect.Type) {
// the passed type may already implement json.Marshaler and we don't need to go deeper
if typ.Implements(reflect.TypeOf(new(json.Marshaler)).Elem()) {
return
}
switch typ.Kind() {
case reflect.Struct:
// a user defined struct could implement json.Marshaler on the pointer receiver, so check there first.
// note that the "non-pointer" receiver is checked before the switch.
pointerType := reflect.TypeOf(reflect.New(typ).Elem().Addr().Interface())
if pointerType.Implements(reflect.TypeOf(new(json.Marshaler)).Elem()) {
return
}
// struct doesn't implement the interface itself, check all individual fields
reflect.New(typ).Pointer()
for i := 0; i < typ.NumField(); i++ {
implementsMarshaler(t, typ.Field(i).Type)
}
return
case reflect.Map:
implementsMarshaler(t, typ.Elem())
implementsMarshaler(t, typ.Key())
case reflect.Ptr:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
fallthrough
case reflect.Chan:
implementsMarshaler(t, typ.Elem())
case reflect.Interface:
if typ != reflect.TypeOf(new(interface{})).Elem() && typ != reflect.TypeOf(new(error)).Elem() {
require.True(
t,
typ.Implements(reflect.TypeOf(new(json.Marshaler)).Elem()),
"type %s does not implement json.Marshaler", typ.String(),
)
}
default:
return
}
}
func setupNodeWithModifiedRPC(t *testing.T) (*nodebuilder.Node, *mockAPI) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
ctrl := gomock.NewController(t)
mockAPI := &mockAPI{
stateMock.NewMockModule(ctrl),
shareMock.NewMockModule(ctrl),
fraudMock.NewMockModule(ctrl),
headerMock.NewMockModule(ctrl),
dasMock.NewMockModule(ctrl),
}
// given the behavior of fx.Invoke, this invoke will be called last as it is added at the root
// level module. For further information, check the documentation on fx.Invoke.
invokeRPC := fx.Invoke(func(srv *rpc.Server) {
srv.RegisterService("state", mockAPI.State)
srv.RegisterService("share", mockAPI.Share)
srv.RegisterService("fraud", mockAPI.Fraud)
srv.RegisterService("header", mockAPI.Header)
srv.RegisterService("das", mockAPI.Das)
})
nd := nodebuilder.TestNode(t, node.Full, invokeRPC)
// start node
err := nd.Start(ctx)
require.NoError(t, err)
t.Cleanup(func() {
err = nd.Stop(ctx)
require.NoError(t, err)
})
return nd, mockAPI
}
type mockAPI struct {
State *stateMock.MockModule
Share *shareMock.MockModule
Fraud *fraudMock.MockModule
Header *headerMock.MockModule
Das *dasMock.MockModule
}