@@ -17,7 +17,7 @@ func TestSizedGroup(t *testing.T) {
1717 var c uint32
1818
1919 for i := 0 ; i < 1000 ; i ++ {
20- swg .Go (func (ctx context.Context ) {
20+ swg .Go (func (context.Context ) {
2121 time .Sleep (5 * time .Millisecond )
2222 atomic .AddUint32 (& c , 1 )
2323 })
@@ -32,7 +32,7 @@ func TestSizedGroup_Discard(t *testing.T) {
3232 var c uint32
3333
3434 for i := 0 ; i < 100 ; i ++ {
35- swg .Go (func (ctx context.Context ) {
35+ swg .Go (func (context.Context ) {
3636 time .Sleep (5 * time .Millisecond )
3737 atomic .AddUint32 (& c , 1 )
3838 })
@@ -42,21 +42,6 @@ func TestSizedGroup_Discard(t *testing.T) {
4242 assert .Equal (t , uint32 (10 ), c , fmt .Sprintf ("%d, not all routines have been executed" , c ))
4343}
4444
45- func TestSizedGroup_DiscardAfterTreshold (t * testing.T ) {
46- swg := NewSizedGroup (10 , DiscardAfterTreshold (10 ))
47- var c uint32
48-
49- for i := 0 ; i < 100 ; i ++ {
50- swg .Go (func (ctx context.Context ) {
51- time .Sleep (5 * time .Millisecond )
52- atomic .AddUint32 (& c , 1 )
53- })
54- }
55- assert .True (t , runtime .NumGoroutine () < 15 , "goroutines %d" , runtime .NumGoroutine ())
56- swg .Wait ()
57- assert .Equal (t , uint32 (20 ), c , fmt .Sprintf ("%d, wrong number of routines have been executed" , c ))
58- }
59-
6045func TestSizedGroup_Preemptive (t * testing.T ) {
6146 swg := NewSizedGroup (10 , Preemptive )
6247 var c uint32
@@ -79,7 +64,7 @@ func TestSizedGroup_Canceled(t *testing.T) {
7964 var c uint32
8065
8166 for i := 0 ; i < 100 ; i ++ {
82- swg .Go (func (ctx context.Context ) {
67+ swg .Go (func (context.Context ) {
8368 select {
8469 case <- ctx .Done ():
8570 return
@@ -92,14 +77,59 @@ func TestSizedGroup_Canceled(t *testing.T) {
9277 assert .True (t , c < 100 )
9378}
9479
80+ func TestSizedGroup_DiscardAfterTreshold (t * testing.T ) {
81+ swg := NewSizedGroup (10 , DiscardAfterTreshold (20 ))
82+ var c uint32
83+
84+ for i := 0 ; i < 100 ; i ++ {
85+ swg .Go (func (context.Context ) {
86+ time .Sleep (5 * time .Millisecond )
87+ atomic .AddUint32 (& c , 1 )
88+ })
89+ }
90+ assert .True (t , runtime .NumGoroutine () < 15 , "goroutines %d" , runtime .NumGoroutine ())
91+ swg .Wait ()
92+ assert .Equal (t , uint32 (20 ), c , fmt .Sprintf ("%d, wrong number of routines have been executed" , c ))
93+ }
94+
95+ func TestSizedGroup_DiscardAfterTreshold_WithNegativeTreshold (t * testing.T ) {
96+ swg := NewSizedGroup (10 , DiscardAfterTreshold (- 1 ))
97+ var c uint32
98+
99+ for i := 0 ; i < 100 ; i ++ {
100+ swg .Go (func (context.Context ) {
101+ time .Sleep (5 * time .Millisecond )
102+ atomic .AddUint32 (& c , 1 )
103+ })
104+ }
105+ assert .True (t , runtime .NumGoroutine () < 15 , "goroutines %d" , runtime .NumGoroutine ())
106+ swg .Wait ()
107+ assert .Equal (t , uint32 (10 ), c , fmt .Sprintf ("%d, wrong number of routines have been executed" , c ))
108+ }
109+
110+ func TestSizedGroup_DiscardAfterTreshold_WithTresholdNotAboveSize (t * testing.T ) {
111+ swg := NewSizedGroup (10 , DiscardAfterTreshold (10 ))
112+ var c uint32
113+
114+ for i := 0 ; i < 100 ; i ++ {
115+ swg .Go (func (context.Context ) {
116+ time .Sleep (5 * time .Millisecond )
117+ atomic .AddUint32 (& c , 1 )
118+ })
119+ }
120+ assert .True (t , runtime .NumGoroutine () < 15 , "goroutines %d" , runtime .NumGoroutine ())
121+ swg .Wait ()
122+ assert .Equal (t , uint32 (10 ), c , fmt .Sprintf ("%d, wrong number of routines have been executed" , c ))
123+ }
124+
95125// illustrates the use of a SizedGroup for concurrent, limited execution of goroutines.
96126func ExampleSizedGroup_go () {
97127
98128 grp := NewSizedGroup (10 ) // create sized waiting group allowing maximum 10 goroutines
99129
100130 var c uint32
101131 for i := 0 ; i < 1000 ; i ++ {
102- grp .Go (func (ctx context.Context ) { // Go call is non-blocking, like regular go statement
132+ grp .Go (func (context.Context ) { // Go call is non-blocking, like regular go statement
103133 // do some work in 10 goroutines in parallel
104134 atomic .AddUint32 (& c , 1 )
105135 time .Sleep (10 * time .Millisecond )
0 commit comments