forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverlock_integration_test.go
127 lines (106 loc) · 3.34 KB
/
serverlock_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package serverlock
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestIntegrationServerLock_LockAndExecute(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()
// 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)
}
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)
}
func TestIntegrationServerLock_LockExecuteAndReleaseWithRetries(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sl := createTestableServerLock(t)
retries := 0
expectedRetries := 10
funcRuns := 0
fn := func(context.Context) {
funcRuns++
}
lockTimeConfig := LockTimeConfig{
MaxInterval: time.Hour,
MinWait: 0 * time.Millisecond,
MaxWait: 1 * time.Millisecond,
}
ctx := context.Background()
actionName := "test-operation"
// Acquire lock so that when `LockExecuteAndReleaseWithRetries` runs, it is forced
// to retry
err := sl.acquireForRelease(ctx, actionName, lockTimeConfig.MaxInterval)
require.NoError(t, err)
wgRetries := sync.WaitGroup{}
wgRetries.Add(expectedRetries)
wgRelease := sync.WaitGroup{}
wgRelease.Add(1)
wgCompleted := sync.WaitGroup{}
wgCompleted.Add(1)
onRetryFn := func(int) error {
retries++
wgRetries.Done()
if retries == expectedRetries {
// When we reach `expectedRetries`, wait for the lock to be released
// to guarantee that next try will succeed
wgRelease.Wait()
}
return nil
}
go func() {
err := sl.LockExecuteAndReleaseWithRetries(ctx, actionName, lockTimeConfig, fn, onRetryFn)
require.NoError(t, err)
wgCompleted.Done()
}()
// Wait to release the lock until `LockExecuteAndReleaseWithRetries` has retried `expectedRetries` times.
wgRetries.Wait()
err = sl.releaseLock(ctx, actionName)
require.NoError(t, err)
wgRelease.Done()
// `LockExecuteAndReleaseWithRetries` has run completely.
// Check that it had to retry because the lock was already taken.
wgCompleted.Wait()
require.Equal(t, expectedRetries, retries)
require.Equal(t, 1, funcRuns)
}