| 
 | 1 | +// Copyright 2016 The Go Authors. All rights reserved.  | 
 | 2 | +// Use of this source code is governed by a BSD-style  | 
 | 3 | +// license that can be found in the LICENSE file.  | 
 | 4 | + | 
 | 5 | +package testing  | 
 | 6 | + | 
 | 7 | +func TestTestContext(t *T) {  | 
 | 8 | +	const (  | 
 | 9 | +		add1 = 0  | 
 | 10 | +		done = 1  | 
 | 11 | +	)  | 
 | 12 | +	// After each of the calls are applied to the context, the  | 
 | 13 | +	type call struct {  | 
 | 14 | +		typ int // run or done  | 
 | 15 | +		// result from applying the call  | 
 | 16 | +		running int  | 
 | 17 | +		waiting int  | 
 | 18 | +		started bool  | 
 | 19 | +	}  | 
 | 20 | +	testCases := []struct {  | 
 | 21 | +		max int  | 
 | 22 | +		run []call  | 
 | 23 | +	}{{  | 
 | 24 | +		max: 1,  | 
 | 25 | +		run: []call{  | 
 | 26 | +			{typ: add1, running: 1, waiting: 0, started: true},  | 
 | 27 | +			{typ: done, running: 0, waiting: 0, started: false},  | 
 | 28 | +		},  | 
 | 29 | +	}, {  | 
 | 30 | +		max: 1,  | 
 | 31 | +		run: []call{  | 
 | 32 | +			{typ: add1, running: 1, waiting: 0, started: true},  | 
 | 33 | +			{typ: add1, running: 1, waiting: 1, started: false},  | 
 | 34 | +			{typ: done, running: 1, waiting: 0, started: true},  | 
 | 35 | +			{typ: done, running: 0, waiting: 0, started: false},  | 
 | 36 | +			{typ: add1, running: 1, waiting: 0, started: true},  | 
 | 37 | +		},  | 
 | 38 | +	}, {  | 
 | 39 | +		max: 3,  | 
 | 40 | +		run: []call{  | 
 | 41 | +			{typ: add1, running: 1, waiting: 0, started: true},  | 
 | 42 | +			{typ: add1, running: 2, waiting: 0, started: true},  | 
 | 43 | +			{typ: add1, running: 3, waiting: 0, started: true},  | 
 | 44 | +			{typ: add1, running: 3, waiting: 1, started: false},  | 
 | 45 | +			{typ: add1, running: 3, waiting: 2, started: false},  | 
 | 46 | +			{typ: add1, running: 3, waiting: 3, started: false},  | 
 | 47 | +			{typ: done, running: 3, waiting: 2, started: true},  | 
 | 48 | +			{typ: add1, running: 3, waiting: 3, started: false},  | 
 | 49 | +			{typ: done, running: 3, waiting: 2, started: true},  | 
 | 50 | +			{typ: done, running: 3, waiting: 1, started: true},  | 
 | 51 | +			{typ: done, running: 3, waiting: 0, started: true},  | 
 | 52 | +			{typ: done, running: 2, waiting: 0, started: false},  | 
 | 53 | +			{typ: done, running: 1, waiting: 0, started: false},  | 
 | 54 | +			{typ: done, running: 0, waiting: 0, started: false},  | 
 | 55 | +		},  | 
 | 56 | +	}}  | 
 | 57 | +	for i, tc := range testCases {  | 
 | 58 | +		ctx := &testContext{  | 
 | 59 | +			startParallel: make(chan bool),  | 
 | 60 | +			maxParallel:   tc.max,  | 
 | 61 | +		}  | 
 | 62 | +		for j, call := range tc.run {  | 
 | 63 | +			doCall := func(f func()) chan bool {  | 
 | 64 | +				done := make(chan bool)  | 
 | 65 | +				go func() {  | 
 | 66 | +					f()  | 
 | 67 | +					done <- true  | 
 | 68 | +				}()  | 
 | 69 | +				return done  | 
 | 70 | +			}  | 
 | 71 | +			started := false  | 
 | 72 | +			switch call.typ {  | 
 | 73 | +			case add1:  | 
 | 74 | +				signal := doCall(ctx.waitParallel)  | 
 | 75 | +				select {  | 
 | 76 | +				case <-signal:  | 
 | 77 | +					started = true  | 
 | 78 | +				case ctx.startParallel <- true:  | 
 | 79 | +					<-signal  | 
 | 80 | +				}  | 
 | 81 | +			case done:  | 
 | 82 | +				signal := doCall(ctx.release)  | 
 | 83 | +				select {  | 
 | 84 | +				case <-signal:  | 
 | 85 | +				case <-ctx.startParallel:  | 
 | 86 | +					started = true  | 
 | 87 | +					<-signal  | 
 | 88 | +				}  | 
 | 89 | +			}  | 
 | 90 | +			if started != call.started {  | 
 | 91 | +				t.Errorf("%d:%d:started: got %v; want %v", i, j, started, call.started)  | 
 | 92 | +			}  | 
 | 93 | +			if ctx.running != call.running {  | 
 | 94 | +				t.Errorf("%d:%d:running: got %v; want %v", i, j, ctx.running, call.running)  | 
 | 95 | +			}  | 
 | 96 | +			if ctx.numWaiting != call.waiting {  | 
 | 97 | +				t.Errorf("%d:%d:waiting: got %v; want %v", i, j, ctx.numWaiting, call.waiting)  | 
 | 98 | +			}  | 
 | 99 | +		}  | 
 | 100 | +	}  | 
 | 101 | +}  | 
0 commit comments