<commit_before>'use strict';

const Server = require('../../..').Server,
  mock = require('../../mock'),
  expect = require('chai').expect,
  ServerSessionPool = require('../../../lib/sessions').ServerSessionPool,
  ServerSession = require('../../../lib/sessions').ServerSession,
  ClientSession = require('../../../lib/sessions').ClientSession,
  genClusterTime = require('./common').genClusterTime;

let test = {};
describe('Sessions', function() {
  describe('ClientSession', function() {
    it('should default to `null` for `clusterTime`', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        const client = new Server();
        const sessionPool = new ServerSessionPool(client);
        const session = new ClientSession(client, sessionPool);
        expect(session.clusterTime).to.not.exist;
      }
    });

    it('should set the internal clusterTime to `initialClusterTime` if provided', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        const clusterTime = genClusterTime(Date.now());
        const client = new Server();
        const sessionPool = new ServerSessionPool(client);
        const session = new ClientSession(client, sessionPool, { initialClusterTime: clusterTime });
        expect(session.clusterTime).to.eql(clusterTime);
      }
    });
  });

  describe('ServerSessionPool', function() {
    afterEach(() => {
      test.client.destroy();
      return mock.cleanup();
    });

    beforeEach(() => {
      return mock
        .createServer()
        .then(server => {
          test.server = server;
          test.server.setMessageHandler(request => {
            var doc = request.document;
            if (doc.ismaster) {
              request.reply(
                Object.assign({}, mock.DEFAULT_ISMASTER, { logicalSessionTimeoutMinutes: 10 })
              );
            }
          });
        })
        .then(() => {
          test.client = new Server(test.server.address());

          return new Promise((resolve, reject) => {
            test.client.once('error', reject);
            test.client.once('connect', resolve);
            test.client.connect();
          });
        });
    });

    it('should create a new session if the pool is empty', {
      metadata: { requires: { topology: 'single' } },
      test: function(done) {
        const pool = new ServerSessionPool(test.client);
        expect(pool.sessions).to.have.length(0);<commit_msg>refactor(sessions): add type checks for required parameters<commit_after>'use strict';

const Server = require('../../..').Server,
  mock = require('../../mock'),
  expect = require('chai').expect,
  ServerSessionPool = require('../../../lib/sessions').ServerSessionPool,
  ServerSession = require('../../../lib/sessions').ServerSession,
  ClientSession = require('../../../lib/sessions').ClientSession,
  genClusterTime = require('./common').genClusterTime;

let test = {};
describe('Sessions', function() {
  describe('ClientSession', function() {
    it('should throw errors with invalid parameters', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        expect(() => {
          new ClientSession();
        }).to.throw(/ClientSession requires a topology/);

        expect(() => {
          new ClientSession({});
        }).to.throw(/ClientSession requires a ServerSessionPool/);

        expect(() => {
          new ClientSession({}, {});
        }).to.throw(/ClientSession requires a ServerSessionPool/);
      }
    });

    it('should default to `null` for `clusterTime`', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        const client = new Server();
        const sessionPool = new ServerSessionPool(client);
        const session = new ClientSession(client, sessionPool);
        expect(session.clusterTime).to.not.exist;
      }
    });

    it('should set the internal clusterTime to `initialClusterTime` if provided', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        const clusterTime = genClusterTime(Date.now());
        const client = new Server();
        const sessionPool = new ServerSessionPool(client);
        const session = new ClientSession(client, sessionPool, { initialClusterTime: clusterTime });
        expect(session.clusterTime).to.eql(clusterTime);
      }
    });
  });

  describe('ServerSessionPool', function() {
    afterEach(() => {
      test.client.destroy();
      return mock.cleanup();
    });

    beforeEach(() => {
      return mock
        .createServer()
        .then(server => {
          test.server = server;
          test.server.setMessageHandler(request => {
            var doc = request.document;
            if (doc.ismaster) {
              request.reply(
                Object.assign({}, mock.DEFAULT_ISMASTER, { logicalSessionTimeoutMinutes: 10 })
              );
            }
          });
        })
        .then(() => {
          test.client = new Server(test.server.address());

          return new Promise((resolve, reject) => {
            test.client.once('error', reject);
            test.client.once('connect', resolve);
            test.client.connect();
          });
        });
    });

    it('should throw errors with invalid parameters', {
      metadata: { requires: { topology: 'single' } },
      test: function() {
        expect(() => {
          new ServerSessionPool();
        }).to.throw(/ServerSessionPool requires a topology/);
      }
    });

    it('should create a new session if the pool is empty', {
      metadata: { requires: { topology: 'single' } },
      test: function(done) {
        const pool = new ServerSessionPool(test.client);
        expect(pool.sessions).to.have.length(0);
{'fraction_non_alphanumeric': 0.1560553633217993, 'fraction_numerical': 0.0010380622837370243, 'mean_word_length': 2.361046511627907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063648', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1493, 'n_words': 339}