|
| 1 | +// Copyright (c) 2017-2021 Uber Technologies Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package worker |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "math/rand" |
| 30 | + |
| 31 | + "github.com/stretchr/testify/assert" |
| 32 | + "go.uber.org/atomic" |
| 33 | +) |
| 34 | + |
| 35 | +func TestPermit_Simulation(t *testing.T) { |
| 36 | + tests := []struct{ |
| 37 | + name string |
| 38 | + capacity []int // update every 50ms |
| 39 | + goroutines int // each would block on acquiring 2-6 tokens for 100ms |
| 40 | + goroutinesAcquireChan int // each would block using AcquireChan for 100ms |
| 41 | + maxTestDuration time.Duration |
| 42 | + expectFailures int |
| 43 | + expectFailuresAtLeast int |
| 44 | + } { |
| 45 | + { |
| 46 | + name: "enough permit, no blocking", |
| 47 | + maxTestDuration: 200*time.Millisecond, |
| 48 | + capacity: []int{1000}, |
| 49 | + goroutines: 100, |
| 50 | + goroutinesAcquireChan: 100, |
| 51 | + expectFailures: 0, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "not enough permit, blocking but all acquire", |
| 55 | + maxTestDuration: 1*time.Second, |
| 56 | + capacity: []int{200}, |
| 57 | + goroutines: 500, |
| 58 | + goroutinesAcquireChan: 500, |
| 59 | + expectFailures: 0, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "not enough permit for some to acquire, fail some", |
| 63 | + maxTestDuration: 100*time.Millisecond, |
| 64 | + capacity: []int{100}, |
| 65 | + goroutines: 500, |
| 66 | + goroutinesAcquireChan: 500, |
| 67 | + expectFailuresAtLeast: 1, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "not enough permit at beginning but due to capacity change, blocking but all acquire", |
| 71 | + maxTestDuration: 100*time.Second, |
| 72 | + capacity: []int{100, 200, 300}, |
| 73 | + goroutines: 500, |
| 74 | + goroutinesAcquireChan: 500, |
| 75 | + expectFailures: 0, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "not enough permit for any acquire, fail all", |
| 79 | + maxTestDuration: 1*time.Second, |
| 80 | + capacity: []int{0}, |
| 81 | + goroutines: 1000, |
| 82 | + expectFailures: 1000, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + wg := &sync.WaitGroup{} |
| 89 | + permit := NewPermit(tt.capacity[0]) |
| 90 | + wg.Add(1) |
| 91 | + go func() { // update quota every 50ms |
| 92 | + defer wg.Done() |
| 93 | + for i := 1; i < len(tt.capacity); i++ { |
| 94 | + time.Sleep(50*time.Millisecond) |
| 95 | + permit.SetQuota(tt.capacity[i]) |
| 96 | + } |
| 97 | + }() |
| 98 | + failures := atomic.NewInt32(0) |
| 99 | + ctx, cancel := context.WithTimeout(context.Background(), tt.maxTestDuration) |
| 100 | + defer cancel() |
| 101 | + for i := 0; i < tt.goroutines; i++ { |
| 102 | + wg.Add(1) |
| 103 | + go func() { |
| 104 | + defer wg.Done() |
| 105 | + num := rand.Intn(2)+2 |
| 106 | + // num := 1 |
| 107 | + if err := permit.Acquire(ctx, num); err != nil { |
| 108 | + failures.Add(1) |
| 109 | + return |
| 110 | + } |
| 111 | + time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 112 | + permit.Release(num) |
| 113 | + }() |
| 114 | + } |
| 115 | + for i := 0; i < tt.goroutinesAcquireChan; i++ { |
| 116 | + wg.Add(1) |
| 117 | + go func() { |
| 118 | + defer wg.Done() |
| 119 | + select { |
| 120 | + case <-permit.AcquireChan(ctx, wg): |
| 121 | + time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 122 | + permit.Release(1) |
| 123 | + case <-ctx.Done(): |
| 124 | + failures.Add(1) |
| 125 | + } |
| 126 | + }() |
| 127 | + } |
| 128 | + |
| 129 | + wg.Wait() |
| 130 | + assert.Equal(t, 0, permit.Count()) |
| 131 | + if tt.expectFailuresAtLeast >0 { |
| 132 | + assert.LessOrEqual(t, tt.expectFailuresAtLeast, int(failures.Load())) |
| 133 | + } else { |
| 134 | + assert.Equal(t, tt.expectFailures, int(failures.Load())) |
| 135 | + } |
| 136 | + assert.Equal(t, tt.capacity[len(tt.capacity)-1], permit.Quota()) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments