Skip to content

Commit 8ba78c2

Browse files
belakprogrium
authored andcommitted
Fix race condition in TestServerClose (#75)
In test server close, 3 things need to happen in order: - Client session start - Server.Close - Client session exit (With io.EOF) This fix ensures the client won't do anything until after the call to close which ensure's we'll get io.EOF rather than a different error.
1 parent ef66069 commit 8ba78c2

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

server_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ func TestServerClose(t *testing.T) {
7373
}
7474
}()
7575

76-
doneCh := make(chan struct{})
76+
clientDoneChan := make(chan struct{})
77+
closeDoneChan := make(chan struct{})
78+
7779
sess, _, cleanup := newClientSession(t, l.Addr().String(), nil)
7880
go func() {
7981
defer cleanup()
80-
defer close(doneCh)
82+
defer close(clientDoneChan)
83+
<-closeDoneChan
8184
if err := sess.Run(""); err != nil && err != io.EOF {
8285
t.Fatal(err)
8386
}
@@ -88,6 +91,7 @@ func TestServerClose(t *testing.T) {
8891
if err != nil {
8992
t.Fatal(err)
9093
}
94+
close(closeDoneChan)
9195
}()
9296

9397
timeout := time.After(100 * time.Millisecond)
@@ -96,7 +100,7 @@ func TestServerClose(t *testing.T) {
96100
t.Error("timeout")
97101
return
98102
case <-s.getDoneChan():
99-
<-doneCh
103+
<-clientDoneChan
100104
return
101105
}
102106
}

0 commit comments

Comments
 (0)