Skip to content

Commit

Permalink
tests: Fixed flaky test by using user joined callback. (shazow#393)
Browse files Browse the repository at this point in the history
Instead of relying on the go scheduler to do the expected thing >_>

Co-authored-by: Akshay <akshay.shekher@gmail.com>
  • Loading branch information
voldyman and Akshay committed May 2, 2021
1 parent e1e5343 commit c3b589b
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit c3b589b

Please sign in to comment.