mongodb-memory-server: ReplicaSet throws error while creating: MongoError: not master

Versions

  • NodeJS: 14.17.5
  • mongodb-memory-server-*: 7.3.6
  • mongodb(the binary version): 4.4.4
  • system: MacOS

package: mongo-memory-server

What is the Problem?

When I try to create MongoMemoryReplSet I get MongoError: not master, but this seems to happen only when I’m running at least two test files in jest.

This is the exact error that I get:

    MongoError: not master

      at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:268:20)
      at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
      at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)

Update 1

Sometimes, there is no error, just timeouts from all tests…

Update 2

I just received a bit more detailed stacktrace:

MongoError: not master
    at MessageStream.messageHandler (/Users/mateuszlewandowski/Projects/konsalnet/konsalnet/scheduling-api/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:400:28)
    at MessageStream.emit (domain.js:470:12)
    at processIncomingData (/Users/mateuszlewandowski/Projects/konsalnet/konsalnet/scheduling-api/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/Users/mateuszlewandowski/Projects/konsalnet/konsalnet/scheduling-api/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:726:22)
    at Socket.emit (events.js:400:28)
    at Socket.emit (domain.js:470:12)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)

Code Example

Setup code

let mongoServer;
export async function setupTestDatabase() {
  mongoServer = await MongoMemoryReplSet.create({ replSet: { count: 3, storageEngine: "wiredTiger" } });
  await mongoServer.waitUntilRunning();
  const mongoUri = mongoServer.getUri();
  await connect(mongoUri, "test-db"); // <- creates mongo client and connects to database, client is then imported in other files
}

export async function tearDownTestDatabase() {
  await disconnect(); // <- disconnects application from mongo
  await mongoServer?.stop();
}

and in each test file I have

beforeAll(() => setupTestDatabase());
afterAll(() => tearDownTestDatabase());

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20

Most upvoted comments

i did not get a response yet for https://github.com/nodkz/mongodb-memory-server/issues/535#issuecomment-907793148, but a small update: version 8.0.0 released yesterday, which upgraded mongod (driver) and default mongodb binary versions, could you maybe try that?

See Migrate 8

With mongo-memory-server 7.4.0-beta.4 I’ve got MongoError: not master:

Determining test suites to run...mongo started {
  mongoUri: 'mongodb://127.0.0.1:55400,127.0.0.1:55401,127.0.0.1:55402/?replicaSet=testset'
}
 FAIL  src/testWithDb.spec.js (4.102 s)
  ✕ should insert and query (273 ms)

  ● should insert and query

    MongoError: not master

      at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:268:20)
      at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
      at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 0 passed, 1 total
Snapshots:   0 total
Time:        5.549 s
Ran all test suites matching /testWithDb.spec/i.

Unfortunately I cannot reproduce error when MONGOMS_DEBUG is set to 1 😦

But there is one additional information: I cannot reproduce it on any other system than MacOS, so it looks like it’s MacOS specific issue. Unfortunately I’m using macbook for work…

UPDATE

I added second test to this file and now I have the same error with MONGOMS_DEBUG=1, but there is not much output there 😦

❯ MONGOMS_DEBUG=1 npm run test -- testWithDb.spec

> @konsalnet/scheduling-api@0.352.0 test /Users/mateuszlewandowski/Projects/konsalnet/konsalnet/scheduling-api
> jest "location-settlements-companies-repository.spec"

Determining test suites to run...mongo started {
  mongoUri: 'mongodb://127.0.0.1:55400,127.0.0.1:55401,127.0.0.1:55402/?replicaSet=testset'
}
 FAIL  src/testWithDb.spec.js (5.202 s)
  ✕ should insert and query (273 ms)
  ✓ should query other data (1716 ms)

  ● should insert and query

    MongoError: not master

      at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:268:20)
      at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
      at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        5.549 s
Ran all test suites matching /testWithDb.spec/i.

There is one interesting thing here is that both tests are doing very little thing, and the second one is taking way more time than the first…

UPDATE 2

I tried to create small repository with reproduction of this issue… unfortunately on this small project there is no reproduction 😦 I have no idea what is the difference between this small example and my project, but it works in example repo and failing in real live project 😦

Here is the example that I hoped to show you with the reproduction: https://github.com/mlewando90/mongo-memory-server-replica-set