Skip to content

Commit 0b907b3

Browse files
author
Ernesto Corbellini
committed
Address @basicNew's comments.
1 parent ede0835 commit 0b907b3

File tree

2 files changed

+70
-102
lines changed

2 files changed

+70
-102
lines changed

src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ClientStateMachine.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static String translateState(int state) {
8989
}
9090
}
9191

92-
public int latestGoalStatus;
92+
int latestGoalStatus;
9393
int state;
9494
int nextState;
9595
private Log log = LogFactory.getLog(ActionClient.class);
@@ -439,17 +439,14 @@ public Vector<Integer> getTransition(int goalStatus)
439439
* @return True if the goal can be cancelled, false otherwise.
440440
*/
441441
public boolean cancel() {
442-
boolean ret;
443442
ArrayList<Integer> cancellableStates = new ArrayList<>(Arrays.asList(ClientStates.WAITING_FOR_GOAL_ACK,
444443
ClientStates.PENDING, ClientStates.ACTIVE));
445-
446-
if (cancellableStates.contains(state)) {
447-
state = ClientStates.WAITING_FOR_CANCEL_ACK;
448-
ret = true;
449-
} else {
450-
ret = false;
451-
}
452-
return ret;
444+
boolean shouldCancel = cancellableStates.contains(state);
445+
446+
if (shouldCancel) {
447+
state = ClientStates.WAITING_FOR_CANCEL_ACK;
448+
}
449+
return shouldCancel;
453450
}
454451

455452
public void resultReceived() {
@@ -462,4 +459,8 @@ public void resultReceived() {
462459
public void markAsLost()
463460
{
464461
}
462+
463+
public int getLatestGoalStatus() {
464+
return latestGoalStatus;
465+
}
465466
}

src/rosjava_actionlib/rosjava_actionlib/src/test/java/TestClientStateMachine.java

Lines changed: 59 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,120 +7,87 @@
77
import actionlib_msgs.GoalStatus;
88

99
public class TestClientStateMachine {
10-
private ClientStateMachine subject;
11-
12-
// Executes before each test.
10+
private ClientStateMachine subject;
11+
12+
// Executes before each test.
1313
@Before
1414
public void setUp() {
1515
subject = new ClientStateMachine();
1616
}
17-
17+
1818
@Test
1919
public void testGetState() {
20-
// Setup
21-
int expectedState = ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK;
22-
int actualState;
23-
24-
subject.setState(expectedState);
25-
26-
// Optional: Precondition
27-
28-
// Exercise
29-
actualState = subject.getState();
30-
31-
// Assertion - Postcondition
32-
assertEquals(expectedState, actualState);
20+
int expectedState = ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK;
21+
int actualState;
22+
subject.setState(expectedState);
23+
actualState = subject.getState();
24+
assertEquals(expectedState, actualState);
3325
}
34-
26+
3527
@Test
3628
public void testSetState() {
37-
// Setup
38-
int expectedState = ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK;
39-
40-
// Optional: Precondition
41-
assertEquals(subject.getState(), 0);
42-
43-
// Exercise
44-
subject.setState(expectedState);
45-
46-
// Assertion - Postcondition
47-
assertEquals(expectedState, subject.getState());
29+
int expectedState = ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK;
30+
assertEquals(subject.getState(), 0);
31+
subject.setState(expectedState);
32+
assertEquals(expectedState, subject.getState());
4833
}
49-
34+
5035
@Test
5136
public void testUpdateStatusWhenStateIsNotDone() {
52-
// Setup
53-
int expectedStatus = 7;
54-
subject.setState(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK);
55-
56-
// Optional: Precondition
57-
assertEquals(0, subject.latestGoalStatus);
58-
59-
// Exercise
60-
subject.updateStatus(expectedStatus);
61-
62-
// Assertion - Postcondition
63-
assertEquals(expectedStatus, subject.latestGoalStatus);
37+
int expectedStatus = 7;
38+
subject.setState(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK);
39+
assertEquals(0, subject.getLatestGoalStatus());
40+
subject.updateStatus(expectedStatus);
41+
assertEquals(expectedStatus, subject.getLatestGoalStatus());
6442
}
65-
43+
6644
@Test
6745
public void testUpdateStatusWhenStateIsDone() {
68-
// Setup
69-
subject.setState(ClientStateMachine.ClientStates.DONE);
70-
71-
// Optional: Precondition
72-
assertEquals(0, subject.latestGoalStatus);
73-
74-
// Exercise
75-
subject.updateStatus(7);
76-
77-
// Assertion - Postcondition
78-
assertEquals(0, subject.latestGoalStatus);
46+
subject.setState(ClientStateMachine.ClientStates.DONE);
47+
assertEquals(0, subject.getLatestGoalStatus());
48+
subject.updateStatus(7);
49+
assertEquals(0, subject.getLatestGoalStatus());
7950
}
80-
51+
8152
@Test
8253
public void testCancelOnCancellableStates() {
83-
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK);
84-
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.PENDING);
85-
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.ACTIVE);
54+
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK);
55+
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.PENDING);
56+
checkCancelOnInitialCancellableState(ClientStateMachine.ClientStates.ACTIVE);
8657
}
87-
58+
8859
@Test
8960
public void testCancelOnNonCancellableStates() {
90-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.INVALID_TRANSITION);
91-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.NO_TRANSITION);
92-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_RESULT);
93-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_CANCEL_ACK);
94-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.RECALLING);
95-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.PREEMPTING);
96-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.DONE);
97-
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.LOST);
98-
}
99-
61+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.INVALID_TRANSITION);
62+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.NO_TRANSITION);
63+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_RESULT);
64+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.WAITING_FOR_CANCEL_ACK);
65+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.RECALLING);
66+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.PREEMPTING);
67+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.DONE);
68+
checkCancelOnInitialNonCancellableState(ClientStateMachine.ClientStates.LOST);
69+
}
70+
10071
private void checkCancelOnInitialCancellableState(int initialState) {
10172
subject.setState(initialState);
102-
103-
assertTrue("Failed test on initial state " + initialState, subject.cancel());
104-
105-
assertEquals("Failed test on initial state " + initialState, ClientStateMachine.ClientStates.WAITING_FOR_CANCEL_ACK, subject.getState());
73+
assertTrue("Failed test on initial state " + initialState, subject.cancel());
74+
assertEquals("Failed test on initial state " + initialState, ClientStateMachine.ClientStates.WAITING_FOR_CANCEL_ACK, subject.getState());
10675
}
10776

10877

10978
private void checkCancelOnInitialNonCancellableState(int initialState) {
11079
subject.setState(initialState);
111-
112-
assertFalse("Failed test on initial state " + initialState, subject.cancel());
113-
114-
assertEquals("Failed test on initial state " + initialState, initialState, subject.getState());
80+
assertFalse("Failed test on initial state " + initialState, subject.cancel());
81+
assertEquals("Failed test on initial state " + initialState, initialState, subject.getState());
11582
}
116-
83+
11784
@Test
11885
public void testResultReceivedWhileWaitingForResult() {
11986
subject.setState(ClientStateMachine.ClientStates.WAITING_FOR_RESULT);
12087
subject.resultReceived();
12188
assertEquals(ClientStateMachine.ClientStates.DONE, subject.getState());
12289
}
123-
90+
12491
@Test
12592
public void testResultReceivedWhileNotWaitingForResult() {
12693
checkResultReceivedWhileNotWaitingForResult(ClientStateMachine.ClientStates.INVALID_TRANSITION);
@@ -134,26 +101,26 @@ public void testResultReceivedWhileNotWaitingForResult() {
134101
checkResultReceivedWhileNotWaitingForResult(ClientStateMachine.ClientStates.DONE);
135102
checkResultReceivedWhileNotWaitingForResult(ClientStateMachine.ClientStates.LOST);
136103
}
137-
104+
138105
private void checkResultReceivedWhileNotWaitingForResult(int state) {
139106
subject.setState(state);
140107
subject.resultReceived();
141-
assertEquals("Failed test on initial state " + state, state, subject.getState());
108+
assertEquals("Failed test on initial state " + state, state, subject.getState());
142109
}
143-
110+
144111
@Test
145112
public void testGetTrasition() {
146-
Vector<Integer> expected;
147-
expected = new Vector<>(Arrays.asList(ClientStateMachine.ClientStates.PENDING));
148-
checkGetTransition(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK,
149-
actionlib_msgs.GoalStatus.PENDING, expected);
150-
151-
expected = new Vector<>(Arrays.asList(ClientStateMachine.ClientStates.PENDING,
152-
ClientStateMachine.ClientStates.WAITING_FOR_RESULT));
153-
checkGetTransition(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK,
154-
actionlib_msgs.GoalStatus.REJECTED, expected);
113+
Vector<Integer> expected;
114+
expected = new Vector<>(Arrays.asList(ClientStateMachine.ClientStates.PENDING));
115+
checkGetTransition(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK,
116+
actionlib_msgs.GoalStatus.PENDING, expected);
117+
118+
expected = new Vector<>(Arrays.asList(ClientStateMachine.ClientStates.PENDING,
119+
ClientStateMachine.ClientStates.WAITING_FOR_RESULT));
120+
checkGetTransition(ClientStateMachine.ClientStates.WAITING_FOR_GOAL_ACK,
121+
actionlib_msgs.GoalStatus.REJECTED, expected);
155122
}
156-
123+
157124
private void checkGetTransition(int initialState, int goalStatus, Vector<Integer> expected) {
158125
subject.setState(initialState);
159126
Vector<Integer> output = subject.getTransition(goalStatus);

0 commit comments

Comments
 (0)