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"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
2018-11-02 05:14:00 +08:00
|
|
|
"runtime"
|
2018-10-26 21:48:22 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-12-13 01:40:29 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-02-23 18:34:58 +08:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
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)
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
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)
|
2023-06-11 22:23:39 +08:00
|
|
|
var gotJobID int64
|
|
|
|
var gotJob *Job
|
2020-12-12 23:35:30 +08:00
|
|
|
job, out, err := jobs.NewJob(ctx, func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
defer close(wait)
|
2023-06-11 22:23:39 +08:00
|
|
|
var ok bool
|
|
|
|
gotJobID, ok = GetJobID(ctx)
|
|
|
|
assert.True(t, ok)
|
|
|
|
gotJob, ok = GetJob(ctx)
|
|
|
|
assert.True(t, ok)
|
2018-10-26 21:48:22 +08:00
|
|
|
return in, nil
|
2020-12-12 23:35:30 +08:00
|
|
|
}, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(out))
|
2018-10-26 21:48:22 +08:00
|
|
|
<-wait
|
2023-06-11 22:23:39 +08:00
|
|
|
assert.Equal(t, job.ID, gotJobID, "check can get JobID from ctx")
|
|
|
|
assert.Equal(t, job, gotJob, "check can get Job from ctx")
|
2018-10-26 21:48:22 +08:00
|
|
|
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()
|
2024-07-04 17:47:05 +08:00
|
|
|
job.EndTime = time.Now().Add(-rc.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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs := newJobs()
|
2020-12-12 23:35:30 +08: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 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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs := newJobs()
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
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) {
|
2022-06-25 00:35:01 +08:00
|
|
|
<-ctx.Done()
|
|
|
|
return nil, ctx.Err()
|
2019-06-17 16:47:29 +08:00
|
|
|
}
|
|
|
|
|
2020-01-23 19:44:56 +08: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) {
|
2022-06-25 00:35:01 +08:00
|
|
|
<-paramCtx.Done()
|
|
|
|
if returnError {
|
|
|
|
return nil, ctx.Err()
|
2020-01-23 19:44:56 +08:00
|
|
|
}
|
2022-06-25 00:35:01 +08:00
|
|
|
return rc.Params{}, nil
|
2020-01-23 19:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs := newJobs()
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err := jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
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)
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
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)
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err = jobs.NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:48:22 +08:00
|
|
|
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()
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err := jobs.NewJob(ctx, boom, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs := newJobs()
|
2020-12-12 23:35:30 +08:00
|
|
|
job, out, err := jobs.NewJob(ctx, noopFn, rc.Params{"_async": true})
|
|
|
|
require.NoError(t, err)
|
2018-10-26 21:48:22 +08:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2020-12-12 23:35:30 +08:00
|
|
|
assert.Equal(t, rc.Params{"jobid": int64(1)}, out)
|
2018-10-26 21:48:22 +08:00
|
|
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
job, out, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
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)
|
2020-12-12 23:35:30 +08:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteJob(t *testing.T) {
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
job, out, err := NewJob(context.Background(), shortFn, rc.Params{})
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 23:35:30 +08:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-13 01:40:29 +08:00
|
|
|
func TestExecuteJobWithConfig(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2021-02-23 18:34:58 +08:00
|
|
|
called := false
|
2020-12-13 01:40:29 +08:00
|
|
|
jobFn := func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-03 03:11:57 +08:00
|
|
|
assert.Equal(t, 42*fs.Mebi, ci.BufferSize)
|
2021-02-23 18:34:58 +08:00
|
|
|
called = true
|
2020-12-13 01:40:29 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
_, _, err := NewJob(context.Background(), jobFn, rc.Params{
|
|
|
|
"_config": rc.Params{
|
|
|
|
"BufferSize": "42M",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-02-23 18:34:58 +08:00
|
|
|
assert.Equal(t, true, called)
|
|
|
|
// Retest with string parameter
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2021-02-23 18:34:58 +08:00
|
|
|
called = false
|
2020-12-13 01:40:29 +08:00
|
|
|
_, _, err = NewJob(ctx, jobFn, rc.Params{
|
|
|
|
"_config": `{"BufferSize": "42M"}`,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-02-23 18:34:58 +08:00
|
|
|
assert.Equal(t, true, called)
|
|
|
|
// Check that wasn't the default
|
2020-12-13 01:40:29 +08:00
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-03 03:11:57 +08:00
|
|
|
assert.NotEqual(t, 42*fs.Mebi, ci.BufferSize)
|
2020-12-13 01:40:29 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:34:58 +08:00
|
|
|
func TestExecuteJobWithFilter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
called := false
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2021-02-23 18:34:58 +08:00
|
|
|
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()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2021-02-23 18:34:58 +08:00
|
|
|
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 20:50:18 +08:00
|
|
|
func TestExecuteJobErrorPropagation(t *testing.T) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2019-08-21 20:50:18 +08:00
|
|
|
|
|
|
|
testErr := errors.New("test error")
|
|
|
|
errorFn := func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
return nil, testErr
|
|
|
|
}
|
2020-12-12 23:35:30 +08:00
|
|
|
_, _, err := NewJob(ctx, errorFn, rc.Params{})
|
2019-08-21 20:50:18 +08:00
|
|
|
assert.Equal(t, testErr, err)
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
func TestRcJobStatus(t *testing.T) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
_, _, err := NewJob(ctx, longFn, rc.Params{"_async": true})
|
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{}
|
2023-06-29 00:06:36 +08:00
|
|
|
out1, err := call.Fn(context.Background(), in)
|
2018-10-26 21:48:22 +08:00
|
|
|
require.NoError(t, err)
|
2023-06-29 00:06:36 +08:00
|
|
|
require.NotNil(t, out1)
|
|
|
|
assert.Equal(t, []int64{1}, out1["jobids"], "should have job listed")
|
|
|
|
|
|
|
|
_, _, err = NewJob(ctx, longFn, rc.Params{"_async": true})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
call = rc.Calls.Get("job/list")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in = rc.Params{}
|
|
|
|
out2, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out2)
|
|
|
|
assert.Equal(t, 2, len(out2["jobids"].([]int64)), "should have all jobs listed")
|
|
|
|
|
|
|
|
require.NotNil(t, out1["executeId"], "should have executeId")
|
|
|
|
assert.Equal(t, out1["executeId"], out2["executeId"], "executeId should be the same")
|
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) {
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
_, _, err := NewJob(ctx, ctxFn, rc.Params{"_async": true})
|
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() {
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-12-12 23:35:30 +08:00
|
|
|
job, out, err := NewJob(ctx, ctxFn, rc.Params{})
|
2019-07-18 18:13:54 +08:00
|
|
|
assert.Error(t, err)
|
2020-12-12 23:35:30 +08:00
|
|
|
assert.Equal(t, int64(1), job.ID)
|
|
|
|
assert.Equal(t, rc.Params{}, out)
|
2019-07-18 18:13:54 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
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"])
|
|
|
|
}
|
2020-01-23 19:44:56 +08:00
|
|
|
|
2022-07-10 22:42:04 +08:00
|
|
|
func TestRcJobStopGroup(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2022-07-10 22:42:04 +08:00
|
|
|
_, _, err := NewJob(ctx, ctxFn, rc.Params{
|
|
|
|
"_async": true,
|
|
|
|
"_group": "myparty",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, _, err = NewJob(ctx, ctxFn, rc.Params{
|
|
|
|
"_async": true,
|
|
|
|
"_group": "myparty",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
call := rc.Calls.Get("job/stopgroup")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := rc.Params{"group": "myparty"}
|
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
|
|
|
|
in = rc.Params{}
|
|
|
|
_, 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)
|
|
|
|
for i := 1; i <= 2; i++ {
|
|
|
|
in = rc.Params{"jobid": i}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, "myparty", out["group"])
|
|
|
|
assert.Equal(t, "context canceled", out["error"])
|
|
|
|
assert.Equal(t, true, out["finished"])
|
|
|
|
assert.Equal(t, false, out["success"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 19:44:56 +08:00
|
|
|
func TestOnFinish(t *testing.T) {
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-01-23 19:44:56 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err := NewJob(ctx, ctxParmFn(ctx, false), rc.Params{"_async": true})
|
2020-01-23 19:44:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 19:44:56 +08: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) {
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID.Store(0)
|
2020-01-23 19:44:56 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2020-12-12 23:35:30 +08:00
|
|
|
job, _, err := NewJob(ctx, shortFn, rc.Params{})
|
2020-01-23 19:44:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
stop, err := OnFinish(job.ID, func() { close(done) })
|
2020-01-23 19:44:56 +08:00
|
|
|
defer stop()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout waiting for OnFinish to fire")
|
|
|
|
}
|
|
|
|
}
|
2024-10-13 16:43:00 +08:00
|
|
|
|
|
|
|
func TestOnFinishDataRace(t *testing.T) {
|
|
|
|
jobID.Store(0)
|
|
|
|
job, _, err := NewJob(context.Background(), ctxFn, rc.Params{"_async": true})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var expect, got uint64
|
|
|
|
finished := make(chan struct{})
|
|
|
|
stop, stopped := make(chan struct{}), make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
break Loop
|
|
|
|
default:
|
|
|
|
_, err := OnFinish(job.ID, func() {
|
|
|
|
finished <- struct{}{}
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expect += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(stopped)
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
job.Stop()
|
|
|
|
|
|
|
|
// Wait for the first OnFinish to fire
|
|
|
|
<-finished
|
|
|
|
got += 1
|
|
|
|
|
|
|
|
// Stop the OnFinish producer
|
|
|
|
close(stop)
|
|
|
|
<-stopped
|
|
|
|
|
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
for {
|
|
|
|
if got == expect {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-finished:
|
|
|
|
got += 1
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("Timeout waiting for all OnFinish calls to fire")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|