-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_builder_cache_test.go
More file actions
75 lines (60 loc) · 1.91 KB
/
Copy pathjob_builder_cache_test.go
File metadata and controls
75 lines (60 loc) · 1.91 KB
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
package scheduler
import (
"context"
"errors"
"testing"
"time"
"github.com/goforj/cache"
"github.com/stretchr/testify/require"
)
type mockCacheLockClient struct {
tryLockResult bool
tryLockErr error
unlockErr error
unlockCalls int
}
func (m *mockCacheLockClient) TryLock(key string, ttl time.Duration) (bool, error) {
return m.tryLockResult, m.tryLockErr
}
func (m *mockCacheLockClient) WithContext(context.Context) cache.LockAPI {
return m
}
func (m *mockCacheLockClient) Lock(key string, ttl, timeout time.Duration) (bool, error) {
return m.tryLockResult, m.tryLockErr
}
func (m *mockCacheLockClient) Unlock(key string) error {
m.unlockCalls++
return m.unlockErr
}
func TestCacheLockerSuccess(t *testing.T) {
client := &mockCacheLockClient{tryLockResult: true}
locker := NewCacheLocker(client, time.Minute)
lock, err := locker.Lock(context.Background(), "job1")
require.NoError(t, err)
require.NotNil(t, lock)
require.NoError(t, lock.Unlock(context.Background()))
require.Equal(t, 1, client.unlockCalls)
}
func TestCacheLockerNotAcquired(t *testing.T) {
client := &mockCacheLockClient{tryLockResult: false}
locker := NewCacheLocker(client, time.Minute)
_, err := locker.Lock(context.Background(), "job1")
require.ErrorIs(t, err, errLockNotAcquired)
}
func TestCacheLockerTryLockError(t *testing.T) {
client := &mockCacheLockClient{tryLockErr: errors.New("boom")}
locker := NewCacheLocker(client, time.Minute)
_, err := locker.Lock(context.Background(), "job1")
require.EqualError(t, err, "boom")
}
func TestCacheLockerUnlockError(t *testing.T) {
client := &mockCacheLockClient{
tryLockResult: true,
unlockErr: errors.New("unlock failed"),
}
locker := NewCacheLocker(client, time.Minute)
lock, err := locker.Lock(context.Background(), "job1")
require.NoError(t, err)
require.EqualError(t, lock.Unlock(context.Background()), "unlock failed")
require.Equal(t, 1, client.unlockCalls)
}