Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting MockClient memory leak log #22

Closed
mir1198yusuf opened this issue May 20, 2022 · 9 comments
Closed

Getting MockClient memory leak log #22

mir1198yusuf opened this issue May 20, 2022 · 9 comments

Comments

@mir1198yusuf
Copy link

mir1198yusuf commented May 20, 2022

After running a bunch of unit-tests through jest, I get this log in the console.

(node:66873) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 start listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
(node:66873) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
(node:66873) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query-error listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
(node:66873) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query-response listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit

I dont know if this is of any concern or it is just due to large number of parallel running async test functions.

Thanks.

@mir1198yusuf
Copy link
Author

I have total 13 trackers.on in directory of unit-tests.

@felixmosh
Copy link
Owner

felixmosh commented May 20, 2022

By trackers.on do you mean the tracker that the lib comes with or something else?

Can you share small code snippet ?

@mir1198yusuf
Copy link
Author

mir1198yusuf commented May 20, 2022

This is how I use it. Since we dont have step in this lib like knex-mock, I try to make it work this way.

test('Name ', async () => {
        
        const response = [
                response of 1st query, response of 2nd query
        ];
        const trx = await knexObj.transaction();

        const a = new A();
        let step = 0;

        tracker.on
            .any(({ method, sql, bindings }: RawQuery) => {
                if (step === 0) {
                    expect(method).toEqual('select');
                    expect(sql).toMatchInlineSnapshot(....);
                    expect(bindings).toEqual([...]);
                }
                step++;

                return true;
            })
            .response(() => {
                return response[step - 1];
            });
        await a.b(trx)
        await trx.commit()
});

@mir1198yusuf
Copy link
Author

By trackers.on do you mean the tracker that the lib comes with or something else?

Tracker of this lib.

@felixmosh
Copy link
Owner

Tracker.on is not using listeners at all

@felixmosh
Copy link
Owner

felixmosh commented May 20, 2022

Your tests are not written as it meant to be used :]
You should no expect inside query matcher (the on part).
The lib provides an history object for each type of queries (select / insert ...)

test('Name ', async () => {
  const trx = await knexObj.transaction();

  const a = new A();
  tracker.on.select(({ sql }) => sql.includes('table_name')).responseOnce({ id: 1, name: 'bla' });
  await a.b(trx);
  await trx.commit();

  expect(tracker.history.select[0].sql).toContain('table_name');
});

Tracker is the one thing that you provide with the query response for a specific query type, and it contains the call history for each one of the requests.

@mir1198yusuf
Copy link
Author

mir1198yusuf commented May 21, 2022

  • I cannot just rely on sql.includes('table_name')) as all queries might be on same table.
  • I want to match entire sql which I am doing using matchInlineSnapshot to see if new commit has not unintentionally added/removed query select/returning fields or where clauses or anything else. If it was an intentional change, I can update the jest snapshot of the sql.
  • I cannot provide a complete sql to match with since it can be large & complex. So using snapshot of generated sql was the easy way for me. Jest automatically saves the generated sql as snapshot to compare next time test is run.
  • I want to test the order of query in code. No one should unintentionally alter the sequence of queries. This I agree that I can it with tracker.history.any but since I have send response also to queries sequentially, I have to use step for response. (For first query hitting the client, this response should be send. For second, this should be send & so on). Since step is being used in response, I have to maintain (increment) it inside query matcher. So added expect there only, so it can fail early if a particular query is not as expected instead of knowing this at end using tracker.history.any. (Should-be second query is unintentionally changed to first query)

@mir1198yusuf
Copy link
Author

Tracker.on is not using listeners at all

Okay! The logs in the first comment regarding listeners in MockClient do not hint any serious concerns right ? In this case, we can close the issue.

@mir1198yusuf
Copy link
Author

mir1198yusuf commented May 21, 2022

I ran node --trace-warnings node_modules/jest/bin/jest.js to trace the log, I got this.

(node:71047) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 start listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:595:17)
    at MockClient.addListener (node:events:617:10)
    at makeTxClient (abc-user/node_modules/knex/lib/execution/transaction.js:335:13)
    at abc-user/node_modules/knex/lib/execution/transaction.js:196:43
    at Transaction_PG.acquireConnection (abc-user/node_modules/knex/lib/execution/transaction.js:254:20)
(node:71047) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:595:17)
    at MockClient.addListener (node:events:617:10)
    at makeTxClient (abc-user/node_modules/knex/lib/execution/transaction.js:340:13)
    at abc-user/node_modules/knex/lib/execution/transaction.js:196:43
    at Transaction_PG.acquireConnection (abc-user/node_modules/knex/lib/execution/transaction.js:254:20)
(node:71047) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query-error listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:595:17)
    at MockClient.addListener (node:events:617:10)
    at makeTxClient (abc-user/node_modules/knex/lib/execution/transaction.js:345:13)
    at abc-user/node_modules/knex/lib/execution/transaction.js:196:43
    at Transaction_PG.acquireConnection (abc-user/node_modules/knex/lib/execution/transaction.js:254:20)
(node:71047) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 query-response listeners added to [MockClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:595:17)
    at MockClient.addListener (node:events:617:10)
    at makeTxClient (abc-user/node_modules/knex/lib/execution/transaction.js:350:13)
    at abc-user/node_modules/knex/lib/execution/transaction.js:196:43
    at Transaction_PG.acquireConnection (abc-user/node_modules/knex/lib/execution/transaction.js:254:20)

The warnings are created from knex lib, not this lib.

Found several issues for this on knex repo, will check in detail soon. Can close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants