github.com/loov/combiner@v0.1.0/testsuite/worker.go (about) 1 package testsuite 2 3 import ( 4 "runtime" 5 "time" 6 ) 7 8 type Worker struct { 9 WorkStart int 10 WorkDo int 11 WorkFinish int 12 13 SleepStart time.Duration 14 SleepDo time.Duration 15 SleepFinish time.Duration 16 17 Total int64 18 Batches int64 19 } 20 21 func NewWorker() *Worker { return &Worker{} } 22 23 func (exe *Worker) Start() { 24 simulateWork(exe.WorkStart, exe.SleepStart) 25 } 26 27 func (exe *Worker) Do(v interface{}) { 28 exe.Total += v.(int64) 29 simulateWork(exe.WorkDo, exe.SleepDo) 30 } 31 32 func (exe *Worker) Finish() { 33 exe.Batches++ 34 simulateWork(exe.WorkFinish, exe.SleepFinish) 35 } 36 37 func simulateWork(amount int, sleep time.Duration) { 38 foo := 1 39 for i := 0; i < amount; i++ { 40 foo *= 2 41 foo /= 2 42 } 43 if amount > 0 { 44 runtime.Gosched() 45 } 46 if sleep > 0 { 47 time.Sleep(sleep) 48 } 49 }