|
| 1 | +/** |
| 2 | + * A simple example showing the use of the cursorstream pause function. |
| 3 | + * |
| 4 | + * @_class cursorstream |
| 5 | + * @_function pause |
| 6 | + * @ignore |
| 7 | + */ |
| 8 | +exports.shouldStreamDocumentsUsingTheCursorStreamPauseFunction = function(configuration, test) { |
| 9 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 10 | + |
| 11 | + // Establish connection to db |
| 12 | + db.open(function(err, db) { |
| 13 | + |
| 14 | + // Create a lot of documents to insert |
| 15 | + var docs = [] |
| 16 | + for(var i = 0; i < 1; i++) { |
| 17 | + docs.push({'a':i}) |
| 18 | + } |
| 19 | + |
| 20 | + // Create a collection |
| 21 | + db.createCollection('test_cursorstream_pause', function(err, collection) { |
| 22 | + test.equal(null, err); |
| 23 | + |
| 24 | + // Insert documents into collection |
| 25 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 26 | + // Peform a find to get a cursor |
| 27 | + var stream = collection.find().stream(); |
| 28 | + |
| 29 | + // For each data item |
| 30 | + stream.on("data", function(item) { |
| 31 | + // Check if cursor is paused |
| 32 | + test.equal(false, stream.paused); |
| 33 | + // Pause stream |
| 34 | + stream.pause(); |
| 35 | + // Check if cursor is paused |
| 36 | + test.equal(true, stream.paused); |
| 37 | + |
| 38 | + // Restart the stream after 1 miliscecond |
| 39 | + setTimeout(function() { |
| 40 | + stream.resume(); |
| 41 | + // Check if cursor is paused |
| 42 | + process.nextTick(function() { |
| 43 | + test.equal(false, stream.paused); |
| 44 | + }) |
| 45 | + }, 1); |
| 46 | + }); |
| 47 | + |
| 48 | + // When the stream is done |
| 49 | + stream.on("close", function() { |
| 50 | + db.close(); |
| 51 | + test.done(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * A simple example showing the use of the cursorstream resume function. |
| 60 | + * |
| 61 | + * @_class cursorstream |
| 62 | + * @_function resume |
| 63 | + * @ignore |
| 64 | + */ |
| 65 | +exports.shouldStreamDocumentsUsingTheCursorStreamResumeFunction = function(configuration, test) { |
| 66 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 67 | + |
| 68 | + // Establish connection to db |
| 69 | + db.open(function(err, db) { |
| 70 | + |
| 71 | + // Create a lot of documents to insert |
| 72 | + var docs = [] |
| 73 | + for(var i = 0; i < 1; i++) { |
| 74 | + docs.push({'a':i}) |
| 75 | + } |
| 76 | + |
| 77 | + // Create a collection |
| 78 | + db.createCollection('test_cursorstream_resume', function(err, collection) { |
| 79 | + test.equal(null, err); |
| 80 | + |
| 81 | + // Insert documents into collection |
| 82 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 83 | + // Peform a find to get a cursor |
| 84 | + var stream = collection.find().stream(); |
| 85 | + |
| 86 | + // For each data item |
| 87 | + stream.on("data", function(item) { |
| 88 | + // Check if cursor is paused |
| 89 | + test.equal(false, stream.paused); |
| 90 | + // Pause stream |
| 91 | + stream.pause(); |
| 92 | + // Check if cursor is paused |
| 93 | + test.equal(true, stream.paused); |
| 94 | + |
| 95 | + // Restart the stream after 1 miliscecond |
| 96 | + setTimeout(function() { |
| 97 | + |
| 98 | + // Resume the stream |
| 99 | + stream.resume(); |
| 100 | + |
| 101 | + // Check if cursor is paused |
| 102 | + process.nextTick(function() { |
| 103 | + test.equal(false, stream.paused); |
| 104 | + }); |
| 105 | + }, 1); |
| 106 | + }); |
| 107 | + |
| 108 | + // When the stream is done |
| 109 | + stream.on("close", function() { |
| 110 | + db.close(); |
| 111 | + test.done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * A simple example showing the use of the cursorstream resume function. |
| 120 | + * |
| 121 | + * @_class cursorstream |
| 122 | + * @_function destroy |
| 123 | + * @ignore |
| 124 | + */ |
| 125 | +exports.shouldStreamDocumentsUsingTheCursorStreamDestroyFunction = function(configuration, test) { |
| 126 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 127 | + |
| 128 | + // Establish connection to db |
| 129 | + db.open(function(err, db) { |
| 130 | + |
| 131 | + // Create a lot of documents to insert |
| 132 | + var docs = [] |
| 133 | + for(var i = 0; i < 1; i++) { |
| 134 | + docs.push({'a':i}) |
| 135 | + } |
| 136 | + |
| 137 | + // Create a collection |
| 138 | + db.createCollection('test_cursorstream_destroy', function(err, collection) { |
| 139 | + test.equal(null, err); |
| 140 | + |
| 141 | + // Insert documents into collection |
| 142 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 143 | + // Peform a find to get a cursor |
| 144 | + var stream = collection.find().stream(); |
| 145 | + |
| 146 | + // For each data item |
| 147 | + stream.on("data", function(item) { |
| 148 | + // Destroy stream |
| 149 | + stream.destroy(); |
| 150 | + }); |
| 151 | + |
| 152 | + // When the stream is done |
| 153 | + stream.on("close", function() { |
| 154 | + db.close(); |
| 155 | + test.done(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | + }); |
| 160 | +} |
| 161 | + |
| 162 | +exports.shouldStreamDocumentsWithPauseAndResumeForFetching = function(configuration, test) { |
| 163 | + var docs = [] |
| 164 | + |
| 165 | + for(var i = 0; i < 3000; i++) { |
| 166 | + docs.push({'a':i}) |
| 167 | + } |
| 168 | + |
| 169 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 170 | + |
| 171 | + // Establish connection to db |
| 172 | + db.open(function(err, db) { |
| 173 | + db.createCollection('test_streaming_function_with_limit_for_fetching2', function(err, collection) { |
| 174 | + |
| 175 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 176 | + // Peform a find to get a cursor |
| 177 | + var stream = collection.find({}).stream(); |
| 178 | + var data = []; |
| 179 | + |
| 180 | + // For each data item |
| 181 | + stream.on("data", function(item) { |
| 182 | + stream.pause() |
| 183 | + |
| 184 | + collection.findOne({}, function(err, result) { |
| 185 | + data.push(1); |
| 186 | + stream.resume(); |
| 187 | + }) |
| 188 | + }); |
| 189 | + |
| 190 | + // When the stream is done |
| 191 | + stream.on("close", function() { |
| 192 | + test.equal(3000, data.length); |
| 193 | + db.close(); |
| 194 | + test.done(); |
| 195 | + }); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | +} |
| 200 | + |
| 201 | +exports.shouldStream10KDocuments = function(configuration, test) { |
| 202 | + var Binary = configuration.getMongoPackage().Binary; |
| 203 | + var docs = [] |
| 204 | + |
| 205 | + for(var i = 0; i < 10000; i++) { |
| 206 | + docs.push({'a':i, bin: new Binary(new Buffer(256))}) |
| 207 | + } |
| 208 | + |
| 209 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 210 | + |
| 211 | + // Establish connection to db |
| 212 | + db.open(function(err, db) { |
| 213 | + db.createCollection('test_streaming_function_with_limit_for_fetching_2', function(err, collection) { |
| 214 | + |
| 215 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 216 | + // Peform a find to get a cursor |
| 217 | + var stream = collection.find({}).stream(); |
| 218 | + var data = []; |
| 219 | + |
| 220 | + // For each data item |
| 221 | + stream.on("data", function(item) { |
| 222 | + stream.pause() |
| 223 | + |
| 224 | + collection.findOne({}, function(err, result) { |
| 225 | + data.push(1); |
| 226 | + stream.resume(); |
| 227 | + }) |
| 228 | + }); |
| 229 | + |
| 230 | + // When the stream is done |
| 231 | + stream.on("close", function() { |
| 232 | + test.equal(10000, data.length); |
| 233 | + db.close(); |
| 234 | + test.done(); |
| 235 | + }); |
| 236 | + }); |
| 237 | + }); |
| 238 | + }); |
| 239 | +} |
| 240 | + |
| 241 | +exports.shouldTriggerMassiveAmountOfGetMores = function(configuration, test) { |
| 242 | + var Binary = configuration.getMongoPackage().Binary; |
| 243 | + var docs = [] |
| 244 | + var counter = 0; |
| 245 | + var counter2 = 0; |
| 246 | + |
| 247 | + for(var i = 0; i < 1000; i++) { |
| 248 | + docs.push({'a':i, bin: new Binary(new Buffer(256))}) |
| 249 | + } |
| 250 | + |
| 251 | + var db = configuration.newDbInstance({w:0}, {poolSize:1}); |
| 252 | + |
| 253 | + // Establish connection to db |
| 254 | + db.open(function(err, db) { |
| 255 | + db.createCollection('test_streaming_function_with_limit_for_fetching_3', function(err, collection) { |
| 256 | + |
| 257 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 258 | + // Peform a find to get a cursor |
| 259 | + var stream = collection.find({}).stream(); |
| 260 | + var data = []; |
| 261 | + |
| 262 | + // For each data item |
| 263 | + stream.on("data", function(item) { |
| 264 | + counter++; |
| 265 | + stream.pause() |
| 266 | + stream.resume(); |
| 267 | + counter2++; |
| 268 | + }); |
| 269 | + |
| 270 | + // When the stream is done |
| 271 | + stream.on("close", function() { |
| 272 | + test.equal(1000, counter); |
| 273 | + test.equal(1000, counter2); |
| 274 | + db.close(); |
| 275 | + test.done(); |
| 276 | + }); |
| 277 | + }); |
| 278 | + }); |
| 279 | + }); |
| 280 | +} |
| 281 | + |
| 282 | +exports.shouldStreamRecordsCallsDataTheRightNumberOfTimes = function(configuration, test) { |
| 283 | + var client = configuration.db(); |
| 284 | + |
| 285 | + client.createCollection('test_stream_records', function(err, collection) { |
| 286 | + |
| 287 | + collection.insert([{'a':1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5}], {w:1}, function(err, ids) { |
| 288 | + var stream = collection.find({}, {'limit' : 3}).streamRecords(); |
| 289 | + var callsToEnd = 0; |
| 290 | + stream.on('end', function() { |
| 291 | + test.done(); |
| 292 | + }); |
| 293 | + |
| 294 | + var callsToData = 0; |
| 295 | + stream.on('data',function(data){ |
| 296 | + callsToData += 1; |
| 297 | + test.ok(callsToData <= 3); |
| 298 | + }); |
| 299 | + }); |
| 300 | + }); |
| 301 | +} |
| 302 | + |
| 303 | +exports.shouldStreamRecordsCallsEndTheRightNumberOfTimes = function(configuration, test) { |
| 304 | + var client = configuration.db(); |
| 305 | + |
| 306 | + client.createCollection('test_stream_records', function(err, collection) { |
| 307 | + |
| 308 | + collection.insert([{'a':1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5}], {w:1}, function(err, ids) { |
| 309 | + var cursor = collection.find({}, {'limit' : 3}); |
| 310 | + var stream = cursor.streamRecords(function(er,item) {}); |
| 311 | + var callsToEnd = 0; |
| 312 | + stream.on('end', function() { |
| 313 | + callsToEnd += 1; |
| 314 | + test.equal(1, callsToEnd); |
| 315 | + setTimeout(function() { |
| 316 | + // Let's close the db |
| 317 | + if (callsToEnd == 1) { |
| 318 | + test.done(); |
| 319 | + } |
| 320 | + }.bind(this), 1000); |
| 321 | + }); |
| 322 | + |
| 323 | + stream.on('data',function(data){ /* nothing here */ }); |
| 324 | + }); |
| 325 | + }); |
| 326 | +} |
| 327 | + |
| 328 | +exports.shouldStreamDocumentsWithLimitForFetching = function(configuration, test) { |
| 329 | + var client = configuration.db(); |
| 330 | + var docs = [] |
| 331 | + |
| 332 | + for(var i = 0; i < 3000; i++) { |
| 333 | + docs.push({'a':i}) |
| 334 | + } |
| 335 | + |
| 336 | + client.createCollection('test_streaming_function_with_limit_for_fetching', function(err, collection) { |
| 337 | + |
| 338 | + collection.insert(docs, {w:1}, function(err, ids) { |
| 339 | + var cursor = collection.find({}); |
| 340 | + // Execute find on all the documents |
| 341 | + var stream = cursor.streamRecords({fetchSize:1000}); |
| 342 | + var callsToEnd = 0; |
| 343 | + stream.on('end', function() { |
| 344 | + test.done(); |
| 345 | + }); |
| 346 | + |
| 347 | + var callsToData = 0; |
| 348 | + stream.on('data',function(data){ |
| 349 | + callsToData += 1; |
| 350 | + test.ok(callsToData <= 3000); |
| 351 | + }); |
| 352 | + }); |
| 353 | + }); |
| 354 | +} |
0 commit comments