*: fix all errcheck warnings in testing code

This commit is contained in:
Roman Khimov 2021-05-12 21:34:07 +03:00
parent 0500c8f9d7
commit e9cefc4bfc
12 changed files with 78 additions and 52 deletions

View file

@ -403,7 +403,8 @@ func TestComlileAndInvokeFunction(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)
acc.ConvertMultisig(2, keys.PublicKeys{acc.PrivateKey().PublicKey(), pk.PublicKey()}) err = acc.ConvertMultisig(2, keys.PublicKeys{acc.PrivateKey().PublicKey(), pk.PublicKey()})
require.NoError(t, err)
t.Run("cosigner is multisig account", func(t *testing.T) { t.Run("cosigner is multisig account", func(t *testing.T) {
t.Run("missing in the wallet", func(t *testing.T) { t.Run("missing in the wallet", func(t *testing.T) {

View file

@ -62,9 +62,11 @@ func TestHandleLoggingParams(t *testing.T) {
func TestInitBCWithMetrics(t *testing.T) { func TestInitBCWithMetrics(t *testing.T) {
d, err := ioutil.TempDir("./", "") d, err := ioutil.TempDir("./", "")
require.NoError(t, err) require.NoError(t, err)
os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Chdir("..") err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d) os.RemoveAll(d)
}) })
@ -91,9 +93,11 @@ func TestDumpDB(t *testing.T) {
t.Run("too low chain", func(t *testing.T) { t.Run("too low chain", func(t *testing.T) {
d, err := ioutil.TempDir("./", "") d, err := ioutil.TempDir("./", "")
require.NoError(t, err) require.NoError(t, err)
os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Chdir("..") err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d) os.RemoveAll(d)
}) })
testDump := "file.acc" testDump := "file.acc"
@ -112,9 +116,11 @@ func TestDumpDB(t *testing.T) {
t.Run("positive", func(t *testing.T) { t.Run("positive", func(t *testing.T) {
d, err := ioutil.TempDir("./", "") d, err := ioutil.TempDir("./", "")
require.NoError(t, err) require.NoError(t, err)
os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Chdir("..") err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d) os.RemoveAll(d)
}) })
testDump := "file.acc" testDump := "file.acc"
@ -136,9 +142,11 @@ func TestRestoreDB(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
testDump := "file1.acc" testDump := "file1.acc"
saveDump := "file2.acc" saveDump := "file2.acc"
os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Chdir("..") err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d) os.RemoveAll(d)
}) })

View file

@ -13,9 +13,11 @@ import (
func TestInitSmartContract(t *testing.T) { func TestInitSmartContract(t *testing.T) {
d, err := ioutil.TempDir("./", "") d, err := ioutil.TempDir("./", "")
require.NoError(t, err) require.NoError(t, err)
os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Chdir("..") err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d) os.RemoveAll(d)
}) })
contractName := "testContract" contractName := "testContract"

View file

@ -18,7 +18,8 @@ import (
func TestDeployGetUpdateDestroyContract(t *testing.T) { func TestDeployGetUpdateDestroyContract(t *testing.T) {
mgmt := newManagement() mgmt := newManagement()
d := dao.NewCached(dao.NewSimple(storage.NewMemoryStore(), false)) d := dao.NewCached(dao.NewSimple(storage.NewMemoryStore(), false))
mgmt.Initialize(&interop.Context{DAO: d}) err := mgmt.Initialize(&interop.Context{DAO: d})
require.NoError(t, err)
script := []byte{byte(opcode.RET)} script := []byte{byte(opcode.RET)}
sender := util.Uint160{1, 2, 3} sender := util.Uint160{1, 2, 3}
ne, err := nef.NewFile(script) ne, err := nef.NewFile(script)

View file

@ -122,7 +122,8 @@ func TestNEO_Vote(t *testing.T) {
ic.VM.Load(priv.PublicKey().GetVerificationScript()) ic.VM.Load(priv.PublicKey().GetVerificationScript())
require.NoError(t, neo.VoteInternal(ic, h, candidates[0])) require.NoError(t, neo.VoteInternal(ic, h, candidates[0]))
ic.DAO.Persist() _, err = ic.DAO.Persist()
require.NoError(t, err)
advanceChain(t) advanceChain(t)
pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO) pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO)
require.NoError(t, err) require.NoError(t, err)

View file

@ -208,7 +208,8 @@ func TestNotaryContractPipeline(t *testing.T) {
checkResult(t, balance, stackitem.Make(3*transaction.NotaryServiceFeePerKey)) checkResult(t, balance, stackitem.Make(3*transaction.NotaryServiceFeePerKey))
// `withdraw`: unlock deposit and transfer GAS back to owner // `withdraw`: unlock deposit and transfer GAS back to owner
chain.genBlocks(depositLock) _, err = chain.genBlocks(depositLock)
require.NoError(t, err)
withdrawRes, err = invokeContractMethod(chain, 100000000, notaryHash, "withdraw", testchain.MultisigScriptHash(), testchain.MultisigScriptHash()) withdrawRes, err = invokeContractMethod(chain, 100000000, notaryHash, "withdraw", testchain.MultisigScriptHash(), testchain.MultisigScriptHash())
require.NoError(t, err) require.NoError(t, err)
checkResult(t, withdrawRes, stackitem.NewBool(true)) checkResult(t, withdrawRes, stackitem.NewBool(true))

View file

@ -145,7 +145,8 @@ func TestNotary(t *testing.T) {
VerificationScript: []byte{}, VerificationScript: []byte{},
}, },
} }
requester.SignTx(testchain.Network(), fallback) err = requester.SignTx(testchain.Network(), fallback)
require.NoError(t, err)
return fallback return fallback
} }

View file

@ -1539,7 +1539,8 @@ func initTestServer(t *testing.T, resp string) *httptest.Server {
ws, err := upgrader.Upgrade(w, req, nil) ws, err := upgrader.Upgrade(w, req, nil)
require.NoError(t, err) require.NoError(t, err)
for { for {
ws.SetReadDeadline(time.Now().Add(2 * time.Second)) err = ws.SetReadDeadline(time.Now().Add(2 * time.Second))
require.NoError(t, err)
_, p, err := ws.ReadMessage() _, p, err := ws.ReadMessage()
if err != nil { if err != nil {
break break
@ -1550,7 +1551,8 @@ func initTestServer(t *testing.T, resp string) *httptest.Server {
t.Fatalf("Cannot decode request body: %s", req.Body) t.Fatalf("Cannot decode request body: %s", req.Body)
} }
response := wrapInitResponse(r, resp) response := wrapInitResponse(r, resp)
ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
require.NoError(t, err)
err = ws.WriteMessage(1, []byte(response)) err = ws.WriteMessage(1, []byte(response))
if err != nil { if err != nil {
break break

View file

@ -128,7 +128,8 @@ func TestWSClientEvents(t *testing.T) {
ws, err := upgrader.Upgrade(w, req, nil) ws, err := upgrader.Upgrade(w, req, nil)
require.NoError(t, err) require.NoError(t, err)
for _, event := range events { for _, event := range events {
ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
require.NoError(t, err)
err = ws.WriteMessage(1, []byte(event)) err = ws.WriteMessage(1, []byte(event))
if err != nil { if err != nil {
break break
@ -313,14 +314,16 @@ func TestWSFilteredSubscriptions(t *testing.T) {
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
ws, err := upgrader.Upgrade(w, req, nil) ws, err := upgrader.Upgrade(w, req, nil)
require.NoError(t, err) require.NoError(t, err)
ws.SetReadDeadline(time.Now().Add(2 * time.Second)) err = ws.SetReadDeadline(time.Now().Add(2 * time.Second))
require.NoError(t, err)
req := request.In{} req := request.In{}
err = ws.ReadJSON(&req) err = ws.ReadJSON(&req)
require.NoError(t, err) require.NoError(t, err)
params, err := req.Params() params, err := req.Params()
require.NoError(t, err) require.NoError(t, err)
c.serverCode(t, params) c.serverCode(t, params)
ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
require.NoError(t, err)
err = ws.WriteMessage(1, []byte(`{"jsonrpc": "2.0", "id": 1, "result": "0"}`)) err = ws.WriteMessage(1, []byte(`{"jsonrpc": "2.0", "id": 1, "result": "0"}`))
require.NoError(t, err) require.NoError(t, err)
ws.Close() ws.Close()

View file

@ -31,7 +31,7 @@ import (
func TestClient_NEP17(t *testing.T) { func TestClient_NEP17(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -74,7 +74,7 @@ func TestClient_NEP17(t *testing.T) {
func TestAddNetworkFeeCalculateNetworkFee(t *testing.T) { func TestAddNetworkFeeCalculateNetworkFee(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
const extraFee = 10 const extraFee = 10
var nonce uint32 var nonce uint32
@ -156,12 +156,13 @@ func TestAddNetworkFeeCalculateNetworkFee(t *testing.T) {
t.Run("Multi", func(t *testing.T) { t.Run("Multi", func(t *testing.T) {
acc0 := wallet.NewAccountFromPrivateKey(testchain.PrivateKeyByID(0)) acc0 := wallet.NewAccountFromPrivateKey(testchain.PrivateKeyByID(0))
acc1 := wallet.NewAccountFromPrivateKey(testchain.PrivateKeyByID(0)) acc1 := wallet.NewAccountFromPrivateKey(testchain.PrivateKeyByID(0))
acc1.ConvertMultisig(3, keys.PublicKeys{ err = acc1.ConvertMultisig(3, keys.PublicKeys{
testchain.PrivateKeyByID(0).PublicKey(), testchain.PrivateKeyByID(0).PublicKey(),
testchain.PrivateKeyByID(1).PublicKey(), testchain.PrivateKeyByID(1).PublicKey(),
testchain.PrivateKeyByID(2).PublicKey(), testchain.PrivateKeyByID(2).PublicKey(),
testchain.PrivateKeyByID(3).PublicKey(), testchain.PrivateKeyByID(3).PublicKey(),
}) })
require.NoError(t, err)
check := func(t *testing.T, extraFee int64) { check := func(t *testing.T, extraFee int64) {
tx := transaction.New([]byte{byte(opcode.PUSH1)}, 0) tx := transaction.New([]byte{byte(opcode.PUSH1)}, 0)
tx.ValidUntilBlock = 20 tx.ValidUntilBlock = 20
@ -322,7 +323,7 @@ func TestAddNetworkFeeCalculateNetworkFee(t *testing.T) {
func TestCalculateNetworkFee(t *testing.T) { func TestCalculateNetworkFee(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
const extraFee = 10 const extraFee = 10
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
@ -392,7 +393,7 @@ func TestCalculateNetworkFee(t *testing.T) {
func TestSignAndPushInvocationTx(t *testing.T) { func TestSignAndPushInvocationTx(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -549,7 +550,7 @@ func TestSignAndPushInvocationTx(t *testing.T) {
func TestSignAndPushP2PNotaryRequest(t *testing.T) { func TestSignAndPushP2PNotaryRequest(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true) chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -620,7 +621,8 @@ func TestSignAndPushP2PNotaryRequest(t *testing.T) {
w, err := wallet.NewWalletFromFile(notaryPath) w, err := wallet.NewWalletFromFile(notaryPath)
require.NoError(t, err) require.NoError(t, err)
ntr := w.Accounts[0] ntr := w.Accounts[0]
ntr.Decrypt(notaryPass) err = ntr.Decrypt(notaryPass)
require.NoError(t, err)
req.FallbackTransaction.Scripts[0] = transaction.Witness{ req.FallbackTransaction.Scripts[0] = transaction.Witness{
InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, ntr.PrivateKey().SignHashable(uint32(testchain.Network()), req.FallbackTransaction)...), InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, ntr.PrivateKey().SignHashable(uint32(testchain.Network()), req.FallbackTransaction)...),
VerificationScript: []byte{}, VerificationScript: []byte{},
@ -639,7 +641,7 @@ func TestSignAndPushP2PNotaryRequest(t *testing.T) {
func TestCalculateNotaryFee(t *testing.T) { func TestCalculateNotaryFee(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -667,7 +669,7 @@ func TestPing(t *testing.T) {
func TestCreateTxFromScript(t *testing.T) { func TestCreateTxFromScript(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -696,7 +698,7 @@ func TestCreateTxFromScript(t *testing.T) {
func TestCreateNEP17TransferTx(t *testing.T) { func TestCreateNEP17TransferTx(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -720,7 +722,7 @@ func TestCreateNEP17TransferTx(t *testing.T) {
func TestInvokeVerify(t *testing.T) { func TestInvokeVerify(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -762,7 +764,7 @@ func TestInvokeVerify(t *testing.T) {
func TestClient_GetNativeContracts(t *testing.T) { func TestClient_GetNativeContracts(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -776,7 +778,7 @@ func TestClient_GetNativeContracts(t *testing.T) {
func TestClient_NEP11(t *testing.T) { func TestClient_NEP11(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)
@ -842,7 +844,7 @@ func TestClient_NEP11(t *testing.T) {
func TestClient_NNS(t *testing.T) { func TestClient_NNS(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{}) c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err) require.NoError(t, err)

View file

@ -1010,7 +1010,7 @@ func TestRPC(t *testing.T) {
func TestSubmitOracle(t *testing.T) { func TestSubmitOracle(t *testing.T) {
chain, rpcSrv, httpSrv := initClearServerWithServices(t, true, false) chain, rpcSrv, httpSrv := initClearServerWithServices(t, true, false)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
rpc := `{"jsonrpc": "2.0", "id": 1, "method": "submitoracleresponse", "params": %s}` rpc := `{"jsonrpc": "2.0", "id": 1, "method": "submitoracleresponse", "params": %s}`
runCase := func(t *testing.T, fail bool, params ...string) func(t *testing.T) { runCase := func(t *testing.T, fail bool, params ...string) func(t *testing.T) {
@ -1046,7 +1046,7 @@ func TestSubmitNotaryRequest(t *testing.T) {
t.Run("disabled P2PSigExtensions", func(t *testing.T) { t.Run("disabled P2PSigExtensions", func(t *testing.T) {
chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false) chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
req := fmt.Sprintf(rpc, "[]") req := fmt.Sprintf(rpc, "[]")
body := doRPCCallOverHTTP(req, httpSrv.URL, t) body := doRPCCallOverHTTP(req, httpSrv.URL, t)
checkErrGetResult(t, body, true) checkErrGetResult(t, body, true)
@ -1054,7 +1054,7 @@ func TestSubmitNotaryRequest(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true) chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
runCase := func(t *testing.T, fail bool, params ...string) func(t *testing.T) { runCase := func(t *testing.T, fail bool, params ...string) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
@ -1156,7 +1156,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
e := &executor{chain: chain, httpSrv: httpSrv} e := &executor{chain: chain, httpSrv: httpSrv}
t.Run("single request", func(t *testing.T) { t.Run("single request", func(t *testing.T) {
@ -1595,9 +1595,11 @@ func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte {
url = "ws" + strings.TrimPrefix(url, "http") url = "ws" + strings.TrimPrefix(url, "http")
c, _, err := dialer.Dial(url+"/ws", nil) c, _, err := dialer.Dial(url+"/ws", nil)
require.NoError(t, err) require.NoError(t, err)
c.SetWriteDeadline(time.Now().Add(time.Second)) err = c.SetWriteDeadline(time.Now().Add(time.Second))
require.NoError(t, err)
require.NoError(t, c.WriteMessage(1, []byte(rpcCall))) require.NoError(t, c.WriteMessage(1, []byte(rpcCall)))
c.SetReadDeadline(time.Now().Add(time.Second)) err = c.SetReadDeadline(time.Now().Add(time.Second))
require.NoError(t, err)
_, body, err := c.ReadMessage() _, body, err := c.ReadMessage()
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, c.Close()) require.NoError(t, c.Close())

View file

@ -20,7 +20,8 @@ const testOverflow = false
func wsReader(t *testing.T, ws *websocket.Conn, msgCh chan<- []byte, isFinished *atomic.Bool) { func wsReader(t *testing.T, ws *websocket.Conn, msgCh chan<- []byte, isFinished *atomic.Bool) {
for { for {
ws.SetReadDeadline(time.Now().Add(time.Second)) err := ws.SetReadDeadline(time.Now().Add(time.Second))
require.NoError(t, err)
_, body, err := ws.ReadMessage() _, body, err := ws.ReadMessage()
if isFinished.Load() { if isFinished.Load() {
require.Error(t, err) require.Error(t, err)
@ -34,7 +35,7 @@ func wsReader(t *testing.T, ws *websocket.Conn, msgCh chan<- []byte, isFinished
func callWSGetRaw(t *testing.T, ws *websocket.Conn, msg string, respCh <-chan []byte) *response.Raw { func callWSGetRaw(t *testing.T, ws *websocket.Conn, msg string, respCh <-chan []byte) *response.Raw {
var resp = new(response.Raw) var resp = new(response.Raw)
ws.SetWriteDeadline(time.Now().Add(time.Second)) require.NoError(t, ws.SetWriteDeadline(time.Now().Add(time.Second)))
require.NoError(t, ws.WriteMessage(websocket.TextMessage, []byte(msg))) require.NoError(t, ws.WriteMessage(websocket.TextMessage, []byte(msg)))
body := <-respCh body := <-respCh
@ -90,7 +91,7 @@ func TestSubscriptions(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
for _, feed := range subFeeds { for _, feed := range subFeeds {
s := callSubscribe(t, c, respMsgs, fmt.Sprintf(`["%s"]`, feed)) s := callSubscribe(t, c, respMsgs, fmt.Sprintf(`["%s"]`, feed))
@ -243,7 +244,7 @@ func TestFilteredSubscriptions(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
// It's used as an end-of-event-stream, so it's always present. // It's used as an end-of-event-stream, so it's always present.
blockSubID := callSubscribe(t, c, respMsgs, `["block_added"]`) blockSubID := callSubscribe(t, c, respMsgs, `["block_added"]`)
@ -283,7 +284,7 @@ func TestFilteredBlockSubscriptions(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
blockSubID := callSubscribe(t, c, respMsgs, `["block_added", {"primary":3}]`) blockSubID := callSubscribe(t, c, respMsgs, `["block_added", {"primary":3}]`)
@ -322,7 +323,7 @@ func TestMaxSubscriptions(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
for i := 0; i < maxFeeds+1; i++ { for i := 0; i < maxFeeds+1; i++ {
var s string var s string
@ -368,7 +369,7 @@ func TestBadSubUnsub(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
testF := func(t *testing.T, cases map[string]string) func(t *testing.T) { testF := func(t *testing.T, cases map[string]string) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
@ -389,19 +390,20 @@ func TestBadSubUnsub(t *testing.T) {
} }
func doSomeWSRequest(t *testing.T, ws *websocket.Conn) { func doSomeWSRequest(t *testing.T, ws *websocket.Conn) {
ws.SetWriteDeadline(time.Now().Add(time.Second)) require.NoError(t, ws.SetWriteDeadline(time.Now().Add(time.Second)))
// It could be just about anything including invalid request, // It could be just about anything including invalid request,
// we only care about server handling being active. // we only care about server handling being active.
require.NoError(t, ws.WriteMessage(websocket.TextMessage, []byte(`{"jsonrpc": "2.0", "method": "getversion", "params": [], "id": 1}`))) require.NoError(t, ws.WriteMessage(websocket.TextMessage, []byte(`{"jsonrpc": "2.0", "method": "getversion", "params": [], "id": 1}`)))
ws.SetReadDeadline(time.Now().Add(time.Second)) err := ws.SetReadDeadline(time.Now().Add(time.Second))
_, _, err := ws.ReadMessage() require.NoError(t, err)
_, _, err = ws.ReadMessage()
require.NoError(t, err) require.NoError(t, err)
} }
func TestWSClientsLimit(t *testing.T) { func TestWSClientsLimit(t *testing.T) {
chain, rpcSrv, httpSrv := initClearServerWithInMemoryChain(t) chain, rpcSrv, httpSrv := initClearServerWithInMemoryChain(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
dialer := websocket.Dialer{HandshakeTimeout: time.Second} dialer := websocket.Dialer{HandshakeTimeout: time.Second}
url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
@ -440,7 +442,7 @@ func TestSubscriptionOverflow(t *testing.T) {
chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t)
defer chain.Close() defer chain.Close()
defer rpcSrv.Shutdown() defer func() { _ = rpcSrv.Shutdown() }()
resp := callWSGetRaw(t, c, `{"jsonrpc": "2.0","method": "subscribe","params": ["block_added"],"id": 1}`, respMsgs) resp := callWSGetRaw(t, c, `{"jsonrpc": "2.0","method": "subscribe","params": ["block_added"],"id": 1}`, respMsgs)
require.Nil(t, resp.Error) require.Nil(t, resp.Error)