Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Goldstein committed Oct 19, 2015
1 parent 13aaade commit 53c3508
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
8 changes: 6 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ type Connection struct {
shutdownLock sync.Mutex
shutdownChan chan error
hasShutdown bool

// for testing https://github.com/docker/spdystream/pull/56
dataFrameHandler func(*spdy.DataFrame) error
}

// NewConnection creates a new spdy connection from an existing
Expand Down Expand Up @@ -219,6 +222,7 @@ func NewConnection(conn net.Conn, server bool) (*Connection, error) {

shutdownChan: make(chan error),
}
session.dataFrameHandler = session.handleDataFrame
idleAwareFramer.conn = session
go idleAwareFramer.monitor()

Expand Down Expand Up @@ -297,7 +301,7 @@ func (s *Connection) Serve(newHandler StreamHandler) {
if err != io.EOF {
fmt.Errorf("frame read error: %s", err)
} else {
debugMessage("(%q) EOF received", s)
debugMessage("(%p) EOF received", s)
}
break
}
Expand Down Expand Up @@ -377,7 +381,7 @@ func (s *Connection) frameHandler(frameQueue *PriorityFrameQueue, newHandler Str
case *spdy.SynReplyFrame:
frameErr = s.handleReplyFrame(frame)
case *spdy.DataFrame:
frameErr = s.handleDataFrame(frame)
frameErr = s.dataFrameHandler(frame)
case *spdy.RstStreamFrame:
frameErr = s.handleResetFrame(frame)
case *spdy.HeadersFrame:
Expand Down
94 changes: 94 additions & 0 deletions spdy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package spdystream
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

"github.com/docker/spdystream/spdy"
)

func TestSpdyStreams(t *testing.T) {
Expand Down Expand Up @@ -877,6 +881,96 @@ func TestFramingAfterRemoteConnectionClosed(t *testing.T) {
conn.Close()
}

func TestGoAwayRace(t *testing.T) {
var done sync.WaitGroup
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Error listening: %v", err)
}
listen := listener.Addr().String()

processDataFrame := make(chan struct{})
serverClosed := make(chan struct{})

done.Add(1)
go func() {
defer done.Done()
serverConn, err := listener.Accept()
if err != nil {
t.Fatalf("Error accepting: %v", err)
}

serverSpdyConn, err := NewConnection(serverConn, true)
if err != nil {
t.Fatalf("Error creating server connection: %v", err)
}
go func() {
<-serverSpdyConn.CloseChan()
close(serverClosed)
}()

// force the data frame handler to sleep before delivering the frame
serverSpdyConn.dataFrameHandler = func(frame *spdy.DataFrame) error {
<-processDataFrame
return serverSpdyConn.handleDataFrame(frame)
}

streamCh := make(chan *Stream)
go serverSpdyConn.Serve(func(s *Stream) {
s.SendReply(http.Header{}, false)
streamCh <- s
})

stream, ok := <-streamCh
if !ok {
t.Fatalf("didn't get a stream")
}
stream.Close()
data, err := ioutil.ReadAll(stream)
if err != nil {
t.Error(err)
}
if e, a := "hello1hello2hello3hello4hello5", string(data); e != a {
t.Errorf("Expected %q, got %q", e, a)
}
}()

dialConn, err := net.Dial("tcp", listen)
if err != nil {
t.Fatalf("Error dialing server: %s", err)
}
conn, err := NewConnection(dialConn, false)
if err != nil {
t.Fatalf("Error creating client connectin: %v", err)
}
go conn.Serve(NoOpStreamHandler)

stream, err := conn.CreateStream(http.Header{}, nil, false)
if err != nil {
t.Fatalf("error creating client stream: %s", err)
}
if err := stream.Wait(); err != nil {
t.Fatalf("error waiting for stream creation: %v", err)
}

fmt.Fprint(stream, "hello1")
fmt.Fprint(stream, "hello2")
fmt.Fprint(stream, "hello3")
fmt.Fprint(stream, "hello4")
fmt.Fprint(stream, "hello5")

stream.Close()
conn.Close()

// wait for the server to get the go away frame
<-serverClosed

// allow the data frames to be delivered to the server's stream
close(processDataFrame)

done.Wait()
}

var authenticated bool

func authStreamHandler(stream *Stream) {
Expand Down

0 comments on commit 53c3508

Please sign in to comment.