All files / test/Ice/middleware Server.js

95.31% Statements 122/128
87.5% Branches 14/16
66.66% Functions 8/12
95.31% Lines 122/128

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x 1x 1x 1x 1x 1x 6x 3x 3x 3x 3x 3x 3x 3x 3x 6x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 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 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 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 1x 1x 1x  
// Copyright (c) ZeroC, Inc.
import { Ice } from "@zeroc/ice";
import { Test } from "./Test.js";
import { TestHelper, test } from "../../Common/TestHelper.js";
class Middleware extends Ice.Object {
    async dispatch(request) {
        if (request.current.operation === "shutdown") {
            return this._next.dispatch(request);
        }
        else {
            this._inLog.push(this._name);
            const response = await this._next.dispatch(request);
            this._outLog.push(this._name);
            return response;
        }
    }
    constructor(next, name, inLog, outLog) {
        super();
        this._next = next;
        this._name = name;
        this._inLog = inLog;
        this._outLog = outLog;
    }
}
class NullLogger {
    print(_message) { }
    trace(_category, _message) { }
    warning(_message) { }
    error(_message) { }
    cloneWithPrefix(_prefix) {
        return this;
    }
}
class MyObjectI extends Test.MyObject {
    constructor() {
        super();
    }
    getName() {
        return "Foo";
    }
    shutdown(current) {
        current.adapter.getCommunicator().shutdown();
    }
}
export class Server extends TestHelper {
    async run(args) {
        let communicator = null;
        let echo = null;
        const inLog = [];
        const outLog = [];
        try {
            [communicator] = this.initialize(args);
            echo = new Test.EchoPrx(communicator, `__echo:${this.getTestEndpoint()}`);
            const adapter = await communicator.createObjectAdapter("");
            await echo.setConnection();
            echo.ice_getCachedConnection().setAdapter(adapter);
            this.serverReady();
            adapter.add(new MyObjectI(), new Ice.Identity("test"));
            adapter
                .use((next) => new Middleware(next, "A", inLog, outLog))
                .use((next) => new Middleware(next, "B", inLog, outLog))
                .use((next) => new Middleware(next, "C", inLog, outLog));
            await communicator.waitForShutdown();
            await this.testMiddlewareFactoryException(communicator);
        }
        finally {
            const out = this.getWriter();
            out.write("testing middleware execution order... ");
            test(inLog.length === 3);
            test(inLog[0] === "A");
            test(inLog[1] === "B");
            test(inLog[2] === "C");
            test(outLog.length === 3);
            test(outLog[0] === "C");
            test(outLog[1] === "B");
            test(outLog[2] === "A");
            out.writeLine("ok");
            if (echo) {
                await echo.shutdown();
            }
            if (communicator) {
                await communicator.destroy();
            }
        }
    }
    // Verifies a middleware factory exception makes all dispatches fail with a generic UnknownException.
    async testMiddlewareFactoryException(invokingCommunicator) {
        const out = this.getWriter();
        out.write("testing middleware factory exception... ");
        // Use a separate communicator with a null logger: the pipeline creation failure is logged as an error.
        const initData = new Ice.InitializationData();
        initData.properties = invokingCommunicator.getProperties().clone();
        initData.logger = new NullLogger();
        const [communicator] = this.initialize(initData);
        try {
            const echo = new Test.EchoPrx(communicator, `__echo:${this.getTestEndpoint()}`);
            const adapter = await communicator.createObjectAdapter("");
            await echo.setConnection();
            echo.ice_getCachedConnection().setAdapter(adapter);
            adapter.add(new MyObjectI(), new Ice.Identity("test"));
            adapter.use(() => {
                throw new Error("middleware factory exception");
            });
            const prx = new Test.MyObjectPrx(invokingCommunicator, `test: ${this.getTestEndpoint()}`);
            try {
                await prx.getName();
                test(false);
            }
            catch (ex) {
                test(ex instanceof Ice.UnknownException);
                // The message does not reveal the middleware factory exception.
                test(!ex.message.includes("middleware factory exception"));
            }
            // The failure is permanent for this object adapter.
            try {
                await prx.getName();
                test(false);
            }
            catch (ex) {
                test(ex instanceof Ice.UnknownException);
            }
        }
        finally {
            await communicator.destroy();
        }
        out.writeLine("ok");
    }
}