2019-07-18 10:13:54 +00:00
|
|
|
package jobs
|
2018-10-26 13:48:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2018-11-01 21:14:00 +00:00
|
|
|
"runtime"
|
2018-10-26 13:48:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-12-12 17:40:29 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-02-23 10:34:58 +00:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-08-10 16:12:22 +00:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
2020-06-15 20:27:40 +00:00
|
|
|
"github.com/rclone/rclone/fstest/testy"
|
2018-10-26 13:48:22 +00: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 20:27:40 +00:00
|
|
|
testy.SkipUnreliable(t)
|
2018-10-26 13:48:22 +00:00
|
|
|
jobs := newJobs()
|
2019-08-10 16:12:22 +00:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
jobs.kickExpire()
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 13:48:22 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsExpire(t *testing.T) {
|
2020-06-15 20:27:40 +00:00
|
|
|
testy.SkipUnreliable(t)
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
wait := make(chan struct{})
|
|
|
|
jobs := newJobs()
|
2019-08-10 16:12:22 +00:00
|
|
|
jobs.opt.JobExpireInterval = time.Millisecond
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
2020-12-12 15:35:30 +00:00
|
|
|
job, out, err := jobs.NewJob(ctx, func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 13:48:22 +00:00
|
|
|
defer close(wait)
|
|
|
|
return in, nil
|
2020-12-12 15:35:30 +00:00
|
|
|
}, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(out))
|
2018-10-26 13:48:22 +00:00
|
|
|
<-wait
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
|
|
|
jobs.Expire()
|
|
|
|
assert.Equal(t, 1, len(jobs.jobs))
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Lock()
|
2019-05-01 12:06:36 +00:00
|
|
|
job.mu.Lock()
|
2019-08-10 16:12:22 +00:00
|
|
|
job.EndTime = time.Now().Add(-rcflags.Opt.JobExpireDuration - 60*time.Second)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, true, jobs.expireRunning)
|
2019-05-01 12:06:36 +00:00
|
|
|
job.mu.Unlock()
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Unlock()
|
2019-09-19 15:53:48 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Lock()
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, jobs.expireRunning)
|
|
|
|
assert.Equal(t, 0, len(jobs.jobs))
|
2018-11-01 21:14:00 +00:00
|
|
|
jobs.mu.Unlock()
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
var noopFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 13:48:22 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsIDs(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobs := newJobs()
|
2020-12-12 15:35:30 +00: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 13:48:22 +00: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 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobs := newJobs()
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, job, jobs.Get(job.ID))
|
|
|
|
assert.Nil(t, jobs.Get(123123123123))
|
|
|
|
}
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
var longFn = func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 13:48:22 +00:00
|
|
|
time.Sleep(1 * time.Hour)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 10:13:54 +00: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 08:47:29 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:44:56 +00: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 12:01:02 +00:00
|
|
|
const (
|
|
|
|
sleepTime = 100 * time.Millisecond
|
2019-09-05 12:59:06 +00:00
|
|
|
floatSleepTime = float64(sleepTime) / 1e9 / 2
|
2018-11-06 12:01:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// sleep for some time so job.Duration is non-0
|
|
|
|
func sleepJob() {
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
}
|
|
|
|
|
2018-10-26 13:48:22 +00:00
|
|
|
func TestJobFinish(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobs := newJobs()
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err := jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 12:01:02 +00:00
|
|
|
sleepJob()
|
2018-10-26 13:48:22 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, job.EndTime.IsZero())
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Equal(t, rc.Params(nil), job.Output)
|
2018-10-26 13:48:22 +00: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 10:13:54 +00:00
|
|
|
wantOut := rc.Params{"a": 1}
|
2018-10-26 13:48:22 +00:00
|
|
|
job.finish(wantOut, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 12:01:02 +00:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 12:01:02 +00:00
|
|
|
sleepJob()
|
2018-10-26 13:48:22 +00:00
|
|
|
job.finish(nil, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 12:01:02 +00:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, "", job.Error)
|
|
|
|
assert.Equal(t, true, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
|
|
|
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-11-06 12:01:02 +00:00
|
|
|
sleepJob()
|
2018-10-26 13:48:22 +00:00
|
|
|
job.finish(wantOut, errors.New("potato"))
|
|
|
|
|
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
|
|
|
assert.Equal(t, wantOut, job.Output)
|
2018-11-06 12:01:02 +00:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2018-10-26 13:48:22 +00: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 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
wait := make(chan struct{})
|
2019-07-18 10:13:54 +00:00
|
|
|
boom := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-11-06 12:01:02 +00:00
|
|
|
sleepJob()
|
2018-10-26 13:48:22 +00:00
|
|
|
defer close(wait)
|
|
|
|
panic("boom")
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs := newJobs()
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err := jobs.NewJob(ctx, boom, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 13:48:22 +00:00
|
|
|
<-wait
|
2018-11-01 21:14:00 +00: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 13:48:22 +00:00
|
|
|
|
2018-11-01 21:14:00 +00:00
|
|
|
job.mu.Lock()
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, job.EndTime.IsZero())
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Equal(t, rc.Params{}, job.Output)
|
2018-11-06 12:01:02 +00:00
|
|
|
assert.True(t, job.Duration >= floatSleepTime)
|
2019-07-30 11:22:06 +00:00
|
|
|
assert.Contains(t, job.Error, "panic received: boom")
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, false, job.Success)
|
|
|
|
assert.Equal(t, true, job.Finished)
|
2018-11-01 21:14:00 +00:00
|
|
|
job.mu.Unlock()
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobsNewJob(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobID = 0
|
|
|
|
jobs := newJobs()
|
2020-12-12 15:35:30 +00:00
|
|
|
job, out, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2020-12-12 15:35:30 +00:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.Equal(t, job, jobs.Get(1))
|
2019-06-17 08:47:29 +00:00
|
|
|
assert.NotEmpty(t, job.Stop)
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartJob(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobID = 0
|
2020-12-12 15:35:30 +00:00
|
|
|
job, out, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
2020-12-12 15:35:30 +00:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2019-07-18 10:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteJob(t *testing.T) {
|
|
|
|
jobID = 0
|
2020-12-12 15:35:30 +00:00
|
|
|
job, out, err := NewJob(context.Background(), shortFn, rc.Params{})
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 15:35:30 +00:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 17:40:29 +00:00
|
|
|
func TestExecuteJobWithConfig(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
jobID = 0
|
2021-02-23 10:34:58 +00:00
|
|
|
called := false
|
2020-12-12 17:40:29 +00:00
|
|
|
jobFn := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-02 19:11:57 +00:00
|
|
|
assert.Equal(t, 42*fs.Mebi, ci.BufferSize)
|
2021-02-23 10:34:58 +00:00
|
|
|
called = true
|
2020-12-12 17:40:29 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
_, _, err := NewJob(context.Background(), jobFn, rc.Params{
|
|
|
|
"_config": rc.Params{
|
|
|
|
"BufferSize": "42M",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-02-23 10:34:58 +00:00
|
|
|
assert.Equal(t, true, called)
|
|
|
|
// Retest with string parameter
|
2020-12-12 17:40:29 +00:00
|
|
|
jobID = 0
|
2021-02-23 10:34:58 +00:00
|
|
|
called = false
|
2020-12-12 17:40:29 +00:00
|
|
|
_, _, err = NewJob(ctx, jobFn, rc.Params{
|
|
|
|
"_config": `{"BufferSize": "42M"}`,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-02-23 10:34:58 +00:00
|
|
|
assert.Equal(t, true, called)
|
|
|
|
// Check that wasn't the default
|
2020-12-12 17:40:29 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-02 19:11:57 +00:00
|
|
|
assert.NotEqual(t, 42*fs.Mebi, ci.BufferSize)
|
2020-12-12 17:40:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 10:34:58 +00:00
|
|
|
func TestExecuteJobWithFilter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
called := false
|
|
|
|
jobID = 0
|
|
|
|
jobFn := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
fi := filter.GetConfig(ctx)
|
|
|
|
assert.Equal(t, fs.SizeSuffix(1024), fi.Opt.MaxSize)
|
|
|
|
assert.Equal(t, []string{"a", "b", "c"}, fi.Opt.IncludeRule)
|
|
|
|
called = true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
_, _, err := NewJob(ctx, jobFn, rc.Params{
|
|
|
|
"_filter": rc.Params{
|
|
|
|
"IncludeRule": []string{"a", "b", "c"},
|
|
|
|
"MaxSize": "1k",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, called)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteJobWithGroup(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
jobID = 0
|
|
|
|
called := false
|
|
|
|
jobFn := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
called = true
|
|
|
|
group, found := accounting.StatsGroupFromContext(ctx)
|
|
|
|
assert.Equal(t, true, found)
|
|
|
|
assert.Equal(t, "myparty", group)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
_, _, err := NewJob(ctx, jobFn, rc.Params{
|
|
|
|
"_group": "myparty",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, called)
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:50:18 +00:00
|
|
|
func TestExecuteJobErrorPropagation(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2019-08-21 12:50:18 +00: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 15:35:30 +00:00
|
|
|
_, _, err := NewJob(ctx, errorFn, rc.Params{})
|
2019-08-21 12:50:18 +00:00
|
|
|
assert.Equal(t, testErr, err)
|
|
|
|
}
|
|
|
|
|
2018-10-26 13:48:22 +00:00
|
|
|
func TestRcJobStatus(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobID = 0
|
2020-12-12 15:35:30 +00:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
call := rc.Calls.Get("job/status")
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 10:13:54 +00:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 08:34:30 +00:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 13:48:22 +00: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 10:13:54 +00:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 08:34:30 +00:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 13:48:22 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 08:34:30 +00:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
2018-10-26 13:48:22 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRcJobList(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2018-10-26 13:48:22 +00:00
|
|
|
jobID = 0
|
2020-12-12 15:35:30 +00:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
call := rc.Calls.Get("job/list")
|
2018-10-26 13:48:22 +00:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 10:13:54 +00:00
|
|
|
in := rc.Params{}
|
2019-06-17 08:34:30 +00:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 13:48:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Equal(t, rc.Params{"jobids": []int64{1}}, out)
|
2018-10-26 13:48:22 +00:00
|
|
|
}
|
2019-06-17 08:47:29 +00:00
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
func TestRcAsyncJobStop(t *testing.T) {
|
2020-12-12 15:35:30 +00:00
|
|
|
ctx := context.Background()
|
2019-06-17 08:47:29 +00:00
|
|
|
jobID = 0
|
2020-12-12 15:35:30 +00:00
|
|
|
_, _, err := NewJob(ctx, ctxFn, rc.Params{"_async": true})
|
2019-06-17 08:47:29 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-18 10:13:54 +00: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 15:35:30 +00:00
|
|
|
job, out, err := NewJob(ctx, ctxFn, rc.Params{})
|
2019-07-18 10:13:54 +00:00
|
|
|
assert.Error(t, err)
|
2020-12-12 15:35:30 +00:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2019-07-18 10:13:54 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
call := rc.Calls.Get("job/stop")
|
2019-06-17 08:47:29 +00:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 10:13:54 +00:00
|
|
|
in := rc.Params{"jobid": 1}
|
2019-06-17 08:47:29 +00:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
in = rc.Params{"jobid": 123123123}
|
2019-06-17 08:47:29 +00:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "job not found")
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
in = rc.Params{"jobidx": 123123123}
|
2019-06-17 08:47:29 +00:00
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Didn't find key")
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
cancel()
|
2019-06-17 08:47:29 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2019-07-18 10:13:54 +00:00
|
|
|
call = rc.Calls.Get("job/status")
|
2019-06-17 08:47:29 +00:00
|
|
|
assert.NotNil(t, call)
|
2019-07-18 10:13:54 +00:00
|
|
|
in = rc.Params{"jobid": 1}
|
2019-06-17 08:47:29 +00: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 11:44:56 +00:00
|
|
|
|
|
|
|
func TestOnFinish(t *testing.T) {
|
|
|
|
jobID = 0
|
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-12-12 15:35:30 +00:00
|
|
|
job, _, err := NewJob(ctx, ctxParmFn(ctx, false), rc.Params{"_async": true})
|
2020-01-23 11:44:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 15:35:30 +00:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 11:44:56 +00: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 15:35:30 +00:00
|
|
|
job, _, err := NewJob(ctx, shortFn, rc.Params{})
|
2020-01-23 11:44:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 15:35:30 +00:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 11:44:56 +00:00
|
|
|
defer stop()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout waiting for OnFinish to fire")
|
|
|
|
}
|
|
|
|
}
|