mirror of
https://github.com/soheilhy/cmux.git
synced 2026-08-25 12:54:31 +08:00
Merge pull request #12 from tamird/fix-read
Reduce the number of calls needed to (*MuxConn).Read
This commit is contained in:
43
cmux_test.go
43
cmux_test.go
@@ -3,6 +3,7 @@ package cmux
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -164,6 +165,48 @@ func runTestRPCClient(t *testing.T, addr net.Addr) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
defer leakCheck(t)()
|
||||
errCh := make(chan error)
|
||||
defer func() {
|
||||
select {
|
||||
case err := <-errCh:
|
||||
t.Fatal(err)
|
||||
default:
|
||||
}
|
||||
}()
|
||||
const payload = "hello world\r\n"
|
||||
const mult = 2
|
||||
|
||||
writer, reader := net.Pipe()
|
||||
go func() {
|
||||
if _, err := io.WriteString(writer, strings.Repeat(payload, mult)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
l := newChanListener()
|
||||
defer close(l.connCh)
|
||||
l.connCh <- reader
|
||||
muxl := New(l)
|
||||
// Register a bogus matcher to force reading from the conn.
|
||||
muxl.Match(HTTP2())
|
||||
anyl := muxl.Match(Any())
|
||||
go safeServe(errCh, muxl)
|
||||
muxedConn, err := anyl.Accept()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var b [mult * len(payload)]byte
|
||||
n, err := muxedConn.Read(b[:])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e := len(b); n != e {
|
||||
t.Errorf("expected to read %d bytes, but read %d bytes", e, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAny(t *testing.T) {
|
||||
defer leakCheck(t)()
|
||||
errCh := make(chan error)
|
||||
|
||||
Reference in New Issue
Block a user