|
| 1 | +package job_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "github.com/cyberhck/jobs" |
| 6 | + "time" |
| 7 | + Assert "github.com/stretchr/testify/assert" |
| 8 | + "errors" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | + "sync" |
| 11 | +) |
| 12 | + |
| 13 | +type LockProviderMock struct { |
| 14 | + mock.Mock |
| 15 | +} |
| 16 | + |
| 17 | +func (m *LockProviderMock) Acquire() error { |
| 18 | + m.Called() |
| 19 | + return nil |
| 20 | +} |
| 21 | + |
| 22 | +func (m *LockProviderMock) Release() error { |
| 23 | + args := m.Called(nil) |
| 24 | + return args.Error(1) |
| 25 | +} |
| 26 | + |
| 27 | +func TestNew(t *testing.T) { |
| 28 | + assert := Assert.New(t) |
| 29 | + |
| 30 | + testJob := job.New() |
| 31 | + |
| 32 | + assert.Equal(testJob.RuntimeProcessingFrequency, 200*time.Millisecond) |
| 33 | + assert.Nil(testJob.ResultProcessor) |
| 34 | + assert.Nil(testJob.RuntimeProcessor) |
| 35 | + assert.Nil(testJob.LockProvider) |
| 36 | +} |
| 37 | + |
| 38 | +type LockProvider struct { |
| 39 | + acquire error |
| 40 | + release error |
| 41 | +} |
| 42 | + |
| 43 | +func (r LockProvider) Acquire() error { return r.acquire } |
| 44 | +func (r LockProvider) Release() error { return r.release } |
| 45 | + |
| 46 | +func getMockedLockProvider(acquire error, release error) job.LockProvider { |
| 47 | + return LockProvider{ |
| 48 | + acquire: acquire, |
| 49 | + release: release, |
| 50 | + } |
| 51 | +} |
| 52 | +func TestWithLockProvider(t *testing.T) { |
| 53 | + testJob := job.New() |
| 54 | + testJob.WithLockProvider(getMockedLockProvider(nil, nil)) |
| 55 | + Assert.NotNil(t, testJob.LockProvider) |
| 56 | +} |
| 57 | + |
| 58 | +func TestWithResultProcessor(t *testing.T) { |
| 59 | + testJob := job.New() |
| 60 | + testJob.WithResultProcessor(func() {}) |
| 61 | + Assert.NotNil(t, testJob.ResultProcessor) |
| 62 | +} |
| 63 | + |
| 64 | +func TestWithRuntimeProcessingFrequency(t *testing.T) { |
| 65 | + testJob := job.New() |
| 66 | + testJob.WithRuntimeProcessingFrequency(1 * time.Second) |
| 67 | + if testJob.RuntimeProcessingFrequency != 1*time.Second { |
| 68 | + t.Error("Should be able to change runtime processing frequency") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestWithRuntimeProcessor(t *testing.T) { |
| 73 | + testJob := job.New() |
| 74 | + testJob.WithRuntimeProcessor(func(tick time.Time, message string, startTime time.Time) error { |
| 75 | + return nil |
| 76 | + }) |
| 77 | + if testJob.RuntimeProcessor == nil { |
| 78 | + t.Error("Should be able to set run time processor") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestRunPanicsIfLockNotAcquired(t *testing.T) { |
| 83 | + assert := Assert.New(t) |
| 84 | + testJob := job.New() |
| 85 | + testJob.WithLockProvider(getMockedLockProvider(errors.New("couldn't acquire lock"), nil)) |
| 86 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) {}) |
| 87 | + assert.Panics(testJob.Run) |
| 88 | +} |
| 89 | + |
| 90 | +func TestRunDoesNotPanicIfLockAcquired(t *testing.T) { |
| 91 | + assert := Assert.New(t) |
| 92 | + testJob := job.New() |
| 93 | + testJob.WithLockProvider(getMockedLockProvider(nil, nil)) |
| 94 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 95 | + doneFunc.Done() |
| 96 | + }) |
| 97 | + assert.NotPanics(testJob.Run) |
| 98 | +} |
| 99 | + |
| 100 | +func TestRunPanicsIfNoWorkerIsDefined(t *testing.T) { |
| 101 | + testJob := job.New() |
| 102 | + mocked := new(LockProviderMock) |
| 103 | + mocked.On("Acquire").Return(nil) |
| 104 | + testJob.WithLockProvider(mocked) |
| 105 | + Assert.Panics(t, testJob.Run) |
| 106 | +} |
| 107 | + |
| 108 | +func TestRunCallsWorker(t *testing.T) { |
| 109 | + testJob := job.New() |
| 110 | + mocked := new(LockProviderMock) |
| 111 | + mocked.On("Acquire").Return(nil) |
| 112 | + testJob.WithLockProvider(mocked) |
| 113 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 114 | + doneFunc.Done() |
| 115 | + }) |
| 116 | + testJob.Run() |
| 117 | + mocked.AssertExpectations(t) |
| 118 | +} |
| 119 | + |
| 120 | +func TestRunWorksWithoutLockProvider(t *testing.T) { |
| 121 | + testJob := job.New() |
| 122 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 123 | + doneFunc.Done() |
| 124 | + }) |
| 125 | + Assert.NotPanics(t, testJob.Run) |
| 126 | +} |
| 127 | + |
| 128 | +func TestDoesNotPanicWhenNoRuntimeProcessorPresent(t *testing.T) { |
| 129 | + testJob := job.New() |
| 130 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 131 | + doneFunc.Done() |
| 132 | + }) |
| 133 | + Assert.NotPanics(t, testJob.Run) |
| 134 | +} |
| 135 | +func TestRunTimeProcessorGetsCalled(t *testing.T) { |
| 136 | + testJob := job.New() |
| 137 | + runtimeProcessorCalled := false |
| 138 | + testJob.WithRuntimeProcessor(func(tick time.Time, message string, startTime time.Time) error { |
| 139 | + runtimeProcessorCalled = true |
| 140 | + return nil |
| 141 | + }) |
| 142 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 143 | + time.Sleep(1 * time.Second) |
| 144 | + doneFunc.Done() |
| 145 | + }) |
| 146 | + testJob.Run() |
| 147 | + Assert.True(t, runtimeProcessorCalled) |
| 148 | +} |
| 149 | +func TestLongRunningProcessorWorksWithoutRuntimeProcessor(t *testing.T) { |
| 150 | + testJob := job.New() |
| 151 | + testJob.SetWorker(func(channel chan string, doneFunc *sync.WaitGroup) { |
| 152 | + time.Sleep(10 * time.Millisecond) |
| 153 | + channel <- "Done..." |
| 154 | + doneFunc.Done() |
| 155 | + }) |
| 156 | + Assert.NotPanics(t, testJob.Run) |
| 157 | +} |
0 commit comments