|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { once } from 'events'; |
3 | 3 | import * as net from 'net'; |
| 4 | +import { Socket } from 'net'; |
4 | 5 | import * as sinon from 'sinon'; |
5 | 6 |
|
6 | 7 | import { |
@@ -135,6 +136,93 @@ describe('class MongoClient', function () { |
135 | 136 | expect(error).to.be.instanceOf(MongoServerSelectionError); |
136 | 137 | }); |
137 | 138 |
|
| 139 | + describe('#connect', function () { |
| 140 | + context('when keepAliveInitialDelay is provided', function () { |
| 141 | + context('when the value is 0', function () { |
| 142 | + const options = { keepAliveInitialDelay: 0 }; |
| 143 | + let client; |
| 144 | + let spy; |
| 145 | + |
| 146 | + beforeEach(async function () { |
| 147 | + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); |
| 148 | + client = this.configuration.newClient(options); |
| 149 | + await client.connect(); |
| 150 | + }); |
| 151 | + |
| 152 | + afterEach(async function () { |
| 153 | + await client?.close(); |
| 154 | + spy.restore(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('passes through the option', function () { |
| 158 | + expect(spy).to.have.been.calledWith(true, 0); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + context('when the value is positive', function () { |
| 163 | + const options = { keepAliveInitialDelay: 100 }; |
| 164 | + let client; |
| 165 | + let spy; |
| 166 | + |
| 167 | + beforeEach(async function () { |
| 168 | + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); |
| 169 | + client = this.configuration.newClient(options); |
| 170 | + await client.connect(); |
| 171 | + }); |
| 172 | + |
| 173 | + afterEach(async function () { |
| 174 | + await client?.close(); |
| 175 | + spy.restore(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('passes through the option', function () { |
| 179 | + expect(spy).to.have.been.calledWith(true, 100); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + context('when the value is negative', function () { |
| 184 | + const options = { keepAliveInitialDelay: -100 }; |
| 185 | + let client; |
| 186 | + let spy; |
| 187 | + |
| 188 | + beforeEach(async function () { |
| 189 | + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); |
| 190 | + client = this.configuration.newClient(options); |
| 191 | + await client.connect(); |
| 192 | + }); |
| 193 | + |
| 194 | + afterEach(async function () { |
| 195 | + await client?.close(); |
| 196 | + spy.restore(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('passes through the option', function () { |
| 200 | + expect(spy).to.have.been.calledWith(true, -100); |
| 201 | + }); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + context('when keepAliveInitialDelay is not provided', function () { |
| 206 | + let client; |
| 207 | + let spy; |
| 208 | + |
| 209 | + beforeEach(async function () { |
| 210 | + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); |
| 211 | + client = this.configuration.newClient(); |
| 212 | + await client.connect(); |
| 213 | + }); |
| 214 | + |
| 215 | + afterEach(async function () { |
| 216 | + await client?.close(); |
| 217 | + spy.restore(); |
| 218 | + }); |
| 219 | + |
| 220 | + it('sets keepalive to 120000', function () { |
| 221 | + expect(spy).to.have.been.calledWith(true, 120000); |
| 222 | + }); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
138 | 226 | it('Should correctly pass through appname', { |
139 | 227 | metadata: { |
140 | 228 | requires: { |
|
0 commit comments