UNPKG

6.25 kBJavaScriptView Raw
1"use strict"
2
3var co = require('co'),
4 f = require('util').format,
5 Logger = require('../lib/logger'),
6 assert = require('assert');
7
8// Polyfill Promise if none exists
9if(!global.Promise) {
10 require('es6-promise').polyfill();
11}
12
13// Get babel polyfill
14require("babel-polyfill");
15
16describe('Sharded', function() {
17 describe('manager', function() {
18 it('establish server version for sharded system', function(done) {
19 this.timeout(200000);
20
21 co(function*() {
22 var Sharded = require('../').Sharded;
23 // Create new instance
24 var topology = new Sharded('mongod');
25 // Perform discovery
26 var version = yield topology.discover();
27 // Expect 3 integers
28 assert.ok(typeof version.version[0] == 'number');
29 assert.ok(typeof version.version[1] == 'number');
30 assert.ok(typeof version.version[2] == 'number');
31 done();
32 }).catch(function(err) {
33 console.log(err.stack);
34 });
35 });
36
37 it('create a sharded system with 2 shards', function(done) {
38 this.timeout(250000);
39
40 co(function*() {
41 var Sharded = require('../').Sharded;
42 // Create new instance
43 var topology = new Sharded({
44 mongod: 'mongod',
45 mongos: 'mongos'
46 });
47
48 // Add one shard
49 yield topology.addShard([{
50 options: {
51 bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
52 }
53 }, {
54 options: {
55 bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
56 }
57 }, {
58 // Type of node
59 arbiter: true,
60 // mongod process options
61 options: {
62 bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
63 }
64 }], {
65 replSet: 'rs1'
66 });
67
68 // Add one shard
69 yield topology.addShard([{
70 options: {
71 bind_ip: 'localhost', port: 31010, dbpath: f('%s/../db/31010', __dirname)
72 }
73 }, {
74 options: {
75 bind_ip: 'localhost', port: 31011, dbpath: f('%s/../db/31011', __dirname)
76 }
77 }, {
78 // Type of node
79 arbiter: true,
80 // mongod process options
81 options: {
82 bind_ip: 'localhost', port: 31012, dbpath: f('%s/../db/31012', __dirname)
83 }
84 }], {
85 replSet: 'rs2'
86 });
87
88 // Add configuration servers
89 yield topology.addConfigurationServers([{
90 options: {
91 bind_ip: 'localhost', port: 35000, dbpath: f('%s/../db/35000', __dirname)
92 }
93 }, {
94 options: {
95 bind_ip: 'localhost', port: 35001, dbpath: f('%s/../db/35001', __dirname)
96 }
97 }, {
98 options: {
99 bind_ip: 'localhost', port: 35002, dbpath: f('%s/../db/35002', __dirname)
100 }
101 }], {
102 replSet: 'rs3'
103 });
104
105 // Add proxies
106 yield topology.addProxies([{
107 bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000,localhost:35001,localhost:35002'
108 }, {
109 bind_ip: 'localhost', port: 51001, configdb: 'localhost:35000,localhost:35001,localhost:35002'
110 }], {
111 binary: 'mongos'
112 });
113
114 // // Set the info level
115 // Logger.setLevel('info');
116
117 // Start up topology
118 yield topology.start();
119
120 // Shard db
121 yield topology.enableSharding('test');
122 // Shard a collection
123 yield topology.shardCollection('test', 'testcollection', {_id: 1});
124
125 // Stop the topology
126 yield topology.stop();
127
128 // All done
129 done();
130 }).catch(function(err) {
131 console.log(err.stack);
132 });
133 });
134
135 it('create a sharded system with a single shard and take down mongos and bring it back', function(done) {
136 this.timeout(250000);
137
138 co(function*() {
139 var Sharded = require('../').Sharded;
140 // Create new instance
141 var topology = new Sharded({
142 mongod: 'mongod',
143 mongos: 'mongos'
144 });
145
146 // Add one shard
147 yield topology.addShard([{
148 options: {
149 bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
150 }
151 }, {
152 options: {
153 bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
154 }
155 }, {
156 // Type of node
157 arbiter: true,
158 // mongod process options
159 options: {
160 bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
161 }
162 }], {
163 replSet: 'rs1'
164 });
165
166 // Add configuration servers
167 yield topology.addConfigurationServers([{
168 options: {
169 bind_ip: 'localhost', port: 35000, dbpath: f('%s/../db/35000', __dirname)
170 }
171 }, {
172 options: {
173 bind_ip: 'localhost', port: 35001, dbpath: f('%s/../db/35001', __dirname)
174 }
175 }, {
176 options: {
177 bind_ip: 'localhost', port: 35002, dbpath: f('%s/../db/35002', __dirname)
178 }
179 }], {
180 replSet: 'rs3'
181 });
182
183 // Add proxies
184 yield topology.addProxies([{
185 bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000,localhost:35001,localhost:35002'
186 }, {
187 bind_ip: 'localhost', port: 51001, configdb: 'localhost:35000,localhost:35001,localhost:35002'
188 }], {
189 binary: 'mongos'
190 });
191
192 // // Set the info level
193 // Logger.setLevel('info');
194
195 // Start up topology
196 yield topology.start();
197
198 // Shard db
199 yield topology.enableSharding('test');
200
201 // Shard a collection
202 yield topology.shardCollection('test', 'testcollection', {_id: 1});
203
204 // Get first proxy
205 var mongos = topology.proxies[0];
206 // Stop the proxy
207 yield mongos.stop();
208
209 // Start the proxy again
210 yield mongos.start();
211
212 // Stop the topology
213 yield topology.stop();
214
215 // All done
216 done();
217 }).catch(function(err) {
218 console.log(err.stack);
219 });
220 });
221 });
222});