Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ linters-settings:
recommendations:
- errors
forbidigo:
analyze-types: true
forbid:
- ^fmt.Print(f|ln)?$
- ^log.(Panic|Fatal|Print)(f|ln)?$
- ^os.Exit$
- ^panic$
- ^print(ln)?$
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
pkg: ^testing$
msg: "use testify/assert instead"
varnamelen:
max-distance: 12
min-name-length: 2
Expand Down Expand Up @@ -127,9 +131,12 @@ issues:
exclude-dirs-use-default: false
exclude-rules:
# Allow complex tests and examples, better to be self contained
- path: (examples|main\.go|_test\.go)
- path: (examples|main\.go)
linters:
- gocognit
- forbidigo
- path: _test\.go
linters:
- gocognit

# Allow forbidden identifiers in CLI commands
Expand Down
124 changes: 49 additions & 75 deletions connctx/connctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package connctx

import (
"bytes"
"context"
"errors"
"io"
"net"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestRead(t *testing.T) {
Expand All @@ -30,20 +31,12 @@ func TestRead(t *testing.T) {
c := New(ca)
b := make([]byte, 100)
n, err := c.ReadContext(context.Background(), b)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Errorf("Wrong data length, expected %d, got %d", len(data), n)
}
if !bytes.Equal(data, b[:n]) {
t.Errorf("Wrong data, expected %v, got %v", data, b)
}
assert.NoError(t, err)
assert.Len(t, data, n)
assert.Equal(t, data, b[:n])

err = <-chErr
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
}

func TestReadTimeout(t *testing.T) {
Expand All @@ -58,12 +51,8 @@ func TestReadTimeout(t *testing.T) {
c := New(ca)
b := make([]byte, 100)
n, err := c.ReadContext(ctx, b)
if err == nil {
t.Error("Read unexpectedly succeeded")
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.Error(t, err)
assert.Empty(t, n)
}

func TestReadCancel(t *testing.T) {
Expand All @@ -81,12 +70,8 @@ func TestReadCancel(t *testing.T) {
c := New(ca)
b := make([]byte, 100)
n, err := c.ReadContext(ctx, b)
if err == nil {
t.Error("Read unexpectedly succeeded")
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.Error(t, err)
assert.Empty(t, n)
}

func TestReadClosed(t *testing.T) {
Expand All @@ -97,12 +82,8 @@ func TestReadClosed(t *testing.T) {

b := make([]byte, 100)
n, err := c.ReadContext(context.Background(), b)
if !errors.Is(err, io.EOF) {
t.Errorf("Expected error '%v', got '%v'", io.EOF, err)
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.ErrorIs(t, err, io.EOF)
assert.Empty(t, n)
}

func TestWrite(t *testing.T) {
Expand All @@ -124,21 +105,13 @@ func TestWrite(t *testing.T) {
c := New(ca)
data := []byte{0x01, 0x02, 0xFF}
n, err := c.WriteContext(context.Background(), data)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Errorf("Wrong data length, expected %d, got %d", len(data), n)
}
assert.NoError(t, err)
assert.Len(t, data, n)

err = <-chErr
b := <-chRead
if !bytes.Equal(data, b) {
t.Errorf("Wrong data, expected %v, got %v", data, b)
}
if err != nil {
t.Fatal(err)
}
assert.Equal(t, b, data)
assert.NoError(t, err)
}

func TestWriteTimeout(t *testing.T) {
Expand All @@ -153,12 +126,8 @@ func TestWriteTimeout(t *testing.T) {
c := New(ca)
b := make([]byte, 100)
n, err := c.WriteContext(ctx, b)
if err == nil {
t.Error("Write unexpectedly succeeded")
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.Error(t, err)
assert.Empty(t, n)
}

func TestWriteCancel(t *testing.T) {
Expand All @@ -176,12 +145,8 @@ func TestWriteCancel(t *testing.T) {
c := New(ca)
b := make([]byte, 100)
n, err := c.WriteContext(ctx, b)
if err == nil {
t.Error("Write unexpectedly succeeded")
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.Error(t, err)
assert.Empty(t, n)
}

func TestWriteClosed(t *testing.T) {
Expand All @@ -192,12 +157,8 @@ func TestWriteClosed(t *testing.T) {

b := make([]byte, 100)
n, err := c.WriteContext(context.Background(), b)
if !errors.Is(err, ErrClosing) {
t.Errorf("Expected error '%v', got '%v'", ErrClosing, err)
}
if n != 0 {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
assert.ErrorIs(t, err, ErrClosing)
assert.Empty(t, n)
}

// Test for TestLocalAddrAndRemoteAddr.
Expand All @@ -211,26 +172,39 @@ func (a stringAddr) String() string { return a.addr }

type connAddrMock struct{}

func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
func (*connAddrMock) Read(_ []byte) (n int, err error) { panic("unimplemented") }
func (*connAddrMock) Write(_ []byte) (n int, err error) { panic("unimplemented") }
func (*connAddrMock) Close() error { panic("unimplemented") }
func (*connAddrMock) SetDeadline(_ time.Time) error { panic("unimplemented") }
func (*connAddrMock) SetReadDeadline(_ time.Time) error { panic("unimplemented") }
func (*connAddrMock) SetWriteDeadline(_ time.Time) error { panic("unimplemented") }
func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
func (*connAddrMock) Read(_ []byte) (n int, err error) {
panic("unimplemented") //nolint
}

func (*connAddrMock) Write(_ []byte) (n int, err error) {
panic("unimplemented") //nolint
}

func (*connAddrMock) Close() error {
panic("unimplemented") //nolint
}

func (*connAddrMock) SetDeadline(_ time.Time) error {
panic("unimplemented") //nolint
}

func (*connAddrMock) SetReadDeadline(_ time.Time) error {
panic("unimplemented") //nolint
}

func (*connAddrMock) SetWriteDeadline(_ time.Time) error {
panic("unimplemented") //nolint
}

func TestLocalAddrAndRemoteAddr(t *testing.T) {
c := New(&connAddrMock{})
al := c.LocalAddr()
ar := c.RemoteAddr()

if al.String() != "local_addr" {
t.Error("Wrong LocalAddr implementation")
}
if ar.String() != "remote_addr" {
t.Error("Wrong RemoteAddr implementation")
}
assert.Equal(t, "local_addr", al.String())
assert.Equal(t, "remote_addr", ar.String())
}

func BenchmarkBase(b *testing.B) {
Expand Down
48 changes: 14 additions & 34 deletions deadline/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package deadline

import (
"bytes"
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestDeadline(t *testing.T) {
Expand All @@ -32,9 +32,7 @@ func TestDeadline(t *testing.T) {

calls := collectCh(ch, 3, 100*time.Millisecond)
expectedCalls := []byte{0, 2, 1}
if !bytes.Equal(calls, expectedCalls) {
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
}
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
})

t.Run("DeadlineExtend", func(t *testing.T) { //nolint:dupl
Expand All @@ -55,9 +53,7 @@ func TestDeadline(t *testing.T) {

calls := collectCh(ch, 3, 100*time.Millisecond)
expectedCalls := []byte{0, 1, 2}
if !bytes.Equal(calls, expectedCalls) {
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
}
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
})

t.Run("DeadlinePretend", func(t *testing.T) { //nolint:dupl
Expand All @@ -78,9 +74,7 @@ func TestDeadline(t *testing.T) {

calls := collectCh(ch, 3, 100*time.Millisecond)
expectedCalls := []byte{2, 0, 1}
if !bytes.Equal(calls, expectedCalls) {
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
}
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
})

t.Run("DeadlineCancel", func(t *testing.T) {
Expand All @@ -98,9 +92,7 @@ func TestDeadline(t *testing.T) {

calls := collectCh(ch, 2, 60*time.Millisecond)
expectedCalls := []byte{0}
if !bytes.Equal(calls, expectedCalls) {
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
}
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
})
}

Expand Down Expand Up @@ -134,42 +126,30 @@ func TestContext(t *testing.T) { //nolint:cyclop

select {
case <-deadline.Done():
t.Fatal("Deadline unexpectedly done")
assert.Fail(t, "Deadline unexpectedly done")
case <-time.After(50 * time.Millisecond):
}
if err := deadline.Err(); err != nil {
t.Errorf("Wrong Err(), expected: nil, got: %v", err)
}
assert.NoError(t, deadline.Err())
deadline.Set(time.Unix(0, 1)) // exceeded
select {
case <-deadline.Done():
case <-time.After(50 * time.Millisecond):
t.Fatal("Timeout")
}
if err := deadline.Err(); !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Wrong Err(), expected: %v, got: %v", context.DeadlineExceeded, err)
assert.Fail(t, "Timeout")
}
assert.ErrorIs(t, deadline.Err(), context.DeadlineExceeded)
})
t.Run("Deadline", func(t *testing.T) {
d := New()
t0, expired0 := d.Deadline()
if !t0.IsZero() {
t.Errorf("Initial Deadline is expected to be 0, got %v", t0)
}
if expired0 {
t.Error("Deadline is not expected to be expired at initial state")
}
assert.True(t, t0.IsZero(), "Initial Deadline is expected to be 0")
assert.False(t, expired0, "Deadline is not expected to be expired at initial state")

dl := time.Unix(12345, 0)
d.Set(dl) // exceeded

t1, expired1 := d.Deadline()
if !t1.Equal(dl) {
t.Errorf("Initial Deadline is expected to be %v, got %v", dl, t1)
}
if !expired1 {
t.Error("Deadline is expected to be expired")
}
assert.True(t, t1.Equal(dl), "Initial Deadline is expected to be %v, got %v", dl, t1)
assert.True(t, expired1, "Deadline is expected to be expired")
})
}

Expand Down
Loading
Loading