|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Server = require('../../..').Server, |
| 4 | + mock = require('../../mock'), |
| 5 | + expect = require('chai').expect, |
| 6 | + ServerSessionPool = require('../../../lib/sessions').ServerSessionPool, |
| 7 | + ServerSession = require('../../../lib/sessions').ServerSession; |
| 8 | + |
| 9 | +let test = {}; |
| 10 | +describe('Sessions', function() { |
| 11 | + describe('ServerSessionPool', function() { |
| 12 | + afterEach(() => { |
| 13 | + test.client.destroy(); |
| 14 | + return mock.cleanup(); |
| 15 | + }); |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + return mock |
| 19 | + .createServer() |
| 20 | + .then(server => { |
| 21 | + test.server = server; |
| 22 | + test.server.setMessageHandler(request => { |
| 23 | + var doc = request.document; |
| 24 | + if (doc.ismaster) { |
| 25 | + request.reply( |
| 26 | + Object.assign({}, mock.DEFAULT_ISMASTER, { logicalSessionTimeoutMinutes: 10 }) |
| 27 | + ); |
| 28 | + } |
| 29 | + }); |
| 30 | + }) |
| 31 | + .then(() => { |
| 32 | + test.client = new Server(test.server.address()); |
| 33 | + |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + test.client.once('error', reject); |
| 36 | + test.client.once('connect', resolve); |
| 37 | + test.client.connect(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should create a new session if the pool is empty', { |
| 43 | + metadata: { requires: { topology: 'single' } }, |
| 44 | + |
| 45 | + test: function(done) { |
| 46 | + const pool = new ServerSessionPool(test.client); |
| 47 | + expect(pool.sessions).to.have.length(0); |
| 48 | + const session = pool.dequeue(); |
| 49 | + expect(session).to.exist; |
| 50 | + expect(pool.sessions).to.have.length(0); |
| 51 | + done(); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('should reuse sessions which have not timed out yet on dequeue', { |
| 56 | + metadata: { requires: { topology: 'single' } }, |
| 57 | + |
| 58 | + test: function(done) { |
| 59 | + const oldSession = new ServerSession(); |
| 60 | + const pool = new ServerSessionPool(test.client); |
| 61 | + pool.sessions.push(oldSession); |
| 62 | + |
| 63 | + const session = pool.dequeue(); |
| 64 | + expect(session).to.exist; |
| 65 | + expect(session).to.eql(oldSession); |
| 66 | + |
| 67 | + done(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it('should remove sessions which have timed out on dequeue, and return a fresh session', { |
| 72 | + metadata: { requires: { topology: 'single' } }, |
| 73 | + |
| 74 | + test: function(done) { |
| 75 | + const oldSession = new ServerSession(); |
| 76 | + oldSession.lastUse = new Date(Date.now() - 30 * 60 * 1000).getTime(); // add 30min |
| 77 | + |
| 78 | + const pool = new ServerSessionPool(test.client); |
| 79 | + pool.sessions.push(oldSession); |
| 80 | + |
| 81 | + const session = pool.dequeue(); |
| 82 | + expect(session).to.exist; |
| 83 | + expect(session).to.not.eql(oldSession); |
| 84 | + |
| 85 | + done(); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('should remove sessions which have timed out on enqueue', { |
| 90 | + metadata: { requires: { topology: 'single' } }, |
| 91 | + |
| 92 | + test: function(done) { |
| 93 | + const newSession = new ServerSession(); |
| 94 | + const oldSessions = [new ServerSession(), new ServerSession()].map(session => { |
| 95 | + session.lastUse = new Date(Date.now() - 30 * 60 * 1000).getTime(); // add 30min |
| 96 | + return session; |
| 97 | + }); |
| 98 | + |
| 99 | + const pool = new ServerSessionPool(test.client); |
| 100 | + pool.sessions = pool.sessions.concat(oldSessions); |
| 101 | + |
| 102 | + pool.enqueue(newSession); |
| 103 | + expect(pool.sessions).to.have.length(1); |
| 104 | + expect(pool.sessions[0]).to.eql(newSession); |
| 105 | + done(); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments