How to use Kill method of runner Package

Best Gauge code snippet using runner.Kill

runner.go

Source:runner.go Github

copy

Full Screen

...62 restartDelay: restartDelay,63 }64 go func() {65 defer runner.tomb.Done()66 runner.tomb.Kill(runner.run())67 }()68 return runner69}70var ErrDead = errors.New("worker runner is not running")71// StartWorker starts a worker running associated with the given id.72// The startFunc function will be called to create the worker;73// when the worker exits, it will be restarted as long as it74// does not return a fatal error.75//76// If there is already a worker with the given id, nothing will be done.77//78// StartWorker returns ErrDead if the runner is not running.79func (runner *runner) StartWorker(id string, startFunc func() (Worker, error)) error {80 select {81 case runner.startc <- startReq{id, startFunc}:82 return nil83 case <-runner.tomb.Dead():84 }85 return ErrDead86}87// StopWorker stops the worker associated with the given id.88// It does nothing if there is no such worker.89//90// StopWorker returns ErrDead if the runner is not running.91func (runner *runner) StopWorker(id string) error {92 select {93 case runner.stopc <- id:94 return nil95 case <-runner.tomb.Dead():96 }97 return ErrDead98}99func (runner *runner) Wait() error {100 return runner.tomb.Wait()101}102func (runner *runner) Kill() {103 logger.Debugf("killing runner %p", runner)104 runner.tomb.Kill(nil)105}106type workerInfo struct {107 start func() (Worker, error)108 worker Worker109 restartDelay time.Duration110 stopping bool111}112func (runner *runner) run() error {113 // workers holds the current set of workers. All workers with a114 // running goroutine have an entry here.115 workers := make(map[string]*workerInfo)116 var finalError error117 // isDying holds whether the runner is currently dying. When it118 // is dying (whether as a result of being killed or due to a119 // fatal error), all existing workers are killed, no new workers120 // will be started, and the loop will exit when all existing121 // workers have stopped.122 isDying := false123 tombDying := runner.tomb.Dying()124 for {125 if isDying && len(workers) == 0 {126 return finalError127 }128 select {129 case <-tombDying:130 logger.Infof("runner is dying")131 isDying = true132 killAll(workers)133 tombDying = nil134 case req := <-runner.startc:135 if isDying {136 logger.Infof("ignoring start request for %q when dying", req.id)137 break138 }139 info := workers[req.id]140 if info == nil {141 workers[req.id] = &workerInfo{142 start: req.start,143 restartDelay: runner.restartDelay,144 }145 go runner.runWorker(0, req.id, req.start)146 break147 }148 if !info.stopping {149 // The worker is already running, so leave it alone150 break151 }152 // The worker previously existed and is153 // currently being stopped. When it eventually154 // does stop, we'll restart it immediately with155 // the new start function.156 info.start = req.start157 info.restartDelay = 0158 case id := <-runner.stopc:159 logger.Debugf("stop %q", id)160 if info := workers[id]; info != nil {161 killWorker(id, info)162 }163 case info := <-runner.startedc:164 logger.Debugf("%q started", info.id)165 workerInfo := workers[info.id]166 workerInfo.worker = info.worker167 if isDying || workerInfo.stopping {168 killWorker(info.id, workerInfo)169 }170 case info := <-runner.donec:171 logger.Debugf("%q done: %v", info.id, info.err)172 workerInfo := workers[info.id]173 if !workerInfo.stopping && info.err == nil {174 logger.Debugf("removing %q from known workers", info.id)175 delete(workers, info.id)176 break177 }178 if info.err != nil {179 if runner.isFatal(info.err) {180 logger.Errorf("fatal %q: %v", info.id, info.err)181 if finalError == nil || runner.moreImportant(info.err, finalError) {182 finalError = info.err183 }184 delete(workers, info.id)185 if !isDying {186 isDying = true187 killAll(workers)188 }189 break190 } else {191 logger.Errorf("exited %q: %v", info.id, info.err)192 }193 }194 if workerInfo.start == nil {195 logger.Debugf("no restart, removing %q from known workers", info.id)196 // The worker has been deliberately stopped;197 // we can now remove it from the list of workers.198 delete(workers, info.id)199 break200 }201 go runner.runWorker(workerInfo.restartDelay, info.id, workerInfo.start)202 workerInfo.restartDelay = runner.restartDelay203 }204 }205}206func killAll(workers map[string]*workerInfo) {207 for id, info := range workers {208 killWorker(id, info)209 }210}211func killWorker(id string, info *workerInfo) {212 if info.worker != nil {213 logger.Debugf("killing %q", id)214 info.worker.Kill()215 info.worker = nil216 } else {217 logger.Debugf("couldn't kill %q, not yet started", id)218 }219 info.stopping = true220 info.start = nil221}222// runWorker starts the given worker after waiting for the given delay.223func (runner *runner) runWorker(delay time.Duration, id string, start func() (Worker, error)) {224 if delay > 0 {225 logger.Infof("restarting %q in %v", id, delay)226 select {227 case <-runner.tomb.Dying():228 runner.donec <- doneInfo{id, nil}...

Full Screen

Full Screen

fake_runner.go

Source:fake_runner.go Github

copy

Full Screen

...20 waitArgsForCall []struct{}21 waitReturns struct {22 result1 error23 }24 KillStub func() error25 killMutex sync.RWMutex26 killArgsForCall []struct{}27 killReturns struct {28 result1 error29 }30}31func (fake *FakeRunner) Run(outbuf *bytes.Buffer, errbuff *bytes.Buffer) error {32 fake.runMutex.Lock()33 fake.runArgsForCall = append(fake.runArgsForCall, struct {34 outbuf *bytes.Buffer35 errbuff *bytes.Buffer36 }{outbuf, errbuff})37 fake.runMutex.Unlock()38 if fake.RunStub != nil {39 return fake.RunStub(outbuf, errbuff)40 } else {41 return fake.runReturns.result142 }43}44func (fake *FakeRunner) RunCallCount() int {45 fake.runMutex.RLock()46 defer fake.runMutex.RUnlock()47 return len(fake.runArgsForCall)48}49func (fake *FakeRunner) RunArgsForCall(i int) (*bytes.Buffer, *bytes.Buffer) {50 fake.runMutex.RLock()51 defer fake.runMutex.RUnlock()52 return fake.runArgsForCall[i].outbuf, fake.runArgsForCall[i].errbuff53}54func (fake *FakeRunner) RunReturns(result1 error) {55 fake.RunStub = nil56 fake.runReturns = struct {57 result1 error58 }{result1}59}60func (fake *FakeRunner) Wait() error {61 fake.waitMutex.Lock()62 fake.waitArgsForCall = append(fake.waitArgsForCall, struct{}{})63 fake.waitMutex.Unlock()64 if fake.WaitStub != nil {65 return fake.WaitStub()66 } else {67 return fake.waitReturns.result168 }69}70func (fake *FakeRunner) WaitCallCount() int {71 fake.waitMutex.RLock()72 defer fake.waitMutex.RUnlock()73 return len(fake.waitArgsForCall)74}75func (fake *FakeRunner) WaitReturns(result1 error) {76 fake.WaitStub = nil77 fake.waitReturns = struct {78 result1 error79 }{result1}80}81func (fake *FakeRunner) Kill() error {82 fake.killMutex.Lock()83 fake.killArgsForCall = append(fake.killArgsForCall, struct{}{})84 fake.killMutex.Unlock()85 if fake.KillStub != nil {86 return fake.KillStub()87 } else {88 return fake.killReturns.result189 }90}91func (fake *FakeRunner) KillCallCount() int {92 fake.killMutex.RLock()93 defer fake.killMutex.RUnlock()94 return len(fake.killArgsForCall)95}96func (fake *FakeRunner) KillReturns(result1 error) {97 fake.KillStub = nil98 fake.killReturns = struct {99 result1 error100 }{result1}101}102var _ commandrunner.Runner = new(FakeRunner)...

Full Screen

Full Screen

fake_command_runner_test.go

Source:fake_command_runner_test.go Github

copy

Full Screen

...14 runner = fake_command_runner.New()15 cmd = &exec.Cmd{}16 cmd2 = &exec.Cmd{}17 })18 Describe("Kill", func() {19 It("should record Kill commands", func() {20 runner.Kill(cmd)21 Expect(runner.KilledCommands()).To(Equal([]*exec.Cmd{cmd}))22 })23 // This may seem like an odd test, but it exposed a bug.24 It("should not confuse Kill and Wait", func() {25 runner.Kill(cmd)26 runner.Wait(cmd2)27 Expect(runner.KilledCommands()).To(Equal([]*exec.Cmd{cmd}))28 })29 })30 Describe("Wait", func() {31 It("should record Wait commands", func() {32 runner.Wait(cmd)33 Expect(runner.WaitedCommands()).To(Equal([]*exec.Cmd{cmd}))34 })35 It("should not confuse Wait and Kill", func() {36 runner.Wait(cmd)37 runner.Kill(cmd2)38 Expect(runner.WaitedCommands()).To(Equal([]*exec.Cmd{cmd}))39 })40 })41})...

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("go", "run", "2.go")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 }8 go func() {9 time.Sleep(3 * time.Second)10 cmd.Process.Kill()11 }()12 err = cmd.Wait()13 if err != nil {14 fmt.Println(err)15 }16}17import "fmt"18func main() {19 for i := 1; i <= 1000; i++ {20 fmt.Println(i)21 }22}23import (24func main() {25 cmd := exec.Command("go", "run", "4.go")26 err := cmd.Start()27 if err != nil {28 fmt.Println(err)29 }30 go func() {31 time.Sleep(3 * time.Second)32 cmd.Process.Kill()33 }()34 err = cmd.Wait()35 if err != nil {36 fmt.Println(err)37 }38}39import "fmt"40func main() {41 for i := 1; i <= 1000; i++ {42 fmt.Println(i)43 }44}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runner := NewRunner(5 * time.Second)4 runner.Add(createTask(), createTask(), createTask())5 if err := runner.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to timeout")8 fmt.Println("Terminating due to interrupt")9 }10 }11 fmt.Println("Process ended")12}13import (14func main() {15 runner := NewRunner(5 * time.Second)16 runner.Add(createTask(), createTask(), createTask())17 runner.Start()18 fmt.Println("Process ended")19}20import (21func main() {22 runner := NewRunner(5 * time.Second)23 runner.Add(createTask(), createTask(), createTask())24 runner.Start()25 fmt.Println("Process ended")26}27import (28func main() {29 runner := NewRunner(5 * time.Second)30 runner.Add(createTask(), createTask(), createTask())31 runner.Start()32 fmt.Println("Process ended")33}34import (35func main() {36 runner := NewRunner(5 * time.Second)37 runner.Add(createTask(), createTask(), createTask())38 runner.Start()39 fmt.Println("Process ended")40}41import (42func main() {43 runner := NewRunner(5 * time.Second)44 runner.Add(createTask(), createTask(), createTask())45 runner.Start()46 fmt.Println("Process ended")47}48import (49func main() {50 runner := NewRunner(5 * time.Second)51 runner.Add(createTask(), createTask(), createTask())52 runner.Start()

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting work.")4 runner := NewRunner(3 * time.Second)5 runner.Add(createTask(), createTask(), createTask())6 runner.Start()7 time.Sleep(10 * time.Second)8 runner.Kill()9 fmt.Println("Terminating program.")10}11import (12func main() {13 fmt.Println("Starting work.")14 runner := NewRunner(3 * time.Second)15 runner.Add(createTask(), createTask(), createTask())16 runner.Start()17 time.Sleep(10 * time.Second)18 runner.Kill()19 fmt.Println("Terminating program.")20}21import (22func main() {23 fmt.Println("Starting work.")24 runner := NewRunner(3 * time.Second)25 runner.Add(createTask(), createTask(), createTask())26 runner.Start()27 time.Sleep(10 * time.Second)28 runner.Kill()29 fmt.Println("Terminating program.")30}31import (32func main() {33 fmt.Println("Starting work.")34 runner := NewRunner(3 * time.Second)35 runner.Add(createTask(), createTask(), createTask())36 runner.Start()37 time.Sleep(10 * time.Second)38 runner.Kill()39 fmt.Println("Terminating program.")40}41import (42func main() {43 fmt.Println("Starting work.")44 runner := NewRunner(3 * time.Second)45 runner.Add(createTask(), createTask(), createTask())46 runner.Start()47 time.Sleep(10 * time.Second)48 runner.Kill()49 fmt.Println("Terminating program.")50}51import (52func main() {53 fmt.Println("Starting work.")54 runner := NewRunner(3 * time.Second)

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runner := NewRunner(10 * time.Second)4 go runner.Start()5 time.Sleep(3 * time.Second)6 runner.Kill()7}8import (9func main() {10 runner := NewRunner(10 * time.Second)11 go runner.Start()12 time.Sleep(3 * time.Second)13 runner.Stop()14}15import (16func main() {17 runner := NewRunner(10 * time.Second)18 go runner.Start()19 time.Sleep(3 * time.Second)20 runner.Stop()21}22import (23func main() {24 runner := NewRunner(10 * time.Second)25 go runner.Start()26 time.Sleep(3 * time.Second)27 runner.Stop()28}29import (30func main() {31 runner := NewRunner(10 * time.Second)32 go runner.Start()33 time.Sleep(3 * time.Second)34 runner.Stop()35}36import (37func main() {38 runner := NewRunner(10 * time.Second)39 go runner.Start()40 time.Sleep(3 * time.Second)41 runner.Stop()42}43import (44func main() {45 runner := NewRunner(10 * time.Second)46 go runner.Start()47 time.Sleep(3 * time.Second)48 runner.Stop()49}50import (51func main() {52 runner := NewRunner(10 * time.Second)53 go runner.Start()54 time.Sleep(3 * time.Second)55 runner.Stop()56}57import (

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("go", "run", "2.go")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 os.Exit(1)8 }9 err = cmd.Wait()10 if err != nil {11 fmt.Println(err)12 }13}14import (15func main() {16 for {17 fmt.Println("hello")18 time.Sleep(1 * time.Second)19 }20}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("In main")4 runner := NewRunner(3 * time.Second)5 go runner.Start()6 time.Sleep(10 * time.Second)7 runner.Kill()8}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting work.")4 runner := Runner{}5 runner.Start(time.Second * 3)6 runner.Kill()7 fmt.Println("End of work.")8}9import (10func main() {11 fmt.Println("Starting work.")12 runner := Runner{}13 runner.Start(time.Second * 3)14 runner.Stop()15 fmt.Println("End of work.")16}17import (18func main() {19 fmt.Println("Starting work.")20 runner := Runner{}21 runner.Start(time.Second * 3)22 runner.StopAndWait()23 fmt.Println("End of work.")24}25import (26func main() {27 fmt.Println("Starting work.")28 runner := Runner{}29 runner.Start(time.Second * 3)30 runner.StopAndWait()31 fmt.Println("End of work.")32}33import (34func main() {35 fmt.Println("Starting work.")36 runner := Runner{}37 runner.Start(time.Second * 3)38 runner.StopAndWait()39 fmt.Println("End of work.")40}41import (42func main() {43 fmt.Println("Starting work.")44 runner := Runner{}45 runner.Start(time.Second * 3)46 runner.StopAndWait()47 fmt.Println("End of work.")48}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Number of CPUs: ", runtime.NumCPU())4 r := New(3 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 if err := r.Start(); err != nil {7 switch err {8 fmt.Println("Terminating due to timeout.")9 fmt.Println("Terminating due to interrupt.")10 }11 }12 fmt.Println("Process ended.")13}14func createTask() func(int) {15 return func(id int) {16 fmt.Printf("Processor - Task #%d.\n", id)17 time.Sleep(time.Duration(id) * time.Second)18 }19}20import (21var ErrTimeout = errors.New("received timeout")22var ErrInterrupt = errors.New("received interrupt")23type Runner struct {24 tasks []func(int)25}26func New(d time.Duration) *Runner {27 return &Runner{28 complete: make(chan error),29 timeout: time.After(d),30 interrupt: make(chan os.Signal, 1),31 }32}33func (r *Runner) Add(tasks ...func(int)) {34 r.tasks = append(r.tasks, tasks...)35}36func (r *Runner) Start() error {37 signal.Notify(r.interrupt, os.Interrupt)38 go func() {39 r.complete <- r.run()40 }()41 select {42 }43}44func (r *Runner) run() error {45 for id, task := range r.tasks {46 if r.gotInterrupt() {47 }48 task(id)

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("go", "run", "2.go")4 err := cmd.Start()5 if err != nil {6 fmt.Printf("Error: %s\n", err)7 os.Exit(1)8 }9 r := NewRunner(3 * time.Second)10 r.Run(cmd)11}12import (13func main() {14 sigCh := make(chan os.Signal, 1)15 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)16 fmt.Println("Starting process")17 for i := 0; i < 5; i++ {18 fmt.Println("Working...")19 time.Sleep(1 * time.Second)20 select {21 fmt.Println("Terminating process")22 os.Exit(0)23 }24 }25 fmt.Println("Process completed")26}27import (28func main() {29 cmd := exec.Command("go", "run", "2.go")30 err := cmd.Start()31 if err != nil {32 fmt.Printf("Error: %s\n", err)33 os.Exit(1)34 }35 r := NewRunner(3 * time.Second)36 r.Run(cmd)37}38import (39func main() {40 sigCh := make(chan os.Signal, 1)41 signal.Notify(sigCh,

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Killing process")4 syscall.Kill(1, syscall.SIGKILL)5}6import (7func main() {8 fmt.Println("Killing process")9 syscall.Kill(1, syscall.SIGKILL)10}11import (12func main() {13 fmt.Println("Killing process")14 syscall.Kill(1, syscall.SIGKILL)15}16import (17func main() {18 fmt.Println("Killing process")19 syscall.Kill(1, syscall.SIGKILL)20}21import (22func main() {23 fmt.Println("Killing process")24 syscall.Kill(1, syscall.SIGKILL)25}26import (27func main() {28 fmt.Println("Killing process")29 syscall.Kill(1, syscall.SIGKILL)30}31import (32func main() {33 fmt.Println("Killing process")34 syscall.Kill(1, syscall.SIGKILL)35}36import (37func main() {38 fmt.Println("Killing process")39 syscall.Kill(1, syscall.SIGKILL)40}41import (42func main() {43 fmt.Println("Killing process")44 syscall.Kill(1, syscall.SIGKILL)45}46import (

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