Fix ring health persistence and page acknowledgements

This commit is contained in:
James Babcock committed 2026-09-17 20:23:15 -07:00
1 parent 22c851a6b9
commit c595cb4b6e
10 files changed
+472 -92

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
package com.faceclaw.app;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
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 boolean ringConnected = true;
private boolean ringNotificationsReady = true;
private boolean ringHealthRspSeen;
private int ringHealthPageCounter;
private int seq;
private boolean failAck;
private boolean answer = true;
private boolean delayRsp;
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};
private static class SystemClock {
static long elapsedRealtime() { return System.nanoTime() / 1_000_000; }
}
private int nextRingSeqLocked() { return ++seq; }
private int nextRingNonceLocked() { return seq; }
private void logLine(String message) {}
private void sendRingDevicePing(int command) {}
private void page(int id) {
synchronized (lock) {
ringOutbound.add(new byte[] {(byte) id});
ringHealthPageCounter++;
lock.notifyAll();
}
}
private boolean writeRingFrame(byte[] frame, String what) {
if (Thread.holdsLock(lock)) throw new AssertionError("GATT write under session lock");
if (what.equals("page ack")) {
if (failAck) return false;
int pageId = frame[0] & 0xff;
writes.add("ack:" + pageId);
// Exercise a write longer than the idle timeout. Page two arrives
// only after page one's ACK has actually completed.
try { Thread.sleep(130); } catch (InterruptedException e) { throw new AssertionError(e); }
if (pageId % 10 == 1) callbacks.schedule(() -> page(pageId + 1), 5, TimeUnit.MILLISECONDS);
} else {
int command = RingProtocol.parse(frame).cmdHi;
writes.add("req:" + command);
if (answer) {
page(command * 10 + 1);
if (delayRsp) {
callbacks.schedule(() -> {
synchronized (lock) { ringHealthRspSeen = true; lock.notifyAll(); }
}, 20, TimeUnit.MILLISECONDS);
} else {
synchronized (lock) { ringHealthRspSeen = true; lock.notifyAll(); }
}
}
}
return true;
}
// TRANSFER_METHODS
@Override public void close() { callbacks.shutdownNow(); }
public static void main(String[] args) {
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.delayRsp = true;
if (!transfer.requestRingHealth()) throw new AssertionError("pull failed");
List<String> expected = new ArrayList<>();
for (int command : RingProtocol.HEALTH_COMMANDS) {
expected.add("req:" + command);
expected.add("ack:" + (command * 10 + 1));
expected.add("ack:" + (command * 10 + 2));
}
if (!transfer.writes.equals(expected)) throw new AssertionError("premature next type: " + transfer.writes);
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.failAck = true;
if (transfer.requestRingHealth()) throw new AssertionError("failed ACK completed pull");
if (transfer.writes.size() != 1) throw new AssertionError("advanced after failed ACK");
if (transfer.ringOutbound.size() != 1) throw new AssertionError("failed ACK was discarded");
transfer.failAck = false;
if (!transfer.awaitRingHealthDataIdle(RING_HEALTH_DATA_IDLE_MS)) throw new AssertionError("retry failed");
if (!transfer.ringOutbound.isEmpty()) throw new AssertionError("retry did not drain ACKs");
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.answer = false;
if (transfer.requestRingHealth()) throw new AssertionError("missing RSP completed pull");
if (transfer.writes.size() != 1) throw new AssertionError("advanced without RSP");
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.ringConnected = false;
if (transfer.awaitRingHealthDataIdle(100)) throw new AssertionError("disconnect completed transfer");
}
}
}
+149
View File
@@ -0,0 +1,149 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const ts = require("typescript");
// Run the actual live ingest, seed and file-backend modules with NativeScript
// files and the Java communicator replaced at their platform boundaries.
function liveEnvironment() {
const files = new Map();
const cache = new Map();
const faults = { append: null, remove: null, write: null };
const today = new Date();
today.setHours(0, 0, 0, 0);
const start = today.getTime();
let consumed = false;
const record = {
cmdHi: 1,
anchorUnixSeconds: (start + today.getTimezoneOffset() * 60000) / 1000,
groups: [{ hourIndex: 0, min: 40, max: 50, avg: 45 }],
};
const communicator = {
takeRingHealthBatch: () => ({
getWatermark: () => 1,
getRecords: () => ({ size: () => consumed ? 0 : 1, get: () => record }),
}),
clearRingHealthRecordsBelow: () => { consumed = true; return 1; },
};
const native = {
File: {
exists: (name) => files.has(name),
fromPath: (name) => ({
readTextSync: () => files.get(name),
appendTextSync: (text, onError) => {
if (faults.append) return onError(faults.append);
files.set(name, (files.get(name) ?? "") + text);
},
writeTextSync: (text, onError) => {
if (faults.write) return onError(faults.write);
files.set(name, text);
},
removeSync: (onError) => {
if (faults.remove) return onError(faults.remove);
files.delete(name);
},
}),
},
knownFolders: { documents: () => ({ getFolder: () => ({
path: "/health",
getEntitiesSync: () => [...files.keys()].map((name) => ({ path: name, name: path.basename(name) })),
}) }) },
};
function load(filename) {
if (!filename.endsWith(".ts")) filename += ".ts";
if (cache.has(filename)) return cache.get(filename).exports;
const module = { exports: {} };
cache.set(filename, module);
const code = ts.transpileModule(fs.readFileSync(filename, "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText;
vm.runInNewContext(code, {
module, exports: module.exports, Date,
require: (name) => name === "@nativescript/core" ? native : load(path.resolve(path.dirname(filename), name)),
console: { log() {}, warn() {} },
com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => communicator } } } },
}, { filename });
return module.exports;
}
return {
files, faults, start, consumed: () => consumed,
load: (name) => load(path.join(__dirname, "../app/health", name)),
};
}
test("first live sync removes fixture samples, sleep and rollups from memory and disk", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
const store = env.load("health-store-files").healthStore();
assert.equal(seed.seedFixturesIfNeeded(2), true);
assert.ok(store.sleepSessions().length > 0);
assert.ok(store.samplesInRange(env.start, env.start + 86400000).length > 1);
env.load("health-live").syncLiveRecords();
assert.equal(seed.isFixtureData(), false);
assert.equal(env.consumed(), true);
assert.equal(store.sleepSessions().length, 0);
const samples = store.samplesInRange(env.start, env.start + 86400000);
assert.equal(samples.length, 1);
assert.equal(samples[0].avg, 45);
const rollups = JSON.parse(env.files.get("/health/rollups.json"));
assert.equal(Object.keys(rollups.days).length, 1);
const day = Object.values(rollups.days)[0];
assert.deepEqual(Object.keys(day), ["heartRate"]);
assert.equal(day.heartRate.count, 1);
});
test("a failed fixture purge retains the badge and ring batch until retry succeeds", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
seed.seedFixturesIfNeeded(2);
const live = env.load("health-live");
env.faults.remove = new Error("delete failed");
assert.throws(() => live.syncLiveRecords(), /delete failed/);
assert.equal(env.consumed(), false);
assert.equal(seed.isFixtureData(), true);
env.faults.remove = null;
live.syncLiveRecords();
assert.equal(env.consumed(), true);
assert.equal(seed.isFixtureData(), false);
});
test("NativeScript append callback failures retain the batch and retry persists it", () => {
const env = liveEnvironment();
const live = env.load("health-live");
env.faults.append = { message: "disk full" };
assert.throws(() => live.syncLiveRecords(), /disk full/);
assert.equal(env.consumed(), false);
assert.equal([...env.files.keys()].filter((name) => name.includes("samples-")).length, 0);
env.faults.append = null;
assert.equal(live.syncLiveRecords().samplesWritten, 1);
assert.equal(env.consumed(), true);
assert.equal([...env.files.keys()].filter((name) => name.includes("samples-")).length, 1);
});
test("NativeScript rollup write callback failures propagate without consuming the batch", () => {
const env = liveEnvironment();
env.faults.write = { message: "write failed" };
assert.throws(() => env.load("health-live").syncLiveRecords(), /write failed/);
assert.equal(env.consumed(), false);
env.faults.write = null;
assert.equal(env.load("health-live").syncLiveRecords().samplesWritten, 0);
assert.equal(env.consumed(), true);
assert.equal(env.files.has("/health/rollups.json"), true);
assert.equal(env.load("health-seed").hasLiveData(), true);
assert.equal(env.load("health-seed").seedFixturesIfNeeded(), false);
});
test("a failed fixture append stays labelled and is purged before live ingest", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
env.faults.append = new Error("disk full");
assert.equal(seed.seedFixturesIfNeeded(2), false);
assert.equal(seed.isFixtureData(), true);
env.faults.append = null;
env.load("health-live").syncLiveRecords();
assert.equal(seed.isFixtureData(), false);
assert.equal(env.load("health-store-files").healthStore().sleepSessions().length, 0);
});
+63
View File
@@ -148,6 +148,69 @@ test("a single unreadable line does not cost the rest of the file", () => {
// ---------------------------------------------------------------------------
// Ingest: the rules that decline to guess
test("failed sample corrections stay retryable, including a partially stored batch", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const first = new Date(2026, 7, 31).getTime();
const second = new Date(2026, 8, 1).getTime();
const original = sample("heartRate", second, 50, 80, 60);
store.ingestSamples([original]);
const append = backend.append;
backend.append = (name, text) => {
if (name === "samples-2026-09.jsonl") throw new Error("disk full");
append(name, text);
};
const batch = [sample("heartRate", first, 50, 90, 70), sample("heartRate", second, 50, 100, 80)];
assert.throws(() => store.ingestSamples(batch), /disk full/);
assert.equal(store.samplesInRange(second, second + DAY_MS)[0].avg, 60);
backend.append = append;
assert.equal(store.ingestSamples(batch), 1, "only the failed shard should need retry");
const reloaded = new HealthStore(backend);
assert.equal(reloaded.samplesInRange(first, second + DAY_MS).length, 2);
assert.equal(reloaded.samplesInRange(second, second + DAY_MS)[0].avg, 80);
});
test("failed sleep appends and corrections do not poison deduplication", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const session = buildFixtures({ days: 1, nowMs: Date.now() }).sleep[0];
assert.ok(session);
const append = backend.append;
backend.append = () => { throw new Error("disk full"); };
assert.throws(() => store.ingestSleep([session]), /disk full/);
assert.equal(store.sleepSessions().length, 0);
backend.append = append;
assert.equal(store.ingestSleep([session]), 1);
const correction = { ...session, totalSec: session.totalSec + 60 };
backend.append = () => { throw new Error("disk full"); };
assert.throws(() => store.ingestSleep([correction]), /disk full/);
assert.equal(store.sleepSessions()[0].totalSec, session.totalSec);
backend.append = append;
assert.equal(store.ingestSleep([correction]), 1);
assert.equal(new HealthStore(backend).sleepSessions()[0].totalSec, correction.totalSec);
});
test("retry after a partial append preserves the first sample and sleep record", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const start = startOfLocalDay(Date.now());
const row = sample("heartRate", start, 50, 90, 70);
const session = buildFixtures({ days: 1, nowMs: Date.now() }).sleep[0];
const append = backend.append;
backend.append = (name, text) => {
append(name, text.slice(0, 12));
throw new Error("partial append");
};
assert.throws(() => store.ingestSamples([row]), /partial append/);
assert.throws(() => store.ingestSleep([session]), /partial append/);
backend.append = append;
assert.equal(store.ingestSamples([row]), 1);
assert.equal(store.ingestSleep([session]), 1);
const recovered = new HealthStore(backend);
assert.equal(recovered.samplesInRange(start, start + DAY_MS)[0].avg, 70);
assert.equal(recovered.sleepSessions()[0].totalSec, session.totalSec);
});
test("an unanchored hourly page is dropped, not dated", () => {
const result = convertRecords([
{
+35
View File
@@ -0,0 +1,35 @@
const test = require("node:test");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { execFileSync } = require("node:child_process");
test("ring health drains ACK-paced pages before advancing and aborts on write failure", () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "faceclaw-ring-health-"));
const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, "bin", name) : name;
const source = fs.readFileSync(path.join(__dirname,
"../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"]
.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");
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));
try {
execFileSync(javaBin("javac"), ["-d", directory, harness,
path.join(__dirname, "../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/RingProtocol.java"),
path.join(__dirname, "../notes/ring-protocol-selftest/RingProtocolSelfTest.java"),
], { stdio: "pipe" });
execFileSync(javaBin("java"), ["-ea", "-cp", directory, "com.faceclaw.app.RingHealthTransferTest"],
{ stdio: "pipe", timeout: 15000 });
execFileSync(javaBin("java"), ["-ea", "-cp", directory, "com.faceclaw.app.RingProtocolSelfTest"],
{ stdio: "pipe", timeout: 15000 });
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
});