Add cron tick of command queue

This commit is contained in:
Marc Di Luzio 2020-06-06 15:52:03 +01:00
parent 0a0a32cf58
commit 573bfbf9c7
7 changed files with 113 additions and 13 deletions

View file

@ -26,6 +26,9 @@ func TestCommand_Move(t *testing.T) {
moveCommand := Command{Command: CommandMove, Bearing: bearing.String(), Duration: duration}
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
// Tick the world
world.ExecuteCommandQueues()
newpos, err := world.RoverPosition(a)
assert.NoError(t, err, "Failed to set position for rover")
pos.Add(Vector{0.0, duration * attribs.Speed}) // We should have moved duration*speed north

View file

@ -204,7 +204,7 @@ func (w *World) Enqueue(rover uuid.UUID, commands ...Command) error {
}
// Execute will execute any commands in the current command queue
func (w *World) Execute() error {
func (w *World) ExecuteCommandQueues() {
w.cmdMutex.Lock()
defer w.cmdMutex.Unlock()
@ -231,14 +231,10 @@ func (w *World) Execute() error {
delete(w.CommandQueue, rover)
}
}
return nil
}
// ExecuteCommand will execute a single command
func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err error) {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
switch c.Command {
case "move":
@ -263,3 +259,27 @@ func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err
return
}
// RLock read locks the world
func (w *World) RLock() {
w.worldMutex.RLock()
w.cmdMutex.RLock()
}
// RUnlock read unlocks the world
func (w *World) RUnlock() {
w.worldMutex.RUnlock()
w.cmdMutex.RUnlock()
}
// Lock locks the world
func (w *World) Lock() {
w.worldMutex.Lock()
w.cmdMutex.Lock()
}
// Unlock unlocks the world
func (w *World) Unlock() {
w.worldMutex.Unlock()
w.cmdMutex.Unlock()
}