This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
captain_test.go
151 lines (127 loc) · 3.99 KB
/
captain_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// external tests for captain package
package captain_test
import (
"errors"
"github.com/fossapps/captain"
Assert "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
type LockProviderMock struct {
mock.Mock
}
func (m *RuntimeProcessor) processor(tick time.Time, message string, startTime time.Time) {
m.Called(tick, message, startTime)
}
type RuntimeProcessor struct {
mock.Mock
}
func (m *LockProviderMock) Acquire() error {
m.Called()
return nil
}
func (m *LockProviderMock) Release() error {
args := m.Called(nil)
return args.Error(1)
}
func TestNew(t *testing.T) {
assert := Assert.New(t)
testJob := captain.CreateJob()
assert.Equal(testJob.RuntimeProcessingFrequency, 200*time.Millisecond)
assert.Nil(testJob.ResultProcessor)
assert.Nil(testJob.RuntimeProcessor)
assert.Nil(testJob.LockProvider)
}
type LockProvider struct {
acquire error
release error
}
func (r LockProvider) Acquire() error { return r.acquire }
func (r LockProvider) Release() error { return r.release }
func getMockedLockProvider(acquire error, release error) captain.LockProvider {
return LockProvider{
acquire: acquire,
release: release,
}
}
func TestWithLockProvider(t *testing.T) {
testJob := captain.CreateJob()
testJob.WithLockProvider(getMockedLockProvider(nil, nil))
Assert.NotNil(t, testJob.LockProvider)
}
func TestWithResultProcessor(t *testing.T) {
testJob := captain.CreateJob()
testJob.WithResultProcessor(func(results []string) {})
Assert.NotNil(t, testJob.ResultProcessor)
}
func TestWithRuntimeProcessingFrequency(t *testing.T) {
testJob := captain.CreateJob()
testJob.WithRuntimeProcessingFrequency(1 * time.Second)
Assert.Equal(t, testJob.RuntimeProcessingFrequency, 1*time.Second)
}
func TestWithRuntimeProcessor(t *testing.T) {
testJob := captain.CreateJob()
testJob.WithRuntimeProcessor(func(tick time.Time, message string, startTime time.Time) {})
Assert.NotNil(t, testJob.RuntimeProcessor)
}
func TestRunPanicsIfLockNotAcquired(t *testing.T) {
assert := Assert.New(t)
testJob := captain.CreateJob()
testJob.WithLockProvider(getMockedLockProvider(errors.New("couldn't acquire lock"), nil))
testJob.SetWorker(func(channels captain.CommChan) {})
assert.Panics(testJob.Run)
}
func TestRunDoesNotPanicIfLockAcquired(t *testing.T) {
assert := Assert.New(t)
testJob := captain.CreateJob()
testJob.WithLockProvider(getMockedLockProvider(nil, nil))
testJob.SetWorker(func(channels captain.CommChan) {})
assert.NotPanics(testJob.Run)
}
func TestRunPanicsIfNoWorkerIsDefined(t *testing.T) {
testJob := captain.CreateJob()
mocked := new(LockProviderMock)
mocked.On("Acquire").Return(nil)
testJob.WithLockProvider(mocked)
Assert.Panics(t, testJob.Run)
}
func TestRunCallsWorker(t *testing.T) {
testJob := captain.CreateJob()
mocked := new(LockProviderMock)
mocked.On("Acquire").Return(nil)
testJob.WithLockProvider(mocked)
testJob.SetWorker(func(channels captain.CommChan) {})
testJob.Run()
mocked.AssertExpectations(t)
}
func TestRunWorksWithoutLockProvider(t *testing.T) {
testJob := captain.CreateJob()
testJob.SetWorker(func(channels captain.CommChan) {})
Assert.NotPanics(t, testJob.Run)
}
func TestDoesNotPanicWhenNoRuntimeProcessorPresent(t *testing.T) {
testJob := captain.CreateJob()
testJob.SetWorker(func(channels captain.CommChan) {})
Assert.NotPanics(t, testJob.Run)
}
func TestLongRunningProcessorWorksWithoutRuntimeProcessor(t *testing.T) {
testJob := captain.CreateJob()
testJob.SetWorker(func(channels captain.CommChan) {
time.Sleep(10 * time.Millisecond)
channels.Logs <- "Done..."
})
Assert.NotPanics(t, testJob.Run)
}
func TestRuntimeProcessor(t *testing.T) {
mocked := new(RuntimeProcessor)
mocked.On("processor", mock.Anything, mock.Anything, mock.Anything).Return(nil)
job := captain.CreateJob()
job.WithRuntimeProcessor(mocked.processor)
job.WithRuntimeProcessingFrequency(20 * time.Millisecond)
job.SetWorker(func(channels captain.CommChan) {
time.Sleep(30 * time.Millisecond)
})
job.Run()
mocked.AssertExpectations(t)
}