@@ -33,53 +33,53 @@ import (
3333)
3434
3535func 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
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
4343 expectFailuresAtLeast int
44- } {
44+ }{
4545 {
46- name : "enough permit, no blocking" ,
47- maxTestDuration : 200 * time .Millisecond ,
48- capacity : []int {1000 },
49- goroutines : 100 ,
46+ name : "enough permit, no blocking" ,
47+ maxTestDuration : 200 * time .Millisecond ,
48+ capacity : []int {1000 },
49+ goroutines : 100 ,
5050 goroutinesAcquireChan : 100 ,
51- expectFailures : 0 ,
51+ expectFailures : 0 ,
5252 },
5353 {
54- name : "not enough permit, blocking but all acquire" ,
55- maxTestDuration : 1 * time .Second ,
56- capacity : []int {200 },
57- goroutines : 500 ,
54+ name : "not enough permit, blocking but all acquire" ,
55+ maxTestDuration : 1 * time .Second ,
56+ capacity : []int {200 },
57+ goroutines : 500 ,
5858 goroutinesAcquireChan : 500 ,
59- expectFailures : 0 ,
59+ expectFailures : 0 ,
6060 },
6161 {
62- name : "not enough permit for some to acquire, fail some" ,
63- maxTestDuration : 100 * time .Millisecond ,
64- capacity : []int {100 },
65- goroutines : 500 ,
62+ name : "not enough permit for some to acquire, fail some" ,
63+ maxTestDuration : 100 * time .Millisecond ,
64+ capacity : []int {100 },
65+ goroutines : 500 ,
6666 goroutinesAcquireChan : 500 ,
6767 expectFailuresAtLeast : 1 ,
6868 },
6969 {
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 ,
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 ,
7474 goroutinesAcquireChan : 500 ,
75- expectFailures : 0 ,
75+ expectFailures : 0 ,
7676 },
7777 {
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 ,
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 ,
8383 },
8484 }
8585
@@ -91,7 +91,7 @@ func TestPermit_Simulation(t *testing.T) {
9191 go func () { // update quota every 50ms
9292 defer wg .Done ()
9393 for i := 1 ; i < len (tt .capacity ); i ++ {
94- time .Sleep (50 * time .Millisecond )
94+ time .Sleep (50 * time .Millisecond )
9595 permit .SetQuota (tt .capacity [i ])
9696 }
9797 }()
@@ -102,7 +102,7 @@ func TestPermit_Simulation(t *testing.T) {
102102 wg .Add (1 )
103103 go func () {
104104 defer wg .Done ()
105- num := rand .Intn (2 )+ 2
105+ num := rand .Intn (2 ) + 2
106106 // num := 1
107107 if err := permit .Acquire (ctx , num ); err != nil {
108108 failures .Add (1 )
@@ -128,7 +128,7 @@ func TestPermit_Simulation(t *testing.T) {
128128
129129 wg .Wait ()
130130 assert .Equal (t , 0 , permit .Count ())
131- if tt .expectFailuresAtLeast > 0 {
131+ if tt .expectFailuresAtLeast > 0 {
132132 assert .LessOrEqual (t , tt .expectFailuresAtLeast , int (failures .Load ()))
133133 } else {
134134 assert .Equal (t , tt .expectFailures , int (failures .Load ()))
0 commit comments