moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
292 stars 118 forks source link

Routes with autoAliases not considered for started #347

Open MichaelErmer opened 6 months ago

MichaelErmer commented 6 months ago

If a API gateway has a configuration using autoAliases, the service will be marker as "ready" (started) even if the routes are not registered yet:

name: "xyz",
routes: [{
    path: "/download",
    use: [cookieparser()],
    whitelist: ["xyz.download"],
    autoAliases: true,
    mappingPolicy: "restrict",
    logging: false,
}]
...
actions: {
    xyz: {
        rest: {
            method: "POST",
            path: "/"
        },
       ...
    }
}

If this syntax is used, it works as intended:

name: "xyz",
routes: [{
    path: "/download",
    use: [cookieparser()],
    whitelist: ["xyz.download"],
    aliases: {
        "POST /": "xyz.download"
    }
    mappingPolicy: "restrict",
    logging: false,
}]
...
actions: {
    download: {
        rest: {
            method: "POST",
            path: "/"
        },
       ...
    }
}

this test will randomly fail for the first case:


describe("test", () => {
    beforeAll(async () => {
        broker.createService(xyzService);
        await broker.start();
        await broker.waitForServices(["xyz"]);
    });
    // test to request /download
});
icebob commented 5 months ago

This is the expected functionality. The reason is, that the auto aliases function executes with a little delay to wait for all services should be started. In the case of a monolith, it looks like no reason to wait, but in a multi-node environment, if API gateway started, it doesn't mean all other services are loaded.

For tests, you should add a delay after the broker started, or set the debounceTime to zero in API gateway settings.