2019-07-18 12:13:54 +02:00
|
|
|
package jobs
|
2018-10-26 15:48:22 +02:00
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-11-01 22:14:00 +01:00
|
|
|
"runtime"
|
2018-10-26 15:48:22 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-08-10 18:12:22 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
2020-06-15 22:27:40 +02:00
|
|
|
"github.com/rclone/rclone/fstest/testy"
|
2018-10-26 15:48:22 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewJobs(t *testing.T) {
|
|
|
|
jobs := newJobs()
|
|
|
|
assert.Equal(t, 0, len(jobs.jobs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsKickExpire(t *testing.T) {
|
2020-06-15 22:27:40 +02:00
|
|
|
testy.SkipUnreliable(t)
|
2018-10-26 15:48:22 +02:00
|
|
|
jobs := newJobs()
|
2019-08-10 18:12:22 +02:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
jobs.kickExpire()
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 15:48:22 +02:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsExpire(t *testing.T) {
|
2020-06-15 22:27:40 +02:00
|
|
|
testy.SkipUnreliable(t)
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
wait := make(chan struct{})
|
|
|
|
jobs := newJobs()
|
2019-08-10 18:12:22 +02:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2020-12-12 16:35:30 +01:00
|
|
|
job, out, err := jobs.NewJob(ctx, func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 15:48:22 +02:00
|
|
|
defer close(wait)
|
|
|
|
return in, nil
|
2020-12-12 16:35:30 +01:00
|
|
|
}, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(out))
|
2018-10-26 15:48:22 +02:00
|
|
|
<-wait
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
|
|
|
jobs.Expire()
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Lock()
|
2019-05-01 14:06:36 +02:00
|
|
|
job.mu.Lock()
|
2019-08-10 18:12:22 +02:00
|
|
|
job.EndTime = time.Now().Add(-rcflags.Opt.JobExpireDuration - 60*time.Second)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2019-05-01 14:06:36 +02:00
|
|
|
job.mu.Unlock()
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Unlock()
|
2019-09-19 17:53:48 +02:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
assert.Equal(t, 0, len(jobs.jobs))
|
2018-11-01 22:14:00 +01:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
var noopFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 15:48:22 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsIDs(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobs := newJobs()
|
2020-12-12 16:35:30 +01:00
|
|
|
job1, _, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
job2, _, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 15:48:22 +02:00
|
|
|
wantIDs := []int64{job1.ID, job2.ID}
|
|
|
|
gotIDs := jobs.IDs()
|
|
|
|
require.Equal(t, 2, len(gotIDs))
|
|
|
|
if gotIDs[0] != wantIDs[0] {
|
|
|
|
gotIDs[0], gotIDs[1] = gotIDs[1], gotIDs[0]
|
|
|
|
}
|
|
|
|
assert.Equal(t, wantIDs, gotIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsGet(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobs := newJobs()
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, job, jobs.Get(job.ID))
|
|
|
|
assert.Nil(t, jobs.Get(123123123123))
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
var longFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 15:48:22 +02:00
|
|
|
time.Sleep(1 * time.Hour)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
var shortFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctxFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2019-06-17 10:47:29 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 12:44:56 +01:00
|
|
|
var ctxParmFn = func(paramCtx context.Context, returnError bool) func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
return func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
select {
|
|
|
|
case <-paramCtx.Done():
|
|
|
|
if returnError {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
return rc.Params{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:01:02 +01:00
|
|
|
const (
|
|
|
|
sleepTime = 100 * time.Millisecond
|
2019-09-05 14:59:06 +02:00
|
|
|
floatSleepTime = float64(sleepTime) / 1e9 / 2
|
2018-11-06 13:01:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// sleep for some time so job.Duration is non-0
|
|
|
|
func sleepJob() {
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
}
|
|
|
|
|
2018-10-26 15:48:22 +02:00
|
|
|
func TestJobFinish(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobs := newJobs()
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err := jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 13:01:02 +01:00
|
|
|
sleepJob()
|
2018-10-26 15:48:22 +02:00
|
|
|
|
|
|
|
assert.Equal(t, true, job.EndTime.IsZero())
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Equal(t, rc.Params(nil), job.Output)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, 0.0, job.Duration)
|
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, false, job.Success)
|
|
|
|
assert.Equal(t, false, job.Finished)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
wantOut := rc.Params{"a": 1}
|
2018-10-26 15:48:22 +02:00
|
|
|
job.finish(wantOut, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 13:01:02 +01:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 13:01:02 +01:00
|
|
|
sleepJob()
|
2018-10-26 15:48:22 +02:00
|
|
|
job.finish(nil, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 13:01:02 +01:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 13:01:02 +01:00
|
|
|
sleepJob()
|
2018-10-26 15:48:22 +02:00
|
|
|
job.finish(wantOut, errors.New("potato"))
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 13:01:02 +01:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, "potato", job.Error)
|
|
|
|
assert.Equal(t, false, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've tested the functionality of run() already as it is
|
|
|
|
// part of NewJob, now just test the panic catching
|
|
|
|
func TestJobRunPanic(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
wait := make(chan struct{})
|
2019-07-18 12:13:54 +02:00
|
|
|
boom := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-11-06 13:01:02 +01:00
|
|
|
sleepJob()
|
2018-10-26 15:48:22 +02:00
|
|
|
defer close(wait)
|
|
|
|
panic("boom")
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs := newJobs()
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err := jobs.NewJob(ctx, boom, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 15:48:22 +02:00
|
|
|
<-wait
|
2018-11-01 22:14:00 +01:00
|
|
|
runtime.Gosched() // yield to make sure job is updated
|
|
|
|
|
|
|
|
// Wait a short time for the panic to propagate
|
|
|
|
for i := uint(0); i < 10; i++ {
|
|
|
|
job.mu.Lock()
|
|
|
|
e := job.Error
|
|
|
|
job.mu.Unlock()
|
|
|
|
if e != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond << i)
|
|
|
|
}
|
2018-10-26 15:48:22 +02:00
|
|
|
|
2018-11-01 22:14:00 +01:00
|
|
|
job.mu.Lock()
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 13:01:02 +01:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2019-07-30 13:22:06 +02:00
|
|
|
assert.Contains(t, job.Error, "panic received: boom")
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, false, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
2018-11-01 22:14:00 +01:00
|
|
|
job.mu.Unlock()
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsNewJob(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobID = 0
|
|
|
|
jobs := newJobs()
|
2020-12-12 16:35:30 +01:00
|
|
|
job, out, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2020-12-12 16:35:30 +01:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.Equal(t, job, jobs.Get(1))
|
2019-06-17 10:47:29 +02:00
|
|
|
assert.NotEmpty(t, job.Stop)
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartJob(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
job, out, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.NoError(t, err)
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
2020-12-12 16:35:30 +01:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2019-07-18 12:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteJob(t *testing.T) {
|
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
job, out, err := NewJob(context.Background(), shortFn, rc.Params{})
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 16:35:30 +01:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 14:50:18 +02:00
|
|
|
func TestExecuteJobErrorPropagation(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2019-08-21 14:50:18 +02:00
|
|
|
jobID = 0
|
|
|
|
|
|
|
|
testErr := errors.New("test error")
|
|
|
|
errorFn := func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
return nil, testErr
|
|
|
|
}
|
2020-12-12 16:35:30 +01:00
|
|
|
_, _, err := NewJob(ctx, errorFn, rc.Params{})
|
2019-08-21 14:50:18 +02:00
|
|
|
assert.Equal(t, testErr, err)
|
|
|
|
}
|
|
|
|
|
2018-10-26 15:48:22 +02:00
|
|
|
func TestRcJobStatus(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
call := rc.Calls.Get("job/status")
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 12:13:54 +02:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, float64(1), out["id"])
|
|
|
|
assert.Equal(t, "", out["error"])
|
|
|
|
assert.Equal(t, false, out["finished"])
|
|
|
|
assert.Equal(t, false, out["success"])
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 10:34:30 +02:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 10:34:30 +02:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRcJobList(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2018-10-26 15:48:22 +02:00
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
call := rc.Calls.Get("job/list")
|
2018-10-26 15:48:22 +02:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 12:13:54 +02:00
|
|
|
in := rc.Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Equal(t, rc.Params{"jobids": []int64{1}}, out)
|
2018-10-26 15:48:22 +02:00
|
|
|
}
|
2019-06-17 10:47:29 +02:00
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
func TestRcAsyncJobStop(t *testing.T) {
|
2020-12-12 16:35:30 +01:00
|
|
|
ctx := context.Background()
|
2019-06-17 10:47:29 +02:00
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
_, _, err := NewJob(ctx, ctxFn, rc.Params{"_async": true})
|
2019-06-17 10:47:29 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
call := rc.Calls.Get("job/stop")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := rc.Params{"jobid": 1}
|
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
|
|
|
|
in = rc.Params{"jobid": 123123123}
|
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
|
|
|
in = rc.Params{"jobidx": 123123123}
|
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
call = rc.Calls.Get("job/status")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in = rc.Params{"jobid": 1}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, float64(1), out["id"])
|
|
|
|
assert.Equal(t, "context canceled", out["error"])
|
|
|
|
assert.Equal(t, true, out["finished"])
|
|
|
|
assert.Equal(t, false, out["success"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRcSyncJobStop(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
jobID = 0
|
2020-12-12 16:35:30 +01:00
|
|
|
job, out, err := NewJob(ctx, ctxFn, rc.Params{})
|
2019-07-18 12:13:54 +02:00
|
|
|
assert.Error(t, err)
|
2020-12-12 16:35:30 +01:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2019-07-18 12:13:54 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
call := rc.Calls.Get("job/stop")
|
2019-06-17 10:47:29 +02:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 12:13:54 +02:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 10:47:29 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 10:47:29 +02:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 10:47:29 +02:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
cancel()
|
2019-06-17 10:47:29 +02:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
call = rc.Calls.Get("job/status")
|
2019-06-17 10:47:29 +02:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 12:13:54 +02:00
|
|
|
in = rc.Params{"jobid": 1}
|
2019-06-17 10:47:29 +02:00
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, float64(1), out["id"])
|
|
|
|
assert.Equal(t, "context canceled", out["error"])
|
|
|
|
assert.Equal(t, true, out["finished"])
|
|
|
|
assert.Equal(t, false, out["success"])
|
|
|
|
}
|
2020-01-23 12:44:56 +01:00
|
|
|
|
|
|
|
func TestOnFinish(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err := NewJob(ctx, ctxParmFn(ctx, false), rc.Params{"_async": true})
|
2020-01-23 12:44:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 16:35:30 +01:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 12:44:56 +01:00
|
|
|
defer stop()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout waiting for OnFinish to fire")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnFinishAlreadyFinished(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2020-12-12 16:35:30 +01:00
|
|
|
job, _, err := NewJob(ctx, shortFn, rc.Params{})
|
2020-01-23 12:44:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 16:35:30 +01:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 12:44:56 +01:00
|
|
|
defer stop()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout waiting for OnFinish to fire")
|
|
|
|
}
|
|
|
|
}
|