mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
serverlock: run tests async should be more linear time wise (#17059)
This commit is contained in:
parent
6c7224c74d
commit
3df94c6f27
@ -7,34 +7,29 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestServerLok(t *testing.T) {
|
||||
sl := createTestableServerLock(t)
|
||||
|
||||
Convey("Server lock integration tests", t, func() {
|
||||
counter := 0
|
||||
var err error
|
||||
incCounter := func() { counter++ }
|
||||
atInterval := time.Second * 1
|
||||
ctx := context.Background()
|
||||
counter := 0
|
||||
fn := func() { counter++ }
|
||||
atInterval := time.Second * 1
|
||||
ctx := context.Background()
|
||||
|
||||
//this time `fn` should be executed
|
||||
So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
|
||||
//this time `fn` should be executed
|
||||
assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
||||
|
||||
//this should not execute `fn`
|
||||
So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
|
||||
So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
|
||||
So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
|
||||
So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
|
||||
//this should not execute `fn`
|
||||
assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
||||
assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
||||
|
||||
// wait 5 second.
|
||||
<-time.After(atInterval * 2)
|
||||
// wait 2 second.
|
||||
<-time.After(time.Second * 2)
|
||||
|
||||
// now `fn` should be executed again
|
||||
err = sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter)
|
||||
So(err, ShouldBeNil)
|
||||
So(counter, ShouldEqual, 2)
|
||||
})
|
||||
// now `fn` should be executed again
|
||||
err := sl.LockAndExecute(ctx, "test-operation", atInterval, fn)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, counter, 2)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user