@terreno/test 57.4.0 → 57.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cache/mongoTestCache.js +175 -427
- package/dist/env/setTerrenoTestEnv.js +15 -65
- package/dist/http/authAsUser.js +28 -153
- package/dist/http/getBaseServer.js +6 -7
- package/dist/logging/setupEnvironment.js +1 -1
- package/dist/logging/silenceLogs.js +36 -93
- package/dist/logging/testLogger.js +4 -4
- package/dist/mocks/sentryBun.js +39 -39
- package/dist/mongo/connection.js +31 -115
- package/dist/mongo/ensureMongoBinary.js +12 -92
- package/dist/mongo/mongoServer.js +142 -390
- package/dist/preload/defaultBackendPreload.js +1 -1
- package/dist/preload/memoryMongo.d.ts +1 -0
- package/dist/preload/memoryMongo.js +1 -0
- package/dist/preload/registerBackendPreload.js +87 -262
- package/dist/transaction/testTransaction.js +224 -444
- package/dist/utils/ensureAllIndexes.js +9 -76
- package/dist/utils/timeout.js +3 -41
- package/dist/utils/waitForDocuments.js +18 -115
- package/package.json +4 -4
- package/tsconfig.json +6 -22
- package/dist/cache/mongoTestCache.test.d.ts +0 -1
- package/dist/cache/mongoTestCache.test.js +0 -182
- package/dist/env/setTerrenoTestEnv.test.d.ts +0 -1
- package/dist/env/setTerrenoTestEnv.test.js +0 -24
- package/dist/http/authAsUser.test.d.ts +0 -1
- package/dist/http/authAsUser.test.js +0 -93
- package/dist/http/getBaseServer.test.d.ts +0 -1
- package/dist/http/getBaseServer.test.js +0 -91
- package/dist/logging/setupEnvironment.test.d.ts +0 -1
- package/dist/logging/setupEnvironment.test.js +0 -23
- package/dist/logging/silenceLogs.test.d.ts +0 -1
- package/dist/logging/silenceLogs.test.js +0 -51
- package/dist/logging/testLogger.test.d.ts +0 -1
- package/dist/logging/testLogger.test.js +0 -56
- package/dist/mocks/sentryBun.test.d.ts +0 -1
- package/dist/mocks/sentryBun.test.js +0 -207
- package/dist/mongo/connection.test.d.ts +0 -1
- package/dist/mongo/connection.test.js +0 -40
- package/dist/mongo/mongoServer.test.d.ts +0 -1
- package/dist/mongo/mongoServer.test.js +0 -196
- package/dist/preload/registerBackendPreload.test.d.ts +0 -1
- package/dist/preload/registerBackendPreload.test.js +0 -10
- package/dist/transaction/testTransaction.test.d.ts +0 -1
- package/dist/transaction/testTransaction.test.js +0 -142
- package/dist/utils/helpers.test.d.ts +0 -1
- package/dist/utils/helpers.test.js +0 -113
|
@@ -40,98 +40,15 @@
|
|
|
40
40
|
* - Native driver operations (mongoose.connection.db.collection(...)) are
|
|
41
41
|
* NOT patched. Only Mongoose-level operations are covered.
|
|
42
42
|
*/
|
|
43
|
-
var __assign = (this && this.__assign) || function () {
|
|
44
|
-
__assign = Object.assign || function(t) {
|
|
45
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
46
|
-
s = arguments[i];
|
|
47
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
48
|
-
t[p] = s[p];
|
|
49
|
-
}
|
|
50
|
-
return t;
|
|
51
|
-
};
|
|
52
|
-
return __assign.apply(this, arguments);
|
|
53
|
-
};
|
|
54
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
55
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
56
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
57
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
58
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
59
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
60
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
64
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
65
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
66
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
67
|
-
function step(op) {
|
|
68
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
69
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
70
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
71
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
72
|
-
switch (op[0]) {
|
|
73
|
-
case 0: case 1: t = op; break;
|
|
74
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
75
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
76
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
77
|
-
default:
|
|
78
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
79
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
80
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
81
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
82
|
-
if (t[2]) _.ops.pop();
|
|
83
|
-
_.trys.pop(); continue;
|
|
84
|
-
}
|
|
85
|
-
op = body.call(thisArg, _);
|
|
86
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
87
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
91
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
92
|
-
if (!m) return o;
|
|
93
|
-
var i = m.call(o), r, ar = [], e;
|
|
94
|
-
try {
|
|
95
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
96
|
-
}
|
|
97
|
-
catch (error) { e = { error: error }; }
|
|
98
|
-
finally {
|
|
99
|
-
try {
|
|
100
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
101
|
-
}
|
|
102
|
-
finally { if (e) throw e.error; }
|
|
103
|
-
}
|
|
104
|
-
return ar;
|
|
105
|
-
};
|
|
106
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
107
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
108
|
-
if (ar || !(i in from)) {
|
|
109
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
110
|
-
ar[i] = from[i];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
114
|
-
};
|
|
115
|
-
var __values = (this && this.__values) || function(o) {
|
|
116
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
117
|
-
if (m) return m.call(o);
|
|
118
|
-
if (o && typeof o.length === "number") return {
|
|
119
|
-
next: function () {
|
|
120
|
-
if (o && i >= o.length) o = void 0;
|
|
121
|
-
return { value: o && o[i++], done: !o };
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
125
|
-
};
|
|
126
43
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
127
44
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
128
45
|
};
|
|
129
46
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
130
47
|
exports.installTransactionPatches = exports.resetTestSessionAfterReconnect = exports.abortTestTransaction = exports.startTestTransaction = exports.getTestSession = void 0;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
48
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
49
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
50
|
+
let currentTestSession = null;
|
|
51
|
+
let patchesInstalled = false;
|
|
135
52
|
// ============================================================================
|
|
136
53
|
// Reentrant Async Mutex
|
|
137
54
|
// ============================================================================
|
|
@@ -139,46 +56,38 @@ var patchesInstalled = false;
|
|
|
139
56
|
* Tracks whether the current async context is already inside a serialized
|
|
140
57
|
* DB operation. When true, nested operations skip the mutex to avoid deadlocks.
|
|
141
58
|
*/
|
|
142
|
-
|
|
59
|
+
const serializationContext = new node_async_hooks_1.AsyncLocalStorage();
|
|
143
60
|
/**
|
|
144
61
|
* Async mutex that serializes access to the MongoDB session.
|
|
145
62
|
* Only one DB operation can be in-flight at a time when a test session is active.
|
|
146
63
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
this.locked = true;
|
|
158
|
-
return [2 /*return*/];
|
|
159
|
-
}
|
|
160
|
-
return [2 /*return*/, new Promise(function (resolve) {
|
|
161
|
-
_this.queue.push(resolve);
|
|
162
|
-
})];
|
|
163
|
-
});
|
|
64
|
+
class AsyncMutex {
|
|
65
|
+
queue = [];
|
|
66
|
+
locked = false;
|
|
67
|
+
async acquire() {
|
|
68
|
+
if (!this.locked) {
|
|
69
|
+
this.locked = true;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
this.queue.push(resolve);
|
|
164
74
|
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
75
|
+
}
|
|
76
|
+
release() {
|
|
77
|
+
const next = this.queue.shift();
|
|
168
78
|
if (next) {
|
|
169
79
|
next();
|
|
170
80
|
}
|
|
171
81
|
else {
|
|
172
82
|
this.locked = false;
|
|
173
83
|
}
|
|
174
|
-
}
|
|
175
|
-
|
|
84
|
+
}
|
|
85
|
+
reset() {
|
|
176
86
|
this.locked = false;
|
|
177
87
|
this.queue = [];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
var sessionMutex = new AsyncMutex();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const sessionMutex = new AsyncMutex();
|
|
182
91
|
/**
|
|
183
92
|
* Returns whether the active test transaction session can be attached to DB
|
|
184
93
|
* operations on the given Mongoose connection. The Node driver's
|
|
@@ -189,15 +98,15 @@ var sessionMutex = new AsyncMutex();
|
|
|
189
98
|
* @param connection - Mongoose connection (`Model.db`), or undefined when unknown
|
|
190
99
|
* @returns True when the session should be passed into driver options / `.session()`
|
|
191
100
|
*/
|
|
192
|
-
|
|
101
|
+
const shouldAttachTestSessionToConnection = (connection) => {
|
|
193
102
|
if (!currentTestSession) {
|
|
194
103
|
return false;
|
|
195
104
|
}
|
|
196
|
-
|
|
105
|
+
const sessionClient = currentTestSession.client;
|
|
197
106
|
if (!connection || typeof connection.getClient !== "function") {
|
|
198
107
|
return true;
|
|
199
108
|
}
|
|
200
|
-
|
|
109
|
+
const connClient = connection.getClient();
|
|
201
110
|
if (!sessionClient || !connClient) {
|
|
202
111
|
return true;
|
|
203
112
|
}
|
|
@@ -210,48 +119,26 @@ var shouldAttachTestSessionToConnection = function (connection) {
|
|
|
210
119
|
* into the next test's transaction window, which can surface as intermittent
|
|
211
120
|
* NoSuchTransaction / transaction-number mismatch errors.
|
|
212
121
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
122
|
+
const waitForSerializedOperationsToDrain = async (timeoutMs = 5000) => {
|
|
123
|
+
let timeoutId;
|
|
124
|
+
try {
|
|
125
|
+
await Promise.race([
|
|
126
|
+
(async () => {
|
|
127
|
+
await sessionMutex.acquire();
|
|
128
|
+
sessionMutex.release();
|
|
129
|
+
})(),
|
|
130
|
+
new Promise((_, reject) => {
|
|
131
|
+
timeoutId = setTimeout(() => {
|
|
132
|
+
reject(new Error(`[testTransaction] Timed out waiting for serialized operations to drain`));
|
|
133
|
+
}, timeoutMs);
|
|
134
|
+
}),
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
if (timeoutId) {
|
|
139
|
+
clearTimeout(timeoutId);
|
|
140
|
+
}
|
|
217
141
|
}
|
|
218
|
-
return __awaiter(void 0, __spreadArray([], __read(args_1), false), void 0, function (timeoutMs) {
|
|
219
|
-
var timeoutId;
|
|
220
|
-
if (timeoutMs === void 0) { timeoutMs = 5000; }
|
|
221
|
-
return __generator(this, function (_a) {
|
|
222
|
-
switch (_a.label) {
|
|
223
|
-
case 0:
|
|
224
|
-
_a.trys.push([0, , 2, 3]);
|
|
225
|
-
return [4 /*yield*/, Promise.race([
|
|
226
|
-
(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
227
|
-
return __generator(this, function (_a) {
|
|
228
|
-
switch (_a.label) {
|
|
229
|
-
case 0: return [4 /*yield*/, sessionMutex.acquire()];
|
|
230
|
-
case 1:
|
|
231
|
-
_a.sent();
|
|
232
|
-
sessionMutex.release();
|
|
233
|
-
return [2 /*return*/];
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
}); })(),
|
|
237
|
-
new Promise(function (_, reject) {
|
|
238
|
-
timeoutId = setTimeout(function () {
|
|
239
|
-
reject(new Error("[testTransaction] Timed out waiting for serialized operations to drain"));
|
|
240
|
-
}, timeoutMs);
|
|
241
|
-
}),
|
|
242
|
-
])];
|
|
243
|
-
case 1:
|
|
244
|
-
_a.sent();
|
|
245
|
-
return [3 /*break*/, 3];
|
|
246
|
-
case 2:
|
|
247
|
-
if (timeoutId) {
|
|
248
|
-
clearTimeout(timeoutId);
|
|
249
|
-
}
|
|
250
|
-
return [7 /*endfinally*/];
|
|
251
|
-
case 3: return [2 /*return*/];
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
142
|
};
|
|
256
143
|
/**
|
|
257
144
|
* Wraps an async DB operation with the session mutex. Reentrant: if the current
|
|
@@ -259,71 +146,58 @@ var waitForSerializedOperationsToDrain = function () {
|
|
|
259
146
|
* without acquiring the mutex (preventing deadlocks from nested operations like
|
|
260
147
|
* pre-save hooks that do queries).
|
|
261
148
|
*/
|
|
262
|
-
|
|
149
|
+
const serialized = (fn) => {
|
|
263
150
|
// If no test session is active, skip serialization entirely
|
|
264
151
|
if (!currentTestSession) {
|
|
265
152
|
return fn();
|
|
266
153
|
}
|
|
267
154
|
// If we're already inside a serialized block (nested call), run directly
|
|
268
|
-
|
|
269
|
-
if (store
|
|
155
|
+
const store = serializationContext.getStore();
|
|
156
|
+
if (store?.active) {
|
|
270
157
|
return fn();
|
|
271
158
|
}
|
|
272
159
|
// Top-level call: acquire mutex, then run inside the serialization context
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
case 3:
|
|
288
|
-
_d.trys.push([3, 5, 11, 12]);
|
|
289
|
-
return [4 /*yield*/, serializationContext.run({ active: true }, fn)];
|
|
290
|
-
case 4: return [2 /*return*/, _d.sent()];
|
|
291
|
-
case 5:
|
|
292
|
-
error_1 = _d.sent();
|
|
293
|
-
isTransactionAborted = ((_c = (_b = error_1 === null || error_1 === void 0 ? void 0 : error_1.errorLabels) === null || _b === void 0 ? void 0 : _b.includes) === null || _c === void 0 ? void 0 : _c.call(_b, "TransientTransactionError")) ||
|
|
294
|
-
(currentTestSession && !currentTestSession.inTransaction());
|
|
295
|
-
if (!(!hasRetried && isTransactionAborted && currentTestSession)) return [3 /*break*/, 10];
|
|
160
|
+
const run = async () => {
|
|
161
|
+
let hasRetried = false;
|
|
162
|
+
while (true) {
|
|
163
|
+
await sessionMutex.acquire();
|
|
164
|
+
try {
|
|
165
|
+
return await serializationContext.run({ active: true }, fn);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
// If this error aborted the transaction (e.g. E11000 duplicate key),
|
|
169
|
+
// restart it and retry once so transient transaction-number mismatches
|
|
170
|
+
// don't fail the test immediately.
|
|
171
|
+
const isTransactionAborted = error?.errorLabels?.includes?.("TransientTransactionError") ||
|
|
172
|
+
(currentTestSession && !currentTestSession.inTransaction());
|
|
173
|
+
if (!hasRetried && isTransactionAborted && currentTestSession) {
|
|
296
174
|
hasRetried = true;
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
return [3 /*break*/, 10];
|
|
311
|
-
case 10: throw error_1;
|
|
312
|
-
case 11:
|
|
313
|
-
sessionMutex.release();
|
|
314
|
-
return [7 /*endfinally*/];
|
|
315
|
-
case 12: return [3 /*break*/, 1];
|
|
316
|
-
case 13: return [2 /*return*/];
|
|
175
|
+
try {
|
|
176
|
+
// Explicitly abort client-side transaction state before restart.
|
|
177
|
+
if (currentTestSession.inTransaction()) {
|
|
178
|
+
await currentTestSession.abortTransaction();
|
|
179
|
+
}
|
|
180
|
+
currentTestSession.startTransaction();
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Ignore — fallback to throwing original error below.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
throw error;
|
|
317
188
|
}
|
|
318
|
-
|
|
319
|
-
|
|
189
|
+
finally {
|
|
190
|
+
sessionMutex.release();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
320
194
|
return run();
|
|
321
195
|
};
|
|
322
196
|
// ============================================================================
|
|
323
197
|
// Public API
|
|
324
198
|
// ============================================================================
|
|
325
199
|
/** Returns the current test session, or null if not in a test transaction. */
|
|
326
|
-
|
|
200
|
+
const getTestSession = () => currentTestSession;
|
|
327
201
|
exports.getTestSession = getTestSession;
|
|
328
202
|
/**
|
|
329
203
|
* Starts a new test transaction. Call in beforeEach.
|
|
@@ -332,49 +206,31 @@ exports.getTestSession = getTestSession;
|
|
|
332
206
|
* created on the very first call or after the previous session has been
|
|
333
207
|
* explicitly ended (e.g., due to an error).
|
|
334
208
|
*/
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
_c.label = 5;
|
|
353
|
-
case 5:
|
|
354
|
-
_c.trys.push([5, 7, , 8]);
|
|
355
|
-
return [4 /*yield*/, currentTestSession.endSession()];
|
|
356
|
-
case 6:
|
|
357
|
-
_c.sent();
|
|
358
|
-
return [3 /*break*/, 8];
|
|
359
|
-
case 7:
|
|
360
|
-
_b = _c.sent();
|
|
361
|
-
return [3 /*break*/, 8];
|
|
362
|
-
case 8:
|
|
363
|
-
currentTestSession = null;
|
|
364
|
-
return [3 /*break*/, 9];
|
|
365
|
-
case 9:
|
|
366
|
-
if (!!currentTestSession) return [3 /*break*/, 11];
|
|
367
|
-
return [4 /*yield*/, mongoose_1.default.startSession()];
|
|
368
|
-
case 10:
|
|
369
|
-
currentTestSession = _c.sent();
|
|
370
|
-
_c.label = 11;
|
|
371
|
-
case 11:
|
|
372
|
-
sessionMutex.reset();
|
|
373
|
-
currentTestSession.startTransaction();
|
|
374
|
-
return [2 /*return*/];
|
|
209
|
+
const startTestTransaction = async () => {
|
|
210
|
+
if (currentTestSession) {
|
|
211
|
+
// Abort any leftover transaction from a previous test that wasn't cleaned up
|
|
212
|
+
try {
|
|
213
|
+
if (currentTestSession.inTransaction()) {
|
|
214
|
+
await currentTestSession.abortTransaction();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// Session may be in a bad state — create a fresh one below
|
|
219
|
+
try {
|
|
220
|
+
await currentTestSession.endSession();
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
// Ignore
|
|
224
|
+
}
|
|
225
|
+
currentTestSession = null;
|
|
375
226
|
}
|
|
376
|
-
}
|
|
377
|
-
|
|
227
|
+
}
|
|
228
|
+
if (!currentTestSession) {
|
|
229
|
+
currentTestSession = await mongoose_1.default.startSession();
|
|
230
|
+
}
|
|
231
|
+
sessionMutex.reset();
|
|
232
|
+
currentTestSession.startTransaction();
|
|
233
|
+
};
|
|
378
234
|
exports.startTestTransaction = startTestTransaction;
|
|
379
235
|
/**
|
|
380
236
|
* Aborts the current test transaction. Call in afterEach.
|
|
@@ -384,55 +240,36 @@ exports.startTestTransaction = startTestTransaction;
|
|
|
384
240
|
* session pool, which prevents transaction number mismatches when in-flight
|
|
385
241
|
* operations from a previous test complete after the session has been recycled.
|
|
386
242
|
*/
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return [4 /*yield*/, currentTestSession.abortTransaction()];
|
|
407
|
-
case 3:
|
|
408
|
-
_b.sent();
|
|
409
|
-
_b.label = 4;
|
|
410
|
-
case 4: return [3 /*break*/, 11];
|
|
411
|
-
case 5:
|
|
412
|
-
error_2 = _b.sent();
|
|
413
|
-
// biome-ignore lint/suspicious/noConsole: test harness diagnostic when abort fails
|
|
414
|
-
console.warn("[testTransaction] Error aborting transaction:", error_2);
|
|
415
|
-
_b.label = 6;
|
|
416
|
-
case 6:
|
|
417
|
-
_b.trys.push([6, 8, , 9]);
|
|
418
|
-
return [4 /*yield*/, currentTestSession.endSession()];
|
|
419
|
-
case 7:
|
|
420
|
-
_b.sent();
|
|
421
|
-
return [3 /*break*/, 9];
|
|
422
|
-
case 8:
|
|
423
|
-
_a = _b.sent();
|
|
424
|
-
return [3 /*break*/, 9];
|
|
425
|
-
case 9:
|
|
426
|
-
currentTestSession = null;
|
|
427
|
-
return [3 /*break*/, 11];
|
|
428
|
-
case 10:
|
|
429
|
-
// Clean slate for the next test transaction.
|
|
430
|
-
sessionMutex.reset();
|
|
431
|
-
return [7 /*endfinally*/];
|
|
432
|
-
case 11: return [2 /*return*/];
|
|
243
|
+
const abortTestTransaction = async () => {
|
|
244
|
+
if (!currentTestSession) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
// Ensure any in-flight/queued DB work from the current test has settled
|
|
249
|
+
// before aborting and starting the next transaction.
|
|
250
|
+
await waitForSerializedOperationsToDrain();
|
|
251
|
+
if (currentTestSession.inTransaction()) {
|
|
252
|
+
await currentTestSession.abortTransaction();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
// biome-ignore lint/suspicious/noConsole: test harness diagnostic when abort fails
|
|
257
|
+
console.warn("[testTransaction] Error aborting transaction:", error);
|
|
258
|
+
// If the abort failed, the session may be in a bad state. End it so
|
|
259
|
+
// startTestTransaction creates a fresh one.
|
|
260
|
+
try {
|
|
261
|
+
await currentTestSession.endSession();
|
|
433
262
|
}
|
|
434
|
-
|
|
435
|
-
|
|
263
|
+
catch {
|
|
264
|
+
// Ignore
|
|
265
|
+
}
|
|
266
|
+
currentTestSession = null;
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
// Clean slate for the next test transaction.
|
|
270
|
+
sessionMutex.reset();
|
|
271
|
+
}
|
|
272
|
+
};
|
|
436
273
|
exports.abortTestTransaction = abortTestTransaction;
|
|
437
274
|
/**
|
|
438
275
|
* Drops the cached test session after an out-of-band mongoose reconnect.
|
|
@@ -441,38 +278,25 @@ exports.abortTestTransaction = abortTestTransaction;
|
|
|
441
278
|
* ClientSession. Clearing here prevents subsequent tests from reusing a stale
|
|
442
279
|
* or expired session.
|
|
443
280
|
*/
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}
|
|
453
|
-
_b.label = 1;
|
|
454
|
-
case 1:
|
|
455
|
-
_b.trys.push([1, 5, 6, 7]);
|
|
456
|
-
if (!currentTestSession.inTransaction()) return [3 /*break*/, 3];
|
|
457
|
-
return [4 /*yield*/, currentTestSession.abortTransaction()];
|
|
458
|
-
case 2:
|
|
459
|
-
_b.sent();
|
|
460
|
-
_b.label = 3;
|
|
461
|
-
case 3: return [4 /*yield*/, currentTestSession.endSession()];
|
|
462
|
-
case 4:
|
|
463
|
-
_b.sent();
|
|
464
|
-
return [3 /*break*/, 7];
|
|
465
|
-
case 5:
|
|
466
|
-
_a = _b.sent();
|
|
467
|
-
return [3 /*break*/, 7];
|
|
468
|
-
case 6:
|
|
469
|
-
currentTestSession = null;
|
|
470
|
-
sessionMutex.reset();
|
|
471
|
-
return [7 /*endfinally*/];
|
|
472
|
-
case 7: return [2 /*return*/];
|
|
281
|
+
const resetTestSessionAfterReconnect = async () => {
|
|
282
|
+
if (!currentTestSession) {
|
|
283
|
+
sessionMutex.reset();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
if (currentTestSession.inTransaction()) {
|
|
288
|
+
await currentTestSession.abortTransaction();
|
|
473
289
|
}
|
|
474
|
-
|
|
475
|
-
}
|
|
290
|
+
await currentTestSession.endSession();
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
// Ignore cleanup failures; session is being discarded regardless.
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
currentTestSession = null;
|
|
297
|
+
sessionMutex.reset();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
476
300
|
exports.resetTestSessionAfterReconnect = resetTestSessionAfterReconnect;
|
|
477
301
|
// ============================================================================
|
|
478
302
|
// Mongoose Monkey-Patches
|
|
@@ -500,22 +324,19 @@ exports.resetTestSessionAfterReconnect = resetTestSessionAfterReconnect;
|
|
|
500
324
|
* created the test session, patches skip session injection for that operation
|
|
501
325
|
* so the driver does not reject mismatched clients.
|
|
502
326
|
*/
|
|
503
|
-
|
|
504
|
-
var e_1, _a, e_2, _b;
|
|
327
|
+
const installTransactionPatches = () => {
|
|
505
328
|
if (patchesInstalled) {
|
|
506
329
|
return;
|
|
507
330
|
}
|
|
508
331
|
// --- Query.prototype.exec ---
|
|
509
|
-
|
|
332
|
+
const originalQueryExec = mongoose_1.default.Query.prototype.exec;
|
|
510
333
|
mongoose_1.default.Query.prototype.exec = function () {
|
|
511
|
-
var _this = this;
|
|
512
|
-
var _a;
|
|
513
334
|
if (currentTestSession) {
|
|
514
|
-
if (!shouldAttachTestSessionToConnection(
|
|
335
|
+
if (!shouldAttachTestSessionToConnection(this.model?.db)) {
|
|
515
336
|
return originalQueryExec.apply(this);
|
|
516
337
|
}
|
|
517
338
|
this.session(currentTestSession);
|
|
518
|
-
return serialized(
|
|
339
|
+
return serialized(() => originalQueryExec.apply(this));
|
|
519
340
|
}
|
|
520
341
|
return originalQueryExec.apply(this);
|
|
521
342
|
};
|
|
@@ -523,68 +344,60 @@ var installTransactionPatches = function () {
|
|
|
523
344
|
// In Mongoose 8, Model.create() calls $save (not save). $save is assigned from
|
|
524
345
|
// save at module load time (Model.prototype.$save = Model.prototype.save), so we
|
|
525
346
|
// must patch both to ensure Model.create() goes through our session injection.
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
var connection = (_a = this.constructor) === null || _a === void 0 ? void 0 : _a.db;
|
|
532
|
-
if (!shouldAttachTestSessionToConnection(connection)) {
|
|
533
|
-
return original.call(this, options);
|
|
534
|
-
}
|
|
535
|
-
if (typeof options === "function") {
|
|
536
|
-
this.$session(currentTestSession);
|
|
537
|
-
return serialized(function () { return original.call(_this, options); });
|
|
538
|
-
}
|
|
539
|
-
options = __assign(__assign({}, (options || {})), { session: currentTestSession });
|
|
540
|
-
return serialized(function () { return original.call(_this, options); });
|
|
347
|
+
const patchSave = (original) => function (options) {
|
|
348
|
+
if (currentTestSession) {
|
|
349
|
+
const connection = this.constructor?.db;
|
|
350
|
+
if (!shouldAttachTestSessionToConnection(connection)) {
|
|
351
|
+
return original.call(this, options);
|
|
541
352
|
}
|
|
542
|
-
|
|
543
|
-
|
|
353
|
+
if (typeof options === "function") {
|
|
354
|
+
this.$session(currentTestSession);
|
|
355
|
+
return serialized(() => original.call(this, options));
|
|
356
|
+
}
|
|
357
|
+
options = { ...(options || {}), session: currentTestSession };
|
|
358
|
+
return serialized(() => original.call(this, options));
|
|
359
|
+
}
|
|
360
|
+
return original.call(this, options);
|
|
544
361
|
};
|
|
545
|
-
|
|
362
|
+
const originalSave = mongoose_1.default.Model.prototype.save;
|
|
546
363
|
mongoose_1.default.Model.prototype.save = patchSave(originalSave);
|
|
547
364
|
// $save is what Model.create() actually calls
|
|
548
|
-
|
|
365
|
+
const original$Save = mongoose_1.default.Model.prototype.$save;
|
|
549
366
|
mongoose_1.default.Model.prototype.$save = patchSave(original$Save);
|
|
550
367
|
// --- Aggregate.prototype.exec ---
|
|
551
|
-
|
|
368
|
+
const originalAggExec = mongoose_1.default.Aggregate.prototype.exec;
|
|
552
369
|
mongoose_1.default.Aggregate.prototype.exec = function () {
|
|
553
|
-
var _this = this;
|
|
554
|
-
var _a, _b;
|
|
555
370
|
if (currentTestSession) {
|
|
556
|
-
|
|
371
|
+
const connection = this._model?.db ?? this._connection;
|
|
557
372
|
if (!shouldAttachTestSessionToConnection(connection)) {
|
|
558
373
|
return originalAggExec.apply(this);
|
|
559
374
|
}
|
|
560
375
|
this.session(currentTestSession);
|
|
561
|
-
return serialized(
|
|
376
|
+
return serialized(() => originalAggExec.apply(this));
|
|
562
377
|
}
|
|
563
378
|
return originalAggExec.apply(this);
|
|
564
379
|
};
|
|
565
380
|
// --- Model.bulkWrite (static) ---
|
|
566
|
-
|
|
381
|
+
const originalBulkWrite = mongoose_1.default.Model.bulkWrite;
|
|
567
382
|
mongoose_1.default.Model.bulkWrite = function (ops, options) {
|
|
568
|
-
var _this = this;
|
|
569
383
|
if (currentTestSession) {
|
|
570
384
|
if (!shouldAttachTestSessionToConnection(this.db)) {
|
|
571
385
|
return originalBulkWrite.call(this, ops, options);
|
|
572
386
|
}
|
|
573
|
-
options =
|
|
574
|
-
return serialized(
|
|
387
|
+
options = { ...(options || {}), session: currentTestSession };
|
|
388
|
+
return serialized(() => originalBulkWrite.call(this, ops, options));
|
|
575
389
|
}
|
|
576
390
|
return originalBulkWrite.call(this, ops, options);
|
|
577
391
|
};
|
|
578
392
|
// --- Model.insertMany (static) ---
|
|
579
|
-
|
|
393
|
+
const originalInsertMany = mongoose_1.default.Model.insertMany;
|
|
580
394
|
mongoose_1.default.Model.insertMany = function (docs, options) {
|
|
581
|
-
var _this = this;
|
|
582
395
|
if (currentTestSession) {
|
|
583
396
|
if (!shouldAttachTestSessionToConnection(this.db)) {
|
|
584
397
|
return originalInsertMany.call(this, docs, options);
|
|
585
398
|
}
|
|
586
|
-
options =
|
|
587
|
-
return serialized(
|
|
399
|
+
options = { ...(options || {}), session: currentTestSession };
|
|
400
|
+
return serialized(() => originalInsertMany.call(this, docs, options));
|
|
588
401
|
}
|
|
589
402
|
return originalInsertMany.call(this, docs, options);
|
|
590
403
|
};
|
|
@@ -594,18 +407,13 @@ var installTransactionPatches = function () {
|
|
|
594
407
|
// serialization guard and race with other operations on the shared test
|
|
595
408
|
// session. Wrapping create ensures transient transaction aborts trigger the
|
|
596
409
|
// same auto-restart logic in serialized().
|
|
597
|
-
|
|
598
|
-
mongoose_1.default.Model.create = function () {
|
|
599
|
-
var _this = this;
|
|
600
|
-
var args = [];
|
|
601
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
602
|
-
args[_i] = arguments[_i];
|
|
603
|
-
}
|
|
410
|
+
const originalCreate = mongoose_1.default.Model.create;
|
|
411
|
+
mongoose_1.default.Model.create = function (...args) {
|
|
604
412
|
if (currentTestSession) {
|
|
605
413
|
if (!shouldAttachTestSessionToConnection(this.db)) {
|
|
606
414
|
return originalCreate.apply(this, args);
|
|
607
415
|
}
|
|
608
|
-
return serialized(
|
|
416
|
+
return serialized(() => originalCreate.apply(this, args));
|
|
609
417
|
}
|
|
610
418
|
return originalCreate.apply(this, args);
|
|
611
419
|
};
|
|
@@ -614,23 +422,20 @@ var installTransactionPatches = function () {
|
|
|
614
422
|
// through the test session. This ensures app code that creates its own
|
|
615
423
|
// transactions (e.g. userExplorerAcquisitionUpload.ts) doesn't bypass
|
|
616
424
|
// test isolation.
|
|
617
|
-
|
|
618
|
-
mongoose_1.default.Model.startSession = function (options) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
return [2 /*return*/, originalStartSession.call(this, options)];
|
|
632
|
-
});
|
|
633
|
-
});
|
|
425
|
+
const originalStartSession = mongoose_1.default.Model.startSession;
|
|
426
|
+
mongoose_1.default.Model.startSession = async function (options) {
|
|
427
|
+
if (currentTestSession) {
|
|
428
|
+
return {
|
|
429
|
+
...currentTestSession,
|
|
430
|
+
abortTransaction: async () => { },
|
|
431
|
+
commitTransaction: async () => { },
|
|
432
|
+
endSession: async () => { },
|
|
433
|
+
id: currentTestSession.id,
|
|
434
|
+
inTransaction: () => currentTestSession?.inTransaction() ?? false,
|
|
435
|
+
startTransaction: () => { },
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
return originalStartSession.call(this, options);
|
|
634
439
|
};
|
|
635
440
|
// --- Collection.prototype (native driver operations) ---
|
|
636
441
|
// Some test files call Model.collection.insertOne() etc. directly, bypassing
|
|
@@ -640,77 +445,52 @@ var installTransactionPatches = function () {
|
|
|
640
445
|
// Methods are grouped by their options argument position:
|
|
641
446
|
// - optionsAt1: (docOrFilter, options?) — insertOne, deleteOne, deleteMany, findOne, etc.
|
|
642
447
|
// - optionsAt2: (filter, update, options?) — updateOne, updateMany, findOneAndUpdate, etc.
|
|
643
|
-
|
|
644
|
-
|
|
448
|
+
const patchCollectionMethod = (method, optionsIndex) => {
|
|
449
|
+
const original = mongoose_1.default.Collection.prototype[method];
|
|
645
450
|
if (typeof original !== "function") {
|
|
646
451
|
return;
|
|
647
452
|
}
|
|
648
|
-
mongoose_1.default.Collection.prototype[method] = function () {
|
|
649
|
-
var _this = this;
|
|
650
|
-
var args = [];
|
|
651
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
652
|
-
args[_i] = arguments[_i];
|
|
653
|
-
}
|
|
453
|
+
mongoose_1.default.Collection.prototype[method] = function (...args) {
|
|
654
454
|
if (!currentTestSession) {
|
|
655
455
|
return original.apply(this, args);
|
|
656
456
|
}
|
|
657
457
|
if (!shouldAttachTestSessionToConnection(this.conn)) {
|
|
658
458
|
return original.apply(this, args);
|
|
659
459
|
}
|
|
660
|
-
return serialized(
|
|
460
|
+
return serialized(() => {
|
|
661
461
|
// Inject session into the options argument at the correct position
|
|
662
462
|
if (args[optionsIndex] && typeof args[optionsIndex] === "object") {
|
|
663
|
-
args[optionsIndex] =
|
|
463
|
+
args[optionsIndex] = { ...args[optionsIndex], session: currentTestSession };
|
|
664
464
|
}
|
|
665
465
|
else {
|
|
666
466
|
args[optionsIndex] = { session: currentTestSession };
|
|
667
467
|
}
|
|
668
|
-
return original.apply(
|
|
468
|
+
return original.apply(this, args);
|
|
669
469
|
});
|
|
670
470
|
};
|
|
671
471
|
};
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
var method = _d.value;
|
|
685
|
-
patchCollectionMethod(method, 1);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
689
|
-
finally {
|
|
690
|
-
try {
|
|
691
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
692
|
-
}
|
|
693
|
-
finally { if (e_1) throw e_1.error; }
|
|
472
|
+
// (docOrFilter, options?) — options at index 1
|
|
473
|
+
for (const method of [
|
|
474
|
+
"insertOne",
|
|
475
|
+
"insertMany",
|
|
476
|
+
"deleteOne",
|
|
477
|
+
"deleteMany",
|
|
478
|
+
"findOne",
|
|
479
|
+
"findOneAndDelete",
|
|
480
|
+
"countDocuments",
|
|
481
|
+
"aggregate",
|
|
482
|
+
]) {
|
|
483
|
+
patchCollectionMethod(method, 1);
|
|
694
484
|
}
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
var method = _f.value;
|
|
705
|
-
patchCollectionMethod(method, 2);
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
709
|
-
finally {
|
|
710
|
-
try {
|
|
711
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
712
|
-
}
|
|
713
|
-
finally { if (e_2) throw e_2.error; }
|
|
485
|
+
// (filter, update/replacement, options?) — options at index 2
|
|
486
|
+
for (const method of [
|
|
487
|
+
"updateOne",
|
|
488
|
+
"updateMany",
|
|
489
|
+
"findOneAndUpdate",
|
|
490
|
+
"findOneAndReplace",
|
|
491
|
+
"replaceOne",
|
|
492
|
+
]) {
|
|
493
|
+
patchCollectionMethod(method, 2);
|
|
714
494
|
}
|
|
715
495
|
patchesInstalled = true;
|
|
716
496
|
if (process.env.DEBUG_TEST_TRANSACTION === "true") {
|