hemerajs / hemera-testsuite

Helper library to write tests for Hemera.
MIT License
2 stars 4 forks source link

Hapi hemera testsuite Timeout error #12

Closed vforv closed 6 years ago

vforv commented 6 years ago

When I try use hemera with server.inject it doesn't work I am getting Timeout error, it looks like add is never called by act....

here is example:

L.test('Create general info', (done) => {
        const nats = new Nats()
        const hemera = new Hemera(nats, {
            logLevel: 'info'
        });

        hemera.ready(() => {
            hemera.add({
                topic: 'kycc',
                individual: false,
                create: 'general-info'
            }, (msg: any, done: any) => {
                console.log("TEST")
                done(null, "mock")
            })
        });

        const options = {
            method: "POST",
            url: "/v1/route",
            payload: {
            }
        };
        server.inject(options, (response: any) => {
            expect(JSON.parse(response.payload).statusCode).to.equal(200);
            done();
        });
    });

Act call will be called inside /v1/route route... What can be problem?

StarpTech commented 6 years ago

Hi, @vforv please post the full code. Do you use Hapi? Please also report all versions.

vforv commented 6 years ago

It's a little bit complex but I will put main part:

This function is hit when route /v1/route is called:

createGeneralInformation(req: HapiRequest, reply: Hapi.ReplyNoContinue) {

        req.hemera
            .act(
            {
                topic: 'kycc',
                individual: false,
                create: 'general-info'
            },
            (err: Error, result: any) => {
                if (err) {
                    return reply(Boom.boomify(err, { statusCode: 500 }));
                }
                return reply({data: result})
            })
    }

I checked route is 100% called bacuse when I print err inside this function it log this message:

{ TimeoutError: Timeout at timeoutHandler (/node_modules/hapi-hemera/node_modules/nats-hemera/lib/index.js:1380:21) at Timeout._onTimeout (/node_modules/nats/lib/nats.js:1233:9) at ontimeout (timers.js:365:14) at tryOnTimeout (timers.js:237:5) at Timer.listOnTimeout (timers.js:207:5) message: 'Timeout', app: 'test', isServer: false, pattern: 'create:general-info,individual:false,topic:kycc' }

I am using this versions:

"hapi": "^16.5.0",
"hapi-hemera": "^1.0.x",
"nats-hemera": "^3.1.6",
"hemera-testsuite": "^2.0.5",

I saw there is new version of hapi-hemera but I cannot run code if I use 2.0.x version...

StarpTech commented 6 years ago

The ready callback happens inside a new tick. You can't act when hemera is not ready.

vforv commented 6 years ago

Same happens if I put server.inject inside ready callback like this:

L.test('Create general info', (done) => {
        const nats = new Nats()
        const hemera = new Hemera(nats, {
            logLevel: 'info'
        });

        hemera.ready(() => {
            hemera.add({
                topic: 'kycc',
                individual: false,
                create: 'general-info'
            }, (msg: any, done: any) => {
                console.log("TEST")
                done(null, "mock")
            })

         const options = {
            method: "POST",
            url: "/v1/route",
            payload: {
            }
        };
        server.inject(options, (response: any) => {
            expect(JSON.parse(response.payload).statusCode).to.equal(200);
            done();
        });

        });
    });
StarpTech commented 6 years ago

Your example is too complex and I don't see the hapi handler.

vforv commented 6 years ago

Here is that part:

register: this.server.route({
                    method: 'POST',
                    path: '/v1/route',
                    handler: this.nonIndividualLogic.createGeneralInformation,
                    config: {
                        auth: 'amerbank-user',
                        description: 'Create general information for non-individuals',
                        validate: {
                            payload: createGeneralInfoValidator,
                            headers: {
                                Authorization: Joi.string()
                            },
                            options: {
                                allowUnknown: true
                            }
                        }
                    }
                })

Everything works well when run service and call routes with other servers... But only I have a problem with testing...

StarpTech commented 6 years ago

Which (Hapi, hemera-hapi, nats-hemera) versions do you use ?

StarpTech commented 6 years ago

Forget it, I see it.

StarpTech commented 6 years ago

Could you simplify your example so that you only use act and add with hapi ? Without auth, validation etc..

vforv commented 6 years ago

What did you mean? Everything works well when I run all services and call route... But the problem when I run it with the test so I think there is some problem with hemera-testsuite?

StarpTech commented 6 years ago

If you could provide an example which can I run immediately it would help me a lot.

StarpTech commented 6 years ago

You could create a repo with the issue. That would be nice.

vforv commented 6 years ago

Here: https://github.com/vforv/hemera-testsuite-error

If you run node index.js and visit route locahost:3000/v1/route add and act will work fine

But if you run node test.js it will return timeout error and add will never run...

StarpTech commented 6 years ago

I know what's the problem is. You pass a connection string to hapi-hemera instead the nats emulation. It's reasonable that hemera try to connect with a real NATS server.

StarpTech commented 6 years ago

You also use an old version and I don't support Hapi < 16. I recommend you to do full integration test or upgrade to Hapi 17.

StarpTech commented 6 years ago

Currently, we don't support to pass the nats emulation object to plugins like hapi-hemera it will create a new connection to the server that's why you get a timeout.

vforv commented 6 years ago

We don't need full integration tests for api gateway... So currently there is no way to emulate nats inside hapi-hemera?

StarpTech commented 6 years ago

It's no hassle to use a real nats server to test your application. We already provide great tooling with hemera-testsuite. If you need in-memory test you can create a PR in hapi-hemera.

vforv commented 6 years ago

Aha, I see now it is changed? We don't need any more to have gnatsd running in localhost to run tests? Is it possible to use remote nats server?

StarpTech commented 6 years ago

Yes, we provide two ways to create a test environment. Look in the readme https://github.com/hemerajs/hemera-testsuite

Is it possible to use remote nats server?

You can connect to any NATS Server but hemera-testsuite is responsible to bootstrap a server on localhost.

vforv commented 6 years ago

So I still need to have installed and running nats daemon in localhost?

StarpTech commented 6 years ago

If you only want to test Hemera you have two options:

The support of both solutions for the Hemera plugins is another question. Please create an issue in the specific repository.