forked from ryandotsmith/ddbsync
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mutex_test.go
119 lines (94 loc) · 2.94 KB
/
mutex_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
package ddbsync
import (
"errors"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
const (
ValidMutexName = "mut-test"
ValidMutexTTL = 4 * time.Second
ValidMutexCreated = 1424385592
ValidMutexRetryWait = 1 * time.Millisecond
)
var dynamoInternalErr = awserr.New(
dynamodb.ErrCodeInternalServerError,
"Dynamo Internal Server Error",
errors.New("dynamodb Internal Server Error"),
)
type mockDB struct{ mock.Mock }
func (m *mockDB) OnAcquire() *mock.Call {
return m.On("Acquire", ValidMutexName, ValidMutexTTL)
}
func (m *mockDB) OnDelete() *mock.Call {
return m.On("Delete", ValidMutexName)
}
func (m *mockDB) Acquire(name string, ttl time.Duration) error {
return m.Called(name, ttl).Error(0)
}
func (m *mockDB) Delete(name string) error {
return m.Called(name).Error(0)
}
func newMockedMutex() (*Mutex, *mockDB) {
db := &mockDB{}
mutex := NewMutex(ValidMutexName, ValidMutexTTL, db)
mutex.ReattemptWait = ValidMutexRetryWait
return mutex, db
}
func TestNew(t *testing.T) {
t.Parallel()
underTest, _ := newMockedMutex()
require.Equal(t, ValidMutexName, underTest.Name)
require.Equal(t, ValidMutexTTL, underTest.TTL)
}
func TestLock(t *testing.T) {
t.Parallel()
underTest, db := newMockedMutex()
defer db.AssertExpectations(t)
db.OnAcquire().Return(nil)
require.NoError(t, underTest.Lock())
}
func TestLockWaitsBeforeRetrying(t *testing.T) {
t.Parallel()
underTest, db := newMockedMutex()
defer db.AssertExpectations(t)
underTest.ReattemptWait = 300 * time.Millisecond
db.OnAcquire().Once().Return(ErrLocked)
db.OnAcquire().Once().Return(dynamoInternalErr)
db.OnAcquire().Once().Return(errors.New("dynamodb glitch"))
db.OnAcquire().Once().Return(nil)
before := time.Now()
require.NoError(t, underTest.Lock())
duration := time.Since(before)
require.True(t, duration > 900*time.Millisecond, "Expected to have waited at least 0.3 secs between each retry, total wait time: %s", duration)
}
func TestLockCutoff(t *testing.T) {
t.Parallel()
underTest, db := newMockedMutex()
defer db.AssertExpectations(t)
underTest.ReattemptWait = 300 * time.Millisecond
underTest.Cutoff = 100 * time.Millisecond
db.OnAcquire().Twice().Return(ErrLocked)
before := time.Now()
err := underTest.Lock()
duration := time.Since(before)
require.EqualError(t, err, "reached cutoff time")
require.True(t, duration > 300*time.Millisecond, "Expected to have waited at least 0.3 secs between each retry, total wait time: %s", duration)
}
func TestUnlock(t *testing.T) {
t.Parallel()
underTest, db := newMockedMutex()
defer db.AssertExpectations(t)
db.OnDelete().Return(nil)
underTest.Unlock()
}
func TestUnlockGivesUpAfterThreeAttempts(t *testing.T) {
t.Parallel()
underTest, db := newMockedMutex()
defer db.AssertExpectations(t)
db.OnDelete().Times(3).Return(errors.New("dynamodb is down"))
underTest.Unlock()
}