resonatecoop / api

The one Resonate API to rule them all

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RedisAdapter always opens a connection and does not offer a way to close it

fgblomqvist opened this issue · comments

Fiddling around with the tests I kept wondering why mocha kept running forever even though all the cases finished. Any test file that imports from the testConfig keep running forever, even if it just just a expect(true).to.eql(true).

I discovered that testConfig imports the Redis adapter from redis-adapter.js and that that file in turn has a side effect of opening a redis connection, no matter what. Furthermore, there is no way to close that connection since the client sits unexported in the global scope.

There are several issues at play here:

  • The client should not connect to anything on import, that is bad practice.
    • Instead, make the connection inside the RedisAdapter constructor (why otherwise have a constructor?) and store it on this (rather than globally). If you want a single connection, you can initialize a single instance of this adapter somewhere and pass it around (or at least do a singleton pattern).
  • It is not possible to close the connection.
    • Add a method to the adapter that calls the this.client.disconnect() method. Once that is in place, any user of it (such as tests and application code) can now properly close the connection at the end.
commented

I think cleaning up the Redis adapter is a good idea, but I'm not experiencing the issues with mocha keeping on running forever except that watch mode is on by default. How are you running the tests?

Even with watch mode turned off it happens. I do yarn docker:compose:test:up and then in a separate window yarn test:all. After saying how many passed and skipped it just hangs. Sounds quite odd that our setups are running so differently 🤔

If I run a single test that does not do any DDB stuff, such as the Template.test.js, and after I've fixed the redis client, that test finishes without issues and mocha exits. But yeah, lots of other oddities. If nothing else, it's fascinating debugging this lol.