From 46991ae0ec1b7c510a62da79e8063cdce9c5a0f1 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 13 Nov 2020 17:39:19 +0700 Subject: [PATCH] add a randomized test for accepting streams --- streams_map_incoming_generic_test.go | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/streams_map_incoming_generic_test.go b/streams_map_incoming_generic_test.go index 0bf4a3d1..376e1811 100644 --- a/streams_map_incoming_generic_test.go +++ b/streams_map_incoming_generic_test.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "errors" + "math/rand" + "time" "github.com/golang/mock/gomock" "github.com/lucas-clemente/quic-go/internal/protocol" @@ -257,4 +259,44 @@ var _ = Describe("Streams Map (incoming)", func() { Expect(m.DeleteStream(1)).To(Succeed()) }) }) + + Context("randomized tests", func() { + const num = 1000 + + BeforeEach(func() { maxNumStreams = num }) + + It("opens and accepts streams", func() { + rand.Seed(GinkgoRandomSeed()) + ids := make([]protocol.StreamNum, num) + for i := 0; i < num; i++ { + ids[i] = protocol.StreamNum(i + 1) + } + rand.Shuffle(len(ids), func(i, j int) { ids[i], ids[j] = ids[j], ids[i] }) + + const timeout = 5 * time.Second + done := make(chan struct{}, 2) + go func() { + defer GinkgoRecover() + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + for i := 0; i < num; i++ { + _, err := m.AcceptStream(ctx) + Expect(err).ToNot(HaveOccurred()) + } + done <- struct{}{} + }() + + go func() { + defer GinkgoRecover() + for i := 0; i < num; i++ { + _, err := m.GetOrOpenStream(ids[i]) + Expect(err).ToNot(HaveOccurred()) + } + done <- struct{}{} + }() + + Eventually(done, timeout*3/2).Should(Receive()) + Eventually(done, timeout*3/2).Should(Receive()) + }) + }) })