Skip to content

Commit

Permalink
Reenable SA1019 lint (#5092)
Browse files Browse the repository at this point in the history
## Motivation
Reenable disabled lint

## Changes
- fixed code
- reenabled staticcheck SA1019
  • Loading branch information
poszu committed Sep 27, 2023
1 parent a8ab1b0 commit 41434a0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
6 changes: 1 addition & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,7 @@ issues:
# exclude:
# - abcdef
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some `staticcheck` messages.
- linters:
- staticcheck
text: "SA1019:"
# exclude-rules:
# Independently of option `exclude` we use default exclude patterns,
# it can be disabled by this option.
# To list all excluded by default patterns execute `golangci-lint run --help`.
Expand Down
29 changes: 13 additions & 16 deletions api/grpcserver/grpcserver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package grpcserver

//lint:file-ignore SA1019 hide deprecated protobuf version error
import (
"bytes"
"context"
Expand All @@ -15,13 +14,10 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/merkle-tree"
Expand All @@ -35,6 +31,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/codec"
Expand Down Expand Up @@ -466,11 +464,10 @@ func (*SmeshingAPIMock) Coinbase() types.Address {
func (*SmeshingAPIMock) SetCoinbase(coinbase types.Address) {
}

func marshalProto(t *testing.T, msg proto.Message) string {
var buf bytes.Buffer
var m jsonpb.Marshaler
require.NoError(t, m.Marshal(&buf, msg))
return buf.String()
func marshalProto(t *testing.T, msg proto.Message) []byte {
buf, err := protojson.Marshal(msg)
require.NoError(t, err)
return buf
}

func launchServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
Expand Down Expand Up @@ -501,15 +498,15 @@ func launchServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
}
}

func callEndpoint(t *testing.T, url, payload string) (string, int) {
resp, err := http.Post(url, "application/json", strings.NewReader(payload))
func callEndpoint(t *testing.T, url string, payload []byte) ([]byte, int) {
resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
require.NoError(t, err)
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
buf, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())

return string(buf), resp.StatusCode
return buf, resp.StatusCode
}

func getFreePort(optionalPort int) (int, error) {
Expand Down Expand Up @@ -2361,7 +2358,7 @@ func TestJsonApi(t *testing.T) {

payload := marshalProto(t, &pb.EchoRequest{Msg: &pb.SimpleString{Value: message}})
url := fmt.Sprintf("http://%s/%s", cfg.JSONListener, "v1/node/echo")
_, err := http.Post(url, "application/json", strings.NewReader(payload))
_, err := http.Post(url, "application/json", bytes.NewReader(payload))
require.Error(t, err)
shutDown()

Expand All @@ -2384,14 +2381,14 @@ func TestJsonApi(t *testing.T) {
respBody, respStatus := callEndpoint(t, fmt.Sprintf("http://%s/%s", cfg.JSONListener, "v1/node/echo"), payload)
require.Equal(t, http.StatusOK, respStatus)
var msg pb.EchoResponse
require.NoError(t, jsonpb.UnmarshalString(respBody, &msg))
require.NoError(t, protojson.Unmarshal(respBody, &msg))
require.Equal(t, message, msg.Msg.Value)

// Test MeshService
respBody2, respStatus2 := callEndpoint(t, fmt.Sprintf("http://%s/%s", cfg.JSONListener, "v1/mesh/genesistime"), "")
respBody2, respStatus2 := callEndpoint(t, fmt.Sprintf("http://%s/%s", cfg.JSONListener, "v1/mesh/genesistime"), nil)
require.Equal(t, http.StatusOK, respStatus2)
var msg2 pb.GenesisTimeResponse
require.NoError(t, jsonpb.UnmarshalString(respBody2, &msg2))
require.NoError(t, protojson.Unmarshal(respBody2, &msg2))
require.Equal(t, uint64(genesis.Unix()), msg2.Unixtime.Value)
}

Expand Down
2 changes: 0 additions & 2 deletions eligibility/fixedoracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

const (
numOfClients = 100
strLen = 128
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

func TestFixedRolacle_Eligible(t *testing.T) {
Expand Down
51 changes: 27 additions & 24 deletions node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package node

//lint:file-ignore SA1019 hide deprecated protobuf version error
import (
"bytes"
"context"
Expand All @@ -12,13 +11,10 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/post/initialization"
Expand All @@ -34,6 +30,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
Expand Down Expand Up @@ -256,23 +254,21 @@ func TestSpacemeshApp_Cmd(t *testing.T) {
r.Equal(config.JSONLogEncoder, app.Config.LOGGING.Encoder)
}

func marshalProto(t *testing.T, msg proto.Message) string {
var buf bytes.Buffer
var m jsonpb.Marshaler
require.NoError(t, m.Marshal(&buf, msg))
return buf.String()
func marshalProto(t *testing.T, msg proto.Message) []byte {
buf, err := protojson.Marshal(msg)
require.NoError(t, err)
return buf
}

func callEndpoint(t *testing.T, endpoint, payload, address string) (string, int) {
url := fmt.Sprintf("http://%s/%s", address, endpoint)
resp, err := http.Post(url, "application/json", strings.NewReader(payload))
func callEndpoint(t *testing.T, url string, payload []byte) ([]byte, int) {
resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
require.NoError(t, err)
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
buf, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())

return string(buf), resp.StatusCode
return buf, resp.StatusCode
}

func TestSpacemeshApp_GrpcService(t *testing.T) {
Expand Down Expand Up @@ -300,11 +296,13 @@ func TestSpacemeshApp_GrpcService(t *testing.T) {
r.NoError(err)
r.Empty(app.Config.API.PublicServices)

_, err = grpc.Dial(
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err = grpc.DialContext(
ctx,
listener,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithTimeout(2*time.Second),
)
r.ErrorContains(err, "context deadline exceeded")

Expand All @@ -317,11 +315,13 @@ func TestSpacemeshApp_GrpcService(t *testing.T) {
r.Equal(listener, app.Config.API.PublicListener)
r.Contains(app.Config.API.PublicServices, "node")

conn, err := grpc.Dial(
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
conn, err := grpc.DialContext(
ctx,
listener,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithTimeout(2*time.Second),
)
r.NoError(err)
t.Cleanup(func() { r.NoError(conn.Close()) })
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestSpacemeshApp_JsonServiceNotRunning(t *testing.T) {

// We expect this one to fail
url := fmt.Sprintf("http://%s/%s", app.Config.API.JSONListener, "v1/node/echo")
_, err = http.Post(url, "application/json", strings.NewReader(payload))
_, err = http.Post(url, "application/json", bytes.NewReader(payload))
r.Error(err)

events.CloseEventReporter()
Expand Down Expand Up @@ -392,18 +392,18 @@ func TestSpacemeshApp_JsonService(t *testing.T) {
r.Contains(app.Config.API.PublicServices, "node")

var (
respBody string
respBody []byte
respStatus int
)
require.Eventually(t, func() bool {
respBody, respStatus = callEndpoint(t, "v1/node/echo", payload, app.Config.API.JSONListener)
respBody, respStatus = callEndpoint(t, fmt.Sprintf("http://%s/v1/node/echo", app.Config.API.JSONListener), payload)
return respStatus == http.StatusOK
}, 2*time.Second, 100*time.Millisecond)
var msg pb.EchoResponse
require.NoError(t, jsonpb.UnmarshalString(respBody, &msg))
require.NoError(t, protojson.Unmarshal(respBody, &msg))
require.Equal(t, message, msg.Msg.Value)
require.Equal(t, http.StatusOK, respStatus)
require.NoError(t, jsonpb.UnmarshalString(respBody, &msg))
require.NoError(t, protojson.Unmarshal(respBody, &msg))
require.Equal(t, message, msg.Msg.Value)
}

Expand Down Expand Up @@ -594,11 +594,14 @@ func TestSpacemeshApp_TransactionService(t *testing.T) {

<-app.Started()
require.Eventually(t, func() bool { return app.syncer.IsSynced(ctx) }, 4*time.Second, 10*time.Millisecond)
conn, err := grpc.Dial(

ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := grpc.DialContext(
ctx,
listener,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithTimeout(5*time.Second),
)
r.NoError(err)
t.Cleanup(func() { r.NoError(conn.Close()) })
Expand Down

0 comments on commit 41434a0

Please sign in to comment.