Skip to content

Commit

Permalink
test(sharding): fixing old sharding tests
Browse files Browse the repository at this point in the history
Fixing tests that were flakey and outdated.
  • Loading branch information
daprahamian authored and mbroadst committed Jan 14, 2019
1 parent 98f0c68 commit 6790a74
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 319 deletions.
201 changes: 70 additions & 131 deletions test/functional/sharding_connection_tests.js
@@ -1,9 +1,7 @@
'use strict';

var co = require('co');
var f = require('util').format;
var test = require('./shared').assert;
var setupDatabase = require('./shared').setupDatabase;
const setupDatabase = require('./shared').setupDatabase;
const expect = require('chai').expect;

describe('Sharding (Connection)', function() {
before(function() {
Expand All @@ -18,35 +16,27 @@ describe('Sharding (Connection)', function() {

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var url = f(
'mongodb://%s:%s,%s:%s/sharded_test_db?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=',
configuration.host,
configuration.port,
configuration.host,
configuration.port + 1
);

const client = configuration.newClient(url, {
mongos: {
haInterval: 500
}
});
const configuration = this.configuration;

const url = `${configuration.url()}?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=`;

const client = configuration.newClient(url, { haInterval: 500, useNewUrlParser: true });

client.connect(function(err, client) {
test.equal(null, err);
test.equal(500, client.topology.haInterval);
var db = client.db(configuration.db);
client.connect(err => {
expect(err).to.not.exist;
expect(client).to.have.nested.property('topology.haInterval', 500);

const db = client.db(configuration.db);

db
.collection('replicaset_mongo_client_collection')
.update({ a: 1 }, { b: 1 }, { upsert: true }, function(err, result) {
test.equal(null, err);
test.equal(1, result.result.n);
.update({ a: 1 }, { b: 1 }, { upsert: true }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.have.nested.property('result.n', 1);

// Perform fetch of document
db.collection('replicaset_mongo_client_collection').findOne(function(err) {
test.equal(null, err);
db.collection('replicaset_mongo_client_collection').findOne(err => {
expect(err).to.not.exist;

client.close();
done();
Expand All @@ -64,54 +54,18 @@ describe('Sharding (Connection)', function() {

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var url = f(
'mongodb://%s:%s,%s:%s,localhost:50002/sharded_test_db?w=1',
configuration.host,
configuration.port,
configuration.host,
configuration.port + 1
);

const client = configuration.newClient(url);
client.connect(function(err, client) {
setTimeout(function() {
test.equal(null, err);
client.close();
done();
}, 2000);
});
}
});
const configuration = this.configuration;
const host = configuration.host;
const port = configuration.port;

/**
* @ignore
*/
it('Should exercise all options on mongos topology', {
metadata: { requires: { topology: 'sharded' } },
// TODO: Better way to do this?
const url = `mongodb://${host}:${port},${host}:${port +
1},localhost:50002/sharded_test_db?w=1`;

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var url = f(
'mongodb://%s:%s,%s:%s/sharded_test_db?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=',
configuration.host,
configuration.port,
configuration.host,
configuration.port + 1
);

const client = configuration.newClient(url, { mongos: { haInterval: 500 } });
client.connect(function(err, client) {
test.equal(null, err);
test.equal(500, client.topology.haInterval);
test.ok(client.topology.capabilities() != null);
test.equal(true, client.topology.isConnected());
test.ok(client.topology.lastIsMaster() != null);
test.ok(client.topology.connections() != null);
test.ok(client.topology.isMasterDoc != null);
test.ok(client.topology.bson != null);
const client = configuration.newClient(url, { useNewUrlParser: true });

client.connect(err => {
expect(err).to.not.exist;
client.close();
done();
});
Expand All @@ -121,29 +75,39 @@ describe('Sharding (Connection)', function() {
/**
* @ignore
*/
it('Should correctly modify the server reconnectTries for all sharded proxy instances', {
it('Should exercise all options on mongos topology', {
metadata: { requires: { topology: 'sharded' } },

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var url = f(
'mongodb://%s:%s,%s:%s/sharded_test_db?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=',
configuration.host,
configuration.port,
configuration.host,
configuration.port + 1
);

const client = configuration.newClient(url, { reconnectTries: 10 });
client.connect(function(err, client) {
test.equal(null, err);
test.ok(client != null);

var servers = client.topology.s.coreTopology.connectedProxies;
for (var i = 0; i < servers.length; i++) {
test.equal(10, servers[i].s.pool.options.reconnectTries);
}
const configuration = this.configuration;

const url = `${configuration.url()}?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=`;

const client = configuration.newClient(url, { useNewUrlParser: true, haInterval: 500 });
client.connect(function(err) {
expect(err).to.not.exist;
expect(client)
.to.have.property('topology')
.that.is.an('object');

const topology = client.topology;

expect(topology).to.have.property('haInterval', 500);
expect(topology).to.have.property('bson').that.does.exist;
expect(topology).to.have.property('isMasterDoc').that.does.exist;

expect(topology)
.to.have.property('isConnected')
.that.is.a('function');
expect(topology.isConnected()).to.equal(true);

['capabilities', 'lastIsMaster', 'connections'].forEach(member => {
expect(topology)
.to.have.property(member)
.that.is.a('function');
expect(topology[member]()).to.exist;
});

client.close();
done();
Expand All @@ -154,53 +118,28 @@ describe('Sharding (Connection)', function() {
/**
* @ignore
*/
it('Should emit close event when mongos is stopped', {
it('Should correctly modify the server reconnectTries for all sharded proxy instances', {
metadata: { requires: { topology: 'sharded' } },

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var manager = configuration.manager;
var mongos = manager.proxies;

co(function*() {
var url = f(
'mongodb://%s:%s,%s:%s/sharded_test_db',
configuration.host,
configuration.port,
configuration.host,
configuration.port + 1
);

const client = configuration.newClient(url);
yield client.connect();
const configuration = this.configuration;
const url = `${configuration.url()}?w=1&readPreference=secondaryPreferred&readPreferenceTags=sf%3A1&readPreferenceTags=`;

var doc = { answer: 42 };
var db = client.db('Test');
var coll = db.collection('docs');
yield coll.insertOne(doc);
const client = configuration.newClient(url, { useNewUrlParser: true, reconnectTries: 10 });
client.connect(function(err) {
expect(err).to.not.exist;
expect(client)
.to.have.nested.property('topology.s.coreTopology.connectedProxies')
.that.is.an('array');

doc = yield coll.findOne({ answer: 42 });
test.ok(!!doc);
client.topology.s.coreTopology.connectedProxies.forEach(server => {
expect(server).to.have.nested.property('s.pool.options.reconnectTries', 10);
});

var waitForClose = new Promise(resolve => db.once('close', resolve));

yield mongos.map(p => p.stop());
yield waitForClose;
yield mongos.map(p => p.start());

doc = yield coll.findOne({ answer: 42 });
test.ok(!!doc);

waitForClose = new Promise(resolve => db.once('close', resolve));

yield mongos.map(p => p.stop());
yield waitForClose;
yield mongos.map(p => p.start());

doc = yield coll.findOne({ answer: 42 });
test.ok(!!doc);
}).then(() => done(), done);
client.close();
done();
});
}
});
});

0 comments on commit 6790a74

Please sign in to comment.