Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it dangerous to share references between states? #71

Open
symbiont-sean-lyons opened this issue Aug 24, 2020 · 1 comment
Open

Is it dangerous to share references between states? #71

symbiont-sean-lyons opened this issue Aug 24, 2020 · 1 comment

Comments

@symbiont-sean-lyons
Copy link

When transitioning a model using Command.NextState -> commands.State, is it dangerous to share pointers or reference types between model states?

For example,

type State struct {
  index map[string]string
}

func (writeFooBarCommand) NextState(s commands.State) commands.State {
  current := s.(State)
  next := current
  next.index["foo"] = "bar"
  return next
}

I believe that the existing model state and the new model state will share a reference to the underlying map, meaning manipulations via the old state instance or the new state instance will reflect in both objects. I currently make it a habit to deep copy my model states when transitioning, but my question is whether or not the deep copy is necessary.

@untoldwind
Copy link
Collaborator

In this specific case it should be no problem mutating the state as long as InitialStateGen always generates a new instance.

The main loop running a sequence of commands is done in this function:

func (a *actions) run(systemUnderTest SystemUnderTest) *gopter.PropResult {
state := a.initialStateProvider()
propResult := &gopter.PropResult{Status: gopter.PropTrue}
for _, shrinkableCommand := range a.sequentialCommands {
if !shrinkableCommand.command.PreCondition(state) {
return &gopter.PropResult{Status: gopter.PropFalse}
}
result := shrinkableCommand.command.Run(systemUnderTest)
state = shrinkableCommand.command.NextState(state)
propResult = propResult.And(shrinkableCommand.command.PostCondition(state, result))
}
return propResult
}

As you can see the previous state is just forgotten after the command has been applied, therefore it does not matter if it has been mutated or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants