forked from Blendman974/faceclaw
Fix remaining health review issues and document ring clock skew
This commit is contained in:
1 parent
7677dd254e
commit
27a5fe47ef
10 files changed
+323
-191
No files matched your search
+32
-2
@@ -6,13 +6,14 @@ import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public final class RingHealthTransferTest implements AutoCloseable {
|
||||
private final Object lock = new Object();
|
||||
private final ArrayDeque<byte[]> ringOutbound = new ArrayDeque<>();
|
||||
private final ScheduledExecutorService callbacks = Executors.newSingleThreadScheduledExecutor();
|
||||
private final List<String> writes = new ArrayList<>();
|
||||
private boolean running = true;
|
||||
private volatile boolean running = true;
|
||||
private boolean ringConnected = true;
|
||||
private boolean ringNotificationsReady = true;
|
||||
private boolean ringHealthRspSeen;
|
||||
@@ -21,6 +22,20 @@ public final class RingHealthTransferTest implements AutoCloseable {
|
||||
private boolean failAck;
|
||||
private boolean answer = true;
|
||||
private boolean delayRsp;
|
||||
private boolean pullRequested = true;
|
||||
private final CountDownLatch requestStarted = new CountDownLatch(1);
|
||||
private final String ringAddress = "ring";
|
||||
private long ringReconnectAfterMs;
|
||||
private static class ConnectionOptions { static final int RING_RECONNECT_DELAY_MS = 1000; }
|
||||
private static class BleManager { void disconnect(String address) {} }
|
||||
private final BleManager bleManager = new BleManager();
|
||||
private static String safeMessage(Throwable t) { return t.getMessage(); }
|
||||
private boolean shouldAttemptRingConnect() { return false; }
|
||||
private void tryConnectRing(String reason) { throw new AssertionError("unexpected connect"); }
|
||||
private void runRequestedRingHealthPull() {
|
||||
if (pullRequested) { pullRequested = false; requestRingHealth(); }
|
||||
}
|
||||
private void resumeAbortedRingHealthPull() {}
|
||||
private static final long RING_HEALTH_RSP_TIMEOUT_MS = 500;
|
||||
private static final long RING_HEALTH_DATA_IDLE_MS = 100;
|
||||
private static final int[] RING_DEVICE_PING_CMD_LO = {10, 1, 2, 11};
|
||||
@@ -53,6 +68,7 @@ public final class RingHealthTransferTest implements AutoCloseable {
|
||||
try { Thread.sleep(130); } catch (InterruptedException e) { throw new AssertionError(e); }
|
||||
if (pageId % 10 == 1) callbacks.schedule(() -> page(pageId + 1), 5, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
requestStarted.countDown();
|
||||
int command = RingProtocol.parse(frame).cmdHi;
|
||||
writes.add("req:" + command);
|
||||
if (answer) {
|
||||
@@ -73,7 +89,21 @@ public final class RingHealthTransferTest implements AutoCloseable {
|
||||
|
||||
@Override public void close() { callbacks.shutdownNow(); }
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
|
||||
transfer.answer = false;
|
||||
Thread worker = new Thread(transfer::runRingWorker);
|
||||
worker.start();
|
||||
if (!transfer.requestStarted.await(1, TimeUnit.SECONDS)) throw new AssertionError("no request");
|
||||
// The glasses sender can still take the session lock while the
|
||||
// ring is waiting on its RSP. Shutdown must cancel that wait too.
|
||||
long start = SystemClock.elapsedRealtime();
|
||||
synchronized (transfer.lock) { transfer.running = false; transfer.lock.notifyAll(); }
|
||||
worker.interrupt();
|
||||
worker.join(300);
|
||||
if (worker.isAlive()) throw new AssertionError("ring worker did not stop");
|
||||
if (SystemClock.elapsedRealtime() - start >= 300) throw new AssertionError("session lock blocked");
|
||||
}
|
||||
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
|
||||
transfer.delayRsp = true;
|
||||
if (!transfer.requestRingHealth()) throw new AssertionError("pull failed");
|
||||
|
||||
@@ -22,6 +22,7 @@ const assert = require("node:assert/strict");
|
||||
const { buildFixtures } = require("../.test-build/app/health/health-fixtures.js");
|
||||
const {
|
||||
dailySummary,
|
||||
dailyHypnogram,
|
||||
sleepNights,
|
||||
sleepSummary,
|
||||
stageSeconds,
|
||||
@@ -103,6 +104,32 @@ test("two sessions on one day are summed, not deduped to the longest", () => {
|
||||
Math.abs(nights[0].lightSec - (3600 + 1800) * 0.6) < 1e-6,
|
||||
"stage totals add across both sessions",
|
||||
);
|
||||
const summary = dailySummary([], [night(3600, 300), night(1800, 60)], today).sleep;
|
||||
assert.equal(summary.totalSec, 5400);
|
||||
assert.equal(summary.wakeSec, 360);
|
||||
assert.equal(summary.efficiencyPercent, 5400 / 5760 * 100);
|
||||
assert.equal(dailySummary([], [], today).sleep, null);
|
||||
});
|
||||
|
||||
test("split-night detail includes both blocks and leaves the gap unknown", () => {
|
||||
const today = startOfLocalDay(Date.now());
|
||||
const block = (hour, stageId) => ({
|
||||
dayStartMs: today, startMs: today + hour * 3600000,
|
||||
endMs: today + (hour + 1) * 3600000,
|
||||
totalSec: 3600, wakeSec: 0, remSec: 0, lightSec: 3600, deepSec: 0,
|
||||
segments: [{ stageId, halfMinutes: 120 }], timeResolved: true,
|
||||
});
|
||||
const first = block(1, 1), second = block(3, 2);
|
||||
const bands = dailyHypnogram([second, first, { ...block(1, 3), dayStartMs: today - DAY_MS }], today);
|
||||
assert.equal(bands.length, 3);
|
||||
assert.deepEqual(bands[1], { stage: null, seconds: 3600 });
|
||||
assert.equal(bands[0].seconds, 3600);
|
||||
assert.equal(bands[2].seconds, 3600);
|
||||
assert.notEqual(bands[0].stage, bands[2].stage);
|
||||
assert.equal(dailySummary([], [first, second], today).sleep.totalSec, 7200,
|
||||
"an unrecorded gap is not counted as sleep");
|
||||
assert.deepEqual(dailyHypnogram([{ ...first, segments: [] }], today),
|
||||
[{ stage: null, seconds: 3600 }]);
|
||||
});
|
||||
|
||||
test("a stage's lane duration comes from the named totals", () => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
@@ -11,12 +12,19 @@ test("ring health drains ACK-paced pages before advancing and aborts on write fa
|
||||
"../App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java"), "utf8");
|
||||
// Compile the actual worker methods with a fake GATT transport. This keeps
|
||||
// Android out of the JVM test without copying the transfer implementation.
|
||||
const methods = ["requestRingHealth", "awaitRingHealthRsp", "awaitRingHealthDataIdle", "flushRingOutbound"]
|
||||
const methods = ["requestRingHealth", "awaitRingHealthRsp", "awaitRingHealthDataIdle", "flushRingOutbound", "runRingWorker"]
|
||||
.map((name) => {
|
||||
const match = source.match(new RegExp(` private (?:boolean|int|void) ${name}\\([^]*?^ }`, "m"));
|
||||
if (!match) throw new Error(`Missing production method: ${name}`);
|
||||
return match[0];
|
||||
}).join("\n");
|
||||
for (const signature of ["@Override public void run()", "private void connectLoopOnce()"]) {
|
||||
const start = source.indexOf(signature);
|
||||
assert.ok(start >= 0);
|
||||
const body = source.slice(start, source.indexOf("\n }", start));
|
||||
assert.doesNotMatch(body, /tryConnectRing\(|requestRingHealth\(|flushRingOutbound\(|runRequestedRingHealthPull\(/,
|
||||
"the glasses sender must not call blocking ring operations");
|
||||
}
|
||||
const fixture = fs.readFileSync(path.join(__dirname, "fixtures/RingHealthTransferTest.java"), "utf8");
|
||||
const harness = path.join(directory, "RingHealthTransferTest.java");
|
||||
fs.writeFileSync(harness, fixture.replace("// TRANSFER_METHODS", methods));
|
||||
|
||||
Reference in new issue
Block a user