Skip to content

Commit 4f279ce

Browse files
committed
Add tests for TimeSource (osrf#22)
* Notify clocks when use_sim_time parameter changes Signed-off-by: Jacob Perron <[email protected]> * Add unit tests for TimeSource class Introduce Mockito dependency to help with creating mock objects. Signed-off-by: Jacob Perron <[email protected]> * Add test dependency on libmockito-java Depends on ros/rosdistro#26308 Signed-off-by: Jacob Perron <[email protected]> * Minor refactor - Switch to using @mock annotation. - Cleanup Signed-off-by: Jacob Perron <[email protected]> * Depend on mockito_vendor package Signed-off-by: Jacob Perron <[email protected]> * Add mockito_vendor to repos file This should make CI green. Signed-off-by: Jacob Perron <[email protected]> * Update branch for mockito_vendor Signed-off-by: Jacob Perron <[email protected]>
1 parent f98ec2f commit 4f279ce

File tree

5 files changed

+194
-2
lines changed

5 files changed

+194
-2
lines changed

rcljava/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ ament_export_jars("share/${PROJECT_NAME}/java/${PROJECT_NAME}.jar")
214214
if(BUILD_TESTING)
215215
find_package(ament_lint_auto REQUIRED)
216216
find_package(std_msgs REQUIRED)
217+
find_package(mockito_vendor REQUIRED)
217218
ament_lint_auto_find_test_dependencies()
218219

219220
set(${PROJECT_NAME}_message_files
@@ -270,6 +271,7 @@ if(BUILD_TESTING)
270271
"src/test/java/org/ros2/rcljava/publisher/PublisherTest.java"
271272
"src/test/java/org/ros2/rcljava/qos/QoSProfileTest.java"
272273
"src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java"
274+
"src/test/java/org/ros2/rcljava/time/TimeSourceTest.java"
273275
"src/test/java/org/ros2/rcljava/timer/TimerTest.java"
274276
)
275277

@@ -286,6 +288,7 @@ if(BUILD_TESTING)
286288
"org.ros2.rcljava.publisher.PublisherTest"
287289
"org.ros2.rcljava.qos.QoSProfileTest"
288290
"org.ros2.rcljava.subscription.SubscriptionTest"
291+
"org.ros2.rcljava.time.TimeSourceTest"
289292
"org.ros2.rcljava.timer.TimerTest"
290293
)
291294

@@ -356,6 +359,7 @@ if(BUILD_TESTING)
356359
"${builtin_interfaces_JARS}"
357360
"${rcl_interfaces_JARS}"
358361
"${rosgraph_msgs_JARS}"
362+
"${mockito_vendor_JARS}"
359363
"${_${PROJECT_NAME}_jar_file}"
360364
"${_${PROJECT_NAME}_messages_jar_file}"
361365
APPEND_LIBRARY_DIRS

rcljava/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<test_depend>ament_lint_auto</test_depend>
4343
<test_depend>ament_lint_common</test_depend>
4444
<test_depend>builtin_interfaces</test_depend>
45+
<test_depend>mockito_vendor</test_depend>
4546
<test_depend>rcl_interfaces</test_depend>
4647
<test_depend>rcljava_common</test_depend>
4748
<test_depend>rmw_implementation_cmake</test_depend>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public rcl_interfaces.msg.SetParametersResult callback(List<ParameterVariant> pa
171171
for (ParameterVariant param : parameters) {
172172
if (param.getName() == "use_sim_time") {
173173
if (param.getType() == ParameterType.PARAMETER_BOOL) {
174-
this.timeSource.rosTimeIsActive = param.asBool();
174+
this.timeSource.setRosTimeIsActive(param.asBool());
175175
} else {
176176
result.setSuccessful(false);
177177
result.setReason("'use_sim_time' parameter must be a boolean");
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/* Copyright 2020 Open Source Robotics Foundation, Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package org.ros2.rcljava.time;
17+
18+
import static org.junit.Assert.assertTrue;
19+
import static org.junit.Assert.assertFalse;
20+
21+
import org.junit.AfterClass;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
25+
import org.junit.runner.RunWith;
26+
27+
import static org.mockito.Mockito.*;
28+
import org.mockito.Mock;
29+
import org.mockito.junit.MockitoJUnitRunner;
30+
31+
import org.ros2.rcljava.consumers.Consumer;
32+
import org.ros2.rcljava.RCLJava;
33+
import org.ros2.rcljava.node.Node;
34+
import org.ros2.rcljava.parameters.ParameterVariant;
35+
import org.ros2.rcljava.subscription.Subscription;
36+
import org.ros2.rcljava.time.TimeSource;
37+
38+
@RunWith(MockitoJUnitRunner.StrictStubs.class)
39+
public class TimeSourceTest {
40+
@Mock
41+
private Node mockedNode;
42+
43+
@Mock
44+
private Clock mockedClock;
45+
46+
@Mock
47+
private Subscription mockSubscription;
48+
49+
@BeforeClass
50+
public static void setupOnce() throws Exception {
51+
// Just to quiet down warnings
52+
org.apache.log4j.BasicConfigurator.configure();
53+
54+
RCLJava.rclJavaInit();
55+
}
56+
57+
@AfterClass
58+
public static void tearDownOnce() {
59+
RCLJava.shutdown();
60+
}
61+
62+
@Test
63+
public final void testEmptyConstructor() {
64+
TimeSource timeSource = new TimeSource();
65+
assertFalse(timeSource.getRosTimeIsActive());
66+
}
67+
68+
@Test
69+
public final void testConstructorWithNode() {
70+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false));
71+
72+
TimeSource timeSource = new TimeSource(mockedNode);
73+
assertFalse(timeSource.getRosTimeIsActive());
74+
}
75+
76+
@Test
77+
public final void testAttachNodeUseSimTimeFalse() {
78+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false));
79+
80+
TimeSource timeSource = new TimeSource();
81+
timeSource.attachNode(mockedNode);
82+
assertFalse(timeSource.getRosTimeIsActive());
83+
}
84+
85+
@Test
86+
public final void testAttachNodeUseSimTimeTrue() {
87+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true));
88+
89+
TimeSource timeSource = new TimeSource();
90+
timeSource.attachNode(mockedNode);
91+
assertTrue(timeSource.getRosTimeIsActive());
92+
}
93+
94+
@Test
95+
public final void testAttachNodeTwice() {
96+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true));
97+
98+
TimeSource timeSource = new TimeSource();
99+
timeSource.attachNode(mockedNode);
100+
assertTrue(timeSource.getRosTimeIsActive());
101+
102+
// Attach the same node again
103+
timeSource.attachNode(mockedNode);
104+
assertTrue(timeSource.getRosTimeIsActive());
105+
}
106+
107+
@Test
108+
public final void testDetachNode() {
109+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true));
110+
111+
// Attaches node with ROS time active
112+
TimeSource timeSource = new TimeSource(mockedNode);
113+
assertTrue(timeSource.getRosTimeIsActive());
114+
115+
timeSource.detachNode();
116+
assertFalse(timeSource.getRosTimeIsActive());
117+
118+
// Calling detach again shouldn't change anything
119+
timeSource.detachNode();
120+
assertFalse(timeSource.getRosTimeIsActive());
121+
}
122+
123+
@Test
124+
public final void testAttachClock() {
125+
when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME);
126+
127+
TimeSource timeSource = new TimeSource();
128+
// Attaching a clock should notifiy the clock
129+
timeSource.attachClock(mockedClock);
130+
verify(mockedClock).setRosTimeIsActive(false);
131+
132+
// Setting ROS time active should notify clock
133+
timeSource.setRosTimeIsActive(true);
134+
verify(mockedClock).setRosTimeIsActive(true);
135+
}
136+
137+
@Test(expected = IllegalArgumentException.class)
138+
public final void testAttachClockInvalidType() {
139+
TimeSource timeSource = new TimeSource();
140+
timeSource.attachClock(mockedClock);
141+
}
142+
143+
@Test
144+
public final void testDetachClock() {
145+
when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME);
146+
147+
TimeSource timeSource = new TimeSource();
148+
timeSource.attachClock(mockedClock);
149+
timeSource.detachClock(mockedClock);
150+
151+
// Setting ROS time active should not notify a detached clock
152+
timeSource.setRosTimeIsActive(true);
153+
verify(mockedClock, never()).setRosTimeIsActive(true);
154+
}
155+
156+
@Test
157+
public final void testSetRosTimeIsActiveNoNode() {
158+
TimeSource timeSource = new TimeSource();
159+
timeSource.setRosTimeIsActive(false);
160+
assertFalse(timeSource.getRosTimeIsActive());
161+
timeSource.setRosTimeIsActive(true);
162+
assertTrue(timeSource.getRosTimeIsActive());
163+
}
164+
165+
@Test
166+
public final void testSetRosTimeIsActiveWithNode() {
167+
when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false));
168+
when(mockedNode.createSubscription(eq(rosgraph_msgs.msg.Clock.class), anyString(), any(Consumer.class)))
169+
.thenReturn(mockSubscription);
170+
171+
TimeSource timeSource = new TimeSource(mockedNode);
172+
timeSource.setRosTimeIsActive(false);
173+
assertFalse(timeSource.getRosTimeIsActive());
174+
timeSource.setRosTimeIsActive(true);
175+
assertTrue(timeSource.getRosTimeIsActive());
176+
// Expect subscription for the "/clock" topic when set active
177+
verify(mockedNode).createSubscription(eq(rosgraph_msgs.msg.Clock.class), eq("/clock"), any(Consumer.class));
178+
timeSource.setRosTimeIsActive(false);
179+
assertFalse(timeSource.getRosTimeIsActive());
180+
// Expect subscription removed when set not active
181+
verify(mockedNode).removeSubscription(any(Subscription.class));
182+
}
183+
}

ros2_java_desktop.repos

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ repositories:
2323
type: git
2424
url: https://github.com/ros2-java/ament_java.git
2525
version: main
26-
ros2-java/ros2_java:
26+
ros2_java/mockito_vendor:
27+
type: git
28+
url: https://github.com/ros2-java/mockito_vendor.git
29+
version: main
30+
ros2_java/ros2_java:
2731
type: git
2832
url: https://github.com/ros2-java/ros2_java.git
2933
version: main

0 commit comments

Comments
 (0)