github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/jobs/helpers_test.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package jobs 12 13 import ( 14 "context" 15 16 "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" 17 "github.com/cockroachdb/cockroach/pkg/kv" 18 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 19 ) 20 21 func ResetConstructors() func() { 22 old := make(map[jobspb.Type]Constructor) 23 for k, v := range constructors { 24 old[k] = v 25 } 26 return func() { constructors = old } 27 } 28 29 // FakeResumer calls optional callbacks during the job lifecycle. 30 type FakeResumer struct { 31 OnResume func(context.Context, chan<- tree.Datums) error 32 FailOrCancel func(context.Context) error 33 Success func() error 34 PauseRequest onPauseRequestFunc 35 } 36 37 var _ Resumer = FakeResumer{} 38 39 func (d FakeResumer) Resume( 40 ctx context.Context, _ interface{}, resultsCh chan<- tree.Datums, 41 ) error { 42 if d.OnResume != nil { 43 if err := d.OnResume(ctx, resultsCh); err != nil { 44 return err 45 } 46 } 47 if d.Success != nil { 48 return d.Success() 49 } 50 return nil 51 } 52 53 func (d FakeResumer) OnFailOrCancel(ctx context.Context, _ interface{}) error { 54 if d.FailOrCancel != nil { 55 return d.FailOrCancel(ctx) 56 } 57 return nil 58 } 59 60 // OnPauseRequestFunc forwards the definition for use in tests. 61 type OnPauseRequestFunc = onPauseRequestFunc 62 63 var _ PauseRequester = FakeResumer{} 64 65 func (d FakeResumer) OnPauseRequest( 66 ctx context.Context, phs interface{}, txn *kv.Txn, details *jobspb.Progress, 67 ) error { 68 if d.PauseRequest == nil { 69 return nil 70 } 71 return d.PauseRequest(ctx, phs, txn, details) 72 } 73 74 // Started is a wrapper around the internal function that moves a job to the 75 // started state. 76 func (j *Job) Started(ctx context.Context) error { 77 return j.started(ctx) 78 } 79 80 // Created is a wrapper around the internal function that creates the initial 81 // job state. 82 func (j *Job) Created(ctx context.Context) error { 83 return j.created(ctx) 84 } 85 86 // Paused is a wrapper around the internal function that moves a job to the 87 // paused state. 88 func (j *Job) Paused(ctx context.Context) error { 89 return j.paused(ctx, nil /* fn */) 90 } 91 92 // Failed is a wrapper around the internal function that moves a job to the 93 // failed state. 94 func (j *Job) Failed(ctx context.Context, causingErr error) error { 95 return j.failed(ctx, causingErr, nil /* fn */) 96 } 97 98 // Succeeded is a wrapper around the internal function that moves a job to the 99 // succeeded state. 100 func (j *Job) Succeeded(ctx context.Context) error { 101 return j.succeeded(ctx, nil /* fn */) 102 }