How to use Start method of runner Package

Best Gauge code snippet using runner.Start

runner_test.go

Source:runner_test.go Github

copy

Full Screen

...24}25func noImportance(err0, err1 error) bool {26 return false27}28func (*RunnerSuite) TestOneWorkerStart(c *gc.C) {29 runner := worker.NewRunner(noneFatal, noImportance, time.Millisecond)30 starter := newTestWorkerStarter()31 err := runner.StartWorker("id", testWorkerStart(starter))32 c.Assert(err, jc.ErrorIsNil)33 starter.assertStarted(c, true)34 c.Assert(worker.Stop(runner), gc.IsNil)35 starter.assertStarted(c, false)36}37func (*RunnerSuite) TestOneWorkerFinish(c *gc.C) {38 runner := worker.NewRunner(noneFatal, noImportance, time.Millisecond)39 starter := newTestWorkerStarter()40 err := runner.StartWorker("id", testWorkerStart(starter))41 c.Assert(err, jc.ErrorIsNil)42 starter.assertStarted(c, true)43 starter.die <- nil44 starter.assertStarted(c, false)45 starter.assertNeverStarted(c)46 c.Assert(worker.Stop(runner), gc.IsNil)47}48func (*RunnerSuite) TestOneWorkerRestart(c *gc.C) {49 runner := worker.NewRunner(noneFatal, noImportance, time.Millisecond)50 starter := newTestWorkerStarter()51 err := runner.StartWorker("id", testWorkerStart(starter))52 c.Assert(err, jc.ErrorIsNil)53 starter.assertStarted(c, true)54 // Check it restarts a few times time.55 for i := 0; i < 3; i++ {56 starter.die <- fmt.Errorf("an error")57 starter.assertStarted(c, false)58 starter.assertStarted(c, true)59 }60 c.Assert(worker.Stop(runner), gc.IsNil)61 starter.assertStarted(c, false)62}63func (*RunnerSuite) TestOneWorkerStartFatalError(c *gc.C) {64 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)65 starter := newTestWorkerStarter()66 starter.startErr = errors.New("cannot start test task")67 err := runner.StartWorker("id", testWorkerStart(starter))68 c.Assert(err, jc.ErrorIsNil)69 err = runner.Wait()70 c.Assert(err, gc.Equals, starter.startErr)71}72func (*RunnerSuite) TestOneWorkerDieFatalError(c *gc.C) {73 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)74 starter := newTestWorkerStarter()75 err := runner.StartWorker("id", testWorkerStart(starter))76 c.Assert(err, jc.ErrorIsNil)77 starter.assertStarted(c, true)78 dieErr := errors.New("error when running")79 starter.die <- dieErr80 err = runner.Wait()81 c.Assert(err, gc.Equals, dieErr)82 starter.assertStarted(c, false)83}84func (*RunnerSuite) TestOneWorkerStartStop(c *gc.C) {85 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)86 starter := newTestWorkerStarter()87 err := runner.StartWorker("id", testWorkerStart(starter))88 c.Assert(err, jc.ErrorIsNil)89 starter.assertStarted(c, true)90 err = runner.StopWorker("id")91 c.Assert(err, jc.ErrorIsNil)92 starter.assertStarted(c, false)93 c.Assert(worker.Stop(runner), gc.IsNil)94}95func (*RunnerSuite) TestOneWorkerStopFatalError(c *gc.C) {96 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)97 starter := newTestWorkerStarter()98 starter.stopErr = errors.New("stop error")99 err := runner.StartWorker("id", testWorkerStart(starter))100 c.Assert(err, jc.ErrorIsNil)101 starter.assertStarted(c, true)102 err = runner.StopWorker("id")103 c.Assert(err, jc.ErrorIsNil)104 err = runner.Wait()105 c.Assert(err, gc.Equals, starter.stopErr)106}107func (*RunnerSuite) TestOneWorkerStartWhenStopping(c *gc.C) {108 runner := worker.NewRunner(allFatal, noImportance, 3*time.Second)109 starter := newTestWorkerStarter()110 starter.stopWait = make(chan struct{})111 err := runner.StartWorker("id", testWorkerStart(starter))112 c.Assert(err, jc.ErrorIsNil)113 starter.assertStarted(c, true)114 err = runner.StopWorker("id")115 c.Assert(err, jc.ErrorIsNil)116 err = runner.StartWorker("id", testWorkerStart(starter))117 c.Assert(err, jc.ErrorIsNil)118 close(starter.stopWait)119 starter.assertStarted(c, false)120 // Check that the task is restarted immediately without121 // the usual restart timeout delay.122 t0 := time.Now()123 starter.assertStarted(c, true)124 restartDuration := time.Since(t0)125 if restartDuration > 1*time.Second {126 c.Fatalf("task did not restart immediately")127 }128 c.Assert(worker.Stop(runner), gc.IsNil)129}130func (*RunnerSuite) TestOneWorkerRestartDelay(c *gc.C) {131 const delay = 100 * time.Millisecond132 runner := worker.NewRunner(noneFatal, noImportance, delay)133 starter := newTestWorkerStarter()134 err := runner.StartWorker("id", testWorkerStart(starter))135 c.Assert(err, jc.ErrorIsNil)136 starter.assertStarted(c, true)137 starter.die <- fmt.Errorf("non-fatal error")138 starter.assertStarted(c, false)139 t0 := time.Now()140 starter.assertStarted(c, true)141 restartDuration := time.Since(t0)142 if restartDuration < delay {143 c.Fatalf("restart delay was not respected; got %v want %v", restartDuration, delay)144 }145 c.Assert(worker.Stop(runner), gc.IsNil)146}147type errorLevel int148func (e errorLevel) Error() string {149 return fmt.Sprintf("error with importance %d", e)150}151func (*RunnerSuite) TestErrorImportance(c *gc.C) {152 moreImportant := func(err0, err1 error) bool {153 return err0.(errorLevel) > err1.(errorLevel)154 }155 id := func(i int) string { return fmt.Sprint(i) }156 runner := worker.NewRunner(allFatal, moreImportant, time.Millisecond)157 for i := 0; i < 10; i++ {158 starter := newTestWorkerStarter()159 starter.stopErr = errorLevel(i)160 err := runner.StartWorker(id(i), testWorkerStart(starter))161 c.Assert(err, jc.ErrorIsNil)162 }163 err := runner.StopWorker(id(4))164 c.Assert(err, jc.ErrorIsNil)165 err = runner.Wait()166 c.Assert(err, gc.Equals, errorLevel(9))167}168func (*RunnerSuite) TestStartWorkerWhenDead(c *gc.C) {169 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)170 c.Assert(worker.Stop(runner), gc.IsNil)171 c.Assert(runner.StartWorker("foo", nil), gc.Equals, worker.ErrDead)172}173func (*RunnerSuite) TestStopWorkerWhenDead(c *gc.C) {174 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)175 c.Assert(worker.Stop(runner), gc.IsNil)176 c.Assert(runner.StopWorker("foo"), gc.Equals, worker.ErrDead)177}178func (*RunnerSuite) TestAllWorkersStoppedWhenOneDiesWithFatalError(c *gc.C) {179 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)180 var starters []*testWorkerStarter181 for i := 0; i < 10; i++ {182 starter := newTestWorkerStarter()183 err := runner.StartWorker(fmt.Sprint(i), testWorkerStart(starter))184 c.Assert(err, jc.ErrorIsNil)185 starters = append(starters, starter)186 }187 for _, starter := range starters {188 starter.assertStarted(c, true)189 }190 dieErr := errors.New("fatal error")191 starters[4].die <- dieErr192 err := runner.Wait()193 c.Assert(err, gc.Equals, dieErr)194 for _, starter := range starters {195 starter.assertStarted(c, false)196 }197}198func (*RunnerSuite) TestFatalErrorWhileStarting(c *gc.C) {199 // Original deadlock problem that this tests for:200 // A worker dies with fatal error while another worker201 // is inside start(). runWorker can't send startInfo on startedc.202 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)203 slowStarter := newTestWorkerStarter()204 // make the startNotify channel synchronous so205 // we can delay the start indefinitely.206 slowStarter.startNotify = make(chan bool)207 err := runner.StartWorker("slow starter", testWorkerStart(slowStarter))208 c.Assert(err, jc.ErrorIsNil)209 fatalStarter := newTestWorkerStarter()210 fatalStarter.startErr = fmt.Errorf("a fatal error")211 err = runner.StartWorker("fatal worker", testWorkerStart(fatalStarter))212 c.Assert(err, jc.ErrorIsNil)213 // Wait for the runner loop to react to the fatal214 // error and go into final shutdown mode.215 time.Sleep(10 * time.Millisecond)216 // At this point, the loop is in shutdown mode, but the217 // slowStarter's worker is still in its start function.218 // When the start function continues (the first assertStarted219 // allows that to happen) and returns the new Worker,220 // runWorker will try to send it on runner.startedc.221 // This test makes sure that succeeds ok.222 slowStarter.assertStarted(c, true)223 slowStarter.assertStarted(c, false)224 err = runner.Wait()225 c.Assert(err, gc.Equals, fatalStarter.startErr)226}227func (*RunnerSuite) TestFatalErrorWhileSelfStartWorker(c *gc.C) {228 // Original deadlock problem that this tests for:229 // A worker tries to call StartWorker in its start function230 // at the same time another worker dies with a fatal error.231 // It might not be able to send on startc.232 runner := worker.NewRunner(allFatal, noImportance, time.Millisecond)233 selfStarter := newTestWorkerStarter()234 // make the startNotify channel synchronous so235 // we can delay the start indefinitely.236 selfStarter.startNotify = make(chan bool)237 selfStarter.hook = func() {238 runner.StartWorker("another", func() (worker.Worker, error) {239 return nil, fmt.Errorf("no worker started")240 })241 }242 err := runner.StartWorker("self starter", testWorkerStart(selfStarter))243 c.Assert(err, jc.ErrorIsNil)244 fatalStarter := newTestWorkerStarter()245 fatalStarter.startErr = fmt.Errorf("a fatal error")246 err = runner.StartWorker("fatal worker", testWorkerStart(fatalStarter))247 c.Assert(err, jc.ErrorIsNil)248 // Wait for the runner loop to react to the fatal249 // error and go into final shutdown mode.250 time.Sleep(10 * time.Millisecond)251 // At this point, the loop is in shutdown mode, but the252 // selfStarter's worker is still in its start function.253 // When the start function continues (the first assertStarted254 // allows that to happen) it will try to create a new255 // worker. This failed in an earlier version of the code because the256 // loop was not ready to receive start requests.257 selfStarter.assertStarted(c, true)258 selfStarter.assertStarted(c, false)259 err = runner.Wait()260 c.Assert(err, gc.Equals, fatalStarter.startErr)261}262type testWorkerStarter struct {263 startCount int32264 // startNotify receives true when the worker starts265 // and false when it exits. If startErr is non-nil,266 // it sends false only.267 startNotify chan bool268 // If stopWait is non-nil, the worker will269 // wait for a value to be sent on it before270 // exiting.271 stopWait chan struct{}272 // Sending a value on die causes the worker273 // to die with the given error.274 die chan error275 // If startErr is non-nil, the worker will die immediately276 // with this error after starting.277 startErr error278 // If stopErr is non-nil, the worker will die with this279 // error when asked to stop.280 stopErr error281 // The hook function is called after starting the worker.282 hook func()283}284func newTestWorkerStarter() *testWorkerStarter {285 return &testWorkerStarter{286 die: make(chan error, 1),287 startNotify: make(chan bool, 100),288 hook: func() {},289 }290}291func (starter *testWorkerStarter) assertStarted(c *gc.C, started bool) {292 select {293 case isStarted := <-starter.startNotify:294 c.Assert(isStarted, gc.Equals, started)295 case <-time.After(testing.LongWait):296 c.Fatalf("timed out waiting for start notification")297 }298}299func (starter *testWorkerStarter) assertNeverStarted(c *gc.C) {300 select {301 case isStarted := <-starter.startNotify:302 c.Fatalf("got unexpected start notification: %v", isStarted)303 case <-time.After(worker.RestartDelay + testing.ShortWait):304 }305}306func testWorkerStart(starter *testWorkerStarter) func() (worker.Worker, error) {307 return func() (worker.Worker, error) {308 return starter.start()309 }310}311func (starter *testWorkerStarter) start() (worker.Worker, error) {312 if count := atomic.AddInt32(&starter.startCount, 1); count != 1 {313 panic(fmt.Errorf("unexpected start count %d; expected 1", count))314 }315 if starter.startErr != nil {316 starter.startNotify <- false317 return nil, starter.startErr318 }319 task := &testWorker{320 starter: starter,321 }322 starter.startNotify <- true323 go task.run()324 return task, nil325}326type testWorker struct {327 starter *testWorkerStarter328 tomb tomb.Tomb329}330func (t *testWorker) Kill() {331 t.tomb.Kill(nil)332}333func (t *testWorker) Wait() error {334 return t.tomb.Wait()335}336func (t *testWorker) run() {337 defer t.tomb.Done()338 t.starter.hook()339 select {340 case <-t.tomb.Dying():341 t.tomb.Kill(t.starter.stopErr)...

Full Screen

Full Screen

status_test.go

Source:status_test.go Github

copy

Full Screen

...125 stat: stat,126 }127}128func (r *runner) startAction(action *status.Action) {129 r.counts.StartedActions++130 r.counts.RunningActions++131 r.stat.StartAction(action, r.counts)132}133func (r *runner) finishAction(result status.ActionResult) {134 r.counts.FinishedActions++135 r.counts.RunningActions--136 r.stat.FinishAction(result, r.counts)137}138func (r *runner) finishAndStartAction(result status.ActionResult, action *status.Action) {139 r.counts.FinishedActions++140 r.stat.FinishAction(result, r.counts)141 r.counts.StartedActions++142 r.stat.StartAction(action, r.counts)143}144var (145 action1 = &status.Action{Description: "action1"}146 result1 = status.ActionResult{Action: action1}147 action2 = &status.Action{Description: "action2"}148 result2 = status.ActionResult{Action: action2}149 action3 = &status.Action{Description: "action3"}150 result3 = status.ActionResult{Action: action3}151)152func twoActions(stat status.StatusOutput) {153 runner := newRunner(stat, 2)154 runner.startAction(action1)155 runner.finishAction(result1)156 runner.startAction(action2)...

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := NewRunner(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to interrupt")8 fmt.Println("Terminating due to timeout")9 }10 }11 fmt.Println("Process ended")12}13import (14type Runner struct {15 tasks []func(int)16}17var ErrTimeout = errors.New("received timeout")18var ErrInterrupt = errors.New("received interrupt")19func NewRunner(timeout time.Duration) *Runner {20 return &Runner{21 interrupt: make(chan os.Signal, 1),22 complete: make(chan error),23 timeout: time.After(timeout),24 }25}26func (r *Runner) Add(tasks ...func(int)) {27 r.tasks = append(r.tasks, tasks...)28}29func (r *Runner) Start() error {30 signal.Notify(r.interrupt, os.Interrupt)31 go func() {32 r.complete <- r.run()33 }()34 select {

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := runner.New(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 println("Terminating due to timeout.")8 os.Exit(1)9 println("Terminating due to interrupt.")10 os.Exit(2)11 }12 }13 println("Process ended.")14}15import (16func main() {17 signals := make(chan os.Signal, 1)18 signal.Notify(signals, os.Interrupt)19}20import (21func main() {22 ticker := time.NewTicker(500 * time.Millisecond)23 go func() {24 for t := range ticker.C {25 fmt.Println("Tick at", t)26 }27 }()28 time.Sleep(1600 * time.Millisecond)29 ticker.Stop()30 fmt.Println("Ticker stopped")31}32import (33func main() {34 timer1 := time.NewTimer(time.Second * 2)35 fmt.Println("Timer 1 expired")36 timer2 := time.NewTimer(time.Second)37 go func() {38 fmt.Println("Timer 2 expired")39 }()40 stop2 := timer2.Stop()41 if stop2 {42 fmt.Println("Timer 2 stopped")43 }44}45import (46func main() {47 timer := time.NewTimer(time.Second)48 fmt.Println("Timer 1 expired")49 timer.Reset(time.Second)50 fmt.Println("Timer 2 expired")51}52import (53func main() {54 timer := time.NewTimer(time.Second)55 go func() {56 fmt.Println("Timer expired")57 }()58 stop := timer.Stop()59 if stop {60 fmt.Println("Timer stopped")61 }62}63import (

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 log.Println("Starting work.")4 r := New(30 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 go func() {7 r.Start()8 }()9 sigChan := make(chan os.Signal, 1)10 signal.Notify(sigChan, syscall.SIGINT)11 log.Printf("Terminating: %v12", r.Stop())13}14import (15type Runner struct {16 tasks []func(int)17}18var ErrTimeout = errors.New("received timeout")19var ErrInterrupt = errors.New("received interrupt")20func New(d time.Duration) *Runner {21 return &Runner{22 interrupt: make(chan os.Signal, 1),23 complete: make(chan error),24 timeout: time.After(d),25 }26}27func (r *Runner) Add(tasks ...func(int)) {28 r.tasks = append(r.tasks, tasks...)29}30func (r *Runner) Start() error {31 signal.Notify(r.interrupt, os.Interrupt)32 go func() {

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting work.")4 r := runner.New(30 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 r.Start()7 sig := make(chan os.Signal, 1)8 signal.Notify(sig, os.Interrupt)9 select {10 r.Stop()11 case result := <-r.Start():12 fmt.Printf("Received result %v13 }14 fmt.Println("Terminating program.")15}

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Start")4 runner := NewRunner(3, false, task)5 runner.Start()6 fmt.Println("End")7}8import "fmt"9func main() {10 fmt.Println("Start")11 runner := NewRunner(3, false, task)12 runner.StartAll()13 fmt.Println("End")14}15import "fmt"16func main() {17 fmt.Println("Start")18 runner := NewRunner(3, false, task)19 runner.StartAll()20 fmt.Println("End")21}22import "fmt"23func main() {24 fmt.Println("Start")25 runner := NewRunner(3, false, task)26 runner.StartAll()27 fmt.Println("End")28}29import "fmt"30func main() {31 fmt.Println("Start")32 runner := NewRunner(3, false, task)33 runner.StartAll()34 fmt.Println("End")35}36func task(id int) {37 time.Sleep(time.Second)38}39func (r *Runner) Stop() {40 r.cancel()41}

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2var (3func main() {4 file, err := os.Open("C:\\Users\\test\\Desktop\\test.txt")5 if err != nil {6 log.Fatal(err)7 }8 defer file.Close()9 scanner := bufio.NewScanner(file)10 for scanner.Scan() {

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting Work.")4 go func(msg string) {5 fmt.Println(msg)6 }("going")7 time.Sleep(time.Second)8 fmt.Println("Done.")9}10import (11func main() {12 fmt.Println("Starting Work.")13 runtime.GOMAXPROCS(1)14 go func(msg string) {15 fmt.Println(msg)16 }("going")17 time.Sleep(time.Second)18 fmt.Println("Done.")19}20import (21func main() {22 fmt.Println("Starting Work.")23 runtime.GOMAXPROCS(2)24 go func(msg string) {25 fmt.Println(msg)26 }("going")27 time.Sleep(time.Second)28 fmt.Println("Done.")29}30import (

Full Screen

Full Screen

Start

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 r := NewRunner(3)4 r.Start()5 fmt.Println("end")6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful