Skip to content

Commit

Permalink
Merge pull request #130 from hashicorp/rboyer/pass-race-and-lint
Browse files Browse the repository at this point in the history
chore: update tests to pass the race detector and linter
  • Loading branch information
rboyer authored Aug 6, 2024
2 parents 6034404 + 88ad8a5 commit 8bd691f
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 425 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI Tests
on:
pull_request:
paths-ignore:
- 'README.md'
push:
branches:
- 'master'
paths-ignore:
- 'README.md'

permissions:
contents: read

jobs:
go-fmt-and-vet:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: '1.20'
cache: true
- run: |
files=$(go fmt ./...)
if [ -n "$files" ]; then
echo "The following file(s) do not conform to go fmt:"
echo "$files"
exit 1
fi
go-test:
needs: go-fmt-and-vet
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: '1.20'
cache: true
- run: |
go test -race ./...
59 changes: 24 additions & 35 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package yamux

import (
"fmt"
"io"
"io/ioutil"
"testing"
)

func BenchmarkPing(b *testing.B) {
client, server := testClientServer()
defer func() {
client.Close()
server.Close()
}()
client, _ := testClientServer(b)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -28,11 +24,7 @@ func BenchmarkPing(b *testing.B) {
}

func BenchmarkAccept(b *testing.B) {
client, server := testClientServer()
defer func() {
client.Close()
server.Close()
}()
client, server := testClientServer(b)

doneCh := make(chan struct{})
b.ReportAllocs()
Expand Down Expand Up @@ -107,25 +99,20 @@ func BenchmarkSendRecvLarge(b *testing.B) {
}

func benchmarkSendRecv(b *testing.B, sendSize, recvSize int) {
client, server := testClientServer()
defer func() {
client.Close()
server.Close()
}()
client, server := testClientServer(b)

sendBuf := make([]byte, sendSize)
recvBuf := make([]byte, recvSize)
doneCh := make(chan struct{})
errCh := make(chan error, 1)

b.SetBytes(int64(sendSize))
b.ReportAllocs()
b.ResetTimer()

go func() {
defer close(doneCh)

stream, err := server.AcceptStream()
if err != nil {
errCh <- err
return
}
defer stream.Close()
Expand All @@ -134,23 +121,27 @@ func benchmarkSendRecv(b *testing.B, sendSize, recvSize int) {
case sendSize == recvSize:
for i := 0; i < b.N; i++ {
if _, err := stream.Read(recvBuf); err != nil {
b.Fatalf("err: %v", err)
errCh <- err
return
}
}

case recvSize > sendSize:
b.Fatalf("bad test case; recvSize was: %d and sendSize was: %d, but recvSize must be <= sendSize!", recvSize, sendSize)
errCh <- fmt.Errorf("bad test case; recvSize was: %d and sendSize was: %d, but recvSize must be <= sendSize!", recvSize, sendSize)
return

default:
chunks := sendSize / recvSize
for i := 0; i < b.N; i++ {
for j := 0; j < chunks; j++ {
if _, err := stream.Read(recvBuf); err != nil {
b.Fatalf("err: %v", err)
errCh <- err
return
}
}
}
}
errCh <- nil
}()

stream, err := client.Open()
Expand All @@ -164,7 +155,8 @@ func benchmarkSendRecv(b *testing.B, sendSize, recvSize int) {
b.Fatalf("err: %v", err)
}
}
<-doneCh

drainErrorsUntil(b, errCh, 1, 0, "")
}

func BenchmarkSendRecvParallel32(b *testing.B) {
Expand Down Expand Up @@ -208,33 +200,29 @@ func BenchmarkSendRecvParallel4096(b *testing.B) {
}

func benchmarkSendRecvParallel(b *testing.B, sendSize int) {
client, server := testClientServer()
defer func() {
client.Close()
server.Close()
}()
client, server := testClientServer(b)

sendBuf := make([]byte, sendSize)
discarder := ioutil.Discard.(io.ReaderFrom)
discarder := io.Discard.(io.ReaderFrom)
b.SetBytes(int64(sendSize))
b.ReportAllocs()
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
doneCh := make(chan struct{})

errCh := make(chan error, 1)
go func() {
defer close(doneCh)

stream, err := server.AcceptStream()
if err != nil {
errCh <- err
return
}
defer stream.Close()

if _, err := discarder.ReadFrom(stream); err != nil {
b.Fatalf("err: %v", err)
errCh <- err
return
}
errCh <- nil
}()

stream, err := client.Open()
Expand All @@ -249,6 +237,7 @@ func benchmarkSendRecvParallel(b *testing.B, sendSize int) {
}

stream.Close()
<-doneCh

drainErrorsUntil(b, errCh, 1, 0, "")
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/hashicorp/yamux

go 1.15
go 1.20
Empty file added go.sum
Empty file.
5 changes: 5 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type Config struct {
Logger Logger
}

func (c *Config) Clone() *Config {
c2 := *c
return &c2
}

// DefaultConfig is used to return a default configuration
func DefaultConfig() *Config {
return &Config{
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (s *Session) Ping() (time.Duration, error) {
}

// Compute the RTT
return time.Now().Sub(start), nil
return time.Since(start), nil
}

// keepalive is a long running goroutine that periodically does
Expand Down
Loading

0 comments on commit 8bd691f

Please sign in to comment.