Skip to content

Commit

Permalink
Buld/TestRun handle multithread delete (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashidlos authored Jun 18, 2024
1 parent 0e96415 commit 6123872
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/_data_/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const TEST_PROJECT: Project = {
id: '1',
name: 'Test Project',
buildsCounter: 2,
maxBuildAllowed: 100,
maxBuildAllowed: 1,
maxBranchLifetime: 30,
mainBranchName: 'master',
createdAt: new Date(),
Expand Down
34 changes: 20 additions & 14 deletions src/builds/builds.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,28 @@ export class BuildsService {
},
});

if (!build) {
this.logger.warn(`Build not found ${id}`);
return;
}

await Promise.all(build.testRuns.map((testRun) => this.testRunsService.delete(testRun.id)));

const promise = this.prismaService.build
.delete({
where: { id },
})
.then((build) => {
this.logger.log('Deleted build:' + JSON.stringify(build.id));
this.eventsGateway.buildDeleted(
new BuildDto({
...build,
})
);
return build;
});
return promise;
try {
await this.prismaService.build.delete({ where: { id } });
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
// workaround https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/435
if (e.code === 'P2025') {
this.logger.warn(`Build already deleted ${id}`);
return;
}
}
}

this.logger.log(`Build deleted ${id}`);
this.eventsGateway.buildDeleted(new BuildDto({ ...build }));
return build;
}

async deleteOldBuilds(projectId: string, keepBuilds: number) {
Expand Down
2 changes: 2 additions & 0 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class ProjectsService {
autoApproveFeature: projectDto.autoApproveFeature,
imageComparison: projectDto.imageComparison,
imageComparisonConfig: projectDto.imageComparisonConfig,
maxBuildAllowed: projectDto.maxBuildAllowed,
maxBranchLifetime: projectDto.maxBranchLifetime,
},
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/test-runs/test-runs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,26 @@ describe('TestRunsService', () => {
expect(eventTestRunDeletedMock).toHaveBeenCalledWith(testRun);
});

it('delete not found', async () => {
const id = 'some id';
const findOneMock = jest.fn().mockResolvedValueOnce(undefined);
const deleteImageMock = jest.fn();
const testRunDeleteMock = jest.fn();
const eventTestRunDeletedMock = jest.fn();
service = await initService({
deleteImageMock,
testRunDeleteMock,
eventTestRunDeletedMock,
});
service.findOne = findOneMock;

const result = await service.delete(id);

expect(deleteImageMock).not.toHaveBeenCalled();
expect(testRunDeleteMock).not.toHaveBeenCalled();
expect(result).toBeUndefined();
});

it('updateIgnoreAreas', async () => {
const testRun = {
id: 'testRunId',
Expand Down
24 changes: 20 additions & 4 deletions src/test-runs/test-runs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CreateTestRequestDto } from './dto/create-test-request.dto';
import { IgnoreAreaDto } from './dto/ignore-area.dto';
import { StaticService } from '../shared/static/static.service';
import { PrismaService } from '../prisma/prisma.service';
import { Baseline, TestRun, TestStatus, TestVariation } from '@prisma/client';
import { Baseline, Prisma, TestRun, TestStatus, TestVariation } from '@prisma/client';
import { DiffResult } from './diffResult';
import { EventsGateway } from '../shared/events/events.gateway';
import { TestRunResultDto } from '../test-runs/dto/testRunResult.dto';
Expand Down Expand Up @@ -244,16 +244,32 @@ export class TestRunsService {
}

async delete(id: string): Promise<TestRun> {
this.logger.debug(`Going to remove TestRun ${id}`);
const testRun = await this.findOne(id);

if (!testRun) {
this.logger.warn(`TestRun not found ${id}`);
return;
}

await Promise.all([
this.staticService.deleteImage(testRun.diffName),
this.staticService.deleteImage(testRun.imageName),
this.prismaService.testRun.delete({
where: { id },
}),
]);

try {
await this.prismaService.testRun.delete({ where: { id } });
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
// workaround https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/435
if (e.code === 'P2025') {
this.logger.warn(`TestRun already deleted ${id}`);
return;
}
}
}

this.logger.log(`TestRun deleted ${id}`);
this.eventsGateway.testRunDeleted(testRun);
return testRun;
}
Expand Down
16 changes: 16 additions & 0 deletions test/builds.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ describe('Builds (e2e)', () => {
branchName: 'branchName',
project: project.name,
};
await haveTestRunCreated(
buildsService,
testRunsService,
project.id,
project.mainBranchName,
'./test/image.png',
false
);
await haveTestRunCreated(
buildsService,
testRunsService,
project.id,
project.mainBranchName,
'./test/image.png',
false
);

const builds = await Promise.all([
buildsController.create(createBuildDto),
Expand Down
6 changes: 6 additions & 0 deletions test/projects.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('Projects (e2e)', () => {
.expect(201)
.expect((res) => {
expect(res.body.name).toBe(project.name);
expect(res.body.mainBranchName).toBe(project.mainBranchName);
expect(res.body.autoApproveFeature).toBe(project.autoApproveFeature);
expect(res.body.imageComparison).toBe(project.imageComparison);
expect(res.body.imageComparisonConfig).toBe(project.imageComparisonConfig);
expect(res.body.maxBuildAllowed).toBe(project.maxBuildAllowed);
expect(res.body.maxBranchLifetime).toBe(project.maxBranchLifetime);
});
});

Expand Down

0 comments on commit 6123872

Please sign in to comment.