go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/tq/tqtesting/options.go (about) 1 // Copyright 2020 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tqtesting 16 17 // RunOption influences behavior of Run call. 18 type RunOption interface { 19 isOption() 20 } 21 22 // StopWhenDrained will stop the scheduler after it finishes executing the 23 // last task and there are no more tasks scheduled. 24 // 25 // It is naturally racy if there are other goroutines that submit tasks 26 // concurrently. In this situation there may be a pending queue of tasks even 27 // if Run stops. 28 func StopWhenDrained() RunOption { 29 return stopWhenDrained{} 30 } 31 32 type stopWhenDrained struct{} 33 34 func (stopWhenDrained) isOption() {} 35 36 // StopAfterTask will stop the scheduler after it finishes executing a task of 37 // the given task class ID. 38 func StopAfterTask(taskClassID string) RunOption { 39 return StopAfter(func(t *Task) bool { return t.Class == taskClassID }) 40 } 41 42 // StopAfter will stop the scheduler if the given function returns true, 43 // given the just finished task. 44 func StopAfter(f func(t *Task) bool) RunOption { 45 return stopAfter{f} 46 } 47 48 type stopAfter struct { 49 examine func(t *Task) bool 50 } 51 52 func (stopAfter) isOption() {} 53 54 // StopBeforeTask will stop the scheduler if the next task to be executed has 55 // the given task class ID. 56 // 57 // The same caveats of StopBefore also apply for StopBeforeTask. 58 func StopBeforeTask(taskClassID string) RunOption { 59 return StopBefore(func(t *Task) bool { return t.Class == taskClassID }) 60 } 61 62 // StopBefore will stop the scheduler if the given function returns true, 63 // given the next task to be executed. 64 // 65 // If such next task has specified ETA, StopBeforeTask does NOT provide any 66 // guarantee about what `clock.Now` returns by the time Run stops. 67 // 68 // It is naturally racy if there are other goroutines that submit tasks 69 // concurrently. In this situation there may be a different next task (by ETA) 70 // when Run stops. 71 func StopBefore(f func(t *Task) bool) RunOption { 72 return stopBefore{f} 73 } 74 75 type stopBefore struct { 76 examine func(t *Task) bool 77 } 78 79 func (stopBefore) isOption() {} 80 81 // ParallelExecute instructs the scheduler to call executor's Execute method 82 // in a separate goroutine instead of serially in Run. 83 // 84 // This more closely resembles real-life behavior but may introduce more 85 // unpredictability into tests due to races. 86 func ParallelExecute() RunOption { 87 return parallelExecute{} 88 } 89 90 type parallelExecute struct{} 91 92 func (parallelExecute) isOption() {}