From 17038234b215b7b7939b36b105574014ab053b8c Mon Sep 17 00:00:00 2001 From: ClaytonNorthey92 Date: Tue, 16 Jul 2024 10:29:01 -0400 Subject: [PATCH] remove lastErr variables from rpc_test.go lastErr was added to handle for something that likely shouldn't have been; it's an un-needed way to handle for errors. replace with simply failing the test if there are errors. fixes #182 --- service/tbc/rpc_test.go | 205 ++++++++++------------------------------ 1 file changed, 50 insertions(+), 155 deletions(-) diff --git a/service/tbc/rpc_test.go b/service/tbc/rpc_test.go index 0ef163d9d..66329bbf2 100644 --- a/service/tbc/rpc_test.go +++ b/service/tbc/rpc_test.go @@ -8,7 +8,6 @@ import ( "context" "encoding/hex" "encoding/json" - "fmt" "slices" "strings" "testing" @@ -56,7 +55,6 @@ func TestBlockHeadersByHeightRaw(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BlockHeadersByHeightRawResponse for { select { @@ -64,20 +62,17 @@ func TestBlockHeadersByHeightRaw(t *testing.T) { case <-ctx.Done(): t.Fatal(ctx.Err()) } - lastErr = nil err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BlockHeadersByHeightRawRequest{ Height: 55, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBlockHeadersByHeightRawResponse { @@ -86,14 +81,10 @@ func TestBlockHeadersByHeightRaw(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - if lastErr != nil { - t.Fatal(lastErr) - } - bh, err := bytes2Header(response.BlockHeaders[0]) if err != nil { t.Fatal(err) @@ -139,7 +130,6 @@ func TestBlockHeadersByHeight(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BlockHeadersByHeightResponse for { select { @@ -147,20 +137,17 @@ func TestBlockHeadersByHeight(t *testing.T) { case <-ctx.Done(): t.Fatal(ctx.Err()) } - lastErr = nil err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BlockHeadersByHeightRequest{ Height: 55, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBlockHeadersByHeightResponse { @@ -169,15 +156,11 @@ func TestBlockHeadersByHeight(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - if lastErr != nil { - t.Fatal(lastErr) - } - if response.Error != nil { t.Errorf("got unwanted error: %v", response.Error) } @@ -216,7 +199,6 @@ func TestBlockHeadersByHeightDoesNotExist(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BlockHeadersByHeightResponse for { select { @@ -224,20 +206,17 @@ func TestBlockHeadersByHeightDoesNotExist(t *testing.T) { case <-ctx.Done(): t.Fatal(ctx.Err()) } - lastErr = nil err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BlockHeadersByHeightRequest{ Height: 550, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBlockHeadersByHeightResponse { @@ -246,15 +225,11 @@ func TestBlockHeadersByHeightDoesNotExist(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - if lastErr != nil { - t.Fatal(lastErr) - } - if response.Error.Message != "block headers not found at height 550" { t.Fatalf("unexpected error message: %s", response.Error.Message) } @@ -287,7 +262,6 @@ func TestBlockHeaderBestRaw(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BlockHeaderBestRawResponse for { select { @@ -295,18 +269,15 @@ func TestBlockHeaderBestRaw(t *testing.T) { case <-ctx.Done(): t.Fatal(ctx.Err()) } - lastErr = nil err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BlockHeaderBestRawRequest{}) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBlockHeaderBestRawResponse { @@ -315,14 +286,10 @@ func TestBlockHeaderBestRaw(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - if lastErr != nil { - t.Fatal(lastErr) - } - bh, err := bytes2Header(response.BlockHeader) if err != nil { t.Fatal(err) @@ -368,7 +335,6 @@ func TestBtcBlockHeaderBest(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BlockHeaderBestResponse for { select { @@ -376,18 +342,16 @@ func TestBtcBlockHeaderBest(t *testing.T) { case <-ctx.Done(): t.Fatal(ctx.Err()) } - lastErr = nil + err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BlockHeaderBestRequest{}) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBlockHeaderBestResponse { @@ -396,15 +360,11 @@ func TestBtcBlockHeaderBest(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - if lastErr != nil { - t.Fatal(lastErr) - } - if response.Error != nil { t.Errorf("got unwanted error: %v", response.Error) } @@ -549,7 +509,6 @@ func TestBalanceByAddress(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.BalanceByAddressResponse for { select { @@ -558,20 +517,18 @@ func TestBalanceByAddress(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.BalanceByAddressRequest{ Address: tti.address(), }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdBalanceByAddressResponse { @@ -602,14 +559,10 @@ func TestBalanceByAddress(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } }) } } @@ -781,7 +734,6 @@ func TestUtxosByAddressRaw(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.UtxosByAddressRawResponse for { select { @@ -790,22 +742,20 @@ func TestUtxosByAddressRaw(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.UtxosByAddressRawRequest{ Address: tti.address(), Start: uint(tti.start), Count: uint(tti.limit), }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdUtxosByAddressRawResponse { @@ -827,14 +777,10 @@ func TestUtxosByAddressRaw(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } }) } } @@ -1006,7 +952,6 @@ func TestUtxosByAddress(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.UtxosByAddressResponse for { select { @@ -1015,22 +960,20 @@ func TestUtxosByAddress(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.UtxosByAddressRequest{ Address: tti.address(), Start: uint(tti.start), Count: uint(tti.limit), }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdUtxosByAddressResponse { @@ -1052,14 +995,10 @@ func TestUtxosByAddress(t *testing.T) { } break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } }) } } @@ -1098,7 +1037,6 @@ func TestTxByIdRaw(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdRawResponse for { select { @@ -1107,7 +1045,7 @@ func TestTxByIdRaw(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1120,15 +1058,13 @@ func TestTxByIdRaw(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdRawResponse { @@ -1156,14 +1092,10 @@ func TestTxByIdRaw(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func TestTxByIdRawInvalid(t *testing.T) { @@ -1199,7 +1131,6 @@ func TestTxByIdRawInvalid(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdRawResponse for { select { @@ -1208,7 +1139,7 @@ func TestTxByIdRawInvalid(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1223,15 +1154,13 @@ func TestTxByIdRawInvalid(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdRawResponse { @@ -1251,14 +1180,10 @@ func TestTxByIdRawInvalid(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func TestTxByIdRawNotFound(t *testing.T) { @@ -1309,7 +1234,6 @@ func TestTxByIdRawNotFound(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdRawResponse for { select { @@ -1318,7 +1242,7 @@ func TestTxByIdRawNotFound(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1333,15 +1257,13 @@ func TestTxByIdRawNotFound(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdRawResponse { @@ -1361,14 +1283,10 @@ func TestTxByIdRawNotFound(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func TestTxById(t *testing.T) { @@ -1405,7 +1323,6 @@ func TestTxById(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdResponse for { select { @@ -1416,7 +1333,6 @@ func TestTxById(t *testing.T) { indexAll(ctx, t, tbcServer) - lastErr = nil txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1427,15 +1343,13 @@ func TestTxById(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdResponse { @@ -1460,14 +1374,10 @@ func TestTxById(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func TestTxByIdInvalid(t *testing.T) { @@ -1503,7 +1413,6 @@ func TestTxByIdInvalid(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdResponse for { select { @@ -1512,7 +1421,7 @@ func TestTxByIdInvalid(t *testing.T) { t.Fatal(ctx.Err()) } indexAll(ctx, t, tbcServer) - lastErr = nil + txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1525,15 +1434,13 @@ func TestTxByIdInvalid(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdResponse { @@ -1553,14 +1460,10 @@ func TestTxByIdInvalid(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func TestTxByIdNotFound(t *testing.T) { @@ -1611,7 +1514,6 @@ func TestTxByIdNotFound(t *testing.T) { conn: protocol.NewWSConn(c), } - var lastErr error var response tbcapi.TxByIdResponse for { select { @@ -1622,7 +1524,6 @@ func TestTxByIdNotFound(t *testing.T) { indexAll(ctx, t, tbcServer) - lastErr = nil txId := getRandomTxId(ctx, t, bitcoindContainer) txIdBytes, err := hex.DecodeString(txId) if err != nil { @@ -1635,15 +1536,13 @@ func TestTxByIdNotFound(t *testing.T) { TxId: txIdBytes, }) if err != nil { - lastErr = err - continue + t.Fatal(err) } var v protocol.Message err = wsjson.Read(ctx, c, &v) if err != nil { - lastErr = err - continue + t.Fatal(err) } if v.Header.Command == tbcapi.CmdTxByIdResponse { @@ -1663,14 +1562,10 @@ func TestTxByIdNotFound(t *testing.T) { break } else { - lastErr = fmt.Errorf("received unexpected command: %s", v.Header.Command) + t.Fatalf("received unexpected command: %s", v.Header.Command) } } - - if lastErr != nil { - t.Fatal(lastErr) - } } func assertPing(ctx context.Context, t *testing.T, c *websocket.Conn, cmd protocol.Command) {