typegoose / mongodb-memory-server

Manage & spin up mongodb server binaries with zero(or slight) configuration for tests.
https://typegoose.github.io/mongodb-memory-server/
MIT License
2.57k stars 185 forks source link

fix(kill): should not try to open a connection to a not running ReplSet #811

Closed arthursimas1 closed 11 months ago

arthursimas1 commented 11 months ago

I've a situation where I listen to kill signals and execute .stop() when needed. The replica set has already been killed because the Ctrl + C signal is propagated to the whole process group, then I waits forever to connect and send a graceful shutdown.

codecov[bot] commented 11 months ago

Codecov Report

Merging #811 (b58958a) into master (c1f2333) will increase coverage by 0.20%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #811      +/-   ##
==========================================
+ Coverage   90.94%   91.14%   +0.20%     
==========================================
  Files          15       15              
  Lines        1965     1965              
  Branches      494      494              
==========================================
+ Hits         1787     1791       +4     
+ Misses        169      165       -4     
  Partials        9        9              
Files Coverage Δ
...ngodb-memory-server-core/src/util/MongoInstance.ts 98.61% <100.00%> (+1.84%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

github-actions[bot] commented 11 months ago

:tada: This PR is included in version 9.0.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

arthursimas1 commented 11 months ago

hey @hasezoey. I've tried the following options:

// 1)
con = await MongoClient.connect(uriTemplate(ip, port, 'admin'), {
  ...this.extraConnectionOptions,
  directConnection: true,
  socketTimeoutMS: 100,
  connectTimeoutMS: 100,
  waitQueueTimeoutMS: 100,
});

// 2)
con = await MongoClient.connect(uriTemplate(ip, port, 'admin'), {
  ...this.extraConnectionOptions,
  directConnection: true,
  serverSelectionTimeoutMS: 100,
});

Only the 2) case works. Right now I cannot think out of my head a test case for this alternative solution.

arthursimas1 commented 11 months ago

Testing some cases, it is possible that the server is killed between the isAlive assert and the connection. I agree that it's better to use the connection options. I'll send another PR.

hasezoey commented 11 months ago

I agree that it's better to use the connection options. I'll send another PR.

no need to replace the change from the current PR, the options are only meant because it should not have much latency because it will always connect to localhost and so 30 second timeout is somewhat high