Skip to content

Commit

Permalink
Merge pull request #89 from thaJeztah/carry_48_fix_unit_test_race
Browse files Browse the repository at this point in the history
Fix unit test races (carry #48)
  • Loading branch information
dmcgowan authored Jun 21, 2024
2 parents 06348fb + 31503b1 commit 478d001
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions spdy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestSpdyStreams(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if streamErr != nil {
t.Fatalf("Error creating stream: %s", streamErr)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestSpdyStreams(t *testing.T) {
t.Fatalf("Error reseting stream: %s", streamResetErr)
}

authenticated = false
setAuthenticated(false)
badStream, badStreamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if badStreamErr != nil {
t.Fatalf("Error creating stream: %s", badStreamErr)
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestHalfClose(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if streamErr != nil {
t.Fatalf("Error creating stream: %s", streamErr)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestUnexpectedRemoteConnectionClosed(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if streamErr != nil {
t.Fatalf("Error creating stream: %s", streamErr)
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestIdleShutdownRace(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, err := spdyConn.CreateStream(http.Header{}, nil, false)
if err != nil {
t.Fatalf("Error creating stream: %v", err)
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestIdleWithData(t *testing.T) {

spdyConn.SetIdleTimeout(25 * time.Millisecond)

authenticated = true
setAuthenticated(true)
stream, err := spdyConn.CreateStream(http.Header{}, nil, false)
if err != nil {
t.Fatalf("Error creating stream: %v", err)
Expand Down Expand Up @@ -626,7 +626,7 @@ func TestIdleRace(t *testing.T) {

spdyConn.SetIdleTimeout(10 * time.Millisecond)

authenticated = true
setAuthenticated(true)

for i := 0; i < 10; i++ {
_, err := spdyConn.CreateStream(http.Header{}, nil, false)
Expand Down Expand Up @@ -711,7 +711,7 @@ func TestStreamReset(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if streamErr != nil {
t.Fatalf("Error creating stream: %s", streamErr)
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestStreamResetWithDataRemaining(t *testing.T) {
}
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
setAuthenticated(true)
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
if streamErr != nil {
t.Fatalf("Error creating stream: %s", streamErr)
Expand Down Expand Up @@ -1153,9 +1153,18 @@ func TestStreamReadUnblocksAfterCloseThenReset(t *testing.T) {
}
}

var authLock sync.RWMutex
var authenticated bool

func setAuthenticated(b bool) {
authLock.Lock()
defer authLock.Unlock()
authenticated = b
}

func authStreamHandler(stream *Stream) {
authLock.RLock()
defer authLock.RUnlock()
if !authenticated {
stream.Refuse()
return
Expand Down
2 changes: 1 addition & 1 deletion ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"strings"
"testing"

"github.com/moby/spdystream"
"github.com/gorilla/websocket"
"github.com/moby/spdystream"
)

var upgrader = websocket.Upgrader{
Expand Down

0 comments on commit 478d001

Please sign in to comment.