Skip to content

Commit 95ca66b

Browse files
committed
apply review comments and add tests
1 parent 6d36fd4 commit 95ca66b

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

src/roslibpy/actionlib.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -432,24 +432,12 @@ def set_succeeded(self, result):
432432
LOGGER.info("Action server {} setting current goal to SUCCEEDED".format(self.server_name))
433433

434434
with self._lock:
435-
436-
self.status_message["status_list"] = [
437-
dict(
438-
goal_id=self.current_goal["goal_id"],
439-
status=GoalStatus.SUCCEEDED)
440-
]
435+
status = dict(goal_id=self.current_goal["goal_id"], status=GoalStatus.SUCCEEDED)
436+
self.status_message["status_list"] = [status]
441437
self._publish_status()
442438
self.status_message["status_list"] = []
443439

444-
result_message = Message(
445-
{
446-
"status": {
447-
"goal_id": self.current_goal["goal_id"],
448-
"status": GoalStatus.SUCCEEDED
449-
},
450-
"result": result
451-
}
452-
)
440+
result_message = Message({"status": status, "result": result})
453441
self.result_publisher.publish(result_message)
454442

455443
if self.next_goal:
@@ -481,23 +469,12 @@ def set_preempted(self):
481469
LOGGER.info("Action server {} preempting current goal".format(self.server_name))
482470

483471
with self._lock:
484-
485-
self.status_message["status_list"] = [
486-
dict(
487-
goal_id=self.current_goal["goal_id"],
488-
status=GoalStatus.PREEMPTED)
489-
]
472+
status = dict(goal_id=self.current_goal["goal_id"], status=GoalStatus.PREEMPTED)
473+
self.status_message["status_list"] = [status]
490474
self._publish_status()
491475
self.status_message["status_list"] = []
492476

493-
result_message = Message(
494-
{
495-
"status": {
496-
"goal_id": self.current_goal["goal_id"],
497-
"status": GoalStatus.PREEMPTED
498-
}
499-
}
500-
)
477+
result_message = Message({"status": status})
501478
self.result_publisher.publish(result_message)
502479

503480
if self.next_goal:

tests/test_actionlib.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import print_function
2+
3+
import time
4+
5+
from roslibpy import Ros
6+
from roslibpy.actionlib import ActionClient, Goal, SimpleActionServer, GoalStatus
7+
8+
9+
def test_action_success():
10+
ros = Ros("127.0.0.1", 9090)
11+
ros.run()
12+
13+
server = SimpleActionServer(ros, "/test_action", "actionlib/TestAction")
14+
15+
def execute(goal):
16+
server.set_succeeded({"result": goal["goal"]})
17+
18+
server.start(execute)
19+
20+
client = ActionClient(ros, "/test_action", "actionlib/TestAction")
21+
goal = Goal(client, {"goal": 13})
22+
23+
goal.send()
24+
result = goal.wait(10)
25+
26+
assert result["result"] == 13
27+
assert goal.status["status"] == GoalStatus.SUCCEEDED
28+
29+
client.dispose()
30+
time.sleep(2)
31+
ros.close()
32+
33+
34+
def test_action_preemt():
35+
ros = Ros("127.0.0.1", 9090)
36+
ros.run()
37+
38+
server = SimpleActionServer(ros, "/test_action", "actionlib/TestAction")
39+
40+
def execute(_goal):
41+
while not server.is_preempt_requested():
42+
time.sleep(0.1)
43+
server.set_preempted()
44+
45+
server.start(execute)
46+
47+
client = ActionClient(ros, "/test_action", "actionlib/TestAction")
48+
goal = Goal(client, {"goal": 13})
49+
50+
goal.send()
51+
time.sleep(0.5)
52+
goal.cancel()
53+
54+
result = goal.wait(10)
55+
56+
assert result["result"] == 0
57+
assert goal.status["status"] == GoalStatus.PREEMPTED
58+
59+
client.dispose()
60+
time.sleep(2)
61+
ros.close()

0 commit comments

Comments
 (0)