diff --git a/.github/.codecov.yml b/.github/.codecov.yml new file mode 100644 index 0000000..e429f3d --- /dev/null +++ b/.github/.codecov.yml @@ -0,0 +1,6 @@ +coverage: + status: + project: + default: + target: 90% + threshold: 5% \ No newline at end of file diff --git a/job.go b/job.go index 70796e2..a79b0ec 100644 --- a/job.go +++ b/job.go @@ -8,10 +8,9 @@ import ( ) type simpleJob struct { - id string - jobFunc JobFunc - requestData interface{} - resultChan chan *Result + id string + jobFunc SimpleJobFunc + params []interface{} status Status statusLock sync.RWMutex @@ -24,19 +23,18 @@ type Result struct { Error error } -type JobFunc func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) +type SimpleJobFunc func(isCanceled chan ChanSignal, params ...interface{}) func NewJobSimple( - jobFunc JobFunc, requestData interface{}, result chan *Result, + jobFunc SimpleJobFunc, params ...interface{}, ) Job { return &simpleJob{ - id: uuid.NewString(), - jobFunc: jobFunc, - requestData: requestData, - resultChan: result, - status: StatusCreated, - cancelChan: make(chan ChanSignal, 2), - doneChan: make(chan ChanSignal), + id: uuid.NewString(), + jobFunc: jobFunc, + params: params, + status: StatusCreated, + cancelChan: make(chan ChanSignal, 2), + doneChan: make(chan ChanSignal), } } @@ -53,7 +51,7 @@ func (s *simpleJob) Do(ctx context.Context) { defer s.setStatus(StatusStopped) go func() { - s.jobFunc(s.cancelChan, s.requestData, s.resultChan) + s.jobFunc(s.cancelChan, s.params...) close(s.doneChan) }() diff --git a/job_test.go b/job_test.go index dc6f568..f111cbc 100644 --- a/job_test.go +++ b/job_test.go @@ -17,13 +17,17 @@ func TestSimpleJobNormal(t *testing.T) { ctx = context.Background() ) - job := NewJobSimple(func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) { + job := NewJobSimple(func(isCanceled chan ChanSignal, params ...interface{}) { + if len(params) != 1 { + return + } + select { case <-time.After(10 * time.Millisecond): - atomic.AddInt32(requestData.(*int32), 1) + atomic.AddInt32(params[0].(*int32), 1) case <-isCanceled: } - }, &data, nil) + }, &data) assert.NotEmpty(t, job.Id()) assert.Equal(t, StatusCreated, job.Status()) @@ -56,13 +60,17 @@ func TestSimpleJobCancellation(t *testing.T) { ctx = context.Background() ) - job := NewJobSimple(func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) { + job := NewJobSimple(func(isCanceled chan ChanSignal, params ...interface{}) { + if len(params) != 1 { + return + } + select { case <-time.After(20 * time.Millisecond): - atomic.AddInt32(requestData.(*int32), 1) + atomic.AddInt32(params[0].(*int32), 1) case <-isCanceled: } - }, &data, nil) + }, &data) assert.NotEmpty(t, job.Id()) assert.Equal(t, StatusCreated, job.Status()) @@ -110,13 +118,17 @@ func TestSimpleJobDeadline(t *testing.T) { ctx = context.Background() ) - job := NewJobSimple(func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) { + job := NewJobSimple(func(isCanceled chan ChanSignal, params ...interface{}) { + if len(params) != 1 { + return + } + select { case <-time.After(20 * time.Millisecond): - atomic.AddInt32(requestData.(*int32), 1) + atomic.AddInt32(params[0].(*int32), 1) case <-isCanceled: } - }, &data, nil) + }, &data) assert.NotEmpty(t, job.Id()) assert.Equal(t, StatusCreated, job.Status()) diff --git a/worker_pool_test.go b/worker_pool_test.go index ba20343..c74207e 100644 --- a/worker_pool_test.go +++ b/worker_pool_test.go @@ -17,13 +17,22 @@ type JobWrapper struct { } func generateDummyJob(timeout time.Duration, number *int32) Job { - return NewJobSimple(func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) { + return NewJobSimple(func(isCanceled chan ChanSignal, params ...interface{}) { + if len(params) != 1 { + return + } + + num, ok := params[0].(*int32) + if !ok { + return + } + select { case <-time.After(timeout): - atomic.AddInt32(number, 1) + atomic.AddInt32(num, 1) case <-isCanceled: } - }, nil, nil) + }, number) } func TestWorkerPoolValidation(t *testing.T) { @@ -51,11 +60,15 @@ func TestWorkerPoolMainFlow(t *testing.T) { value := 0 - job := NewJobSimple(func(isCanceled chan ChanSignal, requestData interface{}, result chan *Result) { - val, ok := requestData.(*int) + job := NewJobSimple(func(isCanceled chan ChanSignal, params ...interface{}) { + if len(params) != 1 { + return + } + + val, ok := params[0].(*int) assert.True(t, ok) *val++ - }, &value, nil) + }, &value) assert.NotEmpty(t, job.Id())