<!doctype html><!-- webkit-test-runner [ IPCTestingAPIEnabled=true ] -->
<title>Test that stream sync message crash is noticed immediately</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<body>
<script>
setup({ single_test: true });
if (window.IPC) { // For compiles with !ENABLE(IPC_TESTING_API)
    const defaultTimeout = 10;
    const bufferSize = 400;
    const streamTesterID = 4557;
    for (const processTarget of IPC.processTargets) {
        if (processTarget == "UI")
            continue; // Crashing UI is not supported.
        const [streamConnection, serverConnectionHandle] = IPC.createStreamClientConnection(bufferSize);
        streamConnection.open();
        IPC.sendMessage(processTarget, 0, IPC.messages.IPCTester_CreateStreamTester.name, [
            { type: 'uint64_t', value: streamTesterID },
            { type: 'StreamServerConnectionHandle', value: serverConnectionHandle },
        ]);
        const arguments = streamConnection.waitForMessage(streamTesterID, IPC.messages.IPCStreamTesterProxy_WasCreated.name, defaultTimeout);
        streamConnection.setSemaphores(arguments[0].value, arguments[1].value);

        // Test starts here.
        const result = streamConnection.sendIPCStreamTesterSyncCrashOnZero(streamTesterID, 78, defaultTimeout);
        assert_equals(result, 78,  `sync messages work for ${ processTarget }`);

        const start = Date.now();
        assert_throws_js(TypeError,
            () => { streamConnection.sendIPCStreamTesterSyncCrashOnZero(streamTesterID, 0, defaultTimeout); },
            `crashing sync message must return failure for ${ processTarget }`);
        assert_less_than(Date.now() - start, 500, `crashing sync message must complete in 500ms for ${ processTarget }`);
        console.log(`ttt: ${Date.now() - start}`);
        streamConnection.invalidate();
    }
}
done();
</script>
</body>
