66 lines
2.5 KiB
Java
66 lines
2.5 KiB
Java
package com.chaegeon.niro;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
|
|
public final class WeatherRefreshCountersTest {
|
|
private static int checks;
|
|
|
|
private static void ok(boolean actual) {
|
|
++checks;
|
|
if (!actual) { throw new AssertionError("check " + checks); }
|
|
}
|
|
|
|
private static String read(File file) throws Exception {
|
|
FileInputStream input = new FileInputStream(file);
|
|
try {
|
|
byte[] data = new byte[32];
|
|
int length = input.read(data);
|
|
ok(length > 0 && input.read() == -1);
|
|
return new String(data, 0, length, "US-ASCII");
|
|
} finally {
|
|
input.close();
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
File directory = File.createTempFile("weather-refresh-counters-", ".dir");
|
|
if (!directory.delete() || !directory.mkdir()) {
|
|
throw new IOException("test directory setup failed");
|
|
}
|
|
File signal = new File(directory, "signal.counter");
|
|
File callback = new File(directory, "callback.counter");
|
|
try {
|
|
WeatherRefreshCounters counters = new WeatherRefreshCounters(directory);
|
|
ok("0\n".equals(read(signal)));
|
|
ok("0\n".equals(read(callback)));
|
|
counters.signalAccepted();
|
|
counters.signalAccepted();
|
|
ok("2\n".equals(read(signal)));
|
|
ok("0\n".equals(read(callback)));
|
|
counters.freshCallbackCompleted();
|
|
ok("2\n".equals(read(signal)));
|
|
ok("1\n".equals(read(callback)));
|
|
ok(!new File(directory, "signal.counter.new").exists());
|
|
ok(!new File(directory, "callback.counter.new").exists());
|
|
File stale = new File(directory, "signal.counter.new");
|
|
ok(stale.createNewFile());
|
|
boolean failedClosed = false;
|
|
try {
|
|
counters.signalAccepted();
|
|
} catch (IOException expected) {
|
|
failedClosed = true;
|
|
}
|
|
ok(failedClosed);
|
|
ok("2\n".equals(read(signal)));
|
|
ok(stale.delete());
|
|
} finally {
|
|
if (signal.exists() && !signal.delete()) { throw new IOException("signal cleanup"); }
|
|
if (callback.exists() && !callback.delete()) { throw new IOException("callback cleanup"); }
|
|
if (!directory.delete()) { throw new IOException("directory cleanup"); }
|
|
}
|
|
System.out.println("result=PASS_WEATHER_REFRESH_COUNTERS checks=" + checks);
|
|
}
|
|
}
|