How to use waiter method of executor Package

Best K6 code snippet using executor.waiter

shell_test.go

Source:shell_test.go Github

copy

Full Screen

1package shell2import (3 "context"4 "errors"5 "os/exec"6 "testing"7 "time"8 "github.com/stretchr/testify/assert"9 "github.com/stretchr/testify/mock"10 "gitlab.com/gitlab-org/gitlab-runner/common"11 "gitlab.com/gitlab-org/gitlab-runner/executors"12 "gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags"13 "gitlab.com/gitlab-org/gitlab-runner/helpers/process"14 "gitlab.com/gitlab-org/gitlab-runner/shells/shellstest"15)16func TestExecutor_Run(t *testing.T) {17 var testErr = errors.New("test error")18 var exitErr = &exec.ExitError{}19 tests := map[string]struct {20 commanderAssertions func(*process.MockCommander, chan time.Time)21 processKillerAssertions func(*process.MockKillWaiter, chan time.Time)22 cancelJob bool23 expectedErr error24 }{25 "canceled job uses new process termination": {26 commanderAssertions: func(mCmd *process.MockCommander, waitCalled chan time.Time) {27 mCmd.On("Start").Return(nil).Once()28 mCmd.On("Wait").Run(func(args mock.Arguments) {29 close(waitCalled)30 }).Return(nil).Once()31 },32 processKillerAssertions: func(mProcessKillWaiter *process.MockKillWaiter, waitCalled chan time.Time) {33 mProcessKillWaiter.34 On("KillAndWait", mock.Anything, mock.Anything).35 Return(nil).36 WaitUntil(waitCalled)37 },38 cancelJob: true,39 expectedErr: nil,40 },41 "cmd fails to start": {42 commanderAssertions: func(mCmd *process.MockCommander, _ chan time.Time) {43 mCmd.On("Start").Return(testErr).Once()44 },45 processKillerAssertions: func(_ *process.MockKillWaiter, _ chan time.Time) {46 },47 expectedErr: testErr,48 },49 "wait returns error": {50 commanderAssertions: func(mCmd *process.MockCommander, waitCalled chan time.Time) {51 mCmd.On("Start").Return(nil).Once()52 mCmd.On("Wait").Run(func(args mock.Arguments) {53 close(waitCalled)54 }).Return(testErr).Once()55 },56 processKillerAssertions: func(mProcessKillWaiter *process.MockKillWaiter, waitCalled chan time.Time) {57 mProcessKillWaiter.58 On("KillAndWait", mock.Anything, mock.Anything).59 Return(nil).60 WaitUntil(waitCalled)61 },62 cancelJob: false,63 expectedErr: testErr,64 },65 "wait returns exit error": {66 commanderAssertions: func(mCmd *process.MockCommander, waitCalled chan time.Time) {67 mCmd.On("Start").Return(nil).Once()68 mCmd.On("Wait").Run(func(args mock.Arguments) {69 close(waitCalled)70 }).Return(exitErr).Once()71 },72 processKillerAssertions: func(mProcessKillWaiter *process.MockKillWaiter, waitCalled chan time.Time) {73 mProcessKillWaiter.74 On("KillAndWait", mock.Anything, mock.Anything).75 Return(nil).76 WaitUntil(waitCalled)77 },78 cancelJob: false,79 expectedErr: &common.BuildError{},80 },81 }82 for name, tt := range tests {83 t.Run(name, func(t *testing.T) {84 shellstest.OnEachShell(t, func(t *testing.T, shell string) {85 mProcessKillWaiter, mCmd, cleanup := setupProcessMocks(t)86 defer cleanup()87 waitCalled := make(chan time.Time)88 tt.commanderAssertions(mCmd, waitCalled)89 tt.processKillerAssertions(mProcessKillWaiter, waitCalled)90 executor := executor{91 AbstractExecutor: executors.AbstractExecutor{92 Build: &common.Build{93 JobResponse: common.JobResponse{},94 Runner: &common.RunnerConfig{},95 },96 BuildShell: &common.ShellConfiguration{97 Command: shell,98 },99 },100 }101 ctx, cancelJob := context.WithCancel(context.Background())102 defer cancelJob()103 cmd := common.ExecutorCommand{104 Script: "echo hello",105 Predefined: false,106 Context: ctx,107 }108 if tt.cancelJob {109 cancelJob()110 }111 err := executor.Run(cmd)112 assert.ErrorIs(t, err, tt.expectedErr)113 })114 })115 }116}117func setupProcessMocks(t *testing.T) (*process.MockKillWaiter, *process.MockCommander, func()) {118 mProcessKillWaiter := new(process.MockKillWaiter)119 defer mProcessKillWaiter.AssertExpectations(t)120 mCmd := new(process.MockCommander)121 defer mCmd.AssertExpectations(t)122 oldNewProcessKillWaiter := newProcessKillWaiter123 oldCmd := newCommander124 newProcessKillWaiter = func(125 logger process.Logger,126 gracefulKillTimeout time.Duration,127 forceKillTimeout time.Duration,128 ) process.KillWaiter {129 return mProcessKillWaiter130 }131 newCommander = func(executable string, args []string, options process.CommandOptions) process.Commander {132 return mCmd133 }134 return mProcessKillWaiter, mCmd, func() {135 newProcessKillWaiter = oldNewProcessKillWaiter136 newCommander = oldCmd137 }138}139// TODO: Remove in 14.0 https://gitlab.com/gitlab-org/gitlab-runner/issues/6413140func TestProcessTermination_Legacy(t *testing.T) {141 shellstest.OnEachShell(t, func(t *testing.T, shell string) {142 // cmd is deprecated and exec.Cmd#Wait does not propagate the correct143 // error on batch so skip this test. batch is being deprecated in 13.0144 // as well https://gitlab.com/gitlab-org/gitlab-runner/issues/6099145 if shell == "cmd" {146 return147 }148 executor := executor{149 AbstractExecutor: executors.AbstractExecutor{150 Build: &common.Build{151 JobResponse: common.JobResponse{152 Variables: common.JobVariables{153 common.JobVariable{Key: featureflags.ShellExecutorUseLegacyProcessKill, Value: "true"},154 },155 },156 Runner: &common.RunnerConfig{},157 },158 BuildShell: &common.ShellConfiguration{159 Command: shell,160 },161 },162 }163 ctx, cancelJob := context.WithCancel(context.Background())164 cmd := common.ExecutorCommand{165 Script: "echo hello",166 Predefined: false,167 Context: ctx,168 }169 cancelJob()170 err := executor.Run(cmd)171 var buildErr *common.BuildError172 assert.ErrorAs(t, err, &buildErr)173 })174}...

Full Screen

Full Screen

waiter_test.go

Source:waiter_test.go Github

copy

Full Screen

...47 close(waitingFinished)48 }()49 <-waitingStarted50 time.Sleep(1 * time.Second)51 // Closing waiter the first time, no error.52 err := hdwGetter().Unlock(inslogger.TestContext(t), pulse, jetID)53 require.NoError(t, err)54 <-waitingFinished55 // Closing waiter the second time, error.56 err = hdwGetter().Unlock(inslogger.TestContext(t), pulse, jetID)57 assert.Error(t, err)58}59func Test_HotDataWaiterConcrete_Close(t *testing.T) {60 t.Parallel()61 // Arrange62 waitingStarted := make(chan struct{}, 1)63 waitingFinished := make(chan struct{})64 hdw := executor.NewChannelWaiter()65 hdwLock := sync.Mutex{}66 hdwGetter := func() *executor.ChannelWaiter {67 hdwLock.Lock()68 defer hdwLock.Unlock()69 return hdw...

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 wg.Add(2)4 go func() {5 defer wg.Done()6 time.Sleep(1 * time.Second)7 fmt.Println("Hello")8 }()9 go func() {10 defer wg.Done()11 time.Sleep(2 * time.Second)12 fmt.Println("World")13 }()14 wg.Wait()15}16Wait() method

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1func main() {2 executor := NewExecutor(2)3 executor.Submit(func() {4 fmt.Println("Hello from task 1")5 })6 executor.Submit(func() {7 fmt.Println("Hello from task 2")8 })9 executor.Submit(func() {10 fmt.Println("Hello from task 3")11 })12 executor.Submit(func() {13 fmt.Println("Hello from task 4")14 })15 executor.Submit(func() {16 fmt.Println("Hello from task 5")17 })18 executor.Submit(func() {19 fmt.Println("Hello from task 6")20 })21 executor.Submit(func() {22 fmt.Println("Hello from task 7")23 })24 executor.Submit(func() {25 fmt.Println("Hello from task 8")26 })27 executor.Submit(func() {28 fmt.Println("Hello from task 9")29 })30 executor.Submit(func() {31 fmt.Println("Hello from task 10")32 })33 executor.Submit(func() {34 fmt.Println("Hello from task 11")35 })36 executor.Submit(func() {37 fmt.Println("Hello from task 12")38 })39 executor.Submit(func() {40 fmt.Println("Hello from task 13")41 })42 executor.Submit(func() {43 fmt.Println("Hello from task 14")44 })45 executor.Submit(func() {46 fmt.Println("Hello from task 15")47 })48 executor.Submit(func() {49 fmt.Println("Hello from task 16")50 })51 executor.Submit(func() {52 fmt.Println("Hello from task 17")53 })54 executor.Submit(func() {55 fmt.Println("Hello from task 18")56 })57 executor.Submit(func() {58 fmt.Println("Hello from task 19")59 })60 executor.Submit(func() {61 fmt.Println("Hello from task 20")62 })63 executor.Submit(func() {64 fmt.Println("Hello from task 21")65 })66 executor.Submit(func() {67 fmt.Println("Hello from task 22")68 })69 executor.Submit(func() {70 fmt.Println("Hello from task 23")71 })72 executor.Submit(func() {73 fmt.Println("Hello from task 24")74 })75 executor.Submit(func() {76 fmt.Println("Hello from task 25")77 })78 executor.Submit(func() {79 fmt.Println("Hello from task 26")80 })81 executor.Submit(func() {82 fmt.Println("Hello from task 27")83 })

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 wg.Add(2)4 go func() {5 time.Sleep(time.Second)6 fmt.Println("first goroutine finished")7 wg.Done()8 }()9 go func() {10 time.Sleep(time.Second)11 fmt.Println("second goroutine finished")12 wg.Done()13 }()14 wg.Wait()15 fmt.Println("all goroutines finished")16}17import (18func main() {19 wg.Add(2)20 go func() {21 time.Sleep(time.Second)22 fmt.Println("first goroutine finished")23 wg.Done()24 }()25 go func() {26 time.Sleep(time.Second)27 fmt.Println("second goroutine finished")28 wg.Done()29 }()30 wg.Wait()31 fmt.Println("all goroutines finished")32}33import (34func main() {35 wg.Add(2)36 go func() {37 time.Sleep(time.Second)38 fmt.Println("first goroutine finished")39 wg.Done()40 }()41 go func() {42 time.Sleep(time.Second)43 fmt.Println("second goroutine finished")44 wg.Done()45 }()46 wg.Wait()47 fmt.Println("all goroutines finished")48}49import (50func main() {51 wg.Add(2)52 go func() {53 time.Sleep(time.Second)54 fmt.Println("first goroutine finished")55 wg.Done()56 }()57 go func() {58 time.Sleep(time.Second)

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting the application...")4 fmt.Println("Going to do some work...")5 time.Sleep(5 * time.Second)6 fmt.Println("Terminating the application...")7}8import (9func main() {10 fmt.Println("Starting the application...")11 fmt.Println("Going to do some work...")12 time.Sleep(5 * time.Second)13 fmt.Println("Terminating the application...")14}

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import java.util.concurrent.*;2import java.io.*;3public class TestWaiter {4 public static void main(String[] args) throws IOException {5 ExecutorService exec = Executors.newCachedThreadPool();6 Waiter waiter = new Waiter();7 exec.execute(new Chef(waiter));8 exec.execute(new WaitPerson(waiter));9 System.out.println("Press 'Enter' to quit");10 System.in.read();11 exec.shutdownNow();12 }13}14import java.util.concurrent.*;15import java.util.*;16public class Waiter implements Runnable {17 private Restaurant restaurant;18 private BlockingQueue<Plate> filledOrders = new LinkedBlockingQueue<Plate>();19 public Waiter(Restaurant rest) { restaurant = rest; }20 public void placeOrder(Customer cust, Food food) {21 try {22 filledOrders.put(new Plate(cust, food));23 } catch(InterruptedException e) {24 System.out.println("Waiter interrupted");25 }26 }27 public void run() {28 try {29 while(!Thread.interrupted()) {30 Plate plate = filledOrders.take();31 System.out.println(plate + " delivered to " + plate.getCustomer());32 }33 } catch(InterruptedException e) {34 System.out.println("Waiter interrupted");35 }36 }37}38import java.util.concurrent.*;39import java.io.*;40import java.util.*;41public class Chef implements Runnable {42 private Restaurant restaurant;43 private int count = 0;44 private Random rand = new Random(47);45 public Chef(Waiter w) { waiter = w; }46 public void run() {47 try {48 while(!Thread.interrupted()) {49 synchronized(this) {50 if(++count == 10) {51 System.out.println("Out of food, closing");52 restaurant.exec.shutdownNow();53 }54 System.out.print("Order up! ");55 waiter.placeOrder(this, new Food("Burrito"));56 TimeUnit.MILLISECONDS.sleep(100);57 }58 }59 } catch(InterruptedException e) {60 System.out.println("Chef interrupted");61 }62 }63}64import java.util.concurrent.*;65import java.util.*;66public class Restaurant {67 Meal meal;68 ExecutorService exec = Executors.newCachedThreadPool();

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 executor := NewExecutor()4 waiter := executor.Waiter()5 task := executor.NewTask(func() {6 fmt.Println("Hello, World!")7 })8 executor.Add(task)9 waiter.Wait()10}11import (12func main() {13 executor := NewExecutor()14 waiter := executor.Waiter()15 task := executor.NewTask(func() {16 fmt.Println("Hello, World!")17 })18 executor.Add(task)19 waiter.Wait()20}21import (22func main() {23 executor := NewExecutor()24 waiter := executor.Waiter()25 task := executor.NewTask(func() {26 fmt.Println("Hello, World!")27 })28 executor.Add(task)29 waiter.Wait()30}31import (32func main() {33 executor := NewExecutor()34 waiter := executor.Waiter()35 task := executor.NewTask(func() {36 fmt.Println("Hello, World!")37 })38 executor.Add(task)39 waiter.Wait()40}41import (42func main() {43 executor := NewExecutor()44 waiter := executor.Waiter()45 task := executor.NewTask(func() {

Full Screen

Full Screen

waiter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Start of main")4 executor := NewExecutor(5)5 wg := NewWaitGroup()6 task := NewTask(func() {7 fmt.Println("Task 1")8 time.Sleep(2 * time.Second)9 fmt.Println("Task 1 complete")10 })11 task2 := NewTask(func() {12 fmt.Println("Task 2")13 time.Sleep(2 * time.Second)14 fmt.Println("Task 2 complete")15 })16 task3 := NewTask(func() {17 fmt.Println("Task 3")18 time.Sleep(2 * time.Second)19 fmt.Println("Task 3 complete")20 })21 task4 := NewTask(func() {22 fmt.Println("Task 4")23 time.Sleep(2 * time.Second)24 fmt.Println("Task 4 complete")25 })26 task5 := NewTask(func() {27 fmt.Println("Task 5")28 time.Sleep(2 * time.Second)29 fmt.Println("Task 5 complete")30 })31 task6 := NewTask(func() {32 fmt.Println("Task 6")33 time.Sleep(2 * time.Second)34 fmt.Println("Task 6 complete")35 })36 task7 := NewTask(func() {37 fmt.Println("Task 7")38 time.Sleep(2 * time.Second)39 fmt.Println("Task 7 complete")40 })41 task8 := NewTask(func() {42 fmt.Println("Task 8")43 time.Sleep(2 * time.Second)44 fmt.Println("Task 8 complete")45 })46 task9 := NewTask(func() {47 fmt.Println("Task 9")48 time.Sleep(2 * time.Second)49 fmt.Println("Task 9 complete")50 })51 task10 := NewTask(func() {52 fmt.Println("Task 10")53 time.Sleep(2 * time.Second)54 fmt.Println("Task 10 complete")55 })56 task11 := NewTask(func() {

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.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful