improve flaky goroutine tests

hopefully fixes #65
This commit is contained in:
Lucas Clemente
2016-05-09 19:21:39 +02:00
parent b42bad8481
commit a219b72968

View File

@@ -256,21 +256,20 @@ var _ = Describe("Session", func() {
) )
BeforeEach(func() { BeforeEach(func() {
time.Sleep(1 * time.Millisecond) // Wait for old goroutines to finish time.Sleep(10 * time.Millisecond) // Wait for old goroutines to finish
nGoRoutinesBefore = runtime.NumGoroutine()
signer, err := crypto.NewRSASigner(testdata.GetTLSConfig()) signer, err := crypto.NewRSASigner(testdata.GetTLSConfig())
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
scfg := handshake.NewServerConfig(crypto.NewCurve25519KEX(), signer) scfg := handshake.NewServerConfig(crypto.NewCurve25519KEX(), signer)
nGoRoutinesBefore = runtime.NumGoroutine()
session = NewSession(conn, 0, 0, scfg, nil, func(protocol.ConnectionID) { closed = true }).(*Session) session = NewSession(conn, 0, 0, scfg, nil, func(protocol.ConnectionID) { closed = true }).(*Session)
go session.Run() go session.Run()
Expect(runtime.NumGoroutine()).To(Equal(nGoRoutinesBefore + 2)) Eventually(func() int { return runtime.NumGoroutine() }).Should(Equal(nGoRoutinesBefore + 2))
}) })
It("shuts down without error", func() { It("shuts down without error", func() {
session.Close(nil, true) session.Close(nil, true)
Expect(closed).To(BeTrue()) Expect(closed).To(BeTrue())
time.Sleep(1 * time.Millisecond) Eventually(func() int { return runtime.NumGoroutine() }).Should(Equal(nGoRoutinesBefore))
Expect(runtime.NumGoroutine()).To(Equal(nGoRoutinesBefore))
}) })
It("closes streams with proper error", func() { It("closes streams with proper error", func() {
@@ -279,8 +278,7 @@ var _ = Describe("Session", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
session.Close(testErr, true) session.Close(testErr, true)
Expect(closed).To(BeTrue()) Expect(closed).To(BeTrue())
time.Sleep(1 * time.Millisecond) Eventually(func() int { return runtime.NumGoroutine() }).Should(Equal(nGoRoutinesBefore))
Expect(runtime.NumGoroutine()).To(Equal(nGoRoutinesBefore))
n, err := s.Read([]byte{0}) n, err := s.Read([]byte{0})
Expect(n).To(BeZero()) Expect(n).To(BeZero())
Expect(err).To(Equal(testErr)) Expect(err).To(Equal(testErr))
@@ -371,8 +369,7 @@ var _ = Describe("Session", func() {
Data: []byte("4242\x00\x00\x00\x00"), Data: []byte("4242\x00\x00\x00\x00"),
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Millisecond) Eventually(func() bool { return session.closed }).Should(BeTrue())
Expect(session.closed).To(BeTrue())
_, err = s.Write([]byte{}) _, err = s.Write([]byte{})
Expect(err).To(MatchError("CryptoSetup: expected CHLO")) Expect(err).To(MatchError("CryptoSetup: expected CHLO"))
}) })