105 lines
5.1 KiB
Java
105 lines
5.1 KiB
Java
package cloud.molberg.mgutility;
|
|
|
|
public final class VehiclePowerOffControllerTest {
|
|
private static final class FakeTransport implements VehiclePowerOffController.Transport {
|
|
boolean compatible = true;
|
|
int gear = 1;
|
|
int gearAfterSpeed = 1;
|
|
float speed;
|
|
boolean accepted = true;
|
|
boolean failRead;
|
|
boolean failSend;
|
|
int reads;
|
|
int writes;
|
|
|
|
@Override public boolean isCompatible() { reads++; return compatible; }
|
|
@Override public int getGear() {
|
|
reads++;
|
|
if (failRead) throw new IllegalStateException("Disconnected");
|
|
return gear;
|
|
}
|
|
@Override public float getSpeed() { reads++; gear = gearAfterSpeed; return speed; }
|
|
@Override public boolean sendPowerOff() {
|
|
writes++;
|
|
if (failSend) throw new IllegalStateException("Disconnected during send");
|
|
return accepted;
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
FakeTransport transport = new FakeTransport();
|
|
VehiclePowerOffController controller = new VehiclePowerOffController(transport);
|
|
check(transport.reads == 0 && transport.writes == 0, "Construction must not access the car");
|
|
expect(VehiclePowerOffController.Result.CANCELLED, controller.request(false));
|
|
check(transport.reads == 0 && transport.writes == 0, "Cancellation must not access the car");
|
|
expect(VehiclePowerOffController.Result.REQUEST_SENT, controller.request(true));
|
|
check(transport.writes == 1, "Confirmed stationary Park sends exactly once");
|
|
expect(VehiclePowerOffController.Result.ALREADY_REQUESTED, controller.request(true));
|
|
check(transport.writes == 1, "Duplicate confirmation must not resend");
|
|
|
|
expect(VehiclePowerOffController.Result.UNSUPPORTED, new VehiclePowerOffController(null).request(true));
|
|
transport = new FakeTransport();
|
|
transport.compatible = false;
|
|
blocked(transport, VehiclePowerOffController.Result.UNSUPPORTED);
|
|
for (int gear : new int[] { -1, 0, 2, 3, 4, 255 }) {
|
|
transport = new FakeTransport();
|
|
transport.gear = gear;
|
|
blocked(transport, VehiclePowerOffController.Result.NOT_PARKED);
|
|
}
|
|
for (float speed : new float[] { Float.NaN, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY,
|
|
-1f, -0.01f, 0.01f, 1f, 100f }) {
|
|
transport = new FakeTransport();
|
|
transport.speed = speed;
|
|
blocked(transport, VehiclePowerOffController.Result.NOT_STATIONARY);
|
|
}
|
|
transport = new FakeTransport();
|
|
transport.gearAfterSpeed = 4;
|
|
blocked(transport, VehiclePowerOffController.Result.NOT_PARKED);
|
|
transport = new FakeTransport();
|
|
transport.failRead = true;
|
|
blocked(transport, VehiclePowerOffController.Result.FAILED);
|
|
|
|
for (boolean throwOnSend : new boolean[] { false, true }) {
|
|
transport = new FakeTransport();
|
|
transport.accepted = false;
|
|
transport.failSend = throwOnSend;
|
|
controller = new VehiclePowerOffController(transport);
|
|
expect(VehiclePowerOffController.Result.FAILED, controller.request(true));
|
|
expect(VehiclePowerOffController.Result.ALREADY_REQUESTED, controller.request(true));
|
|
check(transport.writes == 1, "Uncertain delivery must not retry");
|
|
}
|
|
|
|
transport = new FakeTransport();
|
|
controller = new VehiclePowerOffController(transport);
|
|
transport.gear = 4;
|
|
expect(VehiclePowerOffController.Result.NOT_PARKED, controller.request(true));
|
|
transport.gear = 1;
|
|
expect(VehiclePowerOffController.Result.REQUEST_SENT, controller.request(true));
|
|
check(transport.writes == 1, "A new confirmation rereads live state after a blocked attempt");
|
|
|
|
final FakeTransport concurrentTransport = new FakeTransport();
|
|
final VehiclePowerOffController concurrentController = new VehiclePowerOffController(concurrentTransport);
|
|
Runnable request = new Runnable() {
|
|
@Override public void run() { concurrentController.request(true); }
|
|
};
|
|
Thread first = new Thread(request);
|
|
Thread second = new Thread(request);
|
|
first.start(); second.start(); first.join(); second.join();
|
|
check(concurrentTransport.writes == 1, "Concurrent confirmations must not duplicate writes");
|
|
System.out.println("VehiclePowerOffControllerTest: OK");
|
|
}
|
|
|
|
private static void blocked(FakeTransport transport, VehiclePowerOffController.Result expected) {
|
|
expect(expected, new VehiclePowerOffController(transport).request(true));
|
|
check(transport.writes == 0, "Blocked state must never send a power request");
|
|
}
|
|
|
|
private static void expect(VehiclePowerOffController.Result expected, VehiclePowerOffController.Result actual) {
|
|
check(expected == actual, "Expected " + expected + ", got " + actual);
|
|
}
|
|
|
|
private static void check(boolean condition, String message) {
|
|
if (!condition) throw new AssertionError(message);
|
|
}
|
|
}
|