Skip to content

Commit

Permalink
Fix race condition in test
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 26, 2024
1 parent 9011ed8 commit 9e607d1
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,30 @@ func TestServer_Run(t *testing.T) {

server.AddChannel(channel)

// Run the server
done := make(chan struct{})
go func() {
if err := server.Run(); err != http.ErrServerClosed {
t.Errorf("Expected error %v, but got %v", http.ErrServerClosed, err)
}

close(done)
}()

<-time.After(50 * time.Millisecond)
// Run the server
for i := 0; i < 2; i++ {
go func() {
err := server.Run()
switch err {
case http.ErrServerClosed:
close(done)
case ErrServerAlreadyRunning:
done <- struct{}{}
default:
t.Errorf("Got unexpected error: %v", err)
}
}()
}

if err := server.Run(); err != ErrServerAlreadyRunning {
t.Errorf("Expected error %v, but got %v", ErrServerAlreadyRunning, err)
select {
case _, ok := <-done:
if !ok {
t.Error("Expected server to start")
}
case <-time.After(1 * time.Second):
t.Error("Expected server to start")
}

if err := server.handler.Close(); err != nil {
Expand Down

0 comments on commit 9e607d1

Please sign in to comment.