Skip to content

Commit

Permalink
Added stepped simulation test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vin committed Nov 7, 2024
1 parent ef9d735 commit a452067
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
18 changes: 5 additions & 13 deletions src/scenic/core/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ def __init__(
# properties during setup.
self.updateObjects()

# Set terminationType and terminationReason to default None
self.terminationType = None
self.terminationReason = None

except (RejectSimulationException, RejectionException, GuardViolation) as e:
# This simulation will be thrown out, but attach it to the exception
# to aid in debugging.
Expand All @@ -437,11 +433,11 @@ def _run(self):
while True:
self.advance()

if self.terminationType:
if self.result:
return

def advance(self):
if self.terminationType or self._cleaned:
if self.result or self._cleaned:
raise TerminatedSimulationException()

if self.verbosity >= 3:
Expand Down Expand Up @@ -543,13 +539,9 @@ def advance(self):
self.currentTime += 1
self.updateObjects()

def terminateSimulation(self, terimnationType, terminationReason):
def terminateSimulation(self, terminationType, terminationReason):
import scenic.syntax.veneer as veneer

# Log terminationType and terminationReason
self.terminationType = terimnationType
self.terminationReason = terminationReason

# Stop all remaining scenarios.
# (and reject if some 'require eventually' condition was never satisfied)
for scenario in tuple(reversed(veneer.runningScenarios)):
Expand All @@ -564,8 +556,8 @@ def terminateSimulation(self, terimnationType, terminationReason):
result = SimulationResult(
self.trajectory,
self.actionSequence,
self.terminationType,
self.terminationReason,
terminationType,
terminationReason,
self.records,
)
self.result = result
Expand Down
30 changes: 29 additions & 1 deletion tests/core/test_simulators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest

from scenic.core.simulators import DummySimulation, DummySimulator, Simulation
from scenic.core.simulators import (
DummySimulation,
DummySimulator,
Simulation,
TerminatedSimulationException,
)
from tests.utils import compileScenic, sampleResultFromScene, sampleSceneFrom


Expand Down Expand Up @@ -35,6 +40,29 @@ def test_simulator_destruction():
assert "destroy() called twice" in str(e)


def test_simulator_stepped():
simulator = DummySimulator()
scene = sampleSceneFrom("ego = new Object")

with simulator.simulateStepped(scene, maxSteps=5) as simulation:
while simulation.result is None:
simulation.advance()

assert simulation.result is not None
assert simulation.currentTime == 5

# advance() should do nothing but raise an exception
# if the simulation is already terminated
with pytest.raises(TerminatedSimulationException):
simulation.advance()

assert simulation.currentTime == 5

# Ensure all values are preserved after leaving the context manager
assert simulation.result is not None
assert simulation.currentTime == 5


def test_simulator_set_property():
class TestSimulation(DummySimulation):
def createObjectInSimulator(self, obj):
Expand Down

0 comments on commit a452067

Please sign in to comment.