From 28314326e74ba813d6edee11513ea398054b6e4d Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 12 Jun 2023 23:29:23 +0800 Subject: [PATCH] chore: more tests (#3340) --- core/syncx/timeoutlimit_test.go | 61 +++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/core/syncx/timeoutlimit_test.go b/core/syncx/timeoutlimit_test.go index 5f6e3fe2..ffed96de 100644 --- a/core/syncx/timeoutlimit_test.go +++ b/core/syncx/timeoutlimit_test.go @@ -9,25 +9,44 @@ import ( ) func TestTimeoutLimit(t *testing.T) { - limit := NewTimeoutLimit(2) - assert.Nil(t, limit.Borrow(time.Millisecond*200)) - assert.Nil(t, limit.Borrow(time.Millisecond*200)) - var wait1, wait2, wait3 sync.WaitGroup - wait1.Add(1) - wait2.Add(1) - wait3.Add(1) - go func() { - wait1.Wait() - wait2.Done() - assert.Nil(t, limit.Return()) - wait3.Done() - }() - wait1.Done() - wait2.Wait() - assert.Nil(t, limit.Borrow(time.Second)) - wait3.Wait() - assert.Equal(t, ErrTimeout, limit.Borrow(time.Millisecond*100)) - assert.Nil(t, limit.Return()) - assert.Nil(t, limit.Return()) - assert.Equal(t, ErrLimitReturn, limit.Return()) + tests := []struct { + name string + interval time.Duration + }{ + { + name: "no wait", + }, + { + name: "wait", + interval: time.Millisecond * 100, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + limit := NewTimeoutLimit(2) + assert.Nil(t, limit.Borrow(time.Millisecond*200)) + assert.Nil(t, limit.Borrow(time.Millisecond*200)) + var wait1, wait2, wait3 sync.WaitGroup + wait1.Add(1) + wait2.Add(1) + wait3.Add(1) + go func() { + wait1.Wait() + wait2.Done() + time.Sleep(test.interval) + assert.Nil(t, limit.Return()) + wait3.Done() + }() + wait1.Done() + wait2.Wait() + assert.Nil(t, limit.Borrow(time.Second)) + wait3.Wait() + assert.Equal(t, ErrTimeout, limit.Borrow(time.Millisecond*100)) + assert.Nil(t, limit.Return()) + assert.Nil(t, limit.Return()) + assert.Equal(t, ErrLimitReturn, limit.Return()) + }) + } }