2019-07-18 18:13:54 +08:00
|
|
|
package jobs
|
2018-10-26 21:48:22 +08:00
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2018-11-02 05:14:00 +08:00
|
|
|
"runtime"
|
2018-10-26 21:48:22 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-08-11 00:12:22 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
2020-06-16 04:27:40 +08:00
|
|
|
"github.com/rclone/rclone/fstest/testy"
|
2018-10-26 21:48:22 +08: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-16 04:27:40 +08:00
|
|
|
testy.SkipUnreliable(t)
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs := newJobs()
|
2019-08-11 00:12:22 +08:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
jobs.kickExpire()
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 21:48:22 +08:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsExpire(t *testing.T) {
|
2020-06-16 04:27:40 +08:00
|
|
|
testy.SkipUnreliable(t)
|
2018-10-26 21:48:22 +08:00
|
|
|
wait := make(chan struct{})
|
|
|
|
jobs := newJobs()
|
2019-08-11 00:12:22 +08:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2019-07-18 18:13:54 +08:00
|
|
|
job := jobs.NewAsyncJob(func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
defer close(wait)
|
|
|
|
return in, nil
|
2019-07-18 18:13:54 +08:00
|
|
|
}, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
<-wait
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
|
|
|
jobs.Expire()
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Lock()
|
2019-05-01 20:06:36 +08:00
|
|
|
job.mu.Lock()
|
2019-08-11 00:12:22 +08:00
|
|
|
job.EndTime = time.Now().Add(-rcflags.Opt.JobExpireDuration - 60*time.Second)
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2019-05-01 20:06:36 +08:00
|
|
|
job.mu.Unlock()
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Unlock()
|
2019-09-19 23:53:48 +08:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
assert.Equal(t, 0, len(jobs.jobs))
|
2018-11-02 05:14:00 +08:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
var noopFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsIDs(t *testing.T) {
|
|
|
|
jobs := newJobs()
|
2019-07-18 18:13:54 +08:00
|
|
|
job1 := jobs.NewAsyncJob(noopFn, rc.Params{})
|
|
|
|
job2 := jobs.NewAsyncJob(noopFn, rc.Params{})
|
2018-10-26 21:48:22 +08: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) {
|
|
|
|
jobs := newJobs()
|
2019-07-18 18:13:54 +08:00
|
|
|
job := jobs.NewAsyncJob(noopFn, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, job, jobs.Get(job.ID))
|
|
|
|
assert.Nil(t, jobs.Get(123123123123))
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
var longFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
time.Sleep(1 * time.Hour)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08: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 16:47:29 +08:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 20:01:02 +08:00
|
|
|
const (
|
|
|
|
sleepTime = 100 * time.Millisecond
|
2019-09-05 20:59:06 +08:00
|
|
|
floatSleepTime = float64(sleepTime) / 1e9 / 2
|
2018-11-06 20:01:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// sleep for some time so job.Duration is non-0
|
|
|
|
func sleepJob() {
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
func TestJobFinish(t *testing.T) {
|
|
|
|
jobs := newJobs()
|
2019-07-18 18:13:54 +08:00
|
|
|
job := jobs.NewAsyncJob(longFn, rc.Params{})
|
2018-11-06 20:01:02 +08:00
|
|
|
sleepJob()
|
2018-10-26 21:48:22 +08:00
|
|
|
|
|
|
|
assert.Equal(t, true, job.EndTime.IsZero())
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Equal(t, rc.Params(nil), job.Output)
|
2018-10-26 21:48:22 +08: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 18:13:54 +08:00
|
|
|
wantOut := rc.Params{"a": 1}
|
2018-10-26 21:48:22 +08:00
|
|
|
job.finish(wantOut, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 20:01:02 +08:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
job = jobs.NewAsyncJob(longFn, rc.Params{})
|
2018-11-06 20:01:02 +08:00
|
|
|
sleepJob()
|
2018-10-26 21:48:22 +08:00
|
|
|
job.finish(nil, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 20:01:02 +08:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
job = jobs.NewAsyncJob(longFn, rc.Params{})
|
2018-11-06 20:01:02 +08:00
|
|
|
sleepJob()
|
2018-10-26 21:48:22 +08:00
|
|
|
job.finish(wantOut, errors.New("potato"))
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 20:01:02 +08:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 21:48:22 +08: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) {
|
|
|
|
wait := make(chan struct{})
|
2019-07-18 18:13:54 +08:00
|
|
|
boom := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-11-06 20:01:02 +08:00
|
|
|
sleepJob()
|
2018-10-26 21:48:22 +08:00
|
|
|
defer close(wait)
|
|
|
|
panic("boom")
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs := newJobs()
|
2019-07-18 18:13:54 +08:00
|
|
|
job := jobs.NewAsyncJob(boom, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
<-wait
|
2018-11-02 05:14:00 +08: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 21:48:22 +08:00
|
|
|
|
2018-11-02 05:14:00 +08:00
|
|
|
job.mu.Lock()
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 20:01:02 +08:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2019-07-30 19:22:06 +08:00
|
|
|
assert.Contains(t, job.Error, "panic received: boom")
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, false, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
2018-11-02 05:14:00 +08:00
|
|
|
job.mu.Unlock()
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsNewJob(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
jobs := newJobs()
|
2019-07-18 18:13:54 +08:00
|
|
|
job := jobs.NewAsyncJob(noopFn, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, job, jobs.Get(1))
|
2019-06-17 16:47:29 +08:00
|
|
|
assert.NotEmpty(t, job.Stop)
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartJob(t *testing.T) {
|
|
|
|
jobID = 0
|
2019-07-18 18:13:54 +08:00
|
|
|
out, err := StartAsyncJob(longFn, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.NoError(t, err)
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteJob(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
_, id, err := ExecuteJob(context.Background(), shortFn, rc.Params{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), id)
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:50:18 +08:00
|
|
|
func TestExecuteJobErrorPropagation(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
|
|
|
|
testErr := errors.New("test error")
|
|
|
|
errorFn := func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
return nil, testErr
|
|
|
|
}
|
|
|
|
_, _, err := ExecuteJob(context.Background(), errorFn, rc.Params{})
|
|
|
|
assert.Equal(t, testErr, err)
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
func TestRcJobStatus(t *testing.T) {
|
|
|
|
jobID = 0
|
2019-07-18 18:13:54 +08:00
|
|
|
_, err := StartAsyncJob(longFn, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
call := rc.Calls.Get("job/status")
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 18:13:54 +08:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:48:22 +08: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 18:13:54 +08:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 21:48:22 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 21:48:22 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRcJobList(t *testing.T) {
|
|
|
|
jobID = 0
|
2019-07-18 18:13:54 +08:00
|
|
|
_, err := StartAsyncJob(longFn, rc.Params{})
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
call := rc.Calls.Get("job/list")
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 18:13:54 +08:00
|
|
|
in := rc.Params{}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:48:22 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Equal(t, rc.Params{"jobids": []int64{1}}, out)
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
2019-06-17 16:47:29 +08:00
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
func TestRcAsyncJobStop(t *testing.T) {
|
2019-06-17 16:47:29 +08:00
|
|
|
jobID = 0
|
2019-07-18 18:13:54 +08:00
|
|
|
_, err := StartAsyncJob(ctxFn, rc.Params{})
|
2019-06-17 16:47:29 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 18:13:54 +08: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
|
|
|
|
_, id, err := ExecuteJob(ctx, ctxFn, rc.Params{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, int64(1), id)
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
call := rc.Calls.Get("job/stop")
|
2019-06-17 16:47:29 +08:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 18:13:54 +08:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 16:47:29 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 16:47:29 +08:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 16:47:29 +08:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
cancel()
|
2019-06-17 16:47:29 +08:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
call = rc.Calls.Get("job/status")
|
2019-06-17 16:47:29 +08:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 18:13:54 +08:00
|
|
|
in = rc.Params{"jobid": 1}
|
2019-06-17 16:47:29 +08: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"])
|
|
|
|
}
|