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

bugfix(sumo): Fix call of adjustToSumoTimeStep #327

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,14 @@ private synchronized void receiveInteraction(VehicleSpeedChange vehicleSpeedChan
case WITH_DURATION:
if (vehicleSpeedChange.getDuration() > 0) {
// set speed smoothly with given interval
final long changeSpeedTimestep = vehicleSpeedChange.getTime() + vehicleSpeedChange.getDuration();
log.debug("slow down vehicle {} and schedule change speed event for timestep {} ns ",
vehicleSpeedChange.getVehicleId(), changeSpeedTimestep);
final long changeSpeedTimeStep = vehicleSpeedChange.getTime() + vehicleSpeedChange.getDuration();
log.debug("slow down vehicle {} and schedule change speed event for time step {} ns ",
vehicleSpeedChange.getVehicleId(), changeSpeedTimeStep);
bridge.getVehicleControl()
.slowDown(vehicleSpeedChange.getVehicleId(), vehicleSpeedChange.getSpeed(), vehicleSpeedChange.getDuration());

// set speed permanently after given interval (in the future) via the event scheduler
long adjustedTime = adjustToSumoTimeStep(changeSpeedTimestep, sumoConfig.updateInterval);
long adjustedTime = adjustToSumoTimeStep(changeSpeedTimeStep, sumoConfig.updateInterval * TIME.MILLI_SECOND);
eventScheduler.addEvent(new Event(adjustedTime, this, vehicleSpeedChange)
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void initialize_doNotInitTraci() throws Throwable {

// ASSERT
verify(rtiMock, times(1))
.requestAdvanceTime(eq(0L), eq(0L), eq((byte)(FederatePriority.DEFAULT - 1)));
.requestAdvanceTime(eq(0L), eq(0L), eq((byte) (FederatePriority.DEFAULT - 1)));
assertNull(traciClientBridgeMock);
}

Expand Down Expand Up @@ -222,6 +222,33 @@ public void receiveMessage_VehicleSpeedChangeWithInterval() throws Throwable {
verify(traciClientBridgeMock.getVehicleControl()).setSpeed(eq("veh_0"), eq(10.0));
}

@Test
public void receiveMessage_VehicleSpeedChangeAtNonMultipleOfTimeStep() throws Throwable {
// if the adjustToSumoTimeStep doesn't get a multiple of sumoConfig.updateInterval * TIME.MILLI_SECOND, this fails
sendVehiclePathsAndTypes_doInitTraci();
long sendTime = 1100L;
// RUN
mockSimulationStepResult(0L);
ambassador.advanceTime(sendTime); // advancing time enough to process interaction
VehicleSpeedChange vehicleSpeedChange = new VehicleSpeedChange(sendTime, "veh_0", VehicleSpeedChange.VehicleSpeedChangeType.WITH_DURATION, 10, 5 * TIME.SECOND, 0);
ambassador.processInteraction(vehicleSpeedChange);
// ASSERT
// -> nothing should be called yet
verify(traciClientBridgeMock.getVehicleControl(), never()).setSpeed(anyString(), anyDouble());
verify(traciClientBridgeMock.getVehicleControl(), never()).slowDown(eq("veh_0"), eq(10.0), eq(5 * TIME.SECOND));
// RUN + ASSERT
// slow down should be called at next time step
ambassador.advanceTime(ambassador.sumoConfig.updateInterval * TIME.MILLI_SECOND);
verify(traciClientBridgeMock.getVehicleControl()).slowDown(eq("veh_0"), eq(10.0), eq(5 * TIME.SECOND));
// RUN
for (int i = 1; i <= 5; i++) {
ambassador.advanceTime(i * ambassador.sumoConfig.updateInterval * TIME.MILLI_SECOND);
}
// ASSERT
// after 5 seconds setSpeed should have been called
verify(traciClientBridgeMock.getVehicleControl()).setSpeed(eq("veh_0"), eq(10.0));
}

@Test
public void addVehicleAndSimulate_subscribeToAllVehiclesTrue_vehicleIsAddedAndSubscribedFor() throws Throwable {
testSubscribeToAllVehicles(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@
package org.eclipse.mosaic.fed.sumo.ambassador;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;

import org.eclipse.mosaic.rti.TIME;

import org.junit.Test;

public class SumoAmbassadorTimeStepAdjustmentTest {

private final static long sumoTimeStep = 10000;
private final static long sumoTimeStep = TIME.SECOND;

@Test
public void testEqualTimeStep() {
// Requested time is the same as the sumo timestep. Shouldn't be changed
assertEquals(sumoTimeStep, AbstractSumoAmbassador.adjustToSumoTimeStep(10000, sumoTimeStep));
assertEquals(sumoTimeStep, AbstractSumoAmbassador.adjustToSumoTimeStep(TIME.SECOND, sumoTimeStep));
}

@Test
public void testRoundDown() {
// Just 2 over, should round down
assertEquals(sumoTimeStep, AbstractSumoAmbassador.adjustToSumoTimeStep(10002, sumoTimeStep));
assertEquals(sumoTimeStep, AbstractSumoAmbassador.adjustToSumoTimeStep(TIME.SECOND + 2 * TIME.MILLI_SECOND, sumoTimeStep));
}

@Test
public void testRoundUp() {
// Closer to the next higher value, should round up
assertEquals(sumoTimeStep * 2, AbstractSumoAmbassador.adjustToSumoTimeStep(19999, sumoTimeStep));
assertEquals(sumoTimeStep * 2, AbstractSumoAmbassador.adjustToSumoTimeStep(TIME.SECOND + 999 * TIME.MILLI_SECOND, sumoTimeStep));
}

@Test
Expand Down