Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix golangci-lint 错误 #27

Merged
merged 4 commits into from
May 23, 2024
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
4 changes: 3 additions & 1 deletion autobahn/client/autobahn-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func runTest(caseNo int) {
return
}

go c.ReadLoop()
go func() {
_ = c.ReadLoop()
}()
<-done
}

Expand Down
10 changes: 5 additions & 5 deletions autobahn/server/autobahn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func echoNoContextDecompression(w http.ResponseWriter, r *http.Request) {
return
}

c.ReadLoop()
_ = c.ReadLoop()
}

// 2.测试不接管上下文,压缩和解压
Expand All @@ -81,7 +81,7 @@ func echoNoContextDecompressionAndCompression(w http.ResponseWriter, r *http.Req
return
}

c.ReadLoop()
_ = c.ReadLoop()
}

// 3.测试接管上下文,解压
Expand All @@ -99,7 +99,7 @@ func echoContextTakeoverDecompression(w http.ResponseWriter, r *http.Request) {
return
}

c.ReadLoop()
_ = c.ReadLoop()
}

// 4.测试接管上下文,压缩/解压缩
Expand All @@ -117,7 +117,7 @@ func echoContextTakeoverDecompressionAndCompression(w http.ResponseWriter, r *ht
return
}

c.ReadLoop()
_ = c.ReadLoop()
}
func echoReadTime(w http.ResponseWriter, r *http.Request) {
c, err := quickws.Upgrade(w, r,
Expand All @@ -133,7 +133,7 @@ func echoReadTime(w http.ResponseWriter, r *http.Request) {
return
}

c.ReadLoop()
_ = c.ReadLoop()
}

func startTLSServer(mux *http.ServeMux) {
Expand Down
6 changes: 3 additions & 3 deletions benchmark_read_write_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Benchmark_WriteMessage(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(opcode.Binary, buf)
_ = c.WriteMessage(opcode.Binary, buf)
buf2.Reset()
}
})
Expand Down Expand Up @@ -141,8 +141,8 @@ func Benchmark_ReadMessage(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(opcode.Binary, wbuf)
c.ReadLoop()
_ = c.WriteMessage(opcode.Binary, wbuf)
_ = c.ReadLoop()
buf2.Reset()
}
})
Expand Down
17 changes: 15 additions & 2 deletions callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ func Test_DefaultCallback(t *testing.T) {
}
defer con.Close()

con.WriteMessage(Binary, []byte("hello"))
err = con.WriteMessage(Binary, []byte("hello"))

if err != nil {
t.Error(err)
return
}
select {
case <-done:
case <-time.After(1000 * time.Millisecond):
Expand All @@ -86,6 +91,10 @@ func Test_DefaultCallback(t *testing.T) {
}
c.StartReadLoop()
err = c.WriteMessage(Binary, []byte("hello"))
if err != nil {
t.Error(err)
return
}
atomic.AddInt32(&run, int32(1))
done <- true
}))
Expand All @@ -99,7 +108,11 @@ func Test_DefaultCallback(t *testing.T) {
}
defer con.Close()

con.WriteMessage(Binary, []byte("hello"))
err = con.WriteMessage(Binary, []byte("hello"))
if err != nil {
t.Error(err)
return
}
select {
case <-done:
case <-time.After(100 * time.Millisecond):
Expand Down
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ func (d *DialOption) Dial() (wsCon *Conn, err error) {
bufio2.ClearReader(br)
br = nil
}
// fmt.Println(brw.Reader.Buffered())
conn.SetDeadline(time.Time{})
if err := conn.SetDeadline(time.Time{}); err != nil {
return nil, err
}
wsCon = newConn(conn, true /* client is true*/, &d.Config, fr, br)
wsCon.pd = pd
return wsCon, nil
Expand Down
127 changes: 112 additions & 15 deletions client_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -206,7 +207,11 @@ func Test_ClientOption(t *testing.T) {
t.Run("18 Dial: WithClientDialFunc.1", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := Upgrade(w, r, WithServerOnMessageFunc(func(c *Conn, o Opcode, b []byte) {
c.WriteMessage(o, b)
err := c.WriteMessage(o, b)
if err != nil {
t.Error(err)
return
}
c.Close()
}))
if err != nil {
Expand All @@ -228,7 +233,11 @@ func Test_ClientOption(t *testing.T) {
t.Error(err)
}

newConn.SetDeadline(time.Now().Add(30 * time.Second))
err = newConn.SetDeadline(time.Now().Add(30 * time.Second))
if err != nil {
t.Error(err)
return
}

buf := make([]byte, 128)
if _, err := io.ReadFull(newConn, buf[:3]); err != nil {
Expand Down Expand Up @@ -274,10 +283,19 @@ func Test_ClientOption(t *testing.T) {
defer c2.Close()
done := make(chan struct{})
go func() {
io.Copy(newConn, c2)
_, err := io.Copy(newConn, c2)
if err != nil {
t.Error(err)
return
}

close(done)
}()
io.Copy(c2, newConn)
_, err = io.Copy(c2, newConn)
if err != nil {
t.Error(err)
return
}
<-done
}()

Expand All @@ -294,8 +312,12 @@ func Test_ClientOption(t *testing.T) {
}

data := []byte("hello world")
c.WriteMessage(Binary, data)
c.ReadLoop()
err = c.WriteMessage(Binary, data)
if err != nil {
t.Error(err)
return
}
_ = c.ReadLoop()

t.Log("got", string(got), "want", string(data))
if !bytes.Equal(got, data) {
Expand All @@ -306,7 +328,11 @@ func Test_ClientOption(t *testing.T) {
t.Run("18 Dial: WithClientDialFunc.2", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := Upgrade(w, r, WithServerOnMessageFunc(func(c *Conn, o Opcode, b []byte) {
c.WriteMessage(o, b)
err := c.WriteMessage(o, b)
if err != nil {
t.Error(err)
return
}
c.Close()
}))
if err != nil {
Expand All @@ -328,7 +354,11 @@ func Test_ClientOption(t *testing.T) {
t.Error(err)
}

newConn.SetDeadline(time.Now().Add(30 * time.Second))
err = newConn.SetDeadline(time.Now().Add(30 * time.Second))
if err != nil {
t.Error(err)
return
}

buf := make([]byte, 128)
if _, err := io.ReadFull(newConn, buf[:3]); err != nil {
Expand Down Expand Up @@ -372,13 +402,76 @@ func Test_ClientOption(t *testing.T) {
return
}
defer c2.Close()
done := make(chan struct{})

// done := make(chan struct{})
// newConn = &safeConn{Conn: newConn}
// c2 = &safeConn{Conn: c2}
// go func() {
// _, err = io.Copy(newConn, c2)
// if err != nil {
// t.Error(err)
// return
// }
// close(done)
// }()
// _, err = io.Copy(c2, newConn)
// if err != nil {
// t.Error(err)
// return
// }
// <-done

var (
newConnMu sync.Mutex
c2Mu sync.Mutex
wg sync.WaitGroup
)

wg.Add(2)

go func() {
io.Copy(newConn, c2)
close(done)
defer wg.Done()
buf := make([]byte, 4096)
for {
n, err := c2.Read(buf)
if err != nil {
if err != io.EOF {
t.Error(err)
}
break
}
newConnMu.Lock()
_, err = newConn.Write(buf[:n])
newConnMu.Unlock()
if err != nil {
t.Error(err)
break
}
}
}()
io.Copy(c2, newConn)
<-done

go func() {
defer wg.Done()
buf := make([]byte, 4096)
for {
n, err := newConn.Read(buf)
if err != nil {
if err != io.EOF {
t.Error(err)
}
break
}
c2Mu.Lock()
_, err = c2.Write(buf[:n])
c2Mu.Unlock()
if err != nil {
t.Error(err)
break
}
}
}()

wg.Wait()
}()

got := make([]byte, 0, 128)
Expand All @@ -394,8 +487,12 @@ func Test_ClientOption(t *testing.T) {
}

data := []byte("hello world")
c.WriteMessage(Binary, data)
c.ReadLoop()
err = c.WriteMessage(Binary, data)
if err != nil {
t.Error(err)
return
}
_ = c.ReadLoop()

t.Log("got", string(got), "want", string(data))
if !bytes.Equal(got, data) {
Expand Down
Loading
Loading