fix multiple race conditions in the proxy tests

This commit is contained in:
Marten Seemann 2017-08-23 09:42:13 +07:00
parent 508586ea7e
commit e513cb7ad2

View file

@ -84,8 +84,8 @@ var _ = Describe("QUIC Proxy", func() {
Context("Proxy tests", func() { Context("Proxy tests", func() {
var ( var (
serverConn *net.UDPConn serverConn *net.UDPConn
serverReceivedPackets []packetData serverNumPacketsSent int32
serverNumPacketsSent int serverReceivedPackets chan packetData
clientConn *net.UDPConn clientConn *net.UDPConn
proxy *QuicProxy proxy *QuicProxy
) )
@ -98,9 +98,20 @@ var _ = Describe("QUIC Proxy", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
// getClientDict returns a copy of the clientDict map
getClientDict := func() map[string]*connection {
d := make(map[string]*connection)
proxy.mutex.Lock()
defer proxy.mutex.Unlock()
for k, v := range proxy.clientDict {
d[k] = v
}
return d
}
BeforeEach(func() { BeforeEach(func() {
serverReceivedPackets = serverReceivedPackets[:0] serverReceivedPackets = make(chan packetData, 100)
serverNumPacketsSent = 0 atomic.StoreInt32(&serverNumPacketsSent, 0)
// setup a dump UDP server on port 7331 // setup a dump UDP server on port 7331
// in production this would be a QUIC server // in production this would be a QUIC server
@ -118,10 +129,10 @@ var _ = Describe("QUIC Proxy", func() {
return return
} }
data := buf[0:n] data := buf[0:n]
serverReceivedPackets = append(serverReceivedPackets, packetData(data)) serverReceivedPackets <- packetData(data)
// echo the packet // echo the packet
serverConn.WriteToUDP(data, addr) serverConn.WriteToUDP(data, addr)
serverNumPacketsSent++ atomic.AddInt32(&serverNumPacketsSent, 1)
} }
}() }()
}) })
@ -143,9 +154,9 @@ var _ = Describe("QUIC Proxy", func() {
_, err := clientConn.Write(makePacket(1, []byte("foobar"))) _, err := clientConn.Write(makePacket(1, []byte("foobar")))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Eventually(func() map[string]*connection { return proxy.clientDict }).Should(HaveLen(1)) Eventually(getClientDict).Should(HaveLen(1))
var conn *connection var conn *connection
for _, conn = range proxy.clientDict { for _, conn = range getClientDict() {
Expect(atomic.LoadUint64(&conn.incomingPacketCounter)).To(Equal(uint64(1))) Expect(atomic.LoadUint64(&conn.incomingPacketCounter)).To(Equal(uint64(1)))
} }
@ -153,10 +164,10 @@ var _ = Describe("QUIC Proxy", func() {
_, err = clientConn.Write(makePacket(2, []byte("decafbad"))) _, err = clientConn.Write(makePacket(2, []byte("decafbad")))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(2)) Eventually(serverReceivedPackets).Should(HaveLen(2))
Expect(proxy.clientDict).To(HaveLen(1)) Expect(getClientDict()).To(HaveLen(1))
Expect(string(serverReceivedPackets[0])).To(ContainSubstring("foobar")) Expect(string(<-serverReceivedPackets)).To(ContainSubstring("foobar"))
Expect(string(serverReceivedPackets[1])).To(ContainSubstring("decafbad")) Expect(string(<-serverReceivedPackets)).To(ContainSubstring("decafbad"))
}) })
It("relays packets from the server to the client", func() { It("relays packets from the server to the client", func() {
@ -165,10 +176,10 @@ var _ = Describe("QUIC Proxy", func() {
_, err := clientConn.Write(makePacket(1, []byte("foobar"))) _, err := clientConn.Write(makePacket(1, []byte("foobar")))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Eventually(func() map[string]*connection { return proxy.clientDict }).Should(HaveLen(1)) Eventually(getClientDict).Should(HaveLen(1))
var key string var key string
var conn *connection var conn *connection
for key, conn = range proxy.clientDict { for key, conn = range getClientDict() {
Eventually(func() uint64 { return atomic.LoadUint64(&conn.outgoingPacketCounter) }).Should(Equal(uint64(1))) Eventually(func() uint64 { return atomic.LoadUint64(&conn.outgoingPacketCounter) }).Should(Equal(uint64(1)))
} }
@ -176,10 +187,13 @@ var _ = Describe("QUIC Proxy", func() {
_, err = clientConn.Write(makePacket(2, []byte("decafbad"))) _, err = clientConn.Write(makePacket(2, []byte("decafbad")))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(proxy.clientDict).To(HaveLen(1)) Expect(getClientDict()).To(HaveLen(1))
Eventually(func() uint64 { return proxy.clientDict[key].outgoingPacketCounter }).Should(BeEquivalentTo(2)) Eventually(func() uint64 {
conn := getClientDict()[key]
return atomic.LoadUint64(&conn.outgoingPacketCounter)
}).Should(BeEquivalentTo(2))
var clientReceivedPackets []packetData clientReceivedPackets := make(chan packetData, 2)
// receive the packets echoed by the server on client side // receive the packets echoed by the server on client side
go func() { go func() {
for { for {
@ -190,16 +204,15 @@ var _ = Describe("QUIC Proxy", func() {
return return
} }
data := buf[0:n] data := buf[0:n]
clientReceivedPackets = append(clientReceivedPackets, packetData(data)) clientReceivedPackets <- packetData(data)
} }
}() }()
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(2)) Eventually(serverReceivedPackets).Should(HaveLen(2))
Expect(serverReceivedPackets).To(HaveLen(2)) Expect(atomic.LoadInt32(&serverNumPacketsSent)).To(BeEquivalentTo(2))
Expect(serverNumPacketsSent).To(Equal(2)) Eventually(clientReceivedPackets).Should(HaveLen(2))
Eventually(func() []packetData { return clientReceivedPackets }).Should(HaveLen(2)) Expect(string(<-clientReceivedPackets)).To(ContainSubstring("foobar"))
Expect(string(clientReceivedPackets[0])).To(ContainSubstring("foobar")) Expect(string(<-clientReceivedPackets)).To(ContainSubstring("decafbad"))
Expect(string(clientReceivedPackets[1])).To(ContainSubstring("decafbad"))
}) })
}) })
@ -213,16 +226,16 @@ var _ = Describe("QUIC Proxy", func() {
} }
startProxy(opts) startProxy(opts)
// send 6 packets
for i := 1; i <= 6; i++ { for i := 1; i <= 6; i++ {
_, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i)))) _, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i))))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(3)) Eventually(serverReceivedPackets).Should(HaveLen(3))
Consistently(func() []packetData { return serverReceivedPackets }).Should(HaveLen(3)) Consistently(serverReceivedPackets).Should(HaveLen(3))
}) })
It("drops outgoing packets", func() { It("drops outgoing packets", func() {
const numPackets = 6
opts := Opts{ opts := Opts{
RemoteAddr: serverAddr, RemoteAddr: serverAddr,
DropPacket: func(d Direction, p protocol.PacketNumber) bool { DropPacket: func(d Direction, p protocol.PacketNumber) bool {
@ -231,7 +244,7 @@ var _ = Describe("QUIC Proxy", func() {
} }
startProxy(opts) startProxy(opts)
var clientReceivedPackets []packetData clientReceivedPackets := make(chan packetData, numPackets)
// receive the packets echoed by the server on client side // receive the packets echoed by the server on client side
go func() { go func() {
for { for {
@ -242,18 +255,17 @@ var _ = Describe("QUIC Proxy", func() {
return return
} }
data := buf[0:n] data := buf[0:n]
clientReceivedPackets = append(clientReceivedPackets, packetData(data)) clientReceivedPackets <- packetData(data)
} }
}() }()
// send 6 packets for i := 1; i <= numPackets; i++ {
for i := 1; i <= 6; i++ {
_, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i)))) _, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i))))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
Eventually(func() []packetData { return clientReceivedPackets }).Should(HaveLen(3)) Eventually(clientReceivedPackets).Should(HaveLen(numPackets / 2))
Consistently(func() []packetData { return clientReceivedPackets }).Should(HaveLen(3)) Consistently(clientReceivedPackets).Should(HaveLen(numPackets / 2))
}) })
}) })
@ -288,15 +300,16 @@ var _ = Describe("QUIC Proxy", func() {
_, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i)))) _, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i))))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(1)) Eventually(serverReceivedPackets).Should(HaveLen(1))
expectDelay(start, delay, 1) expectDelay(start, delay, 1)
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(2)) Eventually(serverReceivedPackets).Should(HaveLen(2))
expectDelay(start, delay, 2) expectDelay(start, delay, 2)
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(3)) Eventually(serverReceivedPackets).Should(HaveLen(3))
expectDelay(start, delay, 3) expectDelay(start, delay, 3)
}) })
It("delays outgoing packets", func() { It("delays outgoing packets", func() {
const numPackets = 3
delay := 300 * time.Millisecond delay := 300 * time.Millisecond
opts := Opts{ opts := Opts{
RemoteAddr: serverAddr, RemoteAddr: serverAddr,
@ -312,7 +325,7 @@ var _ = Describe("QUIC Proxy", func() {
} }
startProxy(opts) startProxy(opts)
var clientReceivedPackets []packetData clientReceivedPackets := make(chan packetData, numPackets)
// receive the packets echoed by the server on client side // receive the packets echoed by the server on client side
go func() { go func() {
for { for {
@ -323,24 +336,23 @@ var _ = Describe("QUIC Proxy", func() {
return return
} }
data := buf[0:n] data := buf[0:n]
clientReceivedPackets = append(clientReceivedPackets, packetData(data)) clientReceivedPackets <- packetData(data)
} }
}() }()
// send 3 packets
start := time.Now() start := time.Now()
for i := 1; i <= 3; i++ { for i := 1; i <= numPackets; i++ {
_, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i)))) _, err := clientConn.Write(makePacket(protocol.PacketNumber(i), []byte("foobar"+strconv.Itoa(i))))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
// the packets should have arrived immediately at the server // the packets should have arrived immediately at the server
Eventually(func() []packetData { return serverReceivedPackets }).Should(HaveLen(3)) Eventually(serverReceivedPackets).Should(HaveLen(3))
expectDelay(start, delay, 0) expectDelay(start, delay, 0)
Eventually(func() []packetData { return clientReceivedPackets }).Should(HaveLen(1)) Eventually(clientReceivedPackets).Should(HaveLen(1))
expectDelay(start, delay, 1) expectDelay(start, delay, 1)
Eventually(func() []packetData { return clientReceivedPackets }).Should(HaveLen(2)) Eventually(clientReceivedPackets).Should(HaveLen(2))
expectDelay(start, delay, 2) expectDelay(start, delay, 2)
Eventually(func() []packetData { return clientReceivedPackets }).Should(HaveLen(3)) Eventually(clientReceivedPackets).Should(HaveLen(3))
expectDelay(start, delay, 3) expectDelay(start, delay, 3)
}) })
}) })