Skip to content

Commit 469de08

Browse files
committed
Chg: #86 - add ClockTicksToCallbackRelationshipTest UT to document the clock's behavior
1 parent c8cdea3 commit 469de08

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ subprojects {
5959

6060

6161
dependencies{
62+
testCompile(
63+
'org.mockito:mockito-core:+'
64+
)
6265
testImplementation(
6366
'org.junit.jupiter:junit-jupiter-api:5.1.0',
6467
'org.junit.jupiter:junit-jupiter-params:5.1.0',
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright 2008 - 2019, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io>
3+
*
4+
*
5+
* This file is part of JBotSim.
6+
*
7+
* JBotSim is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* JBotSim is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with JBotSim. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package io.jbotsim.core;
23+
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
import static org.mockito.Mockito.*;
29+
30+
/**
31+
* <p>These tests enforce basic behaviours linking the number of clock ticks to:</p>
32+
* <ul>
33+
* <li>the value returned by {@link ClockManager#currentTime()}</li>
34+
* <li>the number of time some of the main callbacks (onStart(), onPreClock(), onClock(), onPostClock()) have been
35+
* called</li>
36+
* </ul>
37+
*/
38+
class ClockTicksToCallbacksRelationshipTest {
39+
MockNode mockedNode;
40+
Topology topology;
41+
42+
@BeforeEach
43+
void setUp() {
44+
mockedNode = spy(new MockNode());
45+
topology = initializeTopology();
46+
}
47+
48+
private Topology initializeTopology() {
49+
Topology topology = new Topology();
50+
topology.setDefaultNodeModel(MockNode.class);
51+
topology.setClockModel(MockedClock.class);
52+
topology.addNode(mockedNode);
53+
return topology;
54+
}
55+
56+
@Test
57+
void beforeStart_roundZero_noCallbackCalled() {
58+
59+
checkTime(0);
60+
checkOnStart(0, -1);
61+
checkOnClocks(0, -1);
62+
63+
}
64+
65+
66+
@Test
67+
void firstClockTick_roundZero_callbacksCalledOnce() {
68+
69+
int expectedTime = 0;
70+
int expectedNbCalls = 0;
71+
72+
topology.start();
73+
74+
checkStartedCase(expectedTime, expectedNbCalls);
75+
76+
fireMockedClock();
77+
expectedNbCalls++;
78+
checkStartedCase(expectedTime, expectedNbCalls);
79+
80+
}
81+
82+
@Test
83+
void hundredClockTicks_roundsIncrement_onStartOnce_onClocksIncrement() {
84+
85+
int expectedTime = 0;
86+
int expectedNbCalls = 0;
87+
88+
topology.start();
89+
90+
fireMockedClock();
91+
expectedNbCalls++;
92+
checkStartedCase(expectedTime, expectedNbCalls);
93+
94+
95+
for (int i = 1; i < 100; i++) {
96+
fireMockedClock();
97+
expectedTime++;
98+
expectedNbCalls++;
99+
checkStartedCase(expectedTime, expectedNbCalls);
100+
}
101+
}
102+
103+
private void checkTime(int expectedTime) {
104+
assertEquals(expectedTime, topology.getTime());
105+
}
106+
107+
private void checkOnClocks(int expectedNbCalls, int expectedTimeValue) {
108+
verify(mockedNode, times(expectedNbCalls)).onPreClock();
109+
verify(mockedNode, times(expectedNbCalls)).onClock();
110+
verify(mockedNode, times(expectedNbCalls)).onPostClock();
111+
112+
if(expectedNbCalls > 0) {
113+
assertEquals(expectedTimeValue, mockedNode.lastPreClockTime);
114+
assertEquals(expectedTimeValue, mockedNode.lastClockTime);
115+
assertEquals(expectedTimeValue, mockedNode.lastPostClockTime);
116+
}
117+
}
118+
119+
private void checkOnStart(int expectedNbCalls, int expectedTimeValue) {
120+
verify(mockedNode, times(expectedNbCalls)).onStart();
121+
122+
if(expectedNbCalls > 0)
123+
assertEquals(expectedTimeValue, mockedNode.lastStartTime);
124+
125+
}
126+
127+
private void checkStartedCase(int expectedTime, int expectedNbCalls) {
128+
checkTime(expectedTime);
129+
checkOnStart(1, 0);
130+
checkOnClocks(expectedNbCalls, expectedTime);
131+
}
132+
133+
private class MockNode extends Node {
134+
int lastStartTime = -1;
135+
int lastPreClockTime = -1;
136+
int lastClockTime = -1;
137+
int lastPostClockTime = -1;
138+
139+
@Override
140+
public void onStart() {
141+
super.onStart();
142+
lastStartTime = getTopology().getTime();
143+
}
144+
145+
@Override
146+
public void onPreClock() {
147+
super.onPreClock();
148+
lastPreClockTime = getTopology().getTime();
149+
}
150+
@Override
151+
public void onClock() {
152+
super.onClock();
153+
lastClockTime = getTopology().getTime();
154+
}
155+
@Override
156+
public void onPostClock() {
157+
super.onPostClock();
158+
lastPostClockTime = getTopology().getTime();
159+
}
160+
}
161+
162+
163+
private void fireMockedClock() {
164+
((MockedClock) topology.clockManager.clock).fireClock();
165+
}
166+
167+
static public class MockedClock extends Clock {
168+
169+
public void fireClock() {
170+
manager.onClock();
171+
}
172+
173+
public MockedClock(ClockManager manager) {
174+
super(manager);
175+
}
176+
177+
@Override
178+
public int getTimeUnit() {
179+
return 0;
180+
}
181+
182+
@Override
183+
public void setTimeUnit(int timeUnit) {
184+
185+
}
186+
187+
@Override
188+
public boolean isRunning() {
189+
return false;
190+
}
191+
192+
@Override
193+
public void start() {
194+
// fireClock();
195+
}
196+
197+
@Override
198+
public void pause() {
199+
200+
}
201+
202+
@Override
203+
public void resume() {
204+
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)