| 192 | }; |
| 193 | |
| 194 | const getTestOpDefinitions = (threadContext: ThreadContext) => ({ |
| 195 | checkOut: async function (op) { |
| 196 | const timeoutContext = TimeoutContext.create({ |
| 197 | serverSelectionTimeoutMS: 0, |
| 198 | waitQueueTimeoutMS: threadContext.pool.options.waitQueueTimeoutMS |
| 199 | }); |
| 200 | const connection: Connection = await ConnectionPool.prototype.checkOut.call( |
| 201 | threadContext.pool, |
| 202 | { timeoutContext } |
| 203 | ); |
| 204 | if (op.label != null) { |
| 205 | threadContext.connections.set(op.label, connection); |
| 206 | } else { |
| 207 | threadContext.orphans.add(connection); |
| 208 | } |
| 209 | }, |
| 210 | checkIn: function (op) { |
| 211 | const connection = threadContext.connections.get(op.connection); |
| 212 | threadContext.connections.delete(op.connection); |
| 213 | |
| 214 | if (!connection) { |
| 215 | throw new Error(`Attempted to release non-existient connection ${op.connection}`); |
| 216 | } |
| 217 | |
| 218 | return threadContext.pool.checkIn(connection); |
| 219 | }, |
| 220 | clear: function ({ interruptInUseConnections }: { interruptInUseConnections: boolean }) { |
| 221 | return threadContext.pool.clear({ interruptInUseConnections }); |
| 222 | }, |
| 223 | close: function () { |
| 224 | return ConnectionPool.prototype.close.call(threadContext.pool); |
| 225 | }, |
| 226 | ready: function () { |
| 227 | return threadContext.pool.ready(); |
| 228 | }, |
| 229 | wait: function (options) { |
| 230 | const ms = options.ms; |
| 231 | return sleep(ms); |
| 232 | }, |
| 233 | start: function (options) { |
| 234 | const target = options.target; |
| 235 | const thread = threadContext.getThread(target); |
| 236 | thread.start(); |
| 237 | }, |
| 238 | waitForThread: async function (options): Promise<void> { |
| 239 | const name = options.name; |
| 240 | const target = options.target; |
| 241 | |
| 242 | const threadObj = threadContext.threads.get(target); |
| 243 | |
| 244 | if (!threadObj) { |
| 245 | throw new Error(`Attempted to run op ${name} on non-existent thread ${target}`); |
| 246 | } |
| 247 | |
| 248 | await threadObj.finish(); |
| 249 | }, |
| 250 | waitForEvent: function (options): Promise<void> { |
| 251 | const event = options.event; |