|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* global describe before after it */ |
| 4 | + |
| 5 | +/* eslint-disable */ |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { expect, should } = require('chai'); |
| 9 | +const { admin } = require('./admin'); |
| 10 | + |
| 11 | +/* eslint-enable */ |
| 12 | + |
| 13 | +const path = require('path'); |
| 14 | +const chai = require('chai'); |
| 15 | +const chaiAsPromised = require('chai-as-promised'); |
| 16 | +const environment = require('dotenv'); |
| 17 | +const varium = require('varium'); |
| 18 | +const { connect } = require('marpat'); |
| 19 | +const { Filemaker } = require('../index.js'); |
| 20 | + |
| 21 | +const manifestPath = path.join(__dirname, './env.manifest'); |
| 22 | + |
| 23 | +chai.use(chaiAsPromised); |
| 24 | + |
| 25 | +describe('Session Capabilities', () => { |
| 26 | + describe('Session Efficency', () => { |
| 27 | + let database; |
| 28 | + let client; |
| 29 | + const name = 'testing-client'; |
| 30 | + |
| 31 | + before(done => { |
| 32 | + environment.config({ path: './test/.env' }); |
| 33 | + varium({ manifestPath }); |
| 34 | + connect('nedb://memory') |
| 35 | + .then(db => { |
| 36 | + database = db; |
| 37 | + return database.dropDatabase(); |
| 38 | + }) |
| 39 | + .then(() => { |
| 40 | + client = Filemaker.create({ |
| 41 | + name, |
| 42 | + database: process.env.DATABASE, |
| 43 | + server: process.env.SERVER, |
| 44 | + user: process.env.USERNAME, |
| 45 | + concurrency: 25, |
| 46 | + password: process.env.PASSWORD |
| 47 | + }); |
| 48 | + return client.save(); |
| 49 | + }) |
| 50 | + .then(client => admin.login()) |
| 51 | + .then(() => admin.sessions.drop({ userName: process.env.USERNAME })) |
| 52 | + .then(() => setTimeout(() => done(), 15000)); |
| 53 | + }); |
| 54 | + |
| 55 | + after(done => { |
| 56 | + admin |
| 57 | + .logout() |
| 58 | + .then(() => done()) |
| 59 | + .catch(error => done(new Error(error.response.data.messages[0].text))); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should reuse sessions when they are avalable', () => { |
| 63 | + const wait = 5000; |
| 64 | + const repetition = 5; |
| 65 | + const results = []; |
| 66 | + const chain = client => |
| 67 | + new Promise((resolve, reject) => { |
| 68 | + const interval = setInterval(() => { |
| 69 | + client |
| 70 | + .find( |
| 71 | + process.env.LAYOUT, |
| 72 | + { id: '*' }, |
| 73 | + { omit: true, name: 'Darth Vader' } |
| 74 | + ) |
| 75 | + .then(result => { |
| 76 | + results.push(results); |
| 77 | + if (results.length >= repetition) { |
| 78 | + clearInterval(interval); |
| 79 | + resolve({ client, results }); |
| 80 | + } |
| 81 | + }); |
| 82 | + }, wait); |
| 83 | + }); |
| 84 | + |
| 85 | + return expect( |
| 86 | + Filemaker.findOne({ name }) |
| 87 | + .then(client => chain(client)) |
| 88 | + .then( |
| 89 | + ({ client, results }) => |
| 90 | + new Promise(resolve => |
| 91 | + setTimeout(() => resolve({ client, results }), 12000) |
| 92 | + ) |
| 93 | + ) |
| 94 | + .then(() => |
| 95 | + admin.sessions.find({ |
| 96 | + userName: process.env.USERNAME |
| 97 | + }) |
| 98 | + ) |
| 99 | + ) |
| 100 | + .to.eventually.be.a('array') |
| 101 | + .to.have.a.lengthOf(1); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Session Concurrency', () => { |
| 106 | + let database; |
| 107 | + let client; |
| 108 | + const name = 'testing-client'; |
| 109 | + |
| 110 | + before(done => { |
| 111 | + environment.config({ path: './test/.env' }); |
| 112 | + varium({ manifestPath }); |
| 113 | + connect('nedb://memory') |
| 114 | + .then(db => { |
| 115 | + database = db; |
| 116 | + return database.dropDatabase(); |
| 117 | + }) |
| 118 | + .then(() => { |
| 119 | + client = Filemaker.create({ |
| 120 | + name, |
| 121 | + database: process.env.DATABASE, |
| 122 | + server: process.env.SERVER, |
| 123 | + user: process.env.USERNAME, |
| 124 | + concurrency: 25, |
| 125 | + password: process.env.PASSWORD |
| 126 | + }); |
| 127 | + return client.save(); |
| 128 | + }) |
| 129 | + .then(client => admin.login()) |
| 130 | + .then(() => admin.sessions.drop({ userName: process.env.USERNAME })) |
| 131 | + .then(() => setTimeout(() => done(), 15000)); |
| 132 | + }); |
| 133 | + |
| 134 | + after(done => { |
| 135 | + admin |
| 136 | + .logout() |
| 137 | + .then(() => done()) |
| 138 | + .catch(error => done(new Error(error.response.data.messages[0].text))); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should create new sessions to prevent request collisions', () => { |
| 142 | + const wait = 1000; |
| 143 | + let current = 0; |
| 144 | + const repetition = 5; |
| 145 | + const results = []; |
| 146 | + const chain = client => |
| 147 | + new Promise((resolve, reject) => { |
| 148 | + const interval = setInterval(() => { |
| 149 | + current = current + 1; |
| 150 | + if (current <= repetition) { |
| 151 | + client |
| 152 | + .script(process.env.LAYOUT, 'Pause Script', { |
| 153 | + pause: 5 |
| 154 | + }) |
| 155 | + .then(result => { |
| 156 | + results.push(results); |
| 157 | + if (results.length >= repetition) { |
| 158 | + resolve({ client, results }); |
| 159 | + } |
| 160 | + }); |
| 161 | + } else { |
| 162 | + clearInterval(interval); |
| 163 | + } |
| 164 | + }, wait); |
| 165 | + }); |
| 166 | + |
| 167 | + return expect( |
| 168 | + Filemaker.findOne({ name }) |
| 169 | + .then(client => chain(client)) |
| 170 | + .then( |
| 171 | + ({ client, results }) => |
| 172 | + new Promise(resolve => |
| 173 | + setTimeout(() => resolve({ client, results }), 12000) |
| 174 | + ) |
| 175 | + ) |
| 176 | + .then(() => |
| 177 | + admin.sessions.find({ |
| 178 | + userName: process.env.USERNAME |
| 179 | + }) |
| 180 | + ) |
| 181 | + ) |
| 182 | + .to.eventually.be.a('array') |
| 183 | + .to.have.a.lengthOf(repetition); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments