-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from osrf/issue_51
Return test coverage to 100 percent
- Loading branch information
Showing
8 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,34 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
|
||
# import os | ||
# import sys | ||
import unittest | ||
|
||
import rostest | ||
|
||
from capabilities.client import CapabilitiesClient | ||
|
||
# TEST_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
|
||
# sys.path.insert(0, TEST_DIR) | ||
# from unit.common import assert_raises | ||
|
||
TEST_NAME = 'test_client_module' | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def test_client_module(self): | ||
c = CapabilitiesClient() | ||
c.wait_for_services(3) | ||
c.use_capability('minimal_pkg/Minimal', 'minimal_pkg/minimal') | ||
c.use_capability('minimal_pkg/Minimal', 'minimal_pkg/minimal') | ||
c.free_capability('minimal_pkg/Minimal') | ||
c.free_capability('not_a_pkg/NotACap') | ||
c.shutdown() | ||
|
||
if __name__ == '__main__': | ||
import rospy | ||
rospy.init_node(TEST_NAME, anonymous=True) | ||
rostest.unitrun('capabilities', TEST_NAME, Test) |
This file contains 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,7 @@ | ||
<launch> | ||
<node pkg="capabilities" name="capability_server" type="capability_server" output="screen" required="true"> | ||
<env name="ROS_PACKAGE_PATH" | ||
value="$(find capabilities)/test/unit/discovery_workspaces/minimal:$(env ROS_PACKAGE_PATH)" /> | ||
</node> | ||
<test test-name="client_module" pkg="capabilities" type="test_client_module.py" launch-prefix="coverage run --source=$(find capabilities)/src/capabilities --append "/> | ||
</launch> |
This file contains 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,80 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
|
||
import rostest | ||
|
||
import rospy | ||
from rospy.service import ServiceException | ||
|
||
from test_ros_services import assert_raises | ||
from test_ros_services import call_service | ||
from test_ros_services import wait_for_capability_server | ||
|
||
from capabilities.client import CapabilitiesClient | ||
|
||
TEST_NAME = 'test_remapping' | ||
|
||
|
||
def wait_for_result_to_happen(expected, initial_result, tries=10, sleep_period=1): | ||
result = initial_result | ||
count = 0 | ||
while count != tries and sorted(result) != sorted(expected): | ||
rospy.sleep(sleep_period) | ||
count += 1 | ||
resp = call_service('/capability_server/get_running_capabilities') | ||
result = [x.capability.capability for x in resp.running_capabilities] | ||
return result | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def test_use_and_free_capability(self): | ||
wait_for_capability_server(3) | ||
c = CapabilitiesClient() | ||
c.wait_for_services(timeout=3.0) | ||
# Give invalid bond id to use_capability | ||
with assert_raises(ServiceException): | ||
call_service('/capability_server/use_capability', 'minimal_pkg/Minimal', '', 'invalid_bond_id') | ||
# Try to use a non-existent cap | ||
with assert_raises(ServiceException): | ||
c.use_capability('not_a_pkg/NotACap') | ||
# Use cap and wait for it to be running | ||
c.use_capability('minimal_pkg/Minimal', 'minimal_pkg/minimal') | ||
expected = ['minimal_pkg/Minimal'] | ||
result = wait_for_result_to_happen(expected, []) | ||
assert sorted(result) == sorted(expected), (sorted(result), sorted(expected)) | ||
# Try to use it with a different provider | ||
with assert_raises(ServiceException): | ||
c.use_capability('minimal_pkg/Minimal', 'minimal_pkg/specific_minimal') | ||
# Use it a second time, free it once, assert it is stil there | ||
c.use_capability('minimal_pkg/Minimal') | ||
c.free_capability('minimal_pkg/Minimal') | ||
expected = [] | ||
result = wait_for_result_to_happen(expected, ['minimal_pkg/Minimal'], tries=5) | ||
assert sorted(result) != sorted(expected), (sorted(result), sorted(expected)) | ||
# Directly call ~free_capability with an invalid bond_id | ||
with assert_raises(ServiceException): | ||
call_service('/capability_server/free_capability', 'minimal_pkg/Minimal', 'invalid_bond_id') | ||
# Free it again and assert it goes down | ||
c.free_capability('minimal_pkg/Minimal') | ||
expected = [] | ||
result = wait_for_result_to_happen(expected, ['minimal_pkg/Minimal']) | ||
assert sorted(result) == sorted(expected), (sorted(result), sorted(expected)) | ||
# Try to over free it and get an exception | ||
with assert_raises(ServiceException): | ||
c.free_capability('minimal_pkg/Minimal') | ||
# Use it again, then break the bond and assert it goes down because of that | ||
c.use_capability('minimal_pkg/Minimal') | ||
expected = ['minimal_pkg/Minimal'] | ||
result = wait_for_result_to_happen(expected, []) | ||
assert sorted(result) == sorted(expected), (sorted(result), sorted(expected)) | ||
c._bond.break_bond() | ||
expected = [] | ||
result = wait_for_result_to_happen(expected, ['minimal_pkg/Minimal']) | ||
assert sorted(result) == sorted(expected), (sorted(result), sorted(expected)) | ||
|
||
if __name__ == '__main__': | ||
rospy.init_node(TEST_NAME, anonymous=True) | ||
rostest.unitrun('capabilities', TEST_NAME, Test) |
This file contains 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,9 @@ | ||
<launch> | ||
<node pkg="capabilities" name="capability_server" type="capability_server" output="screen" required="true" | ||
launch-prefix="coverage run --append "> | ||
<param name="debug" value="true"/> | ||
<env name="ROS_PACKAGE_PATH" | ||
value="$(find capabilities)/test/unit/discovery_workspaces/minimal:$(env ROS_PACKAGE_PATH)" /> | ||
</node> | ||
<test test-name="client" pkg="capabilities" type="test_client.py"/> | ||
</launch> |
This file contains 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,13 @@ | ||
from common import assert_raises | ||
|
||
from capabilities.client import CapabilitiesClient | ||
|
||
|
||
def test_wait_for_services(): | ||
c = CapabilitiesClient() | ||
with assert_raises(ValueError): | ||
c.wait_for_services(0.1, ['invalid_service']) | ||
assert c.wait_for_services(0.1) is False | ||
c.establish_bond(0.1) | ||
c._used_capabilities.add('some_pkg/SomeCap') | ||
c.free_capability('some_pkg/SomeCap', 0.1) |