Best K6 code snippet using cmd.verifyConstLoopingVUs
config_consolidation_test.go
Source:config_consolidation_test.go
...90 assert.Equal(t, vus, c.VUs)91 assert.Equal(t, iters, c.Iterations)92 }93}94func verifyConstLoopingVUs(vus null.Int, duration time.Duration) func(t *testing.T, c Config) {95 return func(t *testing.T, c Config) {96 sched := c.Execution[lib.DefaultSchedulerName]97 require.NotEmpty(t, sched)98 require.IsType(t, scheduler.ConstantLoopingVUsConfig{}, sched)99 clvc, ok := sched.(scheduler.ConstantLoopingVUsConfig)100 require.True(t, ok)101 assert.Equal(t, vus, clvc.VUs)102 assert.Equal(t, types.NullDurationFrom(duration), clvc.Duration)103 assert.Equal(t, vus, c.VUs)104 assert.Equal(t, types.NullDurationFrom(duration), c.Duration)105 }106}107func verifyVarLoopingVUs(startVus null.Int, stages []scheduler.Stage) func(t *testing.T, c Config) {108 return func(t *testing.T, c Config) {109 sched := c.Execution[lib.DefaultSchedulerName]110 require.NotEmpty(t, sched)111 require.IsType(t, scheduler.VariableLoopingVUsConfig{}, sched)112 clvc, ok := sched.(scheduler.VariableLoopingVUsConfig)113 require.True(t, ok)114 assert.Equal(t, startVus, clvc.StartVUs)115 assert.Equal(t, startVus, c.VUs)116 assert.Equal(t, stages, clvc.Stages)117 assert.Len(t, c.Stages, len(stages))118 for i, s := range stages {119 assert.Equal(t, s.Duration, c.Stages[i].Duration)120 assert.Equal(t, s.Target, c.Stages[i].Target)121 }122 }123}124// A helper function that accepts (duration in second, VUs) pairs and returns125// a valid slice of stage structs126func buildStages(durationsAndVUs ...int64) []scheduler.Stage {127 l := len(durationsAndVUs)128 if l%2 != 0 {129 panic("wrong len")130 }131 result := make([]scheduler.Stage, 0, l/2)132 for i := 0; i < l; i += 2 {133 result = append(result, scheduler.Stage{134 Duration: types.NullDurationFrom(time.Duration(durationsAndVUs[i]) * time.Second),135 Target: null.IntFrom(durationsAndVUs[i+1]),136 })137 }138 return result139}140func mostFlagSets() []flagSetInit {141 //TODO: make this unnecessary... currently these are the only commands in which142 // getConsolidatedConfig() is used, but they also have differences in their CLI flags :/143 // sigh... compromises...144 result := []flagSetInit{}145 for i, fsi := range []flagSetInit{runCmdFlagSet, archiveCmdFlagSet, cloudCmdFlagSet} {146 i, fsi := i, fsi // go...147 result = append(result, func() *pflag.FlagSet {148 flags := pflag.NewFlagSet(fmt.Sprintf("superContrivedFlags_%d", i), pflag.ContinueOnError)149 flags.AddFlagSet(rootCmdPersistentFlagSet())150 flags.AddFlagSet(fsi())151 return flags152 })153 }154 return result155}156type file struct {157 filepath, contents string158}159func getFS(files []file) afero.Fs {160 fs := afero.NewMemMapFs()161 for _, f := range files {162 must(afero.WriteFile(fs, f.filepath, []byte(f.contents), 0644)) // modes don't matter in the afero.MemMapFs163 }164 return fs165}166func defaultConfig(jsonConfig string) afero.Fs {167 return getFS([]file{{defaultConfigFilePath, jsonConfig}})168}169type flagSetInit func() *pflag.FlagSet170type opts struct {171 cli []string172 env []string173 runner *lib.Options174 fs afero.Fs175 //TODO: remove this when the configuration is more reproducible and sane...176 // We use a func, because initializing a FlagSet that points to variables177 // actually will change those variables to their default values :| In our178 // case, this happens only some of the time, for global variables that179 // are configurable only via CLI flags, but not environment variables.180 //181 // For the rest, their default value is their current value, since that182 // has been set from the environment variable. That has a bunch of other183 // issues on its own, and the func() doesn't help at all, and we need to184 // use the resetStickyGlobalVars() hack on top of that...185 cliFlagSetInits []flagSetInit186}187func resetStickyGlobalVars() {188 //TODO: remove after fixing the config, obviously a dirty hack189 exitOnRunning = false190 configFilePath = ""191 runType = ""192 runNoSetup = false193 runNoTeardown = false194}195// Something that makes the test also be a valid io.Writer, useful for passing it196// as an output for logs and CLI flag help messages...197type testOutput struct{ *testing.T }198func (to testOutput) Write(p []byte) (n int, err error) {199 to.Logf("%s", p)200 return len(p), nil201}202var _ io.Writer = testOutput{}203// exp contains the different events or errors we expect our test case to trigger.204// for space and clarity, we use the fact that by default, all of the struct values are false205type exp struct {206 cliParseError bool207 cliReadError bool208 consolidationError bool209 derivationError bool210 validationErrors bool211 logWarning bool //TODO: remove in the next version?212}213// A hell of a complicated test case, that still doesn't test things fully...214type configConsolidationTestCase struct {215 options opts216 expected exp217 customValidator func(t *testing.T, c Config)218}219func getConfigConsolidationTestCases() []configConsolidationTestCase {220 I := null.IntFrom // shortcut for "Valid" (i.e. user-specified) ints221 // This is a function, because some of these test cases actually need for the init() functions222 // to be executed, since they depend on defaultConfigFilePath223 return []configConsolidationTestCase{224 // Check that no options will result in 1 VU 1 iter value for execution225 {opts{}, exp{}, verifyOneIterPerOneVU},226 // Verify some CLI errors227 {opts{cli: []string{"--blah", "blah"}}, exp{cliParseError: true}, nil},228 {opts{cli: []string{"--duration", "blah"}}, exp{cliParseError: true}, nil},229 {opts{cli: []string{"--iterations", "blah"}}, exp{cliParseError: true}, nil},230 {opts{cli: []string{"--execution", ""}}, exp{cliParseError: true}, nil},231 {opts{cli: []string{"--stage", "10:20s"}}, exp{cliReadError: true}, nil},232 // Check if CLI shortcuts generate correct execution values233 {opts{cli: []string{"--vus", "1", "--iterations", "5"}}, exp{}, verifySharedIters(I(1), I(5))},234 {opts{cli: []string{"-u", "2", "-i", "6"}}, exp{}, verifySharedIters(I(2), I(6))},235 {opts{cli: []string{"-d", "123s"}}, exp{}, verifyConstLoopingVUs(null.NewInt(1, false), 123*time.Second)},236 {opts{cli: []string{"-u", "3", "-d", "30s"}}, exp{}, verifyConstLoopingVUs(I(3), 30*time.Second)},237 {opts{cli: []string{"-u", "4", "--duration", "60s"}}, exp{}, verifyConstLoopingVUs(I(4), 1*time.Minute)},238 {239 opts{cli: []string{"--stage", "20s:10", "-s", "3m:5"}}, exp{},240 verifyVarLoopingVUs(null.NewInt(1, false), buildStages(20, 10, 180, 5)),241 },242 {243 opts{cli: []string{"-s", "1m6s:5", "--vus", "10"}}, exp{},244 verifyVarLoopingVUs(null.NewInt(10, true), buildStages(66, 5)),245 },246 {opts{cli: []string{"-u", "1", "-i", "6", "-d", "10s"}}, exp{}, func(t *testing.T, c Config) {247 verifySharedIters(I(1), I(6))(t, c)248 sharedIterConfig := c.Execution[lib.DefaultSchedulerName].(scheduler.SharedIteationsConfig)249 assert.Equal(t, time.Duration(sharedIterConfig.MaxDuration.Duration), 10*time.Second)250 }},251 // This should get a validation error since VUs are more than the shared iterations252 {opts{cli: []string{"--vus", "10", "-i", "6"}}, exp{validationErrors: true}, verifySharedIters(I(10), I(6))},253 {opts{cli: []string{"-s", "10s:5", "-s", "10s:"}}, exp{validationErrors: true}, nil},254 {opts{fs: defaultConfig(`{"stages": [{"duration": "20s"}], "vus": 10}`)}, exp{validationErrors: true}, nil},255 // These should emit a warning256 //TODO: in next version, those should be an error257 {opts{cli: []string{"-u", "2", "-d", "10s", "-s", "10s:20"}}, exp{logWarning: true}, nil},258 {opts{cli: []string{"-u", "3", "-i", "5", "-s", "10s:20"}}, exp{logWarning: true}, nil},259 {opts{cli: []string{"-u", "3", "-d", "0"}}, exp{logWarning: true}, nil},260 {261 opts{runner: &lib.Options{262 VUs: null.IntFrom(5),263 Duration: types.NullDurationFrom(44 * time.Second),264 Stages: []lib.Stage{265 {Duration: types.NullDurationFrom(3 * time.Second), Target: I(20)},266 },267 }}, exp{logWarning: true}, nil,268 },269 {opts{fs: defaultConfig(`{"execution": {}}`)}, exp{logWarning: true}, verifyOneIterPerOneVU},270 // Test if environment variable shortcuts are working as expected271 {opts{env: []string{"K6_VUS=5", "K6_ITERATIONS=15"}}, exp{}, verifySharedIters(I(5), I(15))},272 {opts{env: []string{"K6_VUS=10", "K6_DURATION=20s"}}, exp{}, verifyConstLoopingVUs(I(10), 20*time.Second)},273 {274 opts{env: []string{"K6_STAGES=2m30s:11,1h1m:100"}}, exp{},275 verifyVarLoopingVUs(null.NewInt(1, false), buildStages(150, 11, 3660, 100)),276 },277 {278 opts{env: []string{"K6_STAGES=100s:100,0m30s:0", "K6_VUS=0"}}, exp{},279 verifyVarLoopingVUs(null.NewInt(0, true), buildStages(100, 100, 30, 0)),280 },281 // Test if JSON configs work as expected282 {opts{fs: defaultConfig(`{"iterations": 77, "vus": 7}`)}, exp{}, verifySharedIters(I(7), I(77))},283 {opts{fs: defaultConfig(`wrong-json`)}, exp{consolidationError: true}, nil},284 {opts{fs: getFS(nil), cli: []string{"--config", "/my/config.file"}}, exp{consolidationError: true}, nil},285 // Test combinations between options and levels286 {287 opts{288 fs: getFS([]file{{"/my/config.file", `{"vus": 8, "duration": "2m"}`}}),289 cli: []string{"--config", "/my/config.file"},290 }, exp{}, verifyConstLoopingVUs(I(8), 120*time.Second),291 },292 {293 opts{294 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 20}], "vus": 10}`),295 env: []string{"K6_DURATION=15s"},296 cli: []string{"--stage", ""},297 },298 exp{}, verifyConstLoopingVUs(I(10), 15*time.Second),299 },300 {301 opts{302 runner: &lib.Options{VUs: null.IntFrom(5), Duration: types.NullDurationFrom(50 * time.Second)},303 cli: []string{"--stage", "5s:5"},304 },305 //TODO: this shouldn't be a warning in the next version, but the result will be different306 exp{logWarning: true}, verifyConstLoopingVUs(I(5), 50*time.Second),307 },308 {309 opts{310 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),311 runner: &lib.Options{VUs: null.IntFrom(5)},312 },313 exp{},314 verifyVarLoopingVUs(null.NewInt(5, true), buildStages(20, 10)),315 },316 {317 opts{318 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),319 runner: &lib.Options{VUs: null.IntFrom(5)},320 env: []string{"K6_VUS=15", "K6_ITERATIONS=15"},321 },322 exp{logWarning: true}, //TODO: this won't be a warning in the next version, but the result will be different323 verifySharedIters(I(15), I(15)),324 },325 {326 opts{327 fs: defaultConfig(`{"stages": [{"duration": "11s", "target": 11}]}`),328 runner: &lib.Options{VUs: null.IntFrom(22)},329 env: []string{"K6_VUS=33"},330 cli: []string{"--stage", "44s:44", "-s", "55s:55"},331 },332 exp{},333 verifyVarLoopingVUs(null.NewInt(33, true), buildStages(44, 44, 55, 55)),334 },335 //TODO: test the future full overwriting of the duration/iterations/stages/execution options336 {337 opts{338 fs: defaultConfig(`{339 "execution": { "someKey": {340 "type": "constant-looping-vus", "vus": 10, "duration": "60s", "interruptible": false,341 "iterationTimeout": "10s", "startTime": "70s", "env": {"test": "mest"}, "exec": "someFunc"342 }}}`),343 env: []string{"K6_ITERATIONS=25"},344 cli: []string{"--vus", "12"},345 },346 exp{}, verifySharedIters(I(12), I(25)),347 },348 {349 opts{350 fs: defaultConfig(`351 {352 "execution": {353 "default": {354 "type": "constant-looping-vus",355 "vus": 10,356 "duration": "60s"357 }358 },359 "vus": 10,360 "duration": "60s"361 }`,362 ),363 },364 exp{}, verifyConstLoopingVUs(I(10), 60*time.Second),365 },366 // Just in case, verify that no options will result in the same 1 vu 1 iter config367 {opts{}, exp{}, verifyOneIterPerOneVU},368 // Test system tags369 {opts{}, exp{}, func(t *testing.T, c Config) {370 assert.Equal(t, &stats.DefaultSystemTagSet, c.Options.SystemTags)371 }},372 {opts{cli: []string{"--system-tags", `""`}}, exp{}, func(t *testing.T, c Config) {373 assert.Equal(t, stats.SystemTagSet(0), *c.Options.SystemTags)374 }},375 {376 opts{runner: &lib.Options{377 SystemTags: stats.NewSystemTagSet(stats.TagSubproto, stats.TagURL)},378 },...
verifyConstLoopingVUs
Using AI Code Generation
1import (2func main() {3 sched := lib.NewExecutionScheduler()4 executor := executor.NewConstantLoopingVUsConfig()5 executor.Duration = types.NullDurationFrom(10 * time.Second)6 executor.VUs = null.IntFrom(10)7 executor.VUsMax = null.IntFrom(10)
verifyConstLoopingVUs
Using AI Code Generation
1func main() {2 cmd := &cmd{}3 cmd.verifyConstLoopingVUs()4}5func main() {6 cmd := &cmd{}7 cmd.verifyConstLoopingVUs()8}9func main() {10 cmd := &cmd{}11 cmd.verifyConstLoopingVUs()12}13func main() {14 cmd := &cmd{}15 cmd.verifyConstLoopingVUs()16}17func main() {18 cmd := &cmd{}19 cmd.verifyConstLoopingVUs()20}21func main() {22 cmd := &cmd{}23 cmd.verifyConstLoopingVUs()24}25func main() {26 cmd := &cmd{}27 cmd.verifyConstLoopingVUs()28}29func main() {30 cmd := &cmd{}31 cmd.verifyConstLoopingVUs()32}33func main() {34 cmd := &cmd{}35 cmd.verifyConstLoopingVUs()36}37func main() {38 cmd := &cmd{}39 cmd.verifyConstLoopingVUs()40}41func main() {42 cmd := &cmd{}43 cmd.verifyConstLoopingVUs()44}45func main() {46 cmd := &cmd{}47 cmd.verifyConstLoopingVUs()48}
verifyConstLoopingVUs
Using AI Code Generation
1func (c *cmd) verifyConstLoopingVUs() error {2 if c.options.LoopVUs && c.options.LoopVUs.Int64 > 0 && c.options.VUs.Int64 == 0 {3 return errors.New("the number of VUs to run in loop mode must be greater than 0")4 }5}6func (c *cmd) verifyConstLoopingVUs() error {7 if c.options.LoopVUs && c.options.LoopVUs.Int64 > 0 && c.options.VUs.Int64 == 0 {8 return errors.New("the number of VUs to run in loop mode must be greater than 0")9 }10}11func (c *cmd) verifyConstLoopingVUs() error {12 if c.options.LoopVUs && c.options.LoopVUs.Int64 > 0 && c.options.VUs.Int64 == 0 {13 return errors.New("the number of VUs to run in loop mode must be greater than 0")14 }15}16func (c *cmd) verifyConstLoopingVUs() error {17 if c.options.LoopVUs && c.options.LoopVUs.Int64 > 0 && c.options.VUs.Int64 == 0 {18 return errors.New("the number of VUs to run in loop mode must be greater than 0")19 }20}21func (c *cmd) verifyConstLoopingVUs() error {22 if c.options.LoopVUs && c.options.LoopVUs.Int64 > 0 && c.options.VUs.Int64 == 0 {23 return errors.New("the number of VUs to run in loop mode must be greater than 0")24 }25}26func (c *cmd) verifyConstLoopingVUs() error {
verifyConstLoopingVUs
Using AI Code Generation
1func (cmd *cmd) verifyConstLoopingVUs() {2 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {3 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")4 }5}6func (cmd *cmd) verifyConstLoopingVUs() {7 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {8 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")9 }10}11func (cmd *cmd) verifyConstLoopingVUs() {12 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {13 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")14 }15}16func (cmd *cmd) verifyConstLoopingVUs() {17 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {18 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")19 }20}21func (cmd *cmd) verifyConstLoopingVUs() {22 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {23 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")24 }25}26func (cmd *cmd) verifyConstLoopingVUs() {27 if cmd.options.Iterations != nil && cmd.options.Iterations.Int64 > 0 && cmd.options.VUs.Int64 > 0 {28 cmd.Logger.Warn("Using iterations with the constant-looping-vus executor is not recommended")
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!!