forked from ros2-java/ros2_java
-
Notifications
You must be signed in to change notification settings - Fork 9
Add tests for TimeSource #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca0a225
Notify clocks when use_sim_time parameter changes
jacobperron 0c607ff
Add unit tests for TimeSource class
jacobperron 1b1d628
Add test dependency on libmockito-java
jacobperron ef7bf22
Minor refactor
jacobperron a18ce85
Depend on mockito_vendor package
jacobperron 919f0e1
Add mockito_vendor to repos file
jacobperron 2e5ccb8
Update branch for mockito_vendor
jacobperron 35e926e
Try out action-ros-ci patch
jacobperron c4e8ed7
Switch back to upstream version of GitHub action
jacobperron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.ros2.rcljava.time; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.assertFalse; | ||
|
|
||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| import org.junit.runner.RunWith; | ||
|
|
||
| import static org.mockito.Mockito.*; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import org.ros2.rcljava.consumers.Consumer; | ||
| import org.ros2.rcljava.RCLJava; | ||
| import org.ros2.rcljava.node.Node; | ||
| import org.ros2.rcljava.parameters.ParameterVariant; | ||
| import org.ros2.rcljava.subscription.Subscription; | ||
| import org.ros2.rcljava.time.TimeSource; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.StrictStubs.class) | ||
| public class TimeSourceTest { | ||
| @Mock | ||
| private Node mockedNode; | ||
|
|
||
| @Mock | ||
| private Clock mockedClock; | ||
|
|
||
| @Mock | ||
| private Subscription mockSubscription; | ||
|
|
||
| @BeforeClass | ||
| public static void setupOnce() throws Exception { | ||
| // Just to quiet down warnings | ||
| org.apache.log4j.BasicConfigurator.configure(); | ||
|
|
||
| RCLJava.rclJavaInit(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDownOnce() { | ||
| RCLJava.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testEmptyConstructor() { | ||
| TimeSource timeSource = new TimeSource(); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testConstructorWithNode() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false)); | ||
|
|
||
| TimeSource timeSource = new TimeSource(mockedNode); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testAttachNodeUseSimTimeFalse() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false)); | ||
|
|
||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.attachNode(mockedNode); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testAttachNodeUseSimTimeTrue() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true)); | ||
|
|
||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.attachNode(mockedNode); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testAttachNodeTwice() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true)); | ||
|
|
||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.attachNode(mockedNode); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
|
|
||
| // Attach the same node again | ||
| timeSource.attachNode(mockedNode); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testDetachNode() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", true)); | ||
|
|
||
| // Attaches node with ROS time active | ||
| TimeSource timeSource = new TimeSource(mockedNode); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
|
|
||
| timeSource.detachNode(); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
|
|
||
| // Calling detach again shouldn't change anything | ||
| timeSource.detachNode(); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testAttachClock() { | ||
| when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME); | ||
|
|
||
| TimeSource timeSource = new TimeSource(); | ||
| // Attaching a clock should notifiy the clock | ||
| timeSource.attachClock(mockedClock); | ||
| verify(mockedClock).setRosTimeIsActive(false); | ||
|
|
||
| // Setting ROS time active should notify clock | ||
| timeSource.setRosTimeIsActive(true); | ||
| verify(mockedClock).setRosTimeIsActive(true); | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public final void testAttachClockInvalidType() { | ||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.attachClock(mockedClock); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testDetachClock() { | ||
| when(mockedClock.getClockType()).thenReturn(ClockType.ROS_TIME); | ||
|
|
||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.attachClock(mockedClock); | ||
| timeSource.detachClock(mockedClock); | ||
|
|
||
| // Setting ROS time active should not notify a detached clock | ||
| timeSource.setRosTimeIsActive(true); | ||
| verify(mockedClock, never()).setRosTimeIsActive(true); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testSetRosTimeIsActiveNoNode() { | ||
| TimeSource timeSource = new TimeSource(); | ||
| timeSource.setRosTimeIsActive(false); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| timeSource.setRosTimeIsActive(true); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
| } | ||
|
|
||
| @Test | ||
| public final void testSetRosTimeIsActiveWithNode() { | ||
| when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false)); | ||
| when(mockedNode.createSubscription(eq(rosgraph_msgs.msg.Clock.class), anyString(), any(Consumer.class))) | ||
| .thenReturn(mockSubscription); | ||
|
|
||
| TimeSource timeSource = new TimeSource(mockedNode); | ||
| timeSource.setRosTimeIsActive(false); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| timeSource.setRosTimeIsActive(true); | ||
| assertTrue(timeSource.getRosTimeIsActive()); | ||
| // Expect subscription for the "/clock" topic when set active | ||
| verify(mockedNode).createSubscription(eq(rosgraph_msgs.msg.Clock.class), eq("/clock"), any(Consumer.class)); | ||
| timeSource.setRosTimeIsActive(false); | ||
| assertFalse(timeSource.getRosTimeIsActive()); | ||
| // Expect subscription removed when set not active | ||
| verify(mockedNode).removeSubscription(any(Subscription.class)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ros-tooling/action-ros-ci#343 🎉