How to use mockNextIterations method of executor Package

Best K6 code snippet using executor.mockNextIterations

vu_handle_test.go

Source:vu_handle_test.go Github

copy

Full Screen

...31 "go.k6.io/k6/lib/testutils"32 "go.k6.io/k6/lib/testutils/minirunner"33 "go.k6.io/k6/metrics"34)35func mockNextIterations() (uint64, uint64) {36 return 12, 1537}38// this test is mostly interesting when -race is enabled39func TestVUHandleRace(t *testing.T) {40 t.Parallel()41 ctx, cancel := context.WithCancel(context.Background())42 defer cancel()43 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}44 testLog := logrus.New()45 testLog.AddHook(logHook)46 testLog.SetOutput(testutils.NewTestOutput(t))47 // testLog.Level = logrus.DebugLevel48 logEntry := logrus.NewEntry(testLog)49 runner := &minirunner.MiniRunner{}50 runner.Fn = func(ctx context.Context, _ *lib.State, out chan<- metrics.SampleContainer) error {51 return nil52 }53 var getVUCount int6454 var returnVUCount int6455 getVU := func() (lib.InitializedVU, error) {56 return runner.NewVU(uint64(atomic.AddInt64(&getVUCount, 1)), 0, nil)57 }58 returnVU := func(_ lib.InitializedVU) {59 atomic.AddInt64(&returnVUCount, 1)60 // do something61 }62 var interruptedIter int6463 var fullIterations int6464 runIter := func(ctx context.Context, vu lib.ActiveVU) bool {65 _ = vu.RunOnce()66 select {67 case <-ctx.Done():68 // Don't log errors or emit iterations metrics from cancelled iterations69 atomic.AddInt64(&interruptedIter, 1)70 return false71 default:72 atomic.AddInt64(&fullIterations, 1)73 return true74 }75 }76 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)77 go vuHandle.runLoopsIfPossible(runIter)78 var wg sync.WaitGroup79 wg.Add(3)80 go func() {81 defer wg.Done()82 for i := 0; i < 10000; i++ {83 err := vuHandle.start()84 require.NoError(t, err)85 }86 }()87 go func() {88 defer wg.Done()89 for i := 0; i < 1000; i++ {90 vuHandle.gracefulStop()91 time.Sleep(1 * time.Nanosecond)92 }93 }()94 go func() {95 defer wg.Done()96 for i := 0; i < 100; i++ {97 vuHandle.hardStop()98 time.Sleep(10 * time.Nanosecond)99 }100 }()101 wg.Wait()102 vuHandle.hardStop() // STOP it103 time.Sleep(time.Millisecond * 50)104 interruptedBefore := atomic.LoadInt64(&interruptedIter)105 fullBefore := atomic.LoadInt64(&fullIterations)106 _ = vuHandle.start()107 time.Sleep(time.Millisecond * 50) // just to be sure an iteration will squeeze in108 cancel()109 time.Sleep(time.Millisecond * 50)110 interruptedAfter := atomic.LoadInt64(&interruptedIter)111 fullAfter := atomic.LoadInt64(&fullIterations)112 assert.True(t, interruptedBefore >= interruptedAfter-1,113 "too big of a difference %d >= %d - 1", interruptedBefore, interruptedAfter)114 assert.True(t, fullBefore+1 <= fullAfter,115 "too small of a difference %d + 1 <= %d", fullBefore, fullAfter)116 require.Equal(t, atomic.LoadInt64(&getVUCount), atomic.LoadInt64(&returnVUCount))117}118// this test is mostly interesting when -race is enabled119func TestVUHandleStartStopRace(t *testing.T) {120 t.Parallel()121 ctx, cancel := context.WithCancel(context.Background())122 defer cancel()123 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}124 testLog := logrus.New()125 testLog.AddHook(logHook)126 testLog.SetOutput(testutils.NewTestOutput(t))127 // testLog.Level = logrus.DebugLevel128 logEntry := logrus.NewEntry(testLog)129 runner := &minirunner.MiniRunner{}130 runner.Fn = func(ctx context.Context, _ *lib.State, out chan<- metrics.SampleContainer) error {131 return nil132 }133 var vuID uint64134 testIterations := 10000135 returned := make(chan struct{})136 getVU := func() (lib.InitializedVU, error) {137 returned = make(chan struct{})138 return runner.NewVU(atomic.AddUint64(&vuID, 1), 0, nil)139 }140 returnVU := func(v lib.InitializedVU) {141 require.Equal(t, atomic.LoadUint64(&vuID), v.(*minirunner.VU).ID)142 close(returned)143 }144 var interruptedIter int64145 var fullIterations int64146 runIter := func(ctx context.Context, vu lib.ActiveVU) bool {147 _ = vu.RunOnce()148 select {149 case <-ctx.Done():150 // Don't log errors or emit iterations metrics from cancelled iterations151 atomic.AddInt64(&interruptedIter, 1)152 return false153 default:154 atomic.AddInt64(&fullIterations, 1)155 return true156 }157 }158 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)159 go vuHandle.runLoopsIfPossible(runIter)160 for i := 0; i < testIterations; i++ {161 err := vuHandle.start()162 vuHandle.gracefulStop()163 require.NoError(t, err)164 select {165 case <-returned:166 case <-time.After(100 * time.Millisecond):167 go panic("returning took too long")168 time.Sleep(time.Second)169 }170 }171 vuHandle.hardStop() // STOP it172 time.Sleep(time.Millisecond * 5)173 interruptedBefore := atomic.LoadInt64(&interruptedIter)174 fullBefore := atomic.LoadInt64(&fullIterations)175 _ = vuHandle.start()176 time.Sleep(time.Millisecond * 50) // just to be sure an iteration will squeeze in177 cancel()178 time.Sleep(time.Millisecond * 5)179 interruptedAfter := atomic.LoadInt64(&interruptedIter)180 fullAfter := atomic.LoadInt64(&fullIterations)181 assert.True(t, interruptedBefore >= interruptedAfter-1,182 "too big of a difference %d >= %d - 1", interruptedBefore, interruptedAfter)183 assert.True(t, fullBefore+1 <= fullAfter,184 "too small of a difference %d + 1 <= %d", fullBefore, fullAfter)185}186type handleVUTest struct {187 runner *minirunner.MiniRunner188 getVUCount uint32189 returnVUCount uint32190 interruptedIter int64191 fullIterations int64192}193func (h *handleVUTest) getVU() (lib.InitializedVU, error) {194 return h.runner.NewVU(uint64(atomic.AddUint32(&h.getVUCount, 1)), 0, nil)195}196func (h *handleVUTest) returnVU(_ lib.InitializedVU) {197 atomic.AddUint32(&h.returnVUCount, 1)198}199func (h *handleVUTest) runIter(ctx context.Context, _ lib.ActiveVU) bool {200 select {201 case <-time.After(time.Second):202 case <-ctx.Done():203 }204 select {205 case <-ctx.Done():206 // Don't log errors or emit iterations metrics from cancelled iterations207 atomic.AddInt64(&h.interruptedIter, 1)208 return false209 default:210 atomic.AddInt64(&h.fullIterations, 1)211 return true212 }213}214func TestVUHandleSimple(t *testing.T) {215 t.Parallel()216 t.Run("start before gracefulStop finishes", func(t *testing.T) {217 t.Parallel()218 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}219 testLog := logrus.New()220 testLog.AddHook(logHook)221 testLog.SetOutput(testutils.NewTestOutput(t))222 // testLog.Level = logrus.DebugLevel223 logEntry := logrus.NewEntry(testLog)224 test := &handleVUTest{runner: &minirunner.MiniRunner{}}225 ctx, cancel := context.WithCancel(context.Background())226 defer cancel()227 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)228 var wg sync.WaitGroup229 wg.Add(1)230 go func() {231 defer wg.Done()232 vuHandle.runLoopsIfPossible(test.runIter)233 }()234 err := vuHandle.start()235 require.NoError(t, err)236 time.Sleep(time.Millisecond * 50)237 vuHandle.gracefulStop()238 // time.Sleep(time.Millisecond * 5) // No sleep as we want to always not return the VU239 err = vuHandle.start()240 require.NoError(t, err)241 time.Sleep(time.Millisecond * 1500)242 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))243 assert.EqualValues(t, 0, atomic.LoadUint32(&test.returnVUCount))244 assert.EqualValues(t, 0, atomic.LoadInt64(&test.interruptedIter))245 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))246 cancel()247 wg.Wait()248 time.Sleep(time.Millisecond * 5)249 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))250 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))251 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))252 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))253 })254 t.Run("start after gracefulStop finishes", func(t *testing.T) {255 t.Parallel()256 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}257 testLog := logrus.New()258 testLog.AddHook(logHook)259 testLog.SetOutput(testutils.NewTestOutput(t))260 // testLog.Level = logrus.DebugLevel261 logEntry := logrus.NewEntry(testLog)262 test := &handleVUTest{runner: &minirunner.MiniRunner{}}263 ctx, cancel := context.WithCancel(context.Background())264 defer cancel()265 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)266 var wg sync.WaitGroup267 wg.Add(1)268 go func() {269 defer wg.Done()270 vuHandle.runLoopsIfPossible(test.runIter)271 }()272 err := vuHandle.start()273 require.NoError(t, err)274 time.Sleep(time.Millisecond * 50)275 vuHandle.gracefulStop()276 time.Sleep(time.Millisecond * 1500)277 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))278 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))279 assert.EqualValues(t, 0, atomic.LoadInt64(&test.interruptedIter))280 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))281 err = vuHandle.start()282 require.NoError(t, err)283 time.Sleep(time.Millisecond * 1500)284 cancel()285 wg.Wait()286 time.Sleep(time.Millisecond * 50)287 assert.EqualValues(t, 2, atomic.LoadUint32(&test.getVUCount))288 assert.EqualValues(t, 2, atomic.LoadUint32(&test.returnVUCount))289 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))290 assert.EqualValues(t, 2, atomic.LoadInt64(&test.fullIterations))291 })292 t.Run("start after hardStop", func(t *testing.T) {293 t.Parallel()294 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}295 testLog := logrus.New()296 testLog.AddHook(logHook)297 testLog.SetOutput(testutils.NewTestOutput(t))298 // testLog.Level = logrus.DebugLevel299 logEntry := logrus.NewEntry(testLog)300 test := &handleVUTest{runner: &minirunner.MiniRunner{}}301 ctx, cancel := context.WithCancel(context.Background())302 defer cancel()303 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)304 var wg sync.WaitGroup305 wg.Add(1)306 go func() {307 defer wg.Done()308 vuHandle.runLoopsIfPossible(test.runIter)309 }()310 err := vuHandle.start()311 require.NoError(t, err)312 time.Sleep(time.Millisecond * 5)313 vuHandle.hardStop()314 time.Sleep(time.Millisecond * 15)315 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))316 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))317 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))318 assert.EqualValues(t, 0, atomic.LoadInt64(&test.fullIterations))319 err = vuHandle.start()320 require.NoError(t, err)321 time.Sleep(time.Millisecond * 1500)322 cancel()323 wg.Wait()324 time.Sleep(time.Millisecond * 5)325 assert.EqualValues(t, 2, atomic.LoadUint32(&test.getVUCount))326 assert.EqualValues(t, 2, atomic.LoadUint32(&test.returnVUCount))327 assert.EqualValues(t, 2, atomic.LoadInt64(&test.interruptedIter))328 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))329 })330}331func BenchmarkVUHandleIterations(b *testing.B) {332 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}333 testLog := logrus.New()334 testLog.AddHook(logHook)335 // testLog.Level = logrus.DebugLevel336 logEntry := logrus.NewEntry(testLog)337 var (338 getVUCount uint32339 returnVUCount uint32340 interruptedIter int64341 fullIterations int64342 )343 reset := func() {344 getVUCount = 0345 returnVUCount = 0346 interruptedIter = 0347 fullIterations = 0348 }349 runner := &minirunner.MiniRunner{}350 runner.Fn = func(ctx context.Context, _ *lib.State, out chan<- metrics.SampleContainer) error {351 return nil352 }353 getVU := func() (lib.InitializedVU, error) {354 return runner.NewVU(uint64(atomic.AddUint32(&getVUCount, 1)), 0, nil)355 }356 returnVU := func(_ lib.InitializedVU) {357 atomic.AddUint32(&returnVUCount, 1)358 }359 runIter := func(ctx context.Context, _ lib.ActiveVU) bool {360 // Do nothing361 select {362 case <-ctx.Done():363 // Don't log errors or emit iterations metrics from cancelled iterations364 atomic.AddInt64(&interruptedIter, 1)365 return false366 default:367 atomic.AddInt64(&fullIterations, 1)368 return true369 }370 }371 reset()372 ctx, cancel := context.WithCancel(context.Background())373 defer cancel()374 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)375 var wg sync.WaitGroup376 wg.Add(1)377 go func() {378 defer wg.Done()379 vuHandle.runLoopsIfPossible(runIter)380 }()381 start := time.Now()382 b.ResetTimer()383 err := vuHandle.start()384 require.NoError(b, err)385 time.Sleep(time.Second)386 cancel()387 wg.Wait()388 b.StopTimer()...

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1func (e *Executor) mockNextIterations() {2}3func (e *Executor) mockNextIterations() {4}5func (e *Executor) mockNextIterations() {6}7func (e *Executor) mockNextIterations() {8}9func (e *Executor) mockNextIterations() {10}11func (e *Executor) mockNextIterations() {12}13func (e *Executor) mockNextIterations() {14}15func (e *Executor) mockNextIterations() {16}17func (e *Executor) mockNextIterations() {18}19func (e *Executor) mockNextIterations() {20}21func (e *Executor) mockNextIterations() {22}23func (e *Executor) mockNextIterations() {

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1import (2func TestNextIterations(t *testing.T) {3}4import (5func TestNextIterations(t *testing.T) {6}

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1func main() {2 var executor = new(Executor)3 var mock = new(MockExecutor)4 mock.On("NextIterations", 1).Return([]int{2, 3})5 mock.On("NextIterations", 2).Return([]int{4, 5})6 mock.On("NextIterations", 3).Return([]int{6, 7})7 mock.On("NextIterations", 4).Return([]int{8, 9})8 mock.On("NextIterations", 5).Return([]int{10, 11})9 mock.On("NextIterations", 6).Return([]int{12, 13})10 mock.On("NextIterations", 7).Return([]int{14, 15})11 mock.On("NextIterations", 8).Return([]int{16, 17})12 mock.On("NextIterations", 9).Return([]int{18, 19})13 mock.On("NextIterations", 10).Return([]int{20, 21})14 mock.On("NextIterations", 11).Return([]int{22, 23})15 mock.On("NextIterations", 12).Return([]int{24, 25})16 mock.On("NextIterations", 13).Return([]int{26, 27})17 mock.On("NextIterations", 14).Return([]int{28, 29})18 mock.On("NextIterations", 15).Return([]int{30, 31})19 mock.On("NextIterations", 16).Return([]int{32, 33})20 mock.On("NextIterations", 17).Return([]int{34, 35})21 mock.On("NextIterations", 18).Return([]int{36, 37})22 mock.On("NextIterations", 19).Return([]int{38, 39})23 mock.On("NextIterations", 20).Return([]int{40, 41})24 mock.On("NextIterations", 21).Return([]int{42, 43})25 mock.On("NextIterations", 22).Return([]int{44, 45})26 mock.On("NextIterations", 23).Return([]int{46, 47})27 mock.On("NextIterations", 24).Return([]int{48, 49})28 mock.On("NextIterations", 25).Return([]int{50, 51})

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1func TestNextIterations(t *testing.T) {2}3func TestNextIterations(t *testing.T) {4}5func TestNextIterations(t *testing.T) {6}7func TestNextIterations(t *testing.T) {8}9func TestNextIterations(t *testing.T) {10}11func TestNextIterations(t *testing.T) {12}13func TestNextIterations(t *testing.T) {14}15func TestNextIterations(t *testing.T) {16}17func TestNextIterations(t *testing.T) {18}19func TestNextIterations(t *testing.T) {20}21func TestNextIterations(t *testing.T) {22}

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1import (2func TestNextIterations(t *testing.T) {3 executor := &ExecutorMock{}4 executor.On("NextIterations", 2).Return([]int{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024})5 result := executor.NextIterations(2)6 if !reflect.DeepEqual(result, []int{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}) {7 t.Errorf("Expected 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 but got %v", result)8 }9}10type ExecutorMock struct {11}12func (e *ExecutorMock) NextIterations(n int) []int {13 args := e.Called(n)14 return args.Get(0).([]int)15}16import "fmt"17type Executor struct {18}19func (e *Executor) NextIterations(n int) []int {20 fmt.Println("Executing NextIterations")21 for i := 1; i <= 10; i++ {22 result = append(result, n*i)23 }24}25import "fmt"26type Executor struct {27}28func (e *Executor) NextIterations(n int) []int {29 fmt.Println("Executing NextIterations")30 for i := 1; i <= 10; i++ {31 result = append(result, n*i)32 }33}34import "fmt"35type Executor struct {36}37func (e *Executor) Next

Full Screen

Full Screen

mockNextIterations

Using AI Code Generation

copy

Full Screen

1func TestNextIterations(t *testing.T) {2 mockNextIterations := func() {3 fmt.Println("mocked next iterations")4 }5 executor.NextIterations()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.

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