-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mongodb-timeouts-direct.js
More file actions
executable file
·70 lines (59 loc) · 2.1 KB
/
test-mongodb-timeouts-direct.js
File metadata and controls
executable file
·70 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
'use strict'
const { MongoClient } = require('mongodb')
async function testMongoTimeouts() {
console.log('🧪 Testing MongoDB Timeout Configurations\n')
// Use the same config as your workers
const uri = 'mongodb://127.0.0.1:27017/?replicaSet=rs0&maxPoolSize=150'
const client = new MongoClient(uri, {
socketTimeoutMS: 30000,
serverSelectionTimeoutMS: 30000,
connectTimeoutMS: 10000
})
try {
console.log('1️⃣ Connecting to MongoDB...')
await client.connect()
console.log('✅ Connected\n')
const db = client.db('test_worker_timeouts')
const collection = db.collection('test_wallets')
console.log('2️⃣ Testing write operation...')
const wallet = {
userId: `user-${Date.now()}`,
type: 'test',
createdAt: new Date()
}
const result = await collection.insertOne(wallet)
console.log('✅ Written:', result.insertedId, '\n')
console.log('3️⃣ Testing read operation...')
const found = await collection.findOne({ _id: result.insertedId })
console.log('✅ Read:', found.userId, '\n')
console.log('4️⃣ Testing transaction (requires replica set)...')
const session = client.startSession()
try {
await session.withTransaction(async () => {
await collection.updateOne(
{ _id: result.insertedId },
{ $set: { updated: true } },
{ session }
)
console.log('✅ Transaction committed\n')
}, {
readConcern: { level: 'majority' },
writeConcern: { w: 'majority', wtimeout: 2000 },
maxCommitTimeMS: 1500
})
} finally {
await session.endSession()
}
console.log('✅ All MongoDB operations successful!\n')
console.log('💡 Now test failure scenarios:')
console.log(' In another terminal: docker stop mongo2 mongo3')
console.log(' Then run this script again - operations should timeout')
} catch (err) {
console.error('❌ Error:', err.message)
if (err.code) console.error(' Code:', err.code)
} finally {
await client.close()
}
}
testMongoTimeouts().catch(console.error)