|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const TestHarness = require('./utils').TestHarness; |
| 5 | +const expectMsgToHaveSingleQuery = require('./utils').expectMsgToHaveSingleQuery; |
| 6 | +const executeGetMore = require('../../../../../lib/wireprotocol/3_6_support/execute_get_more'); |
| 7 | + |
| 8 | +describe('Wire Protocol 3.6 GetMore', function() { |
| 9 | + const test = new TestHarness(); |
| 10 | + |
| 11 | + const $db = 'darmok'; |
| 12 | + const collection = 'jalad'; |
| 13 | + const namespace = `${$db}.${collection}`; |
| 14 | + |
| 15 | + let connection; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + connection = { write: test.sinon.stub() }; |
| 19 | + }); |
| 20 | + |
| 21 | + it('should reflect the basic options on to the getMoreCommand', function() { |
| 22 | + const cursorState = { |
| 23 | + cmd: {}, |
| 24 | + cursorId: 8675309 |
| 25 | + }; |
| 26 | + |
| 27 | + const BATCH_SIZE = -23000; |
| 28 | + |
| 29 | + executeGetMore( |
| 30 | + test.bson, |
| 31 | + namespace, |
| 32 | + cursorState, |
| 33 | + BATCH_SIZE, |
| 34 | + undefined, |
| 35 | + connection, |
| 36 | + test.callback |
| 37 | + ); |
| 38 | + |
| 39 | + expect(connection.write).to.have.been.calledOnce; |
| 40 | + |
| 41 | + expectMsgToHaveSingleQuery(connection.write.lastCall.args[0]).to.deep.equal({ |
| 42 | + $db, |
| 43 | + collection, |
| 44 | + batchSize: 23000, |
| 45 | + getMore: cursorState.cursorId |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should attach maxTimeMS', function() { |
| 50 | + const cursorState = { |
| 51 | + cmd: { |
| 52 | + tailable: true, |
| 53 | + maxAwaitTimeMS: 1001 |
| 54 | + }, |
| 55 | + cursorId: 8675309 |
| 56 | + }; |
| 57 | + |
| 58 | + const BATCH_SIZE = -23000; |
| 59 | + |
| 60 | + executeGetMore( |
| 61 | + test.bson, |
| 62 | + namespace, |
| 63 | + cursorState, |
| 64 | + BATCH_SIZE, |
| 65 | + undefined, |
| 66 | + connection, |
| 67 | + test.callback |
| 68 | + ); |
| 69 | + |
| 70 | + expect(connection.write).to.have.been.calledOnce; |
| 71 | + |
| 72 | + expectMsgToHaveSingleQuery(connection.write.lastCall.args[0]).to.deep.equal({ |
| 73 | + $db, |
| 74 | + collection, |
| 75 | + batchSize: 23000, |
| 76 | + getMore: cursorState.cursorId, |
| 77 | + maxTimeMS: cursorState.cmd.maxAwaitTimeMS |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('callback tests', function() { |
| 82 | + let queryCallback; |
| 83 | + |
| 84 | + beforeEach(() => { |
| 85 | + const cursorState = { |
| 86 | + cmd: { |
| 87 | + tailable: true, |
| 88 | + maxAwaitTimeMS: 1001 |
| 89 | + }, |
| 90 | + cursorId: 8675309 |
| 91 | + }; |
| 92 | + |
| 93 | + const BATCH_SIZE = -23000; |
| 94 | + |
| 95 | + executeGetMore( |
| 96 | + test.bson, |
| 97 | + namespace, |
| 98 | + cursorState, |
| 99 | + BATCH_SIZE, |
| 100 | + undefined, |
| 101 | + connection, |
| 102 | + test.callback |
| 103 | + ); |
| 104 | + |
| 105 | + queryCallback = connection.write.lastCall.args[2]; |
| 106 | + }); |
| 107 | + |
| 108 | + it('should pass along any errors to the callback', function() { |
| 109 | + const err = new Error('test error 1'); |
| 110 | + queryCallback(err); |
| 111 | + |
| 112 | + expect(test.callback).to.have.been.calledOnce.and.calledWithExactly(err); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should throw MongoNetworkError if responseFlag is nonzero', function() { |
| 116 | + const err = null; |
| 117 | + const response = { |
| 118 | + message: { |
| 119 | + documents: undefined, |
| 120 | + responseFlags: 1 |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + queryCallback(err, response); |
| 125 | + |
| 126 | + expect(test.callback).to.have.been.calledOnce; |
| 127 | + |
| 128 | + const returnedError = test.callback.lastCall.args[0]; |
| 129 | + |
| 130 | + expect(returnedError) |
| 131 | + .to.be.an.instanceOf(test.MongoNetworkError) |
| 132 | + .and.to.have.property('message') |
| 133 | + .that.equals('cursor killed or timed out'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return a MongoError if ok is 0', function() { |
| 137 | + const err = null; |
| 138 | + const badMsg = { ok: 0 }; |
| 139 | + const response = { |
| 140 | + message: { |
| 141 | + documents: [badMsg] |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + queryCallback(err, response); |
| 146 | + |
| 147 | + expect(test.callback).to.have.been.calledOnce; |
| 148 | + |
| 149 | + const returnedError = test.callback.lastCall.args[0]; |
| 150 | + |
| 151 | + expect(returnedError) |
| 152 | + .to.be.an.instanceOf(test.MongoError) |
| 153 | + .and.to.have.property('message') |
| 154 | + .that.equals('n/a'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should return a valid document and connection to the callback', function() { |
| 158 | + const validDoc = { ok: 1, cursor: { id: 8675309 } }; |
| 159 | + const err = null; |
| 160 | + const response = { |
| 161 | + message: { |
| 162 | + documents: [validDoc], |
| 163 | + connection: {} |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + queryCallback(err, response); |
| 168 | + |
| 169 | + expect(test.callback).to.have.been.calledOnce.and.to.have.been.calledWithExactly( |
| 170 | + null, |
| 171 | + response.message.documents[0], |
| 172 | + response.message.connection |
| 173 | + ); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments