Skip to content

Commit 8c678da

Browse files
authored
Fix use_sim_time not modifying clock (#88)
Previously, when attaching a node that has 'use_sim_time' set to true, we were not setting ROS time on any previously attached clocks or creating a subscription to the /clock topic. This change fixes that by calling the TimeSource setter. Added unit tests that fail without this fix. Signed-off-by: Jacob Perron <[email protected]>
1 parent afde903 commit 8c678da

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

rcljava/src/main/java/org/ros2/rcljava/time/TimeSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void attachNode(Node node) {
152152
ParameterType useSimTimeType = useSimTime.getType();
153153
if (useSimTimeType != ParameterType.PARAMETER_NOT_SET) {
154154
if (useSimTimeType == ParameterType.PARAMETER_BOOL) {
155-
this.rosTimeIsActive = useSimTime.asBool();
155+
this.setRosTimeIsActive(useSimTime.asBool());
156156
} else {
157157
logger.warn("The 'use_sim_time' parameter must be a boolean");
158158
}

rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ public final void testConstructorWithNode() {
7878
@Test
7979
public final void testAttachNodeUseSimTimeFalse() {
8080
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false));
81+
when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME);
8182

8283
TimeSource timeSource = new TimeSource();
84+
// An attached clock should be notified of the 'use_sim_time' value
85+
timeSource.attachClock(mockedClock);
8386
timeSource.attachNode(mockedNode);
8487
assertFalse(timeSource.getRosTimeIsActive());
88+
verify(mockedClock).setRosTimeIsActive(false);
8589
}
8690

8791
@Test
8892
public final void testAttachNodeUseSimTimeTrue() {
8993
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true));
94+
when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME);
9095

9196
TimeSource timeSource = new TimeSource();
97+
// An attached clock should be notified of the 'use_sim_time' value
98+
timeSource.attachClock(mockedClock);
9299
timeSource.attachNode(mockedNode);
93100
assertTrue(timeSource.getRosTimeIsActive());
101+
102+
verify(mockedClock).setRosTimeIsActive(true);
94103
}
95104

96105
@Test

0 commit comments

Comments
 (0)