All files / test/Ice/inactivityTimeout Client.js

100% Statements 88/88
100% Branches 13/13
100% Functions 4/4
100% Lines 88/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
// Copyright (c) ZeroC, Inc.
import { Ice } from "@zeroc/ice";
import { Test } from "./Test.js";
import { TestHelper, test } from "../../Common/TestHelper.js";
async function testClientInactivityTimeout(p, helper, disableCheck) {
    const output = helper.getWriter();
    if (disableCheck) {
        output.write("testing connection with a disabled client-side inactivity check... ");
    }
    else {
        output.write("testing that the client side inactivity timeout shuts down the connection... ");
    }
    await p.ice_ping();
    const connection = await p.ice_getConnection();
    test(connection !== null);
    if (disableCheck) {
        connection.disableInactivityCheck();
    }
    // The inactivity timeout is 3s on the client side and 5s on the server side. 4 seconds tests the client side.
    await Ice.Promise.delay(4000);
    await p.ice_ping();
    const connection2 = await p.ice_getConnection();
    if (disableCheck) {
        test(connection2 == connection);
    }
    else {
        test(connection2 != connection);
    }
    await connection2.close();
    output.writeLine("ok");
}
async function testWithOutstandingRequest(p, oneway, helper) {
    const output = helper.getWriter();
    const onewayString = oneway ? "one-way" : "two-way";
    output.write(`testing the inactivity timeout with an outstanding ${onewayString} request... `);
    if (oneway) {
        p = p.ice_oneway();
    }
    await p.ice_ping();
    const connection = await p.ice_getConnection();
    test(connection !== null);
    // The inactivity timeout is 3s on the client side and 5s on the server side; 4 seconds tests only the
    // client-side.
    await p.sleep(4000); // two-way blocks for 4 seconds; one-way is non-blocking
    if (oneway) {
        await Ice.Promise.delay(4000);
    }
    await p.ice_ping();
    const connection2 = await p.ice_getConnection();
    if (oneway) {
        // With a oneway invocation, the inactivity timeout on the client side shut down the first connection.
        test(connection2 != connection);
    }
    else {
        // With a two-way invocation, the inactivity timeout should not shutdown any connection.
        test(connection2 == connection);
    }
    await connection2.close();
    output.writeLine("ok");
}
export class Client extends TestHelper {
    async allTests() {
        const communicator = this.communicator();
        const p = new Test.TestIntfPrx(communicator, `test: ${this.getTestEndpoint()}`);
        await testClientInactivityTimeout(p, this, false);
        await testClientInactivityTimeout(p, this, true);
        await testWithOutstandingRequest(p, false, this);
        await testWithOutstandingRequest(p, true, this);
        await p.shutdown();
    }
    async run(args) {
        let communicator = null;
        try {
            const [properties] = this.createTestProperties(args);
            // We configure a low idle timeout to make sure we send heartbeats frequently. It's the sending of the
            // heartbeats that schedules the inactivity timer task.
            properties.setProperty("Ice.Connection.Client.IdleTimeout", "1");
            properties.setProperty("Ice.Connection.Client.InactivityTimeout", "3");
            [communicator] = this.initialize(properties);
            await this.allTests();
        }
        finally {
            if (communicator) {
                await communicator.destroy();
            }
        }
    }
}