Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
Cleanup server test

Signed-off-by: Derek McGowan <derek@mcg.dev>
  • Loading branch information
dmcgowan committed Jan 21, 2022
1 parent 9858689 commit 3351170
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
go-version: [1.17]
os: [ubuntu-18.04, macos-10.15, windows-2019]
os: [ubuntu-18.04, macos-10.15, windows-2022]

steps:
- uses: actions/setup-go@v2
Expand Down
54 changes: 54 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
linters:
enable:
- structcheck
- varcheck
- staticcheck
- unconvert
- gofmt
- goimports
- revive
- ineffassign
- vet
- unused
- misspell
disable:
- errcheck

linters-settings:
revive:
ignore-generated-headers: true
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: exported
- name: if-return
- name: increment-decrement
- name: var-naming
arguments: [["UID", "GID"], []]
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
- name: unused-parameter
- name: unreachable-code
- name: redefines-builtin-id

issues:
include:
- EXC0002

run:
timeout: 8m
skip-dirs:
- example
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (c *Client) Call(ctx context.Context, service, method string, req, resp int
}

if dl, ok := ctx.Deadline(); ok {
creq.TimeoutNano = dl.Sub(time.Now()).Nanoseconds()
creq.TimeoutNano = time.Until(dl).Nanoseconds()
}

info := &UnaryClientInfo{
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestUserOnCloseWait(t *testing.T) {
t.Fatalf("expected error %v, but got %v", context.DeadlineExceeded, err)
}

_ = <-dataCh
<-dataCh

if err := client.UserOnCloseWait(ctx); err != nil {
t.Fatalf("expected error nil , but got %v", err)
Expand Down
1 change: 1 addition & 0 deletions example/cmd/handshaker_other.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !linux
// +build !linux

/*
Expand Down
2 changes: 1 addition & 1 deletion handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func (fn handshakerFunc) Handshake(ctx context.Context, conn net.Conn) (net.Conn
return fn(ctx, conn)
}

func noopHandshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error) {
func noopHandshake(_ context.Context, conn net.Conn) (net.Conn, interface{}, error) {
return conn, nil, nil
}
2 changes: 1 addition & 1 deletion interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type UnaryServerInterceptor func(context.Context, Unmarshaler, *UnaryServerInfo,
// UnaryClientInterceptor specifies the interceptor function for client request/response
type UnaryClientInterceptor func(context.Context, *Request, *Response, *UnaryClientInfo, Invoker) error

func defaultServerInterceptor(ctx context.Context, unmarshal Unmarshaler, info *UnaryServerInfo, method Method) (interface{}, error) {
func defaultServerInterceptor(ctx context.Context, unmarshal Unmarshaler, _ *UnaryServerInfo, method Method) (interface{}, error) {
return method(ctx, unmarshal)
}

Expand Down
38 changes: 25 additions & 13 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,23 @@ func TestServer(t *testing.T) {
go server.Serve(ctx, listener)
defer server.Shutdown(ctx)

const calls = 2
results := make(chan callResult, 2)
go roundTrip(ctx, t, tclient, "bar", results)
go roundTrip(ctx, t, tclient, "baz", results)
testCases := []string{"bar", "baz"}
results := make(chan callResult, len(testCases))
for _, tc := range testCases {
go func(expected string) {
results <- roundTrip(ctx, tclient, expected)
}(tc)
}

for i := 0; i < calls; i++ {
for i := 0; i < len(testCases); {
result := <-results
if result.err != nil {
t.Fatalf("(%s): %v", result.name, result.err)
}
if !reflect.DeepEqual(result.received, result.expected) {
t.Fatalf("unexpected response: %+#v != %+#v", result.received, result.expected)
t.Fatalf("(%s): unexpected response: %+#v != %+#v", result.name, result.received, result.expected)
}
i++
}
}

Expand Down Expand Up @@ -483,29 +490,34 @@ func checkServerShutdown(t *testing.T, server *Server) {
}

type callResult struct {
name string
err error
input *testPayload
expected *testPayload
received *testPayload
}

func roundTrip(ctx context.Context, t *testing.T, client *testingClient, value string, results chan callResult) {
t.Helper()
func roundTrip(ctx context.Context, client *testingClient, name string) callResult {
var (
tp = &testPayload{
Foo: "bar",
Foo: name,
}
)

ctx = WithMetadata(ctx, MD{"foo": []string{"bar"}})
ctx = WithMetadata(ctx, MD{"foo": []string{name}})

resp, err := client.Test(ctx, tp)
if err != nil {
t.Fatal(err)
return callResult{
name: name,
err: err,
}
}

results <- callResult{
return callResult{
name: name,
input: tp,
expected: &testPayload{Foo: strings.Repeat(tp.Foo, 2), Metadata: "bar"},
expected: &testPayload{Foo: strings.Repeat(tp.Foo, 2), Metadata: name},
received: resp,
}
}
Expand Down
2 changes: 0 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func (r *StringList) Reset() { *r = StringList{} }
func (r *StringList) String() string { return fmt.Sprintf("%+#v", r) }
func (r *StringList) ProtoMessage() {}

func makeStringList(item ...string) StringList { return StringList{List: item} }

type KeyValue struct {
Key string `protobuf:"bytes,1,opt,name=key,proto3"`
Value string `protobuf:"bytes,2,opt,name=value,proto3"`
Expand Down
6 changes: 1 addition & 5 deletions unixcreds_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

type UnixCredentialsFunc func(*unix.Ucred) error

func (fn UnixCredentialsFunc) Handshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error) {
func (fn UnixCredentialsFunc) Handshake(_ context.Context, conn net.Conn) (net.Conn, interface{}, error) {
uc, err := requireUnixSocket(conn)
if err != nil {
return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: require unix socket: %w", err)
Expand Down Expand Up @@ -88,10 +88,6 @@ func UnixSocketRequireSameUser() UnixCredentialsFunc {
return UnixSocketRequireUidGid(euid, egid)
}

func requireRoot(ucred *unix.Ucred) error {
return requireUidGid(ucred, 0, 0)
}

func requireUidGid(ucred *unix.Ucred, uid, gid int) error {
if (uid != -1 && uint32(uid) != ucred.Uid) || (gid != -1 && uint32(gid) != ucred.Gid) {
return fmt.Errorf("ttrpc: invalid credentials: %v", syscall.EPERM)
Expand Down

0 comments on commit 3351170

Please sign in to comment.