fix flaky client tests

This commit is contained in:
Marten Seemann 2016-12-05 18:38:09 +07:00
parent 05c870ff6f
commit 1ad3a85f5c
No known key found for this signature in database
GPG key ID: 3603F40B121FCDEA
2 changed files with 29 additions and 22 deletions

View file

@ -42,15 +42,21 @@ var _ = Describe("Client", func() {
Expect(err).To(MatchError(qerr.PacketTooLarge)) Expect(err).To(MatchError(qerr.PacketTooLarge))
}) })
It("closes sessions when Close is called", func() { It("properly closes the client", func(done Done) {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0") startUDPConn()
var stoppedListening bool
go func() {
err := client.Listen()
Expect(err).ToNot(HaveOccurred())
stoppedListening = true
}()
err := client.Close()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
client.conn, err = net.ListenUDP("udp", addr) Eventually(session.closed).Should(BeTrue())
Expect(err).ToNot(HaveOccurred())
err = client.Close()
Expect(err).ToNot(HaveOccurred())
Expect(session.closed).To(BeTrue())
Expect(session.closeReason).To(BeNil()) Expect(session.closeReason).To(BeNil())
Eventually(func() bool { return stoppedListening }).Should(BeTrue())
close(done)
}) })
It("creates new sessions with the right parameters", func() { It("creates new sessions with the right parameters", func() {
@ -62,6 +68,9 @@ var _ = Describe("Client", func() {
Expect(client.session).ToNot(BeNil()) Expect(client.session).ToNot(BeNil())
Expect(client.session.(*Session).connectionID).To(Equal(client.connectionID)) Expect(client.session.(*Session).connectionID).To(Equal(client.connectionID))
Expect(client.session.(*Session).version).To(Equal(client.version)) Expect(client.session.(*Session).version).To(Equal(client.version))
err = client.Close()
Expect(err).ToNot(HaveOccurred())
}) })
Context("handling packets", func() { Context("handling packets", func() {
@ -75,11 +84,12 @@ var _ = Describe("Client", func() {
serverConn, err := net.DialUDP("udp", nil, client.conn.LocalAddr().(*net.UDPAddr)) serverConn, err := net.DialUDP("udp", nil, client.conn.LocalAddr().(*net.UDPAddr))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var stoppedListening bool
go func() { go func() {
defer GinkgoRecover() defer GinkgoRecover()
listenErr := client.Listen() _ = client.Listen()
Expect(listenErr).ToNot(HaveOccurred()) // it should continue listening when receiving valid packets
close(done) stoppedListening = true
}() }()
Expect(session.packetCount).To(BeZero()) Expect(session.packetCount).To(BeZero())
@ -96,9 +106,11 @@ var _ = Describe("Client", func() {
Eventually(func() int { return session.packetCount }).Should(Equal(1)) Eventually(func() int { return session.packetCount }).Should(Equal(1))
Expect(session.closed).To(BeFalse()) Expect(session.closed).To(BeFalse())
Eventually(func() bool { return stoppedListening }).Should(BeFalse())
err = client.Close() err = client.Close()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
close(done)
}) })
It("closes the session when encountering an error while handling a packet", func(done Done) { It("closes the session when encountering an error while handling a packet", func(done Done) {
@ -109,20 +121,15 @@ var _ = Describe("Client", func() {
var listenErr error var listenErr error
go func() { go func() {
defer GinkgoRecover() defer GinkgoRecover()
listenErr = client.Listen() _, err = serverConn.Write(bytes.Repeat([]byte{'f'}, 100))
Expect(listenErr).To(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
close(done)
}() }()
// cause a PacketTooLarge error listenErr = client.Listen()
_, err = serverConn.Write(bytes.Repeat([]byte{'f'}, 100)) Expect(listenErr).To(HaveOccurred())
Expect(err).ToNot(HaveOccurred()) Eventually(session.closed).Should(BeTrue())
Eventually(func() bool { return session.closed }).Should(BeTrue())
Expect(session.closeReason).To(MatchError(listenErr)) Expect(session.closeReason).To(MatchError(listenErr))
close(done)
err = client.Close()
Expect(err).ToNot(HaveOccurred())
}) })
}) })

View file

@ -29,8 +29,8 @@ func (s *mockSession) handlePacket(*receivedPacket) {
func (s *mockSession) run() {} func (s *mockSession) run() {}
func (s *mockSession) Close(e error) error { func (s *mockSession) Close(e error) error {
s.closed = true
s.closeReason = e s.closeReason = e
s.closed = true
return nil return nil
} }