|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { isHello, MongoClient } from '../../mongodb'; |
| 4 | +import { ReplSetFixture } from '../../tools/common'; |
| 5 | +import * as mock from '../../tools/mongodb-mock/index'; |
| 6 | +import { type MockServer } from '../../tools/mongodb-mock/src/server'; |
| 7 | + |
| 8 | +const test: { server: MockServer } = { server: undefined }; |
| 9 | +describe('Sessions - client/unit', function () { |
| 10 | + describe('Client', function () { |
| 11 | + afterEach(async () => await mock.cleanup()); |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const server: MockServer = await mock.createServer(); |
| 15 | + test.server = server; |
| 16 | + }); |
| 17 | + |
| 18 | + it('should not throw a synchronous exception if sessions are not supported', async function () { |
| 19 | + test.server.setMessageHandler(request => { |
| 20 | + const doc = request.document; |
| 21 | + if (isHello(doc)) { |
| 22 | + request.reply(Object.assign({}, mock.HELLO)); |
| 23 | + } else if (doc.endSessions) { |
| 24 | + request.reply({ ok: 1 }); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + const client = new MongoClient(`mongodb://${test.server.uri()}/test`); |
| 29 | + await client.connect(); |
| 30 | + expect(() => client.startSession()).to.not.throw( |
| 31 | + 'Current topology does not support sessions' |
| 32 | + ); |
| 33 | + await client.close(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should throw an exception if sessions are not supported on some servers', async function () { |
| 37 | + const replicaSetMock = new ReplSetFixture(); |
| 38 | + let testClient; |
| 39 | + try { |
| 40 | + await replicaSetMock.setup({ doNotInitHandlers: true }); |
| 41 | + replicaSetMock.firstSecondaryServer.setMessageHandler(request => { |
| 42 | + const doc = request.document; |
| 43 | + if (isHello(doc)) { |
| 44 | + const hello = replicaSetMock.firstSecondaryStates[0]; |
| 45 | + hello.logicalSessionTimeoutMinutes = 20; |
| 46 | + request.reply(hello); |
| 47 | + } else if (doc.endSessions) { |
| 48 | + request.reply({ ok: 1 }); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + replicaSetMock.secondSecondaryServer.setMessageHandler(request => { |
| 53 | + const doc = request.document; |
| 54 | + if (isHello(doc)) { |
| 55 | + const hello = replicaSetMock.secondSecondaryStates[0]; |
| 56 | + hello.logicalSessionTimeoutMinutes = 10; |
| 57 | + request.reply(hello); |
| 58 | + } else if (doc.endSessions) { |
| 59 | + request.reply({ ok: 1 }); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + replicaSetMock.arbiterServer.setMessageHandler(request => { |
| 64 | + const doc = request.document; |
| 65 | + if (isHello(doc)) { |
| 66 | + const hello = replicaSetMock.arbiterStates[0]; |
| 67 | + hello.logicalSessionTimeoutMinutes = 30; |
| 68 | + request.reply(hello); |
| 69 | + } else if (doc.endSessions) { |
| 70 | + request.reply({ ok: 1 }); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + replicaSetMock.primaryServer.setMessageHandler(request => { |
| 75 | + const doc = request.document; |
| 76 | + if (isHello(doc)) { |
| 77 | + const hello = replicaSetMock.primaryStates[0]; |
| 78 | + hello.logicalSessionTimeoutMinutes = null; |
| 79 | + request.reply(hello); |
| 80 | + } else if (doc.endSessions) { |
| 81 | + request.reply({ ok: 1 }); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + const uri = replicaSetMock.uri(); |
| 86 | + testClient = new MongoClient(uri); |
| 87 | + const client = await testClient.connect(); |
| 88 | + const session = client.startSession(); |
| 89 | + await client.db().collection('t').insertOne({ a: 1 }, { session }); |
| 90 | + expect.fail('Expected an error to be thrown about not supporting sessions'); |
| 91 | + } catch (error) { |
| 92 | + expect(error.message).to.equal('Current topology does not support sessions'); |
| 93 | + } finally { |
| 94 | + if (testClient) { |
| 95 | + await testClient.close(); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return a client session when requested if the topology supports it', async function () { |
| 101 | + test.server.setMessageHandler(request => { |
| 102 | + const doc = request.document; |
| 103 | + if (isHello(doc)) { |
| 104 | + request.reply( |
| 105 | + Object.assign({}, mock.HELLO, { |
| 106 | + logicalSessionTimeoutMinutes: 10 |
| 107 | + }) |
| 108 | + ); |
| 109 | + } else if (doc.endSessions) { |
| 110 | + request.reply({ ok: 1 }); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + const client = new MongoClient(`mongodb://${test.server.uri()}/test`); |
| 115 | + await client.connect(); |
| 116 | + const session = client.startSession(); |
| 117 | + expect(session).to.exist; |
| 118 | + session.endSession(); |
| 119 | + await client.close(); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments