Skip to content

Commit aa17f4f

Browse files
committed
Fixed up unit tests
1 parent b40e612 commit aa17f4f

File tree

5 files changed

+66
-106
lines changed

5 files changed

+66
-106
lines changed

src/main/java/bwapi/Game.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,6 @@ void addCommand(final CommandType type, final int value1, final int value2) {
26642664
sideEffects.enqueue(SideEffect.addCommand(type, value1, value2));
26652665
}
26662666

2667-
26682667
/**
26692668
* Convenience method for adding a game command from raw arguments.
26702669
*/
@@ -2685,4 +2684,8 @@ void addShape(final ShapeType type, final CoordinateType coordType, final int x1
26852684
void addShape(final ShapeType type, final CoordinateType coordType, final int x1, final int y1, final int x2, final int y2, final String text, final int extra2, final int color, final boolean isSolid) {
26862685
sideEffects.enqueue(SideEffect.addShape(type, coordType, x1, y1, x2, y2, text, extra2, color, isSolid));
26872686
}
2687+
2688+
void setAllUnits(List<Unit> units) {
2689+
allUnits = Collections.unmodifiableList(units);
2690+
}
26882691
}

src/test/java/bwapi/GameTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.io.IOException;
99
import java.nio.ByteBuffer;
1010
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
1113
import java.util.List;
1214
import org.junit.Before;
1315
import org.junit.Test;
@@ -20,13 +22,7 @@
2022
@RunWith(Theories.class)
2123
public class GameTest {
2224

23-
private List<Unit> allUnits = new ArrayList<>();
24-
private Game sut = new Game() {
25-
@Override
26-
public List<Unit> getAllUnits() {
27-
return allUnits;
28-
}
29-
};
25+
private Game sut = new Game();
3026
private Unit dummy;
3127

3228
@DataPoints("overlapping")
@@ -63,7 +59,7 @@ public void setup() {
6359
public void shouldFindOverlappingUnits(
6460
@FromDataPoints("overlapping") Pair<Position, Position> rect) {
6561
// GIVEN
66-
allUnits.add(dummy);
62+
sut.setAllUnits(Collections.singletonList(dummy));
6763

6864
// WHEN
6965
List<Unit> unitsInRectangle = sut
@@ -77,7 +73,7 @@ public void shouldFindOverlappingUnits(
7773
public void shouldNotFindNonOverlappingUnits(
7874
@FromDataPoints("non-overlapping") Pair<Position, Position> rect) {
7975
// GIVEN
80-
allUnits.add(dummy);
76+
sut.setAllUnits(Collections.singletonList(dummy));
8177

8278
// WHEN
8379
List<Unit> unitsInRectangle = sut

src/test/java/bwapi/PointTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.junit.Test;
44

5+
import java.nio.ByteBuffer;
56
import java.util.Random;
67

78
import static org.junit.Assert.*;
9+
import static org.junit.Assert.assertTrue;
810
import static org.mockito.Mockito.mock;
911
import static org.mockito.Mockito.when;
1012

@@ -54,10 +56,16 @@ public void alternativeConstructorTest() {
5456

5557
@Test
5658
public void isValidChecks() {
57-
Game game = mock(Game.class);
58-
when(game.mapPixelWidth()).thenReturn(32 * 256);
59-
when(game.mapPixelHeight()).thenReturn(32 * 256);
60-
59+
Game game = new Game();
60+
game.clientData().setBuffer(GameBuilder.binToBufferUnchecked(GameBuilder.DEFAULT_BUFFER_PATH));
61+
game.clientData().gameData().setMapWidth(256);
62+
game.clientData().gameData().setMapHeight(256);
63+
game.init();
64+
65+
assertEquals(256, game.mapHeight());
66+
assertEquals(256, game.mapWidth());
67+
assertEquals(32 * 256, game.mapPixelHeight());
68+
assertEquals(32 * 256, game.mapPixelWidth());
6169
assertTrue(new Position(0,0).isValid(game));
6270
assertTrue(new Position(1, 1).isValid(game));
6371

src/test/java/bwapi/SynchronizationEnvironment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
class SynchronizationEnvironment {
1717
BWClientConfiguration configuration;
1818
BWClient bwClient;
19-
private BWEventListener listener;
2019
private Client client;
2120
private int onEndFrame;
2221
private long bwapiDelayMs;
2322
private Map<Integer, Runnable> onFrames;
2423

2524
SynchronizationEnvironment() {
25+
BWEventListener listener = mock(BWEventListener.class);
26+
2627
configuration = new BWClientConfiguration();
27-
listener = mock(BWEventListener.class);
2828
client = mock(Client.class);
2929
bwClient = new BWClient(listener);
3030
bwClient.setClient(client);
@@ -42,7 +42,7 @@ class SynchronizationEnvironment {
4242
doAnswer(answer -> {
4343
clientUpdate();
4444
return null;
45-
}).when(client).update();
45+
}).when(client).sendFrameReceiveFrame();
4646
doAnswer(answer -> {
4747
configuration.log("Test: onStart()");
4848
return null;
@@ -52,11 +52,12 @@ class SynchronizationEnvironment {
5252
return null;
5353
}).when(listener).onEnd(anyBoolean());
5454
doAnswer(answer -> {
55-
configuration.log("Test: onFrame()");
55+
configuration.log("Test: onFrame() start");
5656
int botFrame = bwClient.getGame().getFrameCount();
5757
if (onFrames.containsKey(botFrame)) {
5858
onFrames.get(botFrame).run();
5959
}
60+
configuration.log("Test: onFrame() end");
6061
return null;
6162
}).when(listener).onFrame();
6263
}

src/test/java/bwapi/SynchronizationTest.java

Lines changed: 40 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@ public void async_IfBotDelay_ThenClientStalls() {
115115
environment.runGame();
116116
}
117117

118-
@Test
119-
public void async_IfUnsafe_ThenBotProceedsImmediately() {
120-
SynchronizationEnvironment environment = new SynchronizationEnvironment();
121-
environment.configuration.async = true;
122-
environment.configuration.asyncUnsafe = true;
123-
environment.configuration.maxFrameDurationMs = 100;
124-
environment.configuration.logVerbosely = true;
125-
126-
environment.onFrame(1, () -> {
127-
assertEquals("Bot should be observing frame 1", 1, environment.bwClient.getGame().getFrameCount());
128-
assertEquals("Live game should be observing frame 1", 1, environment.liveGameData().getFrameCount());
129-
environment.liveGameData().setFps(12345);
130-
assertEquals("Bot should be observing live game", 12345, environment.bwClient.getGame().getFPS());
131-
sleepUnchecked(50);
132-
assertFalse("Bot should be observing frame buffer", 12345 == environment.bwClient.getGame().getFPS());
133-
});
134-
135-
environment.runGame(3);
136-
}
137-
138118
@Test
139119
public void async_IfFrameZeroWaitsEnabled_ThenAllowInfiniteTime() {
140120
SynchronizationEnvironment environment = new SynchronizationEnvironment();
@@ -150,7 +130,7 @@ public void async_IfFrameZeroWaitsEnabled_ThenAllowInfiniteTime() {
150130
assertEquals("Bot should not be behind the live game", 0, environment.bwClient.framesBehind());
151131
});
152132

153-
environment.runGame();
133+
environment.runGame(2);
154134
}
155135

156136
@Test
@@ -168,25 +148,7 @@ public void async_IfFrameZeroWaitsDisabled_ThenClientBuffers() {
168148
assertEquals("Bot should be behind the live game", 2, environment.bwClient.framesBehind());
169149
});
170150

171-
environment.runGame();
172-
}
173-
174-
@Test
175-
public void async_MeasurePerformance_TotalFrameDuration() {
176-
final int frames = 10;
177-
final int frameSleep = 20;
178-
SynchronizationEnvironment environment = new SynchronizationEnvironment();
179-
environment.configuration.async = true;
180-
environment.configuration.unlimitedFrameZero = true;
181-
environment.configuration.maxFrameDurationMs = frameSleep + 20;
182-
IntStream.range(0, frames).forEach(i -> environment.onFrame(i, () -> {
183-
sleepUnchecked(frameSleep);
184-
}));
185-
environment.runGame(frames);
186-
187-
// Assume copying accounts for almost all the frame time except what the bot uses
188-
double meanCopy = environment.metrics().copyingToBuffer.avgValue;
189-
assertWithin("Total frame duration: Average", environment.metrics().totalFrameDuration.avgValue, meanCopy + frameSleep, MS_MARGIN);
151+
environment.runGame(2);
190152
}
191153

192154
@Test
@@ -199,10 +161,10 @@ public void async_MeasurePerformance_CopyingToBuffer() {
199161
final double maxObserved = 15;
200162
final double meanObserved = (minObserved + maxObserved) / 2;
201163
final double rangeObserved = (maxObserved - minObserved) / 2;
202-
assertWithin("Copy to buffer: minimum", environment.metrics().copyingToBuffer.minValue, meanObserved, rangeObserved);
203-
assertWithin("Copy to buffer: maximum", environment.metrics().copyingToBuffer.maxValue, meanObserved, rangeObserved);
204-
assertWithin("Copy to buffer: average", environment.metrics().copyingToBuffer.avgValue, meanObserved, rangeObserved);
205-
assertWithin("Copy to buffer: previous", environment.metrics().copyingToBuffer.lastValue, meanObserved, rangeObserved);
164+
assertWithin("Copy to buffer: minimum", environment.metrics().copyingToBuffer.runningTotal.min, meanObserved, rangeObserved);
165+
assertWithin("Copy to buffer: maximum", environment.metrics().copyingToBuffer.runningTotal.max, meanObserved, rangeObserved);
166+
assertWithin("Copy to buffer: average", environment.metrics().copyingToBuffer.runningTotal.mean, meanObserved, rangeObserved);
167+
assertWithin("Copy to buffer: previous", environment.metrics().copyingToBuffer.runningTotal.last, meanObserved, rangeObserved);
206168
}
207169

208170
@Test
@@ -214,25 +176,25 @@ public void async_MeasurePerformance_FrameBufferSizeAndFramesBehind() {
214176
environment.configuration.maxFrameDurationMs = 20;
215177

216178
environment.onFrame(5, () -> {
217-
assertWithin("5: Frame buffer average", 0, environment.metrics().frameBufferSize.avgValue, 0.1);
218-
assertWithin("5: Frame buffer minimum", 0, environment.metrics().frameBufferSize.minValue, 0.1);
219-
assertWithin("5: Frame buffer maximum", 0, environment.metrics().frameBufferSize.maxValue, 0.1);
220-
assertWithin("5: Frame buffer previous", 0, environment.metrics().frameBufferSize.lastValue, 0.1);
221-
assertWithin("5: Frames behind average", 0, environment.metrics().framesBehind.avgValue, 0.1);
222-
assertWithin("5: Frames behind minimum", 0, environment.metrics().framesBehind.minValue, 0.1);
223-
assertWithin("5: Frames behind maximum", 0, environment.metrics().framesBehind.maxValue, 0.1);
224-
assertWithin("5: Frames behind previous", 0, environment.metrics().framesBehind.lastValue, 0.1);
179+
assertWithin("5: Frame buffer average", 0, environment.metrics().frameBufferSize.runningTotal.mean, 0.1);
180+
assertWithin("5: Frame buffer minimum", 0, environment.metrics().frameBufferSize.runningTotal.min, 0.1);
181+
assertWithin("5: Frame buffer maximum", 0, environment.metrics().frameBufferSize.runningTotal.max, 0.1);
182+
assertWithin("5: Frame buffer previous", 0, environment.metrics().frameBufferSize.runningTotal.last, 0.1);
183+
assertWithin("5: Frames behind average", 0, environment.metrics().framesBehind.runningTotal.mean, 0.1);
184+
assertWithin("5: Frames behind minimum", 0, environment.metrics().framesBehind.runningTotal.min, 0.1);
185+
assertWithin("5: Frames behind maximum", 0, environment.metrics().framesBehind.runningTotal.max, 0.1);
186+
assertWithin("5: Frames behind previous", 0, environment.metrics().framesBehind.runningTotal.last, 0.1);
225187
sleepUnchecked(200);
226188
});
227189
environment.onFrame(6, () -> {
228-
assertWithin("6: Frame buffer average", 1 / 6.0 + 2 / 7.0, environment.metrics().frameBufferSize.avgValue, 0.1);
229-
assertWithin("6: Frame buffer minimum", 0, environment.metrics().frameBufferSize.minValue, 0.1);
230-
assertWithin("6: Frame buffer maximum", 2, environment.metrics().frameBufferSize.maxValue, 0.1);
231-
assertWithin("6: Frame buffer previous", 2, environment.metrics().frameBufferSize.lastValue, 0.1);
232-
assertWithin("6: Frames behind average", 1 / 6.0, environment.metrics().framesBehind.avgValue, 0.1);
233-
assertWithin("6: Frames behind minimum", 0, environment.metrics().framesBehind.minValue, 0.1);
234-
assertWithin("6: Frames behind maximum", 1, environment.metrics().framesBehind.maxValue, 0.1);
235-
assertWithin("6: Frames behind previous", 1, environment.metrics().framesBehind.lastValue, 0.1);
190+
assertWithin("6: Frame buffer average", 1 / 6.0 + 2 / 7.0, environment.metrics().frameBufferSize.runningTotal.mean, 0.1);
191+
assertWithin("6: Frame buffer minimum", 0, environment.metrics().frameBufferSize.runningTotal.min, 0.1);
192+
assertWithin("6: Frame buffer maximum", 2, environment.metrics().frameBufferSize.runningTotal.max, 0.1);
193+
assertWithin("6: Frame buffer previous", 2, environment.metrics().frameBufferSize.runningTotal.last, 0.1);
194+
assertWithin("6: Frames behind average", 1 / 6.0, environment.metrics().framesBehind.runningTotal.mean, 0.1);
195+
assertWithin("6: Frames behind minimum", 0, environment.metrics().framesBehind.runningTotal.min, 0.1);
196+
assertWithin("6: Frames behind maximum", 1, environment.metrics().framesBehind.runningTotal.max, 0.1);
197+
assertWithin("6: Frames behind previous", 1, environment.metrics().framesBehind.runningTotal.last, 0.1);
236198
});
237199

238200
environment.runGame(8);
@@ -256,36 +218,26 @@ public void MeasurePerformance_BotResponse() {
256218
sleepUnchecked(100);
257219
});
258220
environment.onFrame(2, () -> {
259-
assertWithin("2: Bot response average", 100, environment.metrics().botResponse.avgValue, MS_MARGIN);
260-
assertWithin("2: Bot response minimum", 100, environment.metrics().botResponse.minValue, MS_MARGIN);
261-
assertWithin("2: Bot response maximum", 100, environment.metrics().botResponse.maxValue, MS_MARGIN);
262-
assertWithin("2: Bot response previous", 100, environment.metrics().botResponse.lastValue, MS_MARGIN);
221+
assertWithin("2: Bot response average", 100, environment.metrics().botResponse.runningTotal.mean, MS_MARGIN);
222+
assertWithin("2: Bot response minimum", 100, environment.metrics().botResponse.runningTotal.min, MS_MARGIN);
223+
assertWithin("2: Bot response maximum", 100, environment.metrics().botResponse.runningTotal.max, MS_MARGIN);
224+
assertWithin("2: Bot response previous", 100, environment.metrics().botResponse.runningTotal.last, MS_MARGIN);
263225
sleepUnchecked(300);
264226
});
265227
environment.onFrame(3, () -> {
266-
assertWithin("3: Bot response average", 200, environment.metrics().botResponse.avgValue, MS_MARGIN);
267-
assertWithin("3: Bot response minimum", 100, environment.metrics().botResponse.minValue, MS_MARGIN);
268-
assertWithin("3: Bot response maximum", 300, environment.metrics().botResponse.maxValue, MS_MARGIN);
269-
assertWithin("3: Bot response previous", 300, environment.metrics().botResponse.lastValue, MS_MARGIN);
228+
assertWithin("3: Bot response average", 200, environment.metrics().botResponse.runningTotal.mean, MS_MARGIN);
229+
assertWithin("3: Bot response minimum", 100, environment.metrics().botResponse.runningTotal.min, MS_MARGIN);
230+
assertWithin("3: Bot response maximum", 300, environment.metrics().botResponse.runningTotal.max, MS_MARGIN);
231+
assertWithin("3: Bot response previous", 300, environment.metrics().botResponse.runningTotal.last, MS_MARGIN);
270232
sleepUnchecked(200);
271233
});
272234

273235
environment.runGame(4);
274236

275-
assertWithin("Final: Bot response average", 200, environment.metrics().botResponse.avgValue, MS_MARGIN);
276-
assertWithin("Final: Bot response minimum", 100, environment.metrics().botResponse.minValue, MS_MARGIN);
277-
assertWithin("Final: Bot response maximum", 300, environment.metrics().botResponse.maxValue, MS_MARGIN);
278-
assertWithin("Final: Bot response previous", 200, environment.metrics().botResponse.lastValue, MS_MARGIN);
279-
}
280-
281-
@Test
282-
public void MeasurePerformance_BwapiResponse() {
283-
final long bwapiDelayMs = 50;
284-
SynchronizationEnvironment environment = new SynchronizationEnvironment();
285-
environment.setBwapiDelayMs(bwapiDelayMs);
286-
environment.runGame();
287-
System.out.println(environment.metrics());
288-
assertWithin("BWAPI Response: Average", environment.metrics().bwapiResponse.avgValue, bwapiDelayMs, MS_MARGIN);
237+
assertWithin("Final: Bot response average", 200, environment.metrics().botResponse.runningTotal.mean, MS_MARGIN);
238+
assertWithin("Final: Bot response minimum", 100, environment.metrics().botResponse.runningTotal.min, MS_MARGIN);
239+
assertWithin("Final: Bot response maximum", 300, environment.metrics().botResponse.runningTotal.max, MS_MARGIN);
240+
assertWithin("Final: Bot response previous", 200, environment.metrics().botResponse.runningTotal.last, MS_MARGIN);
289241
}
290242

291243
@Test
@@ -298,8 +250,8 @@ public void MeasurePerformance_BotIdle() {
298250
environment.configuration.unlimitedFrameZero = true;
299251
environment.setBwapiDelayMs(bwapiDelayMs);
300252
environment.runGame(frames);
301-
double expected = environment.metrics().copyingToBuffer.avgValue + bwapiDelayMs;
302-
assertWithin("Bot Idle: Average", environment.metrics().botIdle.avgValue, expected, MS_MARGIN);
253+
double expected = environment.metrics().copyingToBuffer.runningTotal.mean + bwapiDelayMs;
254+
assertWithin("Bot Idle: Average", environment.metrics().botIdle.runningTotal.mean, expected, MS_MARGIN);
303255
}
304256

305257
@Test
@@ -315,10 +267,10 @@ public void async_MeasurePerformance_IntentionallyBlocking() {
315267
});
316268
environment.onFrame(2, () -> {
317269
assertWithin(
318-
"2: Intentionally blocking previous",
319-
environment.metrics().intentionallyBlocking.lastValue,
320-
frameDelayMs - environment.configuration.asyncFrameBufferCapacity * environment.configuration.maxFrameDurationMs,
321-
MS_MARGIN);
270+
"2: Intentionally blocking previous",
271+
environment.metrics().intentionallyBlocking.runningTotal.last,
272+
frameDelayMs - environment.configuration.asyncFrameBufferCapacity * environment.configuration.maxFrameDurationMs,
273+
MS_MARGIN);
322274
sleepUnchecked(100);
323275
});
324276
environment.runGame(3);

0 commit comments

Comments
 (0)