tests: Fixed flaky test by using user joined callback. (#393)

Instead of relying on the go scheduler to do the expected thing >_>

Co-authored-by: Akshay <akshay.shekher@gmail.com>
This commit is contained in:
Akshay Shekher 2021-05-02 10:02:39 -07:00 committed by GitHub
parent e1e534344e
commit c3b589b286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,8 +78,6 @@ func TestHostGetPrompt(t *testing.T) {
}
func TestHostNameCollision(t *testing.T) {
t.Skip("Test is flakey on CI. :(")
key, err := sshd.NewRandomSigner(512)
if err != nil {
t.Fatal(err)
@ -93,9 +91,13 @@ func TestHostNameCollision(t *testing.T) {
}
defer s.Close()
host := NewHost(s, nil)
newUsers := make(chan *message.User)
host.OnUserJoined = func(u *message.User) {
newUsers <- u
}
go host.Serve()
ready := make(chan struct{})
g := errgroup.Group{}
// First client
@ -111,8 +113,8 @@ func TestHostNameCollision(t *testing.T) {
t.Errorf("Got %q; expected %q", actual, expected)
}
// Ready for second client
ready <- struct{}{}
// wait for the second client
<-newUsers
scanner.Scan()
actual = scanner.Text()
@ -127,20 +129,16 @@ func TestHostNameCollision(t *testing.T) {
t.Errorf("Got %q; expected %q", actual, expected)
}
// Wrap it up.
close(ready)
return nil
})
})
// Wait for first client
<-ready
// Second client
g.Go(func() error {
// wait for the first client
<-newUsers
return sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error {
scanner := bufio.NewScanner(r)
// Consume the initial buffer
scanner.Scan()
scanner.Scan()