Ejemplo n.º 1
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');

        primaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(primary[currentIsMasterIndex]);
          } else if (doc.count) {
            request.reply({ ok: 1, n: 1 });
          }
        });

        // Attempt to connect
        var server = new ReplSet([{ host: 'localhost', port: 32000 }], {
          setName: 'rs',
          connectionTimeout: 3000,
          socketTimeout: 0,
          haInterval: 2000,
          size: 1,
          disconnectHandler: {
            add: function() {},
            execute: function() {}
          }
        });

        server.on('connect', function(_server) {
          _server.command('test.test', { count: 'test' }, function() {
            server.destroy();
            done();
          });
        });

        server.connect();
      });
      co(function*() {
        const server = yield mock.createServer();

        server.setMessageHandler(request => {
          var doc = request.document;

          if (doc.ismaster && currentStep === 0) {
            request.reply(serverIsMaster[0]);
            currentStep += 1;
          } else if (doc.insert && currentStep === 1) {
            // Stop responding to any calls (emulate dropping packets on the floor)
            if (stopRespondingPrimary) {
              currentStep += 1;
              stopRespondingPrimary = false;
              setTimeout(() => request.connection.destroy(), 1500);
            }
          } else if (doc.ismaster) {
            request.reply(serverIsMaster[0]);
          } else if (doc.insert && currentStep === 2) {
            request.reply({ ok: 1, n: doc.documents, lastOp: new Date() });
          }
        });

        // Start dropping the packets
        setTimeout(function() {
          stopRespondingPrimary = true;
        }, 500);

        // Attempt to connect
        var mongos = new Mongos([server.address()], {
          connectionTimeout: 3000,
          socketTimeout: 1000,
          haInterval: 500,
          size: 1
        });

        // Are we done
        var finished = false;

        // Add event listeners
        mongos.once('connect', function() {
          // Run an interval
          var intervalId = setInterval(function() {
            mongos.insert('test.test', [{ created: new Date() }], function(err, r) {
              if (r && !finished) {
                finished = true;
                clearInterval(intervalId);
                expect(r.connection.port).to.equal(server.address().port);

                server.destroy();
                done();
              }
            });
          }, 500);
        });

        mongos.on('error', done);
        mongos.connect();
      });
  beforeEach(() => {
    // Default message fields
    const defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, {
      msg: 'isdbgrid'
    });

    // Default message fields
    const defaultRSFields = Object.assign({}, mock.DEFAULT_ISMASTER, {
      setName: 'rs',
      setVersion: 1,
      electionId: new ObjectId(),
      hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'],
      arbiters: ['localhost:32002']
    });

    // Primary server states
    const serverIsMaster = [Object.assign({}, defaultFields), Object.assign({}, defaultRSFields)];

    return Promise.all([mock.createServer(), mock.createServer()]).then(servers => {
      test.mongos1 = servers[0];
      test.mongos2 = servers[1];

      test.mongos1.setMessageHandler(request => {
        var doc = request.document;
        if (doc.ismaster) {
          request.reply(serverIsMaster[0]);
        } else if (doc.insert) {
          request.reply({ ok: 1, n: doc.documents, lastOp: new Date() });
        } else if (doc.endSessions) {
          request.reply({ ok: 1 });
        }
      });

      test.mongos2.setMessageHandler(request => {
        var doc = request.document;
        if (doc.ismaster) {
          request.reply(serverIsMaster[1]);
        } else if (doc.insert) {
          request.reply({ ok: 1, n: doc.documents, lastOp: new Date() });
        } else if (doc.endSessions) {
          request.reply({ ok: 1 });
        }
      });
    });
  });
  beforeEach(() => {
    return mock.createServer().then(server => {
      server.setMessageHandler(request => {
        const doc = request.document;

        if (doc.ismaster) {
          return request.reply(Object.assign({}, mock.DEFAULT_ISMASTER));
        }

        if (doc.listCollections) {
          return request.reply({
            ok: 1,
            cursor: {
              id: 0,
              ns: 'test.$cmd.listCollections',
              firstBatch: [{ name: 'test', type: 'collection' }]
            }
          });
        }
      });
      testHarness.server = server;
    });
  });
Ejemplo n.º 5
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');
        const firstSecondaryServer = yield mock.createServer(32001, 'localhost');
        const secondSecondaryServer = yield mock.createServer(32002, 'localhost');

        primaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(primary[currentIsMasterIndex]);
            }
          }
        });

        firstSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(firstSecondary[currentIsMasterIndex]);
            }
          }
        });

        secondSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(secondSecondary[currentIsMasterIndex]);
            }
          }
        });

        // Attempt to connect
        var server = new ReplSet(
          [
            { host: 'localhost', port: 32000 },
            { host: 'localhost', port: 32001 },
            { host: 'localhost', port: 32002 }
          ],
          {
            setName: 'rs',
            connectionTimeout: 3000,
            socketTimeout: 0,
            haInterval: 100,
            size: 1
          }
        );

        Server.enableServerAccounting();

        server.on('connect', function() {
          server.__connected = true;

          // Perform the two steps
          setTimeout(function() {
            die = true;
            currentIsMasterIndex = currentIsMasterIndex + 1;

            server.on('reconnect', function() {
              server.destroy();
              Server.disableServerAccounting();
              done();
            });

            setTimeout(function() {
              die = false;
              currentIsMasterIndex = currentIsMasterIndex + 1;
            }, 500);
          }, 100);
        });

        server.on('error', done);
        server.connect();
      });
Ejemplo n.º 6
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');
        const firstSecondaryServer = yield mock.createServer(32001, 'localhost');
        const secondSecondaryServer = yield mock.createServer(32002, 'localhost');

        primaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(primary[currentIsMasterIndex]);
            }
          }
        });

        firstSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(firstSecondary[currentIsMasterIndex]);
            }
          }
        });

        secondSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (die) {
            request.connection.destroy();
          } else {
            if (doc.ismaster) {
              request.reply(secondSecondary[currentIsMasterIndex]);
            }
          }
        });

        // Attempt to connect
        var server = new ReplSet(
          [
            { host: 'localhost', port: 32000 },
            { host: 'localhost', port: 32001 },
            { host: 'localhost', port: 32002 }
          ],
          {
            setName: 'rs',
            connectionTimeout: 3000,
            socketTimeout: 0,
            haInterval: 100,
            size: 1
          }
        );

        Server.enableServerAccounting();

        server.on('connect', function() {
          server.__connected = true;

          // Perform the two steps
          setTimeout(function() {
            die = true;
            currentIsMasterIndex = currentIsMasterIndex + 1;

            // Keep the count of joined events
            var joinedEvents = 0;

            // Add listener
            server.on('joined', function(_type, _server) {
              if (_type === 'secondary' && _server.name === 'localhost:32000') {
                joinedEvents = joinedEvents + 1;
              } else if (_type === 'primary' && _server.name === 'localhost:32001') {
                joinedEvents = joinedEvents + 1;
              } else if (_type === 'secondary' && _server.name === 'localhost:32002') {
                joinedEvents = joinedEvents + 1;
              }

              // Got both events
              if (joinedEvents === 3) {
                var expectedServers = ['localhost:32002', 'localhost:32000'];
                expect(server.s.replicaSetState.secondaries).to.have.length(2);
                expect(server.s.replicaSetState.secondaries[0].name).to.be.oneOf(expectedServers);
                expect(server.s.replicaSetState.secondaries[1].name).to.be.oneOf(expectedServers);

                expect(server.s.replicaSetState.primary).to.not.be.null;
                expect(server.s.replicaSetState.primary.name).to.equal('localhost:32001');

                server.destroy();
                Server.disableServerAccounting();
                done();
              }
            });

            setTimeout(function() {
              die = false;
              currentIsMasterIndex = currentIsMasterIndex + 1;
            }, 500);
          }, 100);
        });

        server.on('error', done);
        server.connect();
      });
Ejemplo n.º 7
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');
        const firstSecondaryServer = yield mock.createServer(32001, 'localhost');
        const secondSecondaryServer = yield mock.createServer(32003, 'localhost');
        const arbiterServer = yield mock.createServer(32002, 'localhost');

        primaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(primary[currentIsMasterIndex]);
          }
        });

        firstSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(firstSecondary[currentIsMasterIndex]);
          }
        });

        secondSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(secondSecondary[0]);
          }
        });

        arbiterServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(arbiter[currentIsMasterIndex]);
          }
        });

        // Attempt to connect
        var server = new ReplSet(
          [
            { host: 'localhost', port: 32000 },
            { host: 'localhost', port: 32001 },
            { host: 'localhost', port: 32002 }
          ],
          {
            setName: 'rs',
            connectionTimeout: 3000,
            socketTimeout: 0,
            haInterval: 100,
            size: 1
          }
        );

        var secondaries = {};
        var arbiters = {};
        var allservers = {};

        server.on('serverHeartbeatStarted', function(description) {
          allservers[description.connectionId] = true;
          if (allservers['localhost:32003']) {
            server.destroy();
            done();
          }
        });

        server.on('joined', function(_type, _server) {
          if (_type === 'arbiter') {
            arbiters[_server.name] = _server;
            // Flip the ismaster message
            currentIsMasterIndex = currentIsMasterIndex + 1;
          } else if (_type === 'secondary') {
            // expect(server.__connected).to.be.true;
            secondaries[_server.name] = _server;
            if (Object.keys(secondaries).length === 2) {
              expect(secondaries['localhost:32001']).to.not.be.null;
              expect(secondaries['localhost:32003']).to.not.be.null;
              expect(arbiters['localhost:32002']).to.not.be.null;
            }
          }
        });

        server.on('error', function() {});
        server.on('connect', function() {
          server.__connected = true;
        });

        server.connect();
      });
Ejemplo n.º 8
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');
        const firstSecondaryServer = yield mock.createServer(32001, 'localhost');
        const secondSecondaryServer = yield mock.createServer(32003, 'localhost');
        const arbiterServer = yield mock.createServer(32002, 'localhost');

        primaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(primary[currentIsMasterIndex]);
          }
        });

        firstSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(firstSecondary[currentIsMasterIndex]);
          }
        });

        secondSecondaryServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(secondSecondary[currentIsMasterIndex]);
          }
        });

        arbiterServer.setMessageHandler(request => {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(arbiter[currentIsMasterIndex]);
          }
        });

        // Attempt to connect
        var server = new ReplSet(
          [
            { host: 'localhost', port: 32000 },
            { host: 'localhost', port: 32001 },
            { host: 'localhost', port: 32002 }
          ],
          {
            setName: 'rs',
            connectionTimeout: 3000,
            socketTimeout: 0,
            haInterval: 100,
            size: 1
          }
        );

        setTimeout(function() {
          expect(server.s.replicaSetState.set['localhost:32000'].type).to.equal('RSPrimary');
          expect(server.s.replicaSetState.set['localhost:32001'].type).to.equal('RSSecondary');
          expect(server.s.replicaSetState.set['localhost:32002'].type).to.equal('RSArbiter');
          expect(server.s.replicaSetState.set['localhost:32003'].type).to.equal('RSSecondary');
          currentIsMasterIndex = currentIsMasterIndex + 1;

          // Wait for another sweep
          setTimeout(function() {
            expect(server.s.replicaSetState.set['localhost:32000'].type).to.equal('RSPrimary');
            expect(server.s.replicaSetState.set['localhost:32001'].type).to.equal('RSSecondary');
            expect(server.s.replicaSetState.set['localhost:32002'].type).to.equal('RSArbiter');
            expect(server.s.replicaSetState.set['localhost:32003'].type).to.equal('RSSecondary');
            expect(server.s.replicaSetState.secondaries).to.have.length(2);
            expect(server.s.replicaSetState.arbiters).to.have.length(1);
            expect(server.s.replicaSetState.primary).to.exist;

            // Ensure we have 4 interval ids and
            var intervalIds = server.intervalIds.filter(function(x) {
              return x.__host !== undefined;
            });

            expect(intervalIds).to.have.length(4);
            var hosts = intervalIds.map(function(x) {
              return x.__host;
            });

            expect(hosts.indexOf('localhost:32000')).to.not.equal(-1);
            expect(hosts.indexOf('localhost:32001')).to.not.equal(-1);
            expect(hosts.indexOf('localhost:32002')).to.not.equal(-1);
            expect(hosts.indexOf('localhost:32003')).to.not.equal(-1);

            server.destroy();
            done();
          }, 1000);
        }, 500);

        server.on('left', function(_type, _server) {
          if (_type === 'secondary' && _server.name === 'localhost:32003') {
            expect(server.s.replicaSetState.secondaries).to.have.length(1);
            expect(server.s.replicaSetState.secondaries[0].name).to.equal('localhost:32001');

            expect(server.s.replicaSetState.arbiters).to.have.length(1);
            expect(server.s.replicaSetState.arbiters[0].name).to.equal('localhost:32002');

            expect(server.s.replicaSetState.primary).to.not.be.null;
            expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');
            // Flip the ismaster message
            currentIsMasterIndex = currentIsMasterIndex + 1;
            // global.debug=true
          }
        });

        server.connect();
      });
Ejemplo n.º 9
0
      co(function*() {
        const primaryServer = yield mock.createServer(32000, 'localhost');
        const firstSecondaryServer = yield mock.createServer(32001, 'localhost');
        const secondSecondaryServer = yield mock.createServer(32003, 'localhost');
        const arbiterServer = yield mock.createServer(32002, 'localhost');

        primaryServer.setMessageHandler(function(request) {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(primary[currentIsMasterIndex]);
          }
        });

        firstSecondaryServer.setMessageHandler(function(request) {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(firstSecondary[currentIsMasterIndex]);
          }
        });

        secondSecondaryServer.setMessageHandler(function(request) {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(secondSecondary[currentIsMasterIndex]);
          }
        });

        arbiterServer.setMessageHandler(function(request) {
          var doc = request.document;
          if (doc.ismaster) {
            request.reply(arbiter[currentIsMasterIndex]);
          }
        });

        // Attempt to connect
        var server = new ReplSet(
          [
            { host: 'localhost', port: 32000 },
            { host: 'localhost', port: 32001 },
            { host: 'localhost', port: 32002 }
          ],
          {
            setName: 'rs',
            connectionTimeout: 3000,
            socketTimeout: 0,
            haInterval: 100,
            size: 1
          }
        );

        // Joined
        var joined = 0;

        server.on('joined', function() {
          joined = joined + 1;

          // primary, secondary and arbiter have joined
          if (joined === 4) {
            expect(server.s.replicaSetState.secondaries).to.have.length(2);
            expect(server.s.replicaSetState.secondaries[0].name).to.equal('localhost:32001');
            expect(server.s.replicaSetState.secondaries[1].name).to.equal('localhost:32003');
            expect(server.s.replicaSetState.arbiters).to.have.length(1);
            expect(server.s.replicaSetState.arbiters[0].name).to.equal('localhost:32002');
            expect(server.s.replicaSetState.primary).to.not.be.null;
            expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

            // Flip the ismaster message
            currentIsMasterIndex = currentIsMasterIndex + 1;
          }
        });

        server.on('left', function(_type, _server) {
          if (_type === 'secondary' && _server.name === 'localhost:32003') {
            expect(server.s.replicaSetState.secondaries).to.have.length(1);
            expect(server.s.replicaSetState.secondaries[0].name).to.equal('localhost:32001');

            expect(server.s.replicaSetState.arbiters).to.have.length(1);
            expect(server.s.replicaSetState.arbiters[0].name).to.equal('localhost:32002');

            expect(server.s.replicaSetState.primary).to.not.be.null;
            expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

            server.destroy();
            done();
          }
        });

        server.connect();
      });
Ejemplo n.º 10
0
 beforeEach(() => {
   return mock.createServer().then(mockServer => {
     test.server = mockServer;
   });
 });
Ejemplo n.º 11
0
 beforeEach(() => mock.createServer(0, '::').then(_server => (server = _server)));
Ejemplo n.º 12
0
 beforeEach(() => mock.createServer(0, '127.0.0.1').then(_server => (server = _server)));
      co(function*() {
        const server = yield mock.createServer();

        server.setMessageHandler(request => {
          var doc = request.document;

          if (doc.ismaster) {
            request.reply(serverIsMaster[0]);
          } else if (doc.find) {
            setTimeout(() => {
              // Reply with first batch
              request.reply({
                cursor: {
                  id: Long.fromNumber(1),
                  ns: f('%s.cursor1', 'test'),
                  firstBatch: [{ _id: new ObjectId(), a: 1 }]
                },
                ok: 1
              });
            }, 600);
          } else if (doc.getMore) {
            // Reply with first batch
            request.reply({
              cursor: {
                id: Long.fromNumber(1),
                ns: f('%s.cursor1', 'test'),
                nextBatch: [{ _id: new ObjectId(), a: 1 }]
              },
              ok: 1
            });
          }
        });

        // Attempt to connect
        var mongos = new Mongos([server.address()], {
          connectionTimeout: 30000,
          socketTimeout: 30000,
          haInterval: 500,
          size: 1
        });

        // Add event listeners
        mongos.once('connect', function() {
          // Execute find
          var cursor = mongos.cursor('test.test', {
            find: 'test',
            query: {},
            batchSize: 2
          });

          // Execute next
          cursor.next(function(err, d) {
            expect(err).to.not.exist;
            expect(d).to.exist;

            cursor.next(function(_err, _d) {
              expect(_err).to.not.exist;
              expect(_d).to.exist;

              server.destroy();
              done();
            });
          });
        });

        mongos.on('error', done);
        mongos.connect();
      });