Skip to content

Commit c3b589b

Browse files
voldymanAkshay
andauthored
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>
1 parent e1e5343 commit c3b589b

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

host_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ func TestHostGetPrompt(t *testing.T) {
7878
}
7979

8080
func TestHostNameCollision(t *testing.T) {
81-
t.Skip("Test is flakey on CI. :(")
82-
8381
key, err := sshd.NewRandomSigner(512)
8482
if err != nil {
8583
t.Fatal(err)
@@ -93,9 +91,13 @@ func TestHostNameCollision(t *testing.T) {
9391
}
9492
defer s.Close()
9593
host := NewHost(s, nil)
94+
95+
newUsers := make(chan *message.User)
96+
host.OnUserJoined = func(u *message.User) {
97+
newUsers <- u
98+
}
9699
go host.Serve()
97100

98-
ready := make(chan struct{})
99101
g := errgroup.Group{}
100102

101103
// First client
@@ -111,8 +113,8 @@ func TestHostNameCollision(t *testing.T) {
111113
t.Errorf("Got %q; expected %q", actual, expected)
112114
}
113115

114-
// Ready for second client
115-
ready <- struct{}{}
116+
// wait for the second client
117+
<-newUsers
116118

117119
scanner.Scan()
118120
actual = scanner.Text()
@@ -127,20 +129,16 @@ func TestHostNameCollision(t *testing.T) {
127129
t.Errorf("Got %q; expected %q", actual, expected)
128130
}
129131

130-
// Wrap it up.
131-
close(ready)
132132
return nil
133133
})
134134
})
135135

136-
// Wait for first client
137-
<-ready
138-
139136
// Second client
140137
g.Go(func() error {
138+
// wait for the first client
139+
<-newUsers
141140
return sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error {
142141
scanner := bufio.NewScanner(r)
143-
144142
// Consume the initial buffer
145143
scanner.Scan()
146144
scanner.Scan()

0 commit comments

Comments
 (0)