| 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 |
"golang.org/x/sync/semaphore" |
| 26 |
) |
| 27 |
|
| 28 |
func TestLimitedRegion_Success(t *testing.T) { |
| 29 |
limiter := semaphore.NewWeighted(2) |
| 30 |
ctx := context.Background() |
| 31 |
var counter int32 |
| 32 |
|
| 33 |
err := Go(ctx, limiter, func(ctx context.Context, region *LimitedRegion, i int) error { |
| 34 |
// just sleeps a little and increments counter to simulate task |
| 35 |
time.Sleep(10 * time.Millisecond) |
| 36 |
atomic.AddInt32(&counter, 1) |
| 37 |
return nil |
| 38 |
}, 1, 2, 3, 4, 5) |
| 39 |
|
| 40 |
if err != nil { |
| 41 |
t.Fatalf("expected no error, got %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
// after everything finishes, we expect counter to be 5 |
| 45 |
if want := 5; atomic.LoadInt32(&counter) != int32(want) { |
| 46 |
t.Errorf("expected counter == %v, got %v", want, counter) |
| 47 |
} |
| 48 |
|
| 49 |
// when all work is done the semaphore should have all permits available |
| 50 |
if !limiter.TryAcquire(2) { |
| 51 |
t.Error("semaphore permits were not fully released at the end") |
| 52 |
} |
| 53 |
limiter.Release(2) |
| 54 |
} |
| 55 |
|
| 56 |
func TestLimitedRegion_Cancellation(t *testing.T) { |
| 57 |
limiter := semaphore.NewWeighted(2) |
| 58 |
ctx := context.Background() |
| 59 |
var counter int32 |
| 60 |
|
| 61 |
errTest := errors.New("test error") |
| 62 |
err := Go(ctx, limiter, func(ctx context.Context, region *LimitedRegion, i int) error { |
| 63 |
if i < 0 { |
| 64 |
// trigger an error on negative values |
| 65 |
return errTest |
| 66 |
} |
| 67 |
// just sleeps a little and increments counter to simulate task |
| 68 |
time.Sleep(50 * time.Millisecond) |
| 69 |
atomic.AddInt32(&counter, 1) |
| 70 |
return nil |
| 71 |
}, 1, -1, 2, 0, -2) |
| 72 |
|
| 73 |
// we expect the returned error to be errTest. |
| 74 |
if !errors.Is(err, errTest) { |
| 75 |
t.Fatalf("expected error %v; got %v", errTest, err) |
| 76 |
} |
| 77 |
|
| 78 |
// after everything finishes, we expect counter to be smaller than 5. |
| 79 |
if max := 5; atomic.LoadInt32(&counter) >= int32(max) { |
| 80 |
t.Errorf("expected counter < %v, got %v", max, counter) |
| 81 |
} |
| 82 |
|
| 83 |
// when all work is done the semaphore should have all permits available |
| 84 |
if !limiter.TryAcquire(2) { |
| 85 |
t.Error("semaphore permit was not released after error cancellation") |
| 86 |
} |
| 87 |
limiter.Release(2) |
| 88 |
} |