Best K6 code snippet using executor.changeState
vu_handle.go
Source:vu_handle.go
...124 return nil // nothing to do125 case toGracefulStop: // we raced with the loop, lets not return the vu just to get it back126 vh.logger.Debug("Start")127 close(vh.canStartIter)128 vh.changeState(running)129 case stopped, toHardStop: // we need to reactivate the VU and remake the context for it130 vh.logger.Debug("Start")131 vh.initVU, err = vh.getVU()132 if err != nil {133 return err134 }135 vh.activeVU = vh.initVU.Activate(getVUActivationParams(136 vh.ctx, *vh.config, vh.returnVU, vh.nextIterationCounters))137 close(vh.canStartIter)138 vh.changeState(starting)139 }140 return nil141}142// just a helper function for debugging143func (vh *vuHandle) changeState(newState stateType) {144 // vh.stateH = append(vh.stateH, newState)145 atomic.StoreInt32((*int32)(&vh.state), int32(newState))146}147func (vh *vuHandle) gracefulStop() {148 vh.mutex.Lock()149 defer vh.mutex.Unlock()150 switch vh.state {151 case toGracefulStop, toHardStop, stopped:152 return // nothing to do153 case starting: // we raced with the loop and apparently it won't do a single iteration154 vh.cancel()155 vh.ctx, vh.cancel = context.WithCancel(vh.parentCtx)156 vh.changeState(stopped)157 case running:158 vh.changeState(toGracefulStop)159 }160 vh.logger.Debug("Graceful stop")161 vh.canStartIter = make(chan struct{})162}163func (vh *vuHandle) hardStop() {164 vh.mutex.Lock()165 defer vh.mutex.Unlock()166 switch vh.state {167 case toHardStop, stopped:168 return // nothing to do169 case starting: // we raced with the loop and apparently it won't do a single iteration170 vh.changeState(stopped)171 case running, toGracefulStop:172 vh.changeState(toHardStop)173 }174 vh.logger.Debug("Hard stop")175 vh.cancel()176 vh.ctx, vh.cancel = context.WithCancel(vh.parentCtx)177 vh.canStartIter = make(chan struct{})178}179// runLoopsIfPossible is where all the fun is :D. Unfortunately somewhere we need to check most180// of the cases and this is where this happens.181func (vh *vuHandle) runLoopsIfPossible(runIter func(context.Context, lib.ActiveVU) bool) {182 // We can probably initialize here, but it's also easier to just use the slow path in the second183 // part of the for loop184 defer func() {185 // not sure if this is needed, because here the parentCtx is canceled and I would argue it doesn't matter186 // if we set the correct state187 vh.mutex.Lock()188 vh.changeState(stopped)189 vh.mutex.Unlock()190 }()191 var (192 executorDone = vh.parentCtx.Done()193 ctx context.Context194 cancel func()195 vu lib.ActiveVU196 )197 for {198 state := stateType(atomic.LoadInt32((*int32)(&vh.state)))199 if state == running && runIter(ctx, vu) { // fast path200 continue201 }202 // slow path - something has changed - get what and wait until we can do more iterations203 vh.mutex.Lock()204 select {205 case <-executorDone:206 // The whole executor is done, nothing more to do.207 vh.mutex.Unlock()208 return209 default:210 }211 switch vh.state {212 case running: // start raced us toGracefulStop213 vh.mutex.Unlock()214 continue215 case toGracefulStop:216 if cancel != nil {217 // we need to cancel the context, to return the vu218 // and because *we* did, lets reinitialize it219 cancel()220 vh.ctx, vh.cancel = context.WithCancel(vh.parentCtx)221 }222 fallthrough // to set the state223 case toHardStop:224 // we have *now* stopped225 vh.changeState(stopped)226 case stopped, starting:227 // there is nothing to do228 }229 canStartIter := vh.canStartIter230 ctx = vh.ctx231 vh.mutex.Unlock()232 // We're are stopped, but the executor isn't done yet, so we wait233 // for either one of those conditions.234 select {235 case <-canStartIter: // we can start again236 vh.mutex.Lock()237 select {238 case <-vh.canStartIter: // we check again in case of race239 // reinitialize240 vu, ctx, cancel = vh.activeVU, vh.ctx, vh.cancel241 vh.changeState(running)242 default:243 // well we got raced to here by something ... loop again ...244 }245 vh.mutex.Unlock()246 case <-ctx.Done():247 // hardStop was called, start a fresh iteration to get the new248 // context and signal channel249 case <-executorDone:250 // The whole executor is done, nothing more to do.251 return252 }253 }254}...
cmd.go
Source:cmd.go
1package main2import (3 "flag"4 "fmt"5 "heimatcli/src/heimat/api"6 prompt "github.com/c-bata/go-prompt"7)8func completer(in prompt.Document) []prompt.Suggest {9 s := sm.currentState.Suggestions(in)10 return prompt.FilterFuzzy(s, in.GetWordBeforeCursor(), true)11}12var livePrefixState struct {13 LivePrefix string14 IsEnable bool15}16func executor(in string) {17 newStateKey := sm.currentState.Exe(in)18 if newStateKey == stateKeyNoChange {19 return20 }21 sm.ChangeState(newStateKey)22}23func changeLivePrefix() (string, bool) {24 prefix := sm.CurrentState().Prefix()25 return prefix, true26}27var sm *StateMachine28// Run start the app29func Run() {30 const defaultAPI = "https://heimat.sprinteins.com/api/v1"31 // parse flags32 apiEndpoint := flag.String("api", defaultAPI, "API Endpoint")33 versionRequest := flag.Bool("version", false, "Prints current version")34 flag.Parse()35 if *versionRequest {36 fmt.Println("v0.1.5")37 return38 }39 // Initialize Dependencies40 heimatAPI := api.NewAPI(*apiEndpoint)41 startPrompt(heimatAPI)42}43func cliLogin(api *api.API) {44 loginState := NewStateLogin(api)45 loginState.Exe("")46}47func startPrompt(heimatAPI *api.API) {48 // cancel function49 cancel := func() {50 if !heimatAPI.IsAuthenticated() {51 sm.ChangeState(stateKeyLogin)52 }53 sm.ChangeState(stateKeyHome)54 }55 cancelPrompt := func(b *prompt.Buffer) {56 cancel()57 }58 // Initialize states59 sm = NewStateMachine(stateKeyHome)60 sm.AddState(stateKeyLogin, NewStateLogin(heimatAPI))61 sm.AddState(stateKeyHome, NewStateHome(heimatAPI))62 sm.AddState(stateKeyTimeAdd, NewStateTimeAdd(heimatAPI, cancel))63 sm.AddState(stateKeyTimeDelete, NewStateTimeDelete(heimatAPI, cancel))64 // Skip Login if already authenticated65 if !heimatAPI.IsAuthenticated() {66 stateLogin := NewStateLogin(heimatAPI)67 loginSuccess := stateLogin.Login()68 for !loginSuccess {69 fmt.Println("Wrong username or password!")70 loginSuccess = stateLogin.Login()71 }72 }73 sm.ChangeState(stateKeyHome)74 // Set up go-prompt75 p := prompt.New(76 executor,77 completer,78 prompt.OptionPrefix(">>> "),79 prompt.OptionLivePrefix(changeLivePrefix),80 prompt.OptionTitle("Heimat"),81 prompt.OptionSwitchKeyBindMode(prompt.CommonKeyBind),82 prompt.OptionAddKeyBind(prompt.KeyBind{83 Key: prompt.Escape,84 Fn: cancelPrompt,85 }),86 )87 p.Run()88}...
changeState
Using AI Code Generation
1import (2type Executor struct {3}4func (e *Executor) changeState(state string) {5}6func main() {7 executor := Executor{}8 executor.changeState("Running")9 fmt.Println(executor.state)10}11import (12type Executor struct {13}14func (e *Executor) changeState(state string) {15}16func changeState(e *Executor, state string) {17 e.changeState(state)18}19func main() {20 executor := Executor{}21 changeState(&executor, "Running")22 fmt.Println(executor.state)23}
changeState
Using AI Code Generation
1import (2type Executor struct {3}4func (e *Executor) changeState() {5 if e.state == "on" {6 } else {7 }8}9func main() {10 e := Executor{state: "on"}11 fmt.Println("State:", e.state)12 e.changeState()13 fmt.Println("State:", e.state)14}
changeState
Using AI Code Generation
1import "fmt"2func main() {3 e.changeState(1)4}5import "fmt"6type executor struct {7}8func (e *executor) changeState(state int) {9}10func main() {11 e.changeState(1)12}13import "fmt"14type executor struct {15}16func (e *executor) ChangeState(state int) {17}18func main() {19 e.ChangeState(1)20}21In the above example, you can access the changeState method and the state variable from 1.go because both 1.go and 2.go are in the same package. You can also access the changeState method and the state variable from 2.go because 2.go imports the main package
changeState
Using AI Code Generation
1func main() {2 e.changeState("running")3}4func main() {5 e.changeState("running")6}7func main() {8 e.changeState("running")9}10func main() {11 e.changeState("running")12}13func main() {14 e.changeState("running")15}16func main() {17 e.changeState("running")18}19func main() {20 e.changeState("running")21}22func main() {23 e.changeState("running")24}25func main() {26 e.changeState("running")27}28func main() {29 e.changeState("running")30}31func main() {32 e.changeState("running")33}34func main()
changeState
Using AI Code Generation
1import (2func main() {3 e := executor{}4 s := state{}5 t := task{}6 t1 := task{}7 e.addTask(&t)8 e.addTask(&t1)9 e.changeState(&s)10 fmt.Println(t.state.state)11 fmt.Println(t1.state.state)12}
changeState
Using AI Code Generation
1import (2func main() {3 fmt.Println("main: Start")4 executor := new(Executor)5 executor.start()6 executor.changeState(executor.state)7 time.Sleep(10 * time.Second)8 executor.stop()9 fmt.Println("main: End")10}11import (12type Executor struct {13}14func (e *Executor) start() {15 go func() {16 for {17 e.stateLock.Lock()18 if e.state == 0 {19 fmt.Println("executor: Stopped")20 e.stateLock.Unlock()21 } else {22 fmt.Println("executor: Running")23 e.stateLock.Unlock()24 }25 time.Sleep(1 * time.Second)26 }27 }()28}29func (e *Executor) stop() {30 e.stateLock.Lock()31 defer e.stateLock.Unlock()32}33func (e *Executor) changeState(state int) {34 e.stateLock.Lock()35 defer e.stateLock.Unlock()36}37func main() {38 fmt.Println("main: Start")39 executor := new(Executor)40 executor.start()41 executor.changeState(executor.state)42 time.Sleep(10 * time.Second)43 executor.stop()44 fmt.Println("main: End")45}46import (
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!!