Add LockExecuteAndRelease method to ServerLockService (#53417)

This commit is contained in:
lean.dev
2022-08-10 10:28:25 -03:00
committed by GitHub
parent 6e4b537ba6
commit 07681a737d
3 changed files with 214 additions and 11 deletions
@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestIntegrationServerLok(t *testing.T) {
func TestIntegrationServerLock_LockAndExecute(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
@@ -35,3 +35,30 @@ func TestIntegrationServerLok(t *testing.T) {
require.Nil(t, err)
require.Equal(t, 2, counter)
}
func TestIntegrationServerLock_LockExecuteAndRelease(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sl := createTestableServerLock(t)
counter := 0
fn := func(context.Context) { counter++ }
atInterval := time.Hour
ctx := context.Background()
//
err := sl.LockExecuteAndRelease(ctx, "test-operation", atInterval, fn)
require.NoError(t, err)
require.Equal(t, 1, counter)
// the function will be executed again, as everytime the lock is released
err = sl.LockExecuteAndRelease(ctx, "test-operation", atInterval, fn)
require.NoError(t, err)
err = sl.LockExecuteAndRelease(ctx, "test-operation", atInterval, fn)
require.NoError(t, err)
err = sl.LockExecuteAndRelease(ctx, "test-operation", atInterval, fn)
require.NoError(t, err)
require.Equal(t, 4, counter)
}