Best K6 code snippet using cmd.verifySharedIters
config_consolidation_test.go
Source:config_consolidation_test.go
...41 require.True(t, ok)42 assert.Equal(t, null.NewInt(1, false), perVuIters.Iterations)43 assert.Equal(t, null.NewInt(1, false), perVuIters.VUs)44}45func verifySharedIters(vus, iters null.Int) func(t *testing.T, c Config) {46 return func(t *testing.T, c Config) {47 exec := c.Scenarios[lib.DefaultScenarioName]48 require.NotEmpty(t, exec)49 require.IsType(t, executor.SharedIterationsConfig{}, exec)50 sharedIterConfig, ok := exec.(executor.SharedIterationsConfig)51 require.True(t, ok)52 assert.Equal(t, vus, sharedIterConfig.VUs)53 assert.Equal(t, iters, sharedIterConfig.Iterations)54 assert.Equal(t, vus, c.VUs)55 assert.Equal(t, iters, c.Iterations)56 }57}58func verifyConstLoopingVUs(vus null.Int, duration time.Duration) func(t *testing.T, c Config) {59 return func(t *testing.T, c Config) {60 exec := c.Scenarios[lib.DefaultScenarioName]61 require.NotEmpty(t, exec)62 require.IsType(t, executor.ConstantVUsConfig{}, exec)63 clvc, ok := exec.(executor.ConstantVUsConfig)64 require.True(t, ok)65 assert.Equal(t, vus, clvc.VUs)66 assert.Equal(t, types.NullDurationFrom(duration), clvc.Duration)67 assert.Equal(t, vus, c.VUs)68 assert.Equal(t, types.NullDurationFrom(duration), c.Duration)69 }70}71func verifyExternallyExecuted(scenarioName string, vus null.Int, duration time.Duration) func(t *testing.T, c Config) {72 return func(t *testing.T, c Config) {73 exec := c.Scenarios[scenarioName]74 require.NotEmpty(t, exec)75 require.IsType(t, executor.ExternallyControlledConfig{}, exec)76 ecc, ok := exec.(executor.ExternallyControlledConfig)77 require.True(t, ok)78 assert.Equal(t, vus, ecc.VUs)79 assert.Equal(t, types.NullDurationFrom(duration), ecc.Duration)80 assert.Equal(t, vus, ecc.MaxVUs) // MaxVUs defaults to VUs unless specified81 }82}83func verifyRampingVUs(startVus null.Int, stages []executor.Stage) func(t *testing.T, c Config) {84 return func(t *testing.T, c Config) {85 exec := c.Scenarios[lib.DefaultScenarioName]86 require.NotEmpty(t, exec)87 require.IsType(t, executor.RampingVUsConfig{}, exec)88 clvc, ok := exec.(executor.RampingVUsConfig)89 require.True(t, ok)90 assert.Equal(t, startVus, clvc.StartVUs)91 assert.Equal(t, startVus, c.VUs)92 assert.Equal(t, stages, clvc.Stages)93 assert.Len(t, c.Stages, len(stages))94 for i, s := range stages {95 assert.Equal(t, s.Duration, c.Stages[i].Duration)96 assert.Equal(t, s.Target, c.Stages[i].Target)97 }98 }99}100// A helper function that accepts (duration in second, VUs) pairs and returns101// a valid slice of stage structs102func buildStages(durationsAndVUs ...int64) []executor.Stage {103 l := len(durationsAndVUs)104 if l%2 != 0 {105 panic("wrong len")106 }107 result := make([]executor.Stage, 0, l/2)108 for i := 0; i < l; i += 2 {109 result = append(result, executor.Stage{110 Duration: types.NullDurationFrom(time.Duration(durationsAndVUs[i]) * time.Second),111 Target: null.IntFrom(durationsAndVUs[i+1]),112 })113 }114 return result115}116type file struct {117 filepath, contents string118}119func getFS(files []file) afero.Fs {120 fs := afero.NewMemMapFs()121 for _, f := range files {122 must(afero.WriteFile(fs, f.filepath, []byte(f.contents), 0o644)) // modes don't matter in the afero.MemMapFs123 }124 return fs125}126type opts struct {127 cli []string128 env []string129 runner *lib.Options130 fs afero.Fs131 cmds []string132}133// exp contains the different events or errors we expect our test case to trigger.134// for space and clarity, we use the fact that by default, all of the struct values are false135type exp struct {136 cliParseError bool137 cliReadError bool138 consolidationError bool // Note: consolidationError includes validation errors from envconfig.Process()139 derivationError bool140 validationErrors bool141 logWarning bool142}143// A hell of a complicated test case, that still doesn't test things fully...144type configConsolidationTestCase struct {145 options opts146 expected exp147 customValidator func(t *testing.T, c Config)148}149func getConfigConsolidationTestCases() []configConsolidationTestCase {150 defaultConfig := func(jsonConfig string) afero.Fs {151 return getFS([]file{{152 filepath.Join(".config", "loadimpact", "k6", defaultConfigFileName), // TODO: improve153 jsonConfig,154 }})155 }156 I := null.IntFrom // shortcut for "Valid" (i.e. user-specified) ints157 // This is a function, because some of these test cases actually need for the init() functions158 // to be executed, since they depend on defaultConfigFilePath159 return []configConsolidationTestCase{160 // Check that no options will result in 1 VU 1 iter value for execution161 {opts{}, exp{}, verifyOneIterPerOneVU},162 // Verify some CLI errors163 {opts{cli: []string{"--blah", "blah"}}, exp{cliParseError: true}, nil},164 {opts{cli: []string{"--duration", "blah"}}, exp{cliParseError: true}, nil},165 {opts{cli: []string{"--duration", "1000"}}, exp{cliParseError: true}, nil}, // intentionally unsupported166 {opts{cli: []string{"--iterations", "blah"}}, exp{cliParseError: true}, nil},167 {opts{cli: []string{"--execution", ""}}, exp{cliParseError: true}, nil},168 {opts{cli: []string{"--stage", "10:20s"}}, exp{cliReadError: true}, nil},169 {opts{cli: []string{"--stage", "1000:20"}}, exp{cliReadError: true}, nil}, // intentionally unsupported170 // Check if CLI shortcuts generate correct execution values171 {opts{cli: []string{"--vus", "1", "--iterations", "5"}}, exp{}, verifySharedIters(I(1), I(5))},172 {opts{cli: []string{"-u", "2", "-i", "6"}}, exp{}, verifySharedIters(I(2), I(6))},173 {opts{cli: []string{"-d", "123s"}}, exp{}, verifyConstLoopingVUs(null.NewInt(1, false), 123*time.Second)},174 {opts{cli: []string{"-u", "3", "-d", "30s"}}, exp{}, verifyConstLoopingVUs(I(3), 30*time.Second)},175 {opts{cli: []string{"-u", "4", "--duration", "60s"}}, exp{}, verifyConstLoopingVUs(I(4), 1*time.Minute)},176 {177 opts{cli: []string{"--stage", "20s:10", "-s", "3m:5"}},178 exp{},179 verifyRampingVUs(null.NewInt(1, false), buildStages(20, 10, 180, 5)),180 },181 {182 opts{cli: []string{"-s", "1m6s:5", "--vus", "10"}},183 exp{},184 verifyRampingVUs(null.NewInt(10, true), buildStages(66, 5)),185 },186 {opts{cli: []string{"-u", "1", "-i", "6", "-d", "10s"}}, exp{}, func(t *testing.T, c Config) {187 verifySharedIters(I(1), I(6))(t, c)188 sharedIterConfig, ok := c.Scenarios[lib.DefaultScenarioName].(executor.SharedIterationsConfig)189 require.True(t, ok)190 assert.Equal(t, sharedIterConfig.MaxDuration.TimeDuration(), 10*time.Second)191 }},192 // This should get a validation error since VUs are more than the shared iterations193 {opts{cli: []string{"--vus", "10", "-i", "6"}}, exp{validationErrors: true}, verifySharedIters(I(10), I(6))},194 {opts{cli: []string{"-s", "10s:5", "-s", "10s:"}}, exp{validationErrors: true}, nil},195 {opts{fs: defaultConfig(`{"stages": [{"duration": "20s"}], "vus": 10}`)}, exp{validationErrors: true}, nil},196 // These should emit a derivation error197 {opts{cli: []string{"-u", "2", "-d", "10s", "-s", "10s:20"}}, exp{derivationError: true}, nil},198 {opts{cli: []string{"-u", "3", "-i", "5", "-s", "10s:20"}}, exp{derivationError: true}, nil},199 {opts{cli: []string{"-u", "3", "-d", "0"}}, exp{derivationError: true}, nil},200 {201 opts{runner: &lib.Options{202 VUs: null.IntFrom(5),203 Duration: types.NullDurationFrom(44 * time.Second),204 Stages: []lib.Stage{205 {Duration: types.NullDurationFrom(3 * time.Second), Target: I(20)},206 },207 }}, exp{derivationError: true}, nil,208 },209 {opts{fs: defaultConfig(`{"scenarios": {}}`)}, exp{logWarning: true}, verifyOneIterPerOneVU},210 // Test if environment variable shortcuts are working as expected211 {opts{env: []string{"K6_VUS=5", "K6_ITERATIONS=15"}}, exp{}, verifySharedIters(I(5), I(15))},212 {opts{env: []string{"K6_VUS=10", "K6_DURATION=20s"}}, exp{}, verifyConstLoopingVUs(I(10), 20*time.Second)},213 {opts{env: []string{"K6_VUS=10", "K6_DURATION=10000"}}, exp{}, verifyConstLoopingVUs(I(10), 10*time.Second)},214 {215 opts{env: []string{"K6_STAGES=2m30s:11,1h1m:100"}},216 exp{},217 verifyRampingVUs(null.NewInt(1, false), buildStages(150, 11, 3660, 100)),218 },219 {220 opts{env: []string{"K6_STAGES=100s:100,0m30s:0", "K6_VUS=0"}},221 exp{},222 verifyRampingVUs(null.NewInt(0, true), buildStages(100, 100, 30, 0)),223 },224 {opts{env: []string{"K6_STAGES=1000:100"}}, exp{consolidationError: true}, nil}, // intentionally unsupported225 // Test if JSON configs work as expected226 {opts{fs: defaultConfig(`{"iterations": 77, "vus": 7}`)}, exp{}, verifySharedIters(I(7), I(77))},227 {opts{fs: defaultConfig(`wrong-json`)}, exp{consolidationError: true}, nil},228 {opts{fs: getFS(nil), cli: []string{"--config", "/my/config.file"}}, exp{consolidationError: true}, nil},229 // Test combinations between options and levels230 {opts{cli: []string{"--vus", "1"}}, exp{}, verifyOneIterPerOneVU},231 {opts{cli: []string{"--vus", "10"}}, exp{logWarning: true}, verifyOneIterPerOneVU},232 {233 opts{234 fs: getFS([]file{{"/my/config.file", `{"vus": 8, "duration": "2m"}`}}),235 cli: []string{"--config", "/my/config.file"},236 }, exp{}, verifyConstLoopingVUs(I(8), 120*time.Second),237 },238 {239 opts{240 fs: getFS([]file{{"/my/config.file", `{"duration": 20000}`}}),241 cli: []string{"--config", "/my/config.file"},242 }, exp{}, verifyConstLoopingVUs(null.NewInt(1, false), 20*time.Second),243 },244 {245 opts{246 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 20}], "vus": 10}`),247 env: []string{"K6_DURATION=15s"},248 cli: []string{"--stage", ""},249 },250 exp{logWarning: true},251 verifyOneIterPerOneVU,252 },253 {254 opts{255 runner: &lib.Options{VUs: null.IntFrom(5), Duration: types.NullDurationFrom(50 * time.Second)},256 cli: []string{"--stage", "5s:5"},257 },258 exp{},259 verifyRampingVUs(I(5), buildStages(5, 5)),260 },261 {262 opts{263 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),264 runner: &lib.Options{VUs: null.IntFrom(5)},265 },266 exp{},267 verifyRampingVUs(I(5), buildStages(20, 10)),268 },269 {270 opts{271 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),272 runner: &lib.Options{VUs: null.IntFrom(5)},273 env: []string{"K6_VUS=15", "K6_ITERATIONS=17"},274 },275 exp{},276 verifySharedIters(I(15), I(17)),277 },278 {279 opts{280 fs: defaultConfig(`{"stages": [{"duration": "11s", "target": 11}]}`),281 runner: &lib.Options{VUs: null.IntFrom(22)},282 env: []string{"K6_VUS=33"},283 cli: []string{"--stage", "44s:44", "-s", "55s:55"},284 },285 exp{},286 verifyRampingVUs(null.NewInt(33, true), buildStages(44, 44, 55, 55)),287 },288 // TODO: test the future full overwriting of the duration/iterations/stages/execution options289 {290 opts{291 fs: defaultConfig(`{292 "scenarios": { "someKey": {293 "executor": "constant-vus", "vus": 10, "duration": "60s", "gracefulStop": "10s",294 "startTime": "70s", "env": {"test": "mest"}, "exec": "someFunc"295 }}}`),296 env: []string{"K6_ITERATIONS=25"},297 cli: []string{"--vus", "12"},298 },299 exp{},300 verifySharedIters(I(12), I(25)),301 },302 {303 opts{304 fs: defaultConfig(`{"scenarios": { "foo": {305 "executor": "constant-vus", "vus": 2, "duration": "1d",306 "gracefulStop": "10000", "startTime": 1000.5307 }}}`),308 }, exp{}, func(t *testing.T, c Config) {309 exec := c.Scenarios["foo"]310 require.NotEmpty(t, exec)311 require.IsType(t, executor.ConstantVUsConfig{}, exec)312 clvc, ok := exec.(executor.ConstantVUsConfig)313 require.True(t, ok)314 assert.Equal(t, null.IntFrom(2), clvc.VUs)...
verifySharedIters
Using AI Code Generation
1func main() {2 cmd := &cmd{}3 cmd.verifySharedIters()4}5func main() {6 cmd := &cmd{}7 cmd.verifySharedIters()8}9func main() {10 cmd := &cmd{}11 cmd.verifySharedIters()12}13func main() {14 cmd := &cmd{}15 cmd.verifySharedIters()16}17func main() {18 cmd := &cmd{}19 cmd.verifySharedIters()20}21func main() {22 cmd := &cmd{}23 cmd.verifySharedIters()24}25func main() {26 cmd := &cmd{}27 cmd.verifySharedIters()28}29func main() {30 cmd := &cmd{}31 cmd.verifySharedIters()32}33func main() {34 cmd := &cmd{}35 cmd.verifySharedIters()36}37func main() {38 cmd := &cmd{}39 cmd.verifySharedIters()40}41func main() {42 cmd := &cmd{}43 cmd.verifySharedIters()44}45func main() {46 cmd := &cmd{}47 cmd.verifySharedIters()48}49func main() {50 cmd := &cmd{}51 cmd.verifySharedIters()52}53func main() {
verifySharedIters
Using AI Code Generation
1import (2type cmd struct {3}4func (c *cmd) verifySharedIters() {5 sort.Ints(c.sharedIters)6 for i := 1; i < len(c.sharedIters); i++ {7 if c.sharedIters[i] == c.sharedIters[i-1] {8 fmt.Println("Error: shared iterator", c.sharedIters[i], "is repeated")9 os.Exit(1)10 }11 }12 if c.sharedIters[len(c.sharedIters)-1] > len(c.sharedIters) {13 fmt.Println("Error: shared iterator", c.sharedIters[len(c.sharedIters)-1], "is greater than the number of iters")14 os.Exit(1)15 }16}17func main() {18 c := cmd{}19 scanner := bufio.NewScanner(os.Stdin)20 for scanner.Scan() {21 s = scanner.Text()22 }23 split := strings.Split(s, " ")24 for _, v := range split {25 i, _ := strconv.Atoi(v)26 c.sharedIters = append(c.sharedIters, i)27 }28 c.verifySharedIters()29}30if c.sharedIters[len(c.sharedIters)-1] > len(c.sharedIters) {31type A struct {32}33func (a *A) DoSomething
verifySharedIters
Using AI Code Generation
1import (2func main() {3 debug.SetGCPercent(-1)4 runtime.GOMAXPROCS(2)5 runtime.GC()6 verifySharedIters()7}8func verifySharedIters() {9}10import (11func main() {12 debug.SetGCPercent(-1)13 runtime.GOMAXPROCS(2)14 runtime.GC()15 verifySharedIters()16}17func verifySharedIters() {18}19import (20func main() {21 debug.SetGCPercent(-1)22 runtime.GOMAXPROCS(2)23 runtime.GC()24 verifySharedIters()25}26func verifySharedIters() {27}28import (29func main() {
verifySharedIters
Using AI Code Generation
1import (2func main() {3 cmd := newCmd()4 cmd.verifySharedIters()5 fmt.Println("Hello World!")6}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!