]> Cypherpunks repositories - gostls13.git/commitdiff
testing: add test for not exceeding maximum parallism
authorMarcel van Lohuizen <mpvl@golang.org>
Fri, 18 Mar 2016 18:11:19 +0000 (19:11 +0100)
committerMarcel van Lohuizen <mpvl@golang.org>
Mon, 21 Mar 2016 16:52:04 +0000 (16:52 +0000)
Fixed bug that slipped probably slipped in after rebasing and
explain why it failed on nacl/netbsd/plan9, which set default
maxparallelism to 1.

Change-Id: I4d59682fb2843d138b320334189f53fcdda5b2f6
Reviewed-on: https://go-review.googlesource.com/20980
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/testing/sub_test.go
src/testing/testing.go

index 6455540498ca84a3f14b64fcd5aa822aba748d8f..07f6785c13978789571bbbb6b2fef152a6a439be 100644 (file)
@@ -178,6 +178,57 @@ func TestTRun(t *T) {
                                t.Errorf("count was %d; want 4", count)
                        }
                },
+       }, {
+               desc:   "run no more than *parallel tests concurrently",
+               ok:     true,
+               maxPar: 4,
+               f: func(t *T) {
+                       max := 0
+                       in := make(chan int)
+                       out := make(chan int)
+                       ctx := t.context
+                       t.Run("wait", func(t *T) {
+                               t.Run("controller", func(t *T) {
+                                       // Verify sequential tests don't skew counts.
+                                       t.Run("seq1", func(t *T) {})
+                                       t.Run("seq2", func(t *T) {})
+                                       t.Run("seq3", func(t *T) {})
+                                       t.Parallel()
+                                       for i := 0; i < 80; i++ {
+                                               ctx.mu.Lock()
+                                               if ctx.running > max {
+                                                       max = ctx.running
+                                               }
+                                               ctx.mu.Unlock()
+                                               <-in
+                                               // force a minimum to avoid a race, although it works
+                                               // without it.
+                                               if i >= ctx.maxParallel-2 { // max - this - 1
+                                                       out <- i
+                                               }
+                                       }
+                                       close(out)
+                               })
+                               // Ensure we don't exceed the maximum even with nested parallelism.
+                               for i := 0; i < 2; i++ {
+                                       t.Run("", func(t *T) {
+                                               t.Parallel()
+                                               for j := 0; j < 40; j++ {
+                                                       t.Run("", func(t *T) {
+                                                               t.Run("seq1", func(t *T) {})
+                                                               t.Run("seq2", func(t *T) {})
+                                                               t.Parallel()
+                                                               in <- j
+                                                               <-out
+                                                       })
+                                               }
+                                       })
+                               }
+                       })
+                       if max != ctx.maxParallel {
+                               realTest.Errorf("max: got %d; want: %d", max, ctx.maxParallel)
+                       }
+               },
        }, {
                desc: "alternate sequential and parallel",
                // Sequential tests should partake in the counting of running threads.
index 0aa60d9ddc6c5994b3be77c0584440dc42d65d9a..03a7fbfdddaf890bc740d8b94f9868e3d50fc27e 100644 (file)
@@ -602,7 +602,7 @@ type testContext struct {
 func newTestContext(maxParallel int) *testContext {
        return &testContext{
                startParallel: make(chan bool),
-               maxParallel:   *parallel,
+               maxParallel:   maxParallel,
                running:       1, // Set the count to 1 for the main (sequential) test.
        }
 }