Skip to content

Commit

Permalink
Fix unit test races
Browse files Browse the repository at this point in the history
Signed-off-by: Derek McGowan <[email protected]> (github: dmcgowan)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
dmcgowan authored and thaJeztah committed Dec 19, 2022
1 parent dbc7151 commit 73570da
Showing 1 changed file with 18 additions and 9 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

0 comments on commit 73570da

Please sign in to comment.