|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const mock = require('../../tools/mongodb-mock/index'); |
| 5 | +const { Topology } = require('../../../src/sdam/topology'); |
| 6 | +const { Long } = require('bson'); |
| 7 | +const { MongoDBNamespace, isHello } = require('../../../src/utils'); |
| 8 | +const { AggregationCursor } = require('../../../src/cursor/aggregation_cursor'); |
| 9 | + |
| 10 | +const test = {}; |
| 11 | +describe('Aggregation Cursor', function () { |
| 12 | + describe('#next', function () { |
| 13 | + afterEach(function () { |
| 14 | + mock.cleanup(); |
| 15 | + }); |
| 16 | + beforeEach(function () { |
| 17 | + return mock.createServer().then(mockServer => { |
| 18 | + test.server = mockServer; |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + context('when there is a data bearing server', function () { |
| 23 | + beforeEach(function () { |
| 24 | + test.server.setMessageHandler(request => { |
| 25 | + const doc = request.document; |
| 26 | + if (isHello(doc)) { |
| 27 | + request.reply(mock.HELLO); |
| 28 | + } else if (doc.aggregate) { |
| 29 | + request.reply({ |
| 30 | + cursor: { |
| 31 | + id: Long.fromNumber(1), |
| 32 | + ns: 'test.test', |
| 33 | + firstBatch: [{ _id: 1, name: 'test' }] |
| 34 | + }, |
| 35 | + ok: 1 |
| 36 | + }); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('sets the session on the cursor', function (done) { |
| 42 | + const topology = new Topology(test.server.hostAddress()); |
| 43 | + const cursor = new AggregationCursor( |
| 44 | + topology, |
| 45 | + MongoDBNamespace.fromString('test.test'), |
| 46 | + [], |
| 47 | + {} |
| 48 | + ); |
| 49 | + topology.connect(function () { |
| 50 | + cursor.next(function () { |
| 51 | + expect(cursor.session).to.exist; |
| 52 | + topology.close(done); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + context('when there is no data bearing server', function () { |
| 59 | + beforeEach(function () { |
| 60 | + test.server.setMessageHandler(request => { |
| 61 | + const doc = request.document; |
| 62 | + if (isHello(doc)) { |
| 63 | + request.reply({ errmsg: 'network error' }); |
| 64 | + } else if (doc.aggregate) { |
| 65 | + request.reply({ |
| 66 | + cursor: { |
| 67 | + id: Long.fromNumber(1), |
| 68 | + ns: 'test.test', |
| 69 | + firstBatch: [{ _id: 1, name: 'test' }] |
| 70 | + }, |
| 71 | + ok: 1 |
| 72 | + }); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not set the session on the cursor', function (done) { |
| 78 | + const topology = new Topology(test.server.hostAddress(), { |
| 79 | + serverSelectionTimeoutMS: 1000 |
| 80 | + }); |
| 81 | + const cursor = new AggregationCursor( |
| 82 | + topology, |
| 83 | + MongoDBNamespace.fromString('test.test'), |
| 84 | + [], |
| 85 | + {} |
| 86 | + ); |
| 87 | + topology.connect(function () { |
| 88 | + cursor.next(function () { |
| 89 | + expect(cursor.session).to.not.exist; |
| 90 | + topology.close(done); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + context('when a data bearing server becomes available', function () { |
| 97 | + beforeEach(function () { |
| 98 | + let helloCalls = 0; |
| 99 | + test.server.setMessageHandler(request => { |
| 100 | + const doc = request.document; |
| 101 | + if (isHello(doc)) { |
| 102 | + request.reply(helloCalls > 0 ? { errmsg: 'network error' } : mock.HELLO); |
| 103 | + } else if (doc.aggregate) { |
| 104 | + request.reply({ |
| 105 | + cursor: { |
| 106 | + id: Long.fromNumber(1), |
| 107 | + ns: 'test.test', |
| 108 | + firstBatch: [{ _id: 1, name: 'test' }] |
| 109 | + }, |
| 110 | + ok: 1 |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('sets the session on the cursor', function (done) { |
| 117 | + const topology = new Topology(test.server.hostAddress(), { |
| 118 | + serverSelectionTimeoutMS: 1000 |
| 119 | + }); |
| 120 | + const cursor = new AggregationCursor( |
| 121 | + topology, |
| 122 | + MongoDBNamespace.fromString('test.test'), |
| 123 | + [], |
| 124 | + {} |
| 125 | + ); |
| 126 | + topology.connect(function () { |
| 127 | + cursor.next(function () { |
| 128 | + expect(cursor.session).to.exist; |
| 129 | + topology.close(done); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments