Skip to content

Commit

Permalink
Fixes linter and test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Jan 13, 2025
1 parent 05d7e90 commit c73bfce
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 52 deletions.
16 changes: 4 additions & 12 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"flag"
"fmt"
"math/big"
"os"
"testing"
Expand All @@ -18,7 +17,6 @@ import (
"github.com/NethermindEth/starknet.go/mocks"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
"github.com/joho/godotenv"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
Expand All @@ -43,18 +41,12 @@ var (
//
// none
func TestMain(m *testing.M) {
flag.StringVar(&testEnv, "env", "mock", "set the test environment")
flag.StringVar(&testEnv, "env", "devnet", "set the test environment")
flag.Parse()
if testEnv == "mock" {
return
}
base = os.Getenv("INTEGRATION_BASE")
if base == "" {
if err := godotenv.Load(fmt.Sprintf(".env.%s", testEnv)); err != nil {
panic(fmt.Sprintf("Failed to load .env.%s, err: %s", testEnv, err))
}
base = os.Getenv("INTEGRATION_BASE")
}
base = "http://localhost:5050"
os.Exit(m.Run())
}

Expand Down Expand Up @@ -1076,7 +1068,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
Timeout: 3, // Should poll 3 times
Hash: new(felt.Felt).SetUint64(100),
ExpectedReceipt: rpc.TransactionReceipt{},
ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "Post \"http://localhost:5050\": context deadline exceeded"}),
ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "context deadline exceeded"}),
},
},
}[testEnv]
Expand All @@ -1090,7 +1082,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
rpcErr, ok := err.(*rpc.RPCError)
require.True(t, ok)
require.Equal(t, test.ExpectedErr.Code, rpcErr.Code)
require.Equal(t, test.ExpectedErr.Data.Message, rpcErr.Data.Message)
require.Contains(t, rpcErr.Data.Message, test.ExpectedErr.Data.Message) // sometimes the error message starts with "Post \"http://localhost:5050\":..."
} else {
require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (*resp).ExecutionStatus)
}
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func (c *Client) read(codec ServerCodec) {
msgs, batch, err := codec.readBatch()
if _, ok := err.(*json.SyntaxError); ok {
msg := errorMessage(&parseError{err.Error()})
codec.writeJSON(context.Background(), msg, true)
_ = codec.writeJSON(context.Background(), msg, true)
}
if err != nil {
c.readErr <- err
Expand Down
12 changes: 7 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func TestClientSubscribeInvalidArg(t *testing.T) {
t.Error(string(buf))
}
}()
client.EthSubscribe(context.Background(), arg, "foo_bar")
_, _ = client.EthSubscribe(context.Background(), arg, "foo_bar")
}
check(true, nil)
check(true, 1)
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestUnsubscribeTimeout(t *testing.T) {
t.Parallel()

srv := NewServer()
srv.RegisterName("nftest", new(notificationTestService))
_ = srv.RegisterName("nftest", new(notificationTestService))

// Setup middleware to block on unsubscribe.
p1, p2 := net.Pipe()
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestClientSubscriptionUnsubscribeServer(t *testing.T) {

// Create the server.
srv := NewServer()
srv.RegisterName("nftest", new(notificationTestService))
_ = srv.RegisterName("nftest", new(notificationTestService))
p1, p2 := net.Pipe()
recorder := &unsubscribeRecorder{ServerCodec: NewCodec(p1)}
go srv.ServeCodec(recorder, OptionMethodInvocation|OptionSubscriptions)
Expand Down Expand Up @@ -680,7 +680,7 @@ func TestClientSubscriptionChannelClose(t *testing.T) {
defer srv.Stop()
defer httpsrv.Close()

srv.RegisterName("nftest", new(notificationTestService))
_ = srv.RegisterName("nftest", new(notificationTestService))
client, _ := Dial(wsURL)
defer client.Close()

Expand Down Expand Up @@ -842,7 +842,9 @@ func TestClientReconnect(t *testing.T) {
if err != nil {
t.Fatal("can't listen:", err)
}
go http.Serve(l, srv.WebsocketHandler([]string{"*"}))
go func() {
_ = http.Serve(l, srv.WebsocketHandler([]string{"*"}))
}()
return srv, l
}

Expand Down
14 changes: 7 additions & 7 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (b *batchCallBuffer) doWrite(ctx context.Context, conn jsonWriter, isErrorR
}
b.wrote = true // can only write once
if len(b.resp) > 0 {
conn.writeJSON(ctx, b.resp, isErrorResponse)
_ = conn.writeJSON(ctx, b.resp, isErrorResponse)
}
}

Expand All @@ -173,7 +173,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
if len(msgs) == 0 {
h.startCallProc(func(cp *callProc) {
resp := errorMessage(&invalidRequestError{"empty batch"})
h.conn.writeJSON(cp.ctx, resp, true)
_ = h.conn.writeJSON(cp.ctx, resp, true)
})
return
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
h.addSubscriptions(cp.notifiers)
callBuffer.write(cp.ctx, h.conn)
for _, n := range cp.notifiers {
n.activate()
_ = n.activate()
}
})
}
Expand All @@ -261,7 +261,7 @@ func (h *handler) respondWithBatchTooLarge(cp *callProc, batch []*jsonrpcMessage
break
}
}
h.conn.writeJSON(cp.ctx, []*jsonrpcMessage{resp}, true)
_ = h.conn.writeJSON(cp.ctx, []*jsonrpcMessage{resp}, true)
}

// handleMsg handles a single non-batch message.
Expand Down Expand Up @@ -291,7 +291,7 @@ func (h *handler) handleNonBatchCall(cp *callProc, msg *jsonrpcMessage) {
cancel()
responded.Do(func() {
resp := msg.errorResponse(&internalServerError{errcodeTimeout, errMsgTimeout})
h.conn.writeJSON(cp.ctx, resp, true)
_ = h.conn.writeJSON(cp.ctx, resp, true)
})
})
}
Expand All @@ -303,11 +303,11 @@ func (h *handler) handleNonBatchCall(cp *callProc, msg *jsonrpcMessage) {
h.addSubscriptions(cp.notifiers)
if answer != nil {
responded.Do(func() {
h.conn.writeJSON(cp.ctx, answer, false)
_ = h.conn.writeJSON(cp.ctx, answer, false)
})
}
for _, n := range cp.notifiers {
n.activate()
_ = n.activate()
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestHTTPRespBodyUnlimited(t *testing.T) {

s := NewServer()
defer s.Stop()
s.RegisterName("test", largeRespService{respLength})
_ = s.RegisterName("test", largeRespService{respLength})
ts := httptest.NewServer(s)
defer ts.Close()

Expand Down
8 changes: 4 additions & 4 deletions client/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (c *jsonCodec) writeJSON(ctx context.Context, v interface{}, isErrorRespons
if !ok {
deadline = time.Now().Add(defaultWriteTimeout)
}
c.conn.SetWriteDeadline(deadline)
_ = c.conn.SetWriteDeadline(deadline)
return c.encode(v, isErrorResponse)
}

Expand All @@ -283,15 +283,15 @@ func (c *jsonCodec) closed() <-chan interface{} {
func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) {
if !isBatch(raw) {
msgs := []*jsonrpcMessage{{}}
json.Unmarshal(raw, &msgs[0])
_ = json.Unmarshal(raw, &msgs[0])
return msgs, false
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.Token() // skip '['
_, _ = dec.Token() // skip '['
var msgs []*jsonrpcMessage
for dec.More() {
msgs = append(msgs, new(jsonrpcMessage))
dec.Decode(&msgs[len(msgs)-1])
_ = dec.Decode(&msgs[len(msgs)-1])
}
return msgs, true
}
Expand Down
2 changes: 1 addition & 1 deletion client/log/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (h *TerminalHandler) Handle(_ context.Context, r slog.Record) error {
h.mu.Lock()
defer h.mu.Unlock()
buf := h.format(h.buf, r, h.useColor)
h.wr.Write(buf)
_, _ = h.wr.Write(buf)
h.buf = buf[:0]
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion client/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (l *logger) Write(level slog.Level, msg string, attrs ...any) {
}
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.Add(attrs...)
l.inner.Handler().Handle(context.Background(), r)
_ = l.inner.Handler().Handle(context.Background(), r)
}

func (l *logger) Log(level slog.Level, msg string, attrs ...any) {
Expand Down
4 changes: 2 additions & 2 deletions client/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewServer() *Server {
// Register the default service providing meta information about the RPC service such
// as the services and methods it offers.
rpcService := &RPCService{server}
server.RegisterName(MetadataApi, rpcService)
_ = server.RegisterName(MetadataApi, rpcService)
return server
}

Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
if err != nil {
if msg := messageForReadError(err); msg != "" {
resp := errorMessage(&invalidMessageError{msg})
codec.writeJSON(ctx, resp, true)
_ = codec.writeJSON(ctx, resp, true)
}
return
}
Expand Down
14 changes: 8 additions & 6 deletions client/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func runTestScript(t *testing.T, file string) {
case strings.HasPrefix(line, "--> "):
t.Log(line)
// write to connection
clientConn.SetWriteDeadline(time.Now().Add(5 * time.Second))
_ = clientConn.SetWriteDeadline(time.Now().Add(5 * time.Second))
if _, err := io.WriteString(clientConn, line[4:]+"\n"); err != nil {
t.Fatalf("write error: %v", err)
}
case strings.HasPrefix(line, "<-- "):
t.Log(line)
want := line[4:]
// read line from connection and compare text
clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
_ = clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
sent, err := readbuf.ReadString('\n')
if err != nil {
t.Fatalf("read error: %v", err)
Expand All @@ -124,7 +124,9 @@ func TestServerShortLivedConn(t *testing.T) {
t.Fatal("can't listen:", err)
}
defer listener.Close()
go server.ServeListener(listener)
go func() {
_ = server.ServeListener(listener)
}()

var (
request = `{"jsonrpc":"2.0","id":1,"method":"rpc_modules"}` + "\n"
Expand All @@ -137,10 +139,10 @@ func TestServerShortLivedConn(t *testing.T) {
t.Fatal("can't dial:", err)
}

conn.SetDeadline(deadline)
_ = conn.SetDeadline(deadline)
// Write the request, then half-close the connection so the server stops reading.
conn.Write([]byte(request))
conn.(*net.TCPConn).CloseWrite()
_, _ = conn.Write([]byte(request))
_ = conn.(*net.TCPConn).CloseWrite()
// Now try to get the response.
buf := make([]byte, 2000)
n, err := conn.Read(buf)
Expand Down
2 changes: 1 addition & 1 deletion client/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (sub *ClientSubscription) run() {

// Call the unsubscribe method on the server.
if unsubscribe {
sub.requestUnsubscribe()
_ = sub.requestUnsubscribe()
}

// Send the error.
Expand Down
5 changes: 0 additions & 5 deletions client/testdata/invalid-syntax.json

This file was deleted.

4 changes: 2 additions & 2 deletions client/testservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *testService) CallMeBackLater(ctx context.Context, method string, args [
go func() {
<-ctx.Done()
var result interface{}
c.Call(&result, method, args...)
_ = c.Call(&result, method, args...)
}()
return nil
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (s *notificationTestService) HangSubscription(ctx context.Context, val int)
subscription := notifier.CreateSubscription()

go func() {
notifier.Notify(subscription.ID, val)
_ = notifier.Notify(subscription.ID, val)
}()
return subscription, nil
}
Expand Down
8 changes: 4 additions & 4 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@ func (wc *websocketCodec) pingLoop() {

case <-pingTimer.C:
wc.jsonCodec.encMu.Lock()
wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
wc.conn.WriteMessage(websocket.PingMessage, nil)
wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
_ = wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
_ = wc.conn.WriteMessage(websocket.PingMessage, nil)
_ = wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
wc.jsonCodec.encMu.Unlock()
pingTimer.Reset(wsPingInterval)

case <-wc.pongReceived:
wc.conn.SetReadDeadline(time.Time{})
_ = wc.conn.SetReadDeadline(time.Time{})
}
}
}

0 comments on commit c73bfce

Please sign in to comment.