Skip to content

Commit bbfbaaf

Browse files
committed
chore: no ns getter and util client closed
1 parent bacb203 commit bbfbaaf

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

test/benchmarks/driver_bench/src/driver.mts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,11 @@ export function metrics(test_name: string, result: number, count: number) {
135135
* For use in setup/teardown mostly.
136136
*/
137137
export class DriverTester {
138-
private utilClient: mongodb.MongoClient;
139138
public client: mongodb.MongoClient;
140139
constructor() {
141-
this.utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
142140
this.client = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
143141
}
144142

145-
private get ns() {
146-
return this.utilClient.db(DB_NAME).collection(COLLECTION_NAME);
147-
}
148-
149143
public get db() {
150144
return this.client.db(DB_NAME);
151145
}
@@ -159,15 +153,21 @@ export class DriverTester {
159153
}
160154

161155
async drop() {
162-
await this.ns.drop().catch(() => null);
163-
await this.utilClient
164-
.db(DB_NAME)
165-
.dropDatabase()
166-
.catch(() => null);
156+
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
157+
const db = utilClient.db(DB_NAME);
158+
const collection = db.collection(COLLECTION_NAME);
159+
await collection.drop().catch(() => null);
160+
await db.dropDatabase().catch(() => null);
161+
utilClient.close();
167162
}
168163

169164
async create() {
170-
await this.utilClient.db(DB_NAME).createCollection(COLLECTION_NAME);
165+
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
166+
try {
167+
await utilClient.db(DB_NAME).createCollection(COLLECTION_NAME);
168+
} finally {
169+
utilClient.close();
170+
}
171171
}
172172

173173
async load(filePath: string, type: 'json' | 'string' | 'buffer'): Promise<any> {
@@ -180,14 +180,20 @@ export class DriverTester {
180180
}
181181

182182
async insertManyOf(document: Record<string, any>, length: number, addId = false) {
183-
await this.ns.insertMany(
184-
Array.from({ length }, (_, _id) => ({ ...(addId ? { _id } : {}), ...document })) as any[]
185-
);
183+
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
184+
const db = utilClient.db(DB_NAME);
185+
const collection = db.collection(COLLECTION_NAME);
186+
try {
187+
await collection.insertMany(
188+
Array.from({ length }, (_, _id) => ({ ...(addId ? { _id } : {}), ...document })) as any[]
189+
);
190+
} finally {
191+
utilClient.close();
192+
}
186193
}
187194

188195
async close() {
189196
await this.client.close();
190-
await this.utilClient.close();
191197
}
192198
}
193199

0 commit comments

Comments
 (0)