| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package syncutil |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"sync/atomic" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
) |
| 25 |
|
| 26 |
func TestLimitedGroup_Success(t *testing.T) { |
| 27 |
ctx := context.Background() |
| 28 |
numTasks := 5 |
| 29 |
|
| 30 |
// Create a limited group with a concurrency limit of 2. |
| 31 |
lg, _ := LimitGroup(ctx, 2) |
| 32 |
var counter int32 |
| 33 |
|
| 34 |
for range numTasks { |
| 35 |
lg.Go(func() error { |
| 36 |
// simulate some work. |
| 37 |
time.Sleep(10 * time.Millisecond) |
| 38 |
atomic.AddInt32(&counter, 1) |
| 39 |
return nil |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
if err := lg.Wait(); err != nil { |
| 44 |
t.Fatalf("unexpected error: %v", err) |
| 45 |
} |
| 46 |
|
| 47 |
if got := atomic.LoadInt32(&counter); got != int32(numTasks) { |
| 48 |
t.Errorf("expected counter %d, got %d", numTasks, got) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestLimitedGroup_Error(t *testing.T) { |
| 53 |
ctx := context.Background() |
| 54 |
lg, _ := LimitGroup(ctx, 2) |
| 55 |
errTest := errors.New("test error") |
| 56 |
var executed int32 |
| 57 |
|
| 58 |
lg.Go(func() error { |
| 59 |
// delay a bit so that other tasks are scheduled. |
| 60 |
time.Sleep(20 * time.Millisecond) |
| 61 |
atomic.AddInt32(&executed, 1) |
| 62 |
return errTest |
| 63 |
}) |
| 64 |
|
| 65 |
// simulates a slower, normal task. |
| 66 |
lg.Go(func() error { |
| 67 |
// wait until cancellation is (hopefully) in effect. |
| 68 |
time.Sleep(50 * time.Millisecond) |
| 69 |
atomic.AddInt32(&executed, 1) |
| 70 |
return nil |
| 71 |
}) |
| 72 |
|
| 73 |
err := lg.Wait() |
| 74 |
if !errors.Is(err, errTest) { |
| 75 |
t.Fatalf("expected error %v, got %v", errTest, err) |
| 76 |
} |
| 77 |
|
| 78 |
if atomic.LoadInt32(&executed) < 1 { |
| 79 |
t.Errorf("expected at least one task executed, got %d", executed) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestLimitedGroup_Limit(t *testing.T) { |
| 84 |
ctx := context.Background() |
| 85 |
limit := 2 |
| 86 |
lg, _ := LimitGroup(ctx, limit) |
| 87 |
var concurrent, maxConcurrent int32 |
| 88 |
numTasks := 10 |
| 89 |
|
| 90 |
for range numTasks { |
| 91 |
lg.Go(func() error { |
| 92 |
// increment the count of concurrently active tasks. |
| 93 |
cur := atomic.AddInt32(&concurrent, 1) |
| 94 |
// update the max concurrent tasks if needed. |
| 95 |
for { |
| 96 |
prevMax := atomic.LoadInt32(&maxConcurrent) |
| 97 |
if cur > prevMax { |
| 98 |
if atomic.CompareAndSwapInt32(&maxConcurrent, prevMax, cur) { |
| 99 |
break |
| 100 |
} |
| 101 |
} else { |
| 102 |
break |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// simulate a short task. |
| 107 |
time.Sleep(20 * time.Millisecond) |
| 108 |
atomic.AddInt32(&concurrent, -1) |
| 109 |
return nil |
| 110 |
}) |
| 111 |
} |
| 112 |
|
| 113 |
if err := lg.Wait(); err != nil { |
| 114 |
t.Fatalf("unexpected error: %v", err) |
| 115 |
} |
| 116 |
|
| 117 |
if maxConcurrent > int32(limit) { |
| 118 |
t.Errorf("expected max concurrent tasks <= %d, got %d", limit, maxConcurrent) |
| 119 |
} |
| 120 |
} |