rpc/server: start and shutdown Server in tests

It will be important for proper subscription testing and it doesn't hurt even
though technically we've got two http servers listening after this change (one
is a regular Server's http.Server and one is httptest's Server). Reusing
rpc.Server would be nice, but it requires some changes to Start sequence to
start Listener with net.Listen and then communicate back its resulting
Addr. It's not very convenient especially given that no other code needs it,
so doing these changes just for a bit cleaner testing seems like and
overkill.

Update config appropriately. Update Start comment along the way.
This commit is contained in:
Roman Khimov 2020-05-09 23:59:21 +03:00
parent d686fe4e5d
commit d5df1212c2
4 changed files with 11 additions and 6 deletions

View file

@ -49,9 +49,10 @@ ApplicationConfiguration:
AttemptConnPeers: 5 AttemptConnPeers: 5
MinPeers: 1 MinPeers: 1
RPC: RPC:
Address: 127.0.0.1
Enabled: true Enabled: true
EnableCORSWorkaround: false EnableCORSWorkaround: false
Port: 20332 Port: 0 # let the system choose port dynamically
Prometheus: Prometheus:
Enabled: false #since it's not useful for unit tests. Enabled: false #since it's not useful for unit tests.
Port: 2112 Port: 2112

View file

@ -122,8 +122,9 @@ func New(chain core.Blockchainer, conf rpc.Config, coreServer *network.Server, l
} }
} }
// Start creates a new JSON-RPC server // Start creates a new JSON-RPC server listening on the configured port. It's
// listening on the configured port. // supposed to be run as a separate goroutine (like http.Server's Serve) and it
// returns its errors via given errChan.
func (s *Server) Start(errChan chan error) { func (s *Server) Start(errChan chan error) {
if !s.config.Enabled { if !s.config.Enabled {
s.log.Info("RPC server is not enabled") s.log.Info("RPC server is not enabled")

View file

@ -18,7 +18,7 @@ import (
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *httptest.Server) { func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *Server, *httptest.Server) {
var nBlocks uint32 var nBlocks uint32
net := config.ModeUnitTestNet net := config.ModeUnitTestNet
@ -56,11 +56,13 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *httptest.Serv
server, err := network.NewServer(serverConfig, chain, logger) server, err := network.NewServer(serverConfig, chain, logger)
require.NoError(t, err) require.NoError(t, err)
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger) rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
errCh := make(chan error, 2)
go rpcServer.Start(errCh)
handler := http.HandlerFunc(rpcServer.handleHTTPRequest) handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
srv := httptest.NewServer(handler) srv := httptest.NewServer(handler)
return chain, srv return chain, &rpcServer, srv
} }
type FeerStub struct{} type FeerStub struct{}

View file

@ -896,9 +896,10 @@ func TestRPC(t *testing.T) {
// calls. Some tests change the chain state, thus we reinitialize the chain from // calls. Some tests change the chain state, thus we reinitialize the chain from
// scratch here. // scratch here.
func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []byte) { func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []byte) {
chain, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown()
e := &executor{chain: chain, httpSrv: httpSrv} e := &executor{chain: chain, httpSrv: httpSrv}
for method, cases := range rpcTestCases { for method, cases := range rpcTestCases {