Skip to content

Commit 440a4c4

Browse files
committed
Fix tests
1 parent 2947014 commit 440a4c4

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

components/gitpod-db/src/typeorm/workspace-db-impl.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,17 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
506506
.limit(limit);
507507

508508
const results: WorkspaceOwnerAndDeletionEligibility[] = await qb.getMany();
509-
return results.filter(async (ws) => {
510-
// we don't want to delete workspaces that have a running instance
511-
const latestInstance = await this.findRunningInstance(ws.id);
512-
return latestInstance === undefined;
513-
});
509+
510+
// hack to have an async filter predicate
511+
const filtered = await Promise.all(
512+
results.map(async (ws) => {
513+
// we don't want to delete workspaces that have a running instance
514+
const latestInstance = await this.findRunningInstance(ws.id);
515+
return { ws, keep: latestInstance === undefined };
516+
}),
517+
);
518+
519+
return filtered.filter((result) => result.keep).map((result) => result.ws);
514520
}
515521

516522
public async findWorkspacesForPurging(

components/gitpod-db/src/workspace-db.spec.db.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class WorkspaceDBSpec {
2626
readonly timeWs = new Date(2018, 2, 16, 10, 0, 0).toISOString();
2727
readonly timeBefore = new Date(2018, 2, 16, 11, 5, 10).toISOString();
2828
readonly timeAfter = new Date(2019, 2, 16, 11, 5, 10).toISOString();
29+
readonly timeAfter2 = new Date(2019, 2, 17, 4, 20, 10).toISOString();
2930
readonly userId = "12345";
3031
readonly projectAID = "projectA";
3132
readonly projectBID = "projectB";
@@ -101,6 +102,30 @@ class WorkspaceDBSpec {
101102
deleted: false,
102103
usageAttributionId: undefined,
103104
};
105+
readonly wsi3: WorkspaceInstance = {
106+
workspaceId: this.ws.id,
107+
id: "12345",
108+
ideUrl: "example.org",
109+
region: "unknown",
110+
workspaceClass: undefined,
111+
workspaceImage: "abc.io/test/image:123",
112+
creationTime: this.timeAfter2,
113+
startedTime: undefined,
114+
deployedTime: undefined,
115+
stoppingTime: undefined,
116+
stoppedTime: undefined,
117+
status: {
118+
version: 1,
119+
phase: "stopped",
120+
conditions: {},
121+
},
122+
configuration: {
123+
theiaVersion: "unknown",
124+
ideImage: "unknown",
125+
},
126+
deleted: false,
127+
usageAttributionId: undefined,
128+
};
104129
readonly ws2: Workspace = {
105130
id: "2",
106131
type: "regular",
@@ -235,7 +260,7 @@ class WorkspaceDBSpec {
235260
}
236261

237262
@test(timeout(10000))
238-
public async testFindEligableWorkspacesForSoftDeletion_markedEligable_Prebuild() {
263+
public async testFindEligibleWorkspacesForSoftDeletion_markedEligible_Prebuild() {
239264
const { ws } = await this.createPrebuild(20, 15);
240265
const dbResult = await this.db.findEligibleWorkspacesForSoftDeletion(new Date(), 10, "prebuild");
241266
expect(dbResult.length).to.equal(1);
@@ -244,7 +269,7 @@ class WorkspaceDBSpec {
244269
}
245270

246271
@test(timeout(10000))
247-
public async testFindEligableWorkspacesForSoftDeletion_notMarkedEligable_Prebuild() {
272+
public async testFindEligibleWorkspacesForSoftDeletion_notMarkedEligible_Prebuild() {
248273
await this.createPrebuild(20, -7);
249274
const dbResult = await this.db.findEligibleWorkspacesForSoftDeletion(new Date(), 10, "prebuild");
250275
expect(dbResult.length).to.eq(0);
@@ -311,16 +336,34 @@ class WorkspaceDBSpec {
311336
}
312337

313338
@test(timeout(10000))
314-
public async testFindEligableWorkspacesForSoftDeletion_markedEligable() {
339+
public async testFindEligibleWorkspacesForSoftDeletion_markedEligible() {
315340
this.ws.deletionEligibilityTime = this.timeWs;
316-
await Promise.all([this.db.store(this.ws), this.db.storeInstance(this.wsi1), this.db.storeInstance(this.wsi2)]);
341+
await Promise.all([
342+
this.db.store(this.ws),
343+
this.db.storeInstance(this.wsi1),
344+
this.db.storeInstance(this.wsi2),
345+
this.db.storeInstance(this.wsi3),
346+
]);
317347
const dbResult = await this.db.findEligibleWorkspacesForSoftDeletion(new Date(this.timeAfter), 10);
318348
expect(dbResult[0].id).to.eq(this.ws.id);
319349
expect(dbResult[0].ownerId).to.eq(this.ws.ownerId);
320350
}
321351

322352
@test(timeout(10000))
323-
public async testFindEligableWorkspacesForSoftDeletion_notMarkedEligable() {
353+
public async testFindEligibleWorkspacesForSoftDeletion_notMarkedEligible() {
354+
await Promise.all([
355+
this.db.store(this.ws),
356+
this.db.storeInstance(this.wsi1),
357+
this.db.storeInstance(this.wsi2),
358+
this.db.storeInstance(this.wsi3),
359+
]);
360+
const dbResult = await this.db.findEligibleWorkspacesForSoftDeletion(new Date(this.timeAfter), 10);
361+
expect(dbResult.length).to.eq(0);
362+
}
363+
364+
@test(timeout(10000))
365+
public async testFindEligibleWorkspacesForSoftDeletion_notMarkedEligibleRunningInstance() {
366+
this.ws.deletionEligibilityTime = this.timeWs;
324367
await Promise.all([this.db.store(this.ws), this.db.storeInstance(this.wsi1), this.db.storeInstance(this.wsi2)]);
325368
const dbResult = await this.db.findEligibleWorkspacesForSoftDeletion(new Date(this.timeAfter), 10);
326369
expect(dbResult.length).to.eq(0);

0 commit comments

Comments
 (0)