mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 13:39:19 -06:00
38 lines
872 B
Go
38 lines
872 B
Go
//go:build integration
|
|
// +build integration
|
|
|
|
package serverlock
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestServerLok(t *testing.T) {
|
|
sl := createTestableServerLock(t)
|
|
|
|
counter := 0
|
|
fn := func(context.Context) { counter++ }
|
|
atInterval := time.Hour
|
|
ctx := context.Background()
|
|
|
|
//this time `fn` should be executed
|
|
require.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
|
require.Equal(t, 1, counter)
|
|
|
|
//this should not execute `fn`
|
|
require.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
|
require.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
|
|
require.Equal(t, 1, counter)
|
|
|
|
atInterval = time.Millisecond
|
|
|
|
// now `fn` should be executed again
|
|
err := sl.LockAndExecute(ctx, "test-operation", atInterval, fn)
|
|
require.Nil(t, err)
|
|
require.Equal(t, 2, counter)
|
|
}
|