1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480 |
- package yamux
- import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "net"
- "reflect"
- "runtime"
- "strings"
- "sync"
- "testing"
- "time"
- )
- type logCapture struct{ bytes.Buffer }
- func (l *logCapture) logs() []string {
- return strings.Split(strings.TrimSpace(l.String()), "\n")
- }
- func (l *logCapture) match(expect []string) bool {
- return reflect.DeepEqual(l.logs(), expect)
- }
- func captureLogs(s *Session) *logCapture {
- buf := new(logCapture)
- s.logger = log.New(buf, "", 0)
- return buf
- }
- type pipeConn struct {
- reader *io.PipeReader
- writer *io.PipeWriter
- writeBlocker sync.Mutex
- }
- func (p *pipeConn) Read(b []byte) (int, error) {
- return p.reader.Read(b)
- }
- func (p *pipeConn) Write(b []byte) (int, error) {
- p.writeBlocker.Lock()
- defer p.writeBlocker.Unlock()
- return p.writer.Write(b)
- }
- func (p *pipeConn) Close() error {
- p.reader.Close()
- return p.writer.Close()
- }
- func testConn() (io.ReadWriteCloser, io.ReadWriteCloser) {
- read1, write1 := io.Pipe()
- read2, write2 := io.Pipe()
- conn1 := &pipeConn{reader: read1, writer: write2}
- conn2 := &pipeConn{reader: read2, writer: write1}
- return conn1, conn2
- }
- func testConf() *Config {
- conf := DefaultConfig()
- conf.AcceptBacklog = 64
- conf.KeepAliveInterval = 100 * time.Millisecond
- conf.ConnectionWriteTimeout = 250 * time.Millisecond
- return conf
- }
- func testConfNoKeepAlive() *Config {
- conf := testConf()
- conf.EnableKeepAlive = false
- return conf
- }
- func testClientServer() (*Session, *Session) {
- return testClientServerConfig(testConf())
- }
- func testClientServerConfig(conf *Config) (*Session, *Session) {
- conn1, conn2 := testConn()
- client, _ := Client(conn1, conf)
- server, _ := Server(conn2, conf)
- return client, server
- }
- func TestPing(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- rtt, err := client.Ping()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if rtt == 0 {
- t.Fatalf("bad: %v", rtt)
- }
- rtt, err = server.Ping()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if rtt == 0 {
- t.Fatalf("bad: %v", rtt)
- }
- }
- func TestPing_Timeout(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- // Prevent the client from responding
- clientConn := client.conn.(*pipeConn)
- clientConn.writeBlocker.Lock()
- errCh := make(chan error, 1)
- go func() {
- _, err := server.Ping() // Ping via the server session
- errCh <- err
- }()
- select {
- case err := <-errCh:
- if err != ErrTimeout {
- t.Fatalf("err: %v", err)
- }
- case <-time.After(client.config.ConnectionWriteTimeout * 2):
- t.Fatalf("failed to timeout within expected %v", client.config.ConnectionWriteTimeout)
- }
- // Verify that we recover, even if we gave up
- clientConn.writeBlocker.Unlock()
- go func() {
- _, err := server.Ping() // Ping via the server session
- errCh <- err
- }()
- select {
- case err := <-errCh:
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- case <-time.After(client.config.ConnectionWriteTimeout):
- t.Fatalf("timeout")
- }
- }
- func TestCloseBeforeAck(t *testing.T) {
- cfg := testConf()
- cfg.AcceptBacklog = 8
- client, server := testClientServerConfig(cfg)
- defer client.Close()
- defer server.Close()
- for i := 0; i < 8; i++ {
- s, err := client.OpenStream()
- if err != nil {
- t.Fatal(err)
- }
- s.Close()
- }
- for i := 0; i < 8; i++ {
- s, err := server.AcceptStream()
- if err != nil {
- t.Fatal(err)
- }
- s.Close()
- }
- done := make(chan struct{})
- go func() {
- defer close(done)
- s, err := client.OpenStream()
- if err != nil {
- t.Fatal(err)
- }
- s.Close()
- }()
- select {
- case <-done:
- case <-time.After(time.Second * 5):
- t.Fatal("timed out trying to open stream")
- }
- }
- func TestAccept(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- if client.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- if server.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- wg := &sync.WaitGroup{}
- wg.Add(4)
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if id := stream.StreamID(); id != 1 {
- t.Fatalf("bad: %v", id)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- go func() {
- defer wg.Done()
- stream, err := client.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if id := stream.StreamID(); id != 2 {
- t.Fatalf("bad: %v", id)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- go func() {
- defer wg.Done()
- stream, err := server.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if id := stream.StreamID(); id != 2 {
- t.Fatalf("bad: %v", id)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- go func() {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if id := stream.StreamID(); id != 1 {
- t.Fatalf("bad: %v", id)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- doneCh := make(chan struct{})
- go func() {
- wg.Wait()
- close(doneCh)
- }()
- select {
- case <-doneCh:
- case <-time.After(time.Second):
- panic("timeout")
- }
- }
- func TestOpenStreamTimeout(t *testing.T) {
- const timeout = 25 * time.Millisecond
- cfg := testConf()
- cfg.StreamOpenTimeout = timeout
- client, server := testClientServerConfig(cfg)
- defer client.Close()
- defer server.Close()
- clientLogs := captureLogs(client)
- // Open a single stream without a server to acknowledge it.
- s, err := client.OpenStream()
- if err != nil {
- t.Fatal(err)
- }
- // Sleep for longer than the stream open timeout.
- // Since no ACKs are received, the stream and session should be closed.
- time.Sleep(timeout * 5)
- if !clientLogs.match([]string{"[ERR] yamux: aborted stream open (destination=yamux:remote): i/o deadline reached"}) {
- t.Fatalf("server log incorect: %v", clientLogs.logs())
- }
- if s.state != streamClosed {
- t.Fatalf("stream should have been closed")
- }
- if !client.IsClosed() {
- t.Fatalf("session should have been closed")
- }
- }
- func TestClose_closeTimeout(t *testing.T) {
- conf := testConf()
- conf.StreamCloseTimeout = 10 * time.Millisecond
- client, server := testClientServerConfig(conf)
- defer client.Close()
- defer server.Close()
- if client.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- if server.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- wg := &sync.WaitGroup{}
- wg.Add(2)
- // Open a stream on the client but only close it on the server.
- // We want to see if the stream ever gets cleaned up on the client.
- var clientStream *Stream
- go func() {
- defer wg.Done()
- var err error
- clientStream, err = client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- doneCh := make(chan struct{})
- go func() {
- wg.Wait()
- close(doneCh)
- }()
- select {
- case <-doneCh:
- case <-time.After(time.Second):
- panic("timeout")
- }
- // We should have zero streams after our timeout period
- time.Sleep(100 * time.Millisecond)
- if v := server.NumStreams(); v > 0 {
- t.Fatalf("should have zero streams: %d", v)
- }
- if v := client.NumStreams(); v > 0 {
- t.Fatalf("should have zero streams: %d", v)
- }
- if _, err := clientStream.Write([]byte("hello")); err == nil {
- t.Fatal("should error on write")
- } else if err.Error() != "connection reset" {
- t.Fatalf("expected connection reset, got %q", err)
- }
- }
- func TestNonNilInterface(t *testing.T) {
- _, server := testClientServer()
- server.Close()
- conn, err := server.Accept()
- if err != nil && conn != nil {
- t.Error("bad: accept should return a connection of nil value")
- }
- conn, err = server.Open()
- if err != nil && conn != nil {
- t.Error("bad: open should return a connection of nil value")
- }
- }
- func TestSendData_Small(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- wg := &sync.WaitGroup{}
- wg.Add(2)
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if server.NumStreams() != 1 {
- t.Fatalf("bad")
- }
- buf := make([]byte, 4)
- for i := 0; i < 1000; i++ {
- n, err := stream.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("short read: %d", n)
- }
- if string(buf) != "test" {
- t.Fatalf("bad: %s", buf)
- }
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- go func() {
- defer wg.Done()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if client.NumStreams() != 1 {
- t.Fatalf("bad")
- }
- for i := 0; i < 1000; i++ {
- n, err := stream.Write([]byte("test"))
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("short write %d", n)
- }
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- doneCh := make(chan struct{})
- go func() {
- wg.Wait()
- close(doneCh)
- }()
- select {
- case <-doneCh:
- if client.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- if server.NumStreams() != 0 {
- t.Fatalf("bad")
- }
- return
- case <-time.After(time.Second):
- panic("timeout")
- }
- }
- func TestSendData_Large(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- const (
- sendSize = 250 * 1024 * 1024
- recvSize = 4 * 1024
- )
- data := make([]byte, sendSize)
- for idx := range data {
- data[idx] = byte(idx % 256)
- }
- wg := &sync.WaitGroup{}
- wg.Add(2)
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- var sz int
- buf := make([]byte, recvSize)
- for i := 0; i < sendSize/recvSize; i++ {
- n, err := stream.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != recvSize {
- t.Fatalf("short read: %d", n)
- }
- sz += n
- for idx := range buf {
- if buf[idx] != byte(idx%256) {
- t.Fatalf("bad: %v %v %v", i, idx, buf[idx])
- }
- }
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- t.Logf("cap=%d, n=%d\n", stream.recvBuf.Cap(), sz)
- }()
- go func() {
- defer wg.Done()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- n, err := stream.Write(data)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != len(data) {
- t.Fatalf("short write %d", n)
- }
- if err := stream.Close(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- doneCh := make(chan struct{})
- go func() {
- wg.Wait()
- close(doneCh)
- }()
- select {
- case <-doneCh:
- return
- case <-time.After(5 * time.Second):
- panic("timeout")
- }
- }
- func TestGoAway(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- if err := server.GoAway(); err != nil {
- t.Fatalf("err: %v", err)
- }
- _, err := client.Open()
- if err != ErrRemoteGoAway {
- t.Fatalf("err: %v", err)
- }
- }
- func TestManyStreams(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- wg := &sync.WaitGroup{}
- acceptor := func(i int) {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- buf := make([]byte, 512)
- for {
- n, err := stream.Read(buf)
- if err == io.EOF {
- return
- }
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n == 0 {
- t.Fatalf("err: %v", err)
- }
- }
- }
- sender := func(i int) {
- defer wg.Done()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- msg := fmt.Sprintf("%08d", i)
- for i := 0; i < 1000; i++ {
- n, err := stream.Write([]byte(msg))
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != len(msg) {
- t.Fatalf("short write %d", n)
- }
- }
- }
- for i := 0; i < 50; i++ {
- wg.Add(2)
- go acceptor(i)
- go sender(i)
- }
- wg.Wait()
- }
- func TestManyStreams_PingPong(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- wg := &sync.WaitGroup{}
- ping := []byte("ping")
- pong := []byte("pong")
- acceptor := func(i int) {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- buf := make([]byte, 4)
- for {
- // Read the 'ping'
- n, err := stream.Read(buf)
- if err == io.EOF {
- return
- }
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("err: %v", err)
- }
- if !bytes.Equal(buf, ping) {
- t.Fatalf("bad: %s", buf)
- }
- // Shrink the internal buffer!
- stream.Shrink()
- // Write out the 'pong'
- n, err = stream.Write(pong)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("err: %v", err)
- }
- }
- }
- sender := func(i int) {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- buf := make([]byte, 4)
- for i := 0; i < 1000; i++ {
- // Send the 'ping'
- n, err := stream.Write(ping)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("short write %d", n)
- }
- // Read the 'pong'
- n, err = stream.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 4 {
- t.Fatalf("err: %v", err)
- }
- if !bytes.Equal(buf, pong) {
- t.Fatalf("bad: %s", buf)
- }
- // Shrink the buffer
- stream.Shrink()
- }
- }
- for i := 0; i < 50; i++ {
- wg.Add(2)
- go acceptor(i)
- go sender(i)
- }
- wg.Wait()
- }
- func TestHalfClose(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if _, err = stream.Write([]byte("a")); err != nil {
- t.Fatalf("err: %v", err)
- }
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- stream2.Close() // Half close
- buf := make([]byte, 4)
- n, err := stream2.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 1 {
- t.Fatalf("bad: %v", n)
- }
- // Send more
- if _, err = stream.Write([]byte("bcd")); err != nil {
- t.Fatalf("err: %v", err)
- }
- stream.Close()
- // Read after close
- n, err = stream2.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != 3 {
- t.Fatalf("bad: %v", n)
- }
- // EOF after close
- n, err = stream2.Read(buf)
- if err != io.EOF {
- t.Fatalf("err: %v", err)
- }
- if n != 0 {
- t.Fatalf("bad: %v", n)
- }
- }
- func TestReadDeadline(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream2.Close()
- if err := stream.SetReadDeadline(time.Now().Add(5 * time.Millisecond)); err != nil {
- t.Fatalf("err: %v", err)
- }
- buf := make([]byte, 4)
- _, err = stream.Read(buf)
- if err != ErrTimeout {
- t.Fatalf("err: %v", err)
- }
- // See https://github.com/hashicorp/yamux/issues/90
- // The standard library's http server package will read from connections in
- // the background to detect if they are alive.
- //
- // It sets a read deadline on connections and detect if the returned error
- // is a network timeout error which implements net.Error.
- //
- // The HTTP server will cancel all server requests if it isn't timeout error
- // from the connection.
- //
- // We assert that we return an error meeting the interface to avoid
- // accidently breaking yamux session compatability with the standard
- // library's http server implementation.
- if netErr, ok := err.(net.Error); !ok || !netErr.Timeout() {
- t.Fatalf("reading timeout error is expected to implement net.Error and return true when calling Timeout()")
- }
- }
- func TestReadDeadline_BlockedRead(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream2.Close()
- // Start a read that will block
- errCh := make(chan error, 1)
- go func() {
- buf := make([]byte, 4)
- _, err := stream.Read(buf)
- errCh <- err
- close(errCh)
- }()
- // Wait to ensure the read has started.
- time.Sleep(5 * time.Millisecond)
- // Update the read deadline
- if err := stream.SetReadDeadline(time.Now().Add(5 * time.Millisecond)); err != nil {
- t.Fatalf("err: %v", err)
- }
- select {
- case <-time.After(100 * time.Millisecond):
- t.Fatal("expected read timeout")
- case err := <-errCh:
- if err != ErrTimeout {
- t.Fatalf("expected ErrTimeout; got %v", err)
- }
- }
- }
- func TestWriteDeadline(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream2.Close()
- if err := stream.SetWriteDeadline(time.Now().Add(50 * time.Millisecond)); err != nil {
- t.Fatalf("err: %v", err)
- }
- buf := make([]byte, 512)
- for i := 0; i < int(initialStreamWindow); i++ {
- _, err := stream.Write(buf)
- if err != nil && err == ErrTimeout {
- return
- } else if err != nil {
- t.Fatalf("err: %v", err)
- }
- }
- t.Fatalf("Expected timeout")
- }
- func TestWriteDeadline_BlockedWrite(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream2.Close()
- // Start a goroutine making writes that will block
- errCh := make(chan error, 1)
- go func() {
- buf := make([]byte, 512)
- for i := 0; i < int(initialStreamWindow); i++ {
- _, err := stream.Write(buf)
- if err == nil {
- continue
- }
- errCh <- err
- close(errCh)
- return
- }
- close(errCh)
- }()
- // Wait to ensure the write has started.
- time.Sleep(5 * time.Millisecond)
- // Update the write deadline
- if err := stream.SetWriteDeadline(time.Now().Add(5 * time.Millisecond)); err != nil {
- t.Fatalf("err: %v", err)
- }
- select {
- case <-time.After(1 * time.Second):
- t.Fatal("expected write timeout")
- case err := <-errCh:
- if err != ErrTimeout {
- t.Fatalf("expected ErrTimeout; got %v", err)
- }
- }
- }
- func TestBacklogExceeded(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- // Fill the backlog
- max := client.config.AcceptBacklog
- for i := 0; i < max; i++ {
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- if _, err := stream.Write([]byte("foo")); err != nil {
- t.Fatalf("err: %v", err)
- }
- }
- // Attempt to open a new stream
- errCh := make(chan error, 1)
- go func() {
- _, err := client.Open()
- errCh <- err
- }()
- // Shutdown the server
- go func() {
- time.Sleep(10 * time.Millisecond)
- server.Close()
- }()
- select {
- case err := <-errCh:
- if err == nil {
- t.Fatalf("open should fail")
- }
- case <-time.After(time.Second):
- t.Fatalf("timeout")
- }
- }
- func TestKeepAlive(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- time.Sleep(200 * time.Millisecond)
- // Ping value should increase
- client.pingLock.Lock()
- defer client.pingLock.Unlock()
- if client.pingID == 0 {
- t.Fatalf("should ping")
- }
- server.pingLock.Lock()
- defer server.pingLock.Unlock()
- if server.pingID == 0 {
- t.Fatalf("should ping")
- }
- }
- func TestKeepAlive_Timeout(t *testing.T) {
- conn1, conn2 := testConn()
- clientConf := testConf()
- clientConf.ConnectionWriteTimeout = time.Hour // We're testing keep alives, not connection writes
- clientConf.EnableKeepAlive = false // Just test one direction, so it's deterministic who hangs up on whom
- client, _ := Client(conn1, clientConf)
- defer client.Close()
- server, _ := Server(conn2, testConf())
- defer server.Close()
- _ = captureLogs(client) // Client logs aren't part of the test
- serverLogs := captureLogs(server)
- errCh := make(chan error, 1)
- go func() {
- _, err := server.Accept() // Wait until server closes
- errCh <- err
- }()
- // Prevent the client from responding
- clientConn := client.conn.(*pipeConn)
- clientConn.writeBlocker.Lock()
- select {
- case err := <-errCh:
- if err != ErrKeepAliveTimeout {
- t.Fatalf("unexpected error: %v", err)
- }
- case <-time.After(1 * time.Second):
- t.Fatalf("timeout waiting for timeout")
- }
- clientConn.writeBlocker.Unlock()
- if !server.IsClosed() {
- t.Fatalf("server should have closed")
- }
- if !serverLogs.match([]string{"[ERR] yamux: keepalive failed: i/o deadline reached"}) {
- t.Fatalf("server log incorect: %v", serverLogs.logs())
- }
- }
- func TestLargeWindow(t *testing.T) {
- conf := DefaultConfig()
- conf.MaxStreamWindowSize *= 2
- client, server := testClientServerConfig(conf)
- defer client.Close()
- defer server.Close()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- stream2, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream2.Close()
- stream.SetWriteDeadline(time.Now().Add(10 * time.Millisecond))
- buf := make([]byte, conf.MaxStreamWindowSize)
- n, err := stream.Write(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if n != len(buf) {
- t.Fatalf("short write: %d", n)
- }
- }
- type UnlimitedReader struct{}
- func (u *UnlimitedReader) Read(p []byte) (int, error) {
- runtime.Gosched()
- return len(p), nil
- }
- func TestSendData_VeryLarge(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- var n int64 = 1 * 1024 * 1024 * 1024
- var workers int = 16
- wg := &sync.WaitGroup{}
- wg.Add(workers * 2)
- for i := 0; i < workers; i++ {
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- buf := make([]byte, 4)
- _, err = stream.Read(buf)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if !bytes.Equal(buf, []byte{0, 1, 2, 3}) {
- t.Fatalf("bad header")
- }
- recv, err := io.Copy(ioutil.Discard, stream)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if recv != n {
- t.Fatalf("bad: %v", recv)
- }
- }()
- }
- for i := 0; i < workers; i++ {
- go func() {
- defer wg.Done()
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- _, err = stream.Write([]byte{0, 1, 2, 3})
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- unlimited := &UnlimitedReader{}
- sent, err := io.Copy(stream, io.LimitReader(unlimited, n))
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if sent != n {
- t.Fatalf("bad: %v", sent)
- }
- }()
- }
- doneCh := make(chan struct{})
- go func() {
- wg.Wait()
- close(doneCh)
- }()
- select {
- case <-doneCh:
- case <-time.After(20 * time.Second):
- panic("timeout")
- }
- }
- func TestBacklogExceeded_Accept(t *testing.T) {
- client, server := testClientServer()
- defer client.Close()
- defer server.Close()
- max := 5 * client.config.AcceptBacklog
- go func() {
- for i := 0; i < max; i++ {
- stream, err := server.Accept()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- }
- }()
- // Fill the backlog
- for i := 0; i < max; i++ {
- stream, err := client.Open()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- if _, err := stream.Write([]byte("foo")); err != nil {
- t.Fatalf("err: %v", err)
- }
- }
- }
- func TestSession_WindowUpdateWriteDuringRead(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- var wg sync.WaitGroup
- wg.Add(2)
- // Choose a huge flood size that we know will result in a window update.
- flood := int64(client.config.MaxStreamWindowSize) - 1
- // The server will accept a new stream and then flood data to it.
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- n, err := stream.Write(make([]byte, flood))
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if int64(n) != flood {
- t.Fatalf("short write: %d", n)
- }
- }()
- // The client will open a stream, block outbound writes, and then
- // listen to the flood from the server, which should time out since
- // it won't be able to send the window update.
- go func() {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- conn := client.conn.(*pipeConn)
- conn.writeBlocker.Lock()
- defer conn.writeBlocker.Unlock()
- _, err = stream.Read(make([]byte, flood))
- if err != ErrConnectionWriteTimeout {
- t.Fatalf("err: %v", err)
- }
- }()
- wg.Wait()
- }
- func TestSession_PartialReadWindowUpdate(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- var wg sync.WaitGroup
- wg.Add(1)
- // Choose a huge flood size that we know will result in a window update.
- flood := int64(client.config.MaxStreamWindowSize)
- var wr *Stream
- // The server will accept a new stream and then flood data to it.
- go func() {
- defer wg.Done()
- var err error
- wr, err = server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer wr.Close()
- if wr.sendWindow != client.config.MaxStreamWindowSize {
- t.Fatalf("sendWindow: exp=%d, got=%d", client.config.MaxStreamWindowSize, wr.sendWindow)
- }
- n, err := wr.Write(make([]byte, flood))
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if int64(n) != flood {
- t.Fatalf("short write: %d", n)
- }
- if wr.sendWindow != 0 {
- t.Fatalf("sendWindow: exp=%d, got=%d", 0, wr.sendWindow)
- }
- }()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- wg.Wait()
- _, err = stream.Read(make([]byte, flood/2+1))
- if exp := uint32(flood/2 + 1); wr.sendWindow != exp {
- t.Errorf("sendWindow: exp=%d, got=%d", exp, wr.sendWindow)
- }
- }
- func TestSession_sendNoWait_Timeout(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- var wg sync.WaitGroup
- wg.Add(2)
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- }()
- // The client will open the stream and then block outbound writes, we'll
- // probe sendNoWait once it gets into that state.
- go func() {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- conn := client.conn.(*pipeConn)
- conn.writeBlocker.Lock()
- defer conn.writeBlocker.Unlock()
- hdr := header(make([]byte, headerSize))
- hdr.encode(typePing, flagACK, 0, 0)
- for {
- err = client.sendNoWait(hdr)
- if err == nil {
- continue
- } else if err == ErrConnectionWriteTimeout {
- break
- } else {
- t.Fatalf("err: %v", err)
- }
- }
- }()
- wg.Wait()
- }
- func TestSession_PingOfDeath(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- var wg sync.WaitGroup
- wg.Add(2)
- var doPingOfDeath sync.Mutex
- doPingOfDeath.Lock()
- // This is used later to block outbound writes.
- conn := server.conn.(*pipeConn)
- // The server will accept a stream, block outbound writes, and then
- // flood its send channel so that no more headers can be queued.
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- conn.writeBlocker.Lock()
- for {
- hdr := header(make([]byte, headerSize))
- hdr.encode(typePing, 0, 0, 0)
- err = server.sendNoWait(hdr)
- if err == nil {
- continue
- } else if err == ErrConnectionWriteTimeout {
- break
- } else {
- t.Fatalf("err: %v", err)
- }
- }
- doPingOfDeath.Unlock()
- }()
- // The client will open a stream and then send the server a ping once it
- // can no longer write. This makes sure the server doesn't deadlock reads
- // while trying to reply to the ping with no ability to write.
- go func() {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- // This ping will never unblock because the ping id will never
- // show up in a response.
- doPingOfDeath.Lock()
- go func() { client.Ping() }()
- // Wait for a while to make sure the previous ping times out,
- // then turn writes back on and make sure a ping works again.
- time.Sleep(2 * server.config.ConnectionWriteTimeout)
- conn.writeBlocker.Unlock()
- if _, err = client.Ping(); err != nil {
- t.Fatalf("err: %v", err)
- }
- }()
- wg.Wait()
- }
- func TestSession_ConnectionWriteTimeout(t *testing.T) {
- client, server := testClientServerConfig(testConfNoKeepAlive())
- defer client.Close()
- defer server.Close()
- var wg sync.WaitGroup
- wg.Add(2)
- go func() {
- defer wg.Done()
- stream, err := server.AcceptStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- }()
- // The client will open the stream and then block outbound writes, we'll
- // tee up a write and make sure it eventually times out.
- go func() {
- defer wg.Done()
- stream, err := client.OpenStream()
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- defer stream.Close()
- conn := client.conn.(*pipeConn)
- conn.writeBlocker.Lock()
- defer conn.writeBlocker.Unlock()
- // Since the write goroutine is blocked then this will return a
- // timeout since it can't get feedback about whether the write
- // worked.
- n, err := stream.Write([]byte("hello"))
- if err != ErrConnectionWriteTimeout {
- t.Fatalf("err: %v", err)
- }
- if n != 0 {
- t.Fatalf("lied about writes: %d", n)
- }
- }()
- wg.Wait()
- }
|