|
| 1 | +import { TaskService } from "@/transient-services/task/task.service"; |
| 2 | +import { TestingModule } from "@nestjs/testing"; |
| 3 | +import { DataSource } from "typeorm"; |
| 4 | +import { getIntegrationTestModule } from "../../_src/modules/integration-test.module"; |
| 5 | +import { clearDatabase } from "../../_src/utils/utils"; |
| 6 | + |
| 7 | +describe("Task Service Integration Tests ", () => { |
| 8 | + let taskService: TaskService; |
| 9 | + let testingModule: TestingModule; |
| 10 | + let testingDataSource: DataSource; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + const { module, dataSource } = await getIntegrationTestModule(); |
| 14 | + testingModule = module; |
| 15 | + testingDataSource = dataSource; |
| 16 | + |
| 17 | + taskService = module.get(TaskService); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(async () => { |
| 21 | + await testingModule.close(); |
| 22 | + }); |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + await clearDatabase(testingDataSource); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("createOneTimeTask", () => { |
| 29 | + it("should create and execute a one-time task successfully", async () => { |
| 30 | + // Arrange |
| 31 | + const taskId = "test-task-1"; |
| 32 | + let taskExecuted = false; |
| 33 | + const task = async () => { |
| 34 | + taskExecuted = true; |
| 35 | + }; |
| 36 | + const runInMs = 100; |
| 37 | + |
| 38 | + // Act |
| 39 | + await taskService.createOneTimeTask(taskId, task, runInMs); |
| 40 | + |
| 41 | + // Assert |
| 42 | + expect(taskExecuted).toBeFalsy(); // Task shouldn't execute immediately |
| 43 | + |
| 44 | + // Wait for task execution |
| 45 | + await new Promise((resolve) => setTimeout(resolve, runInMs + 50)); |
| 46 | + expect(taskExecuted).toBeTruthy(); |
| 47 | + |
| 48 | + taskExecuted = false; // reset |
| 49 | + await new Promise((resolve) => setTimeout(resolve, runInMs + 101)); |
| 50 | + expect(taskExecuted).toBeFalsy(); // should not have been executed again |
| 51 | + }); |
| 52 | + |
| 53 | + it("should throw error when runInMs is less than or equal to 0", async () => { |
| 54 | + // Arrange |
| 55 | + const taskId = "test-task-2"; |
| 56 | + const task = async () => {}; |
| 57 | + const runInMs = 0; |
| 58 | + |
| 59 | + // Act & Assert |
| 60 | + await expect( |
| 61 | + taskService.createOneTimeTask(taskId, task, runInMs), |
| 62 | + ).rejects.toThrow( |
| 63 | + "Invalid value for runInMs, must be greater than 0: 0", |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should handle task execution errors gracefully", async () => { |
| 68 | + // Arrange |
| 69 | + const taskId = "test-task-3"; |
| 70 | + const task = async () => { |
| 71 | + throw new Error("Task execution failed"); |
| 72 | + }; |
| 73 | + const runInMs = 100; |
| 74 | + |
| 75 | + // Act |
| 76 | + await taskService.createOneTimeTask(taskId, task, runInMs); |
| 77 | + |
| 78 | + // Assert - should not throw error |
| 79 | + await new Promise((resolve) => setTimeout(resolve, runInMs + 50)); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("abortOneTimeTask", () => { |
| 84 | + it("should successfully abort a scheduled task", async () => { |
| 85 | + // Arrange |
| 86 | + const taskId = "test-task-4"; |
| 87 | + let taskExecuted = false; |
| 88 | + const task = async () => { |
| 89 | + taskExecuted = true; |
| 90 | + }; |
| 91 | + const runInMs = 200; |
| 92 | + |
| 93 | + // Act |
| 94 | + await taskService.createOneTimeTask(taskId, task, runInMs); |
| 95 | + |
| 96 | + // Wait a bit but not enough for task execution |
| 97 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 98 | + |
| 99 | + const abortResult = await taskService.abortOneTimeTask(taskId); |
| 100 | + |
| 101 | + // Wait to ensure task doesn't execute |
| 102 | + await new Promise((resolve) => setTimeout(resolve, runInMs + 50)); |
| 103 | + |
| 104 | + // Assert |
| 105 | + expect(abortResult).toBeTruthy(); |
| 106 | + expect(taskExecuted).toBeFalsy(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should return false when trying to abort non-existent task", async () => { |
| 110 | + // Arrange |
| 111 | + const nonExistentTaskId = "non-existent-task"; |
| 112 | + |
| 113 | + // Act |
| 114 | + const result = |
| 115 | + await taskService.abortOneTimeTask(nonExistentTaskId); |
| 116 | + |
| 117 | + // Assert |
| 118 | + expect(result).toBeFalsy(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should allow creating new task after aborting previous one with same id", async () => { |
| 122 | + // Arrange |
| 123 | + const taskId = "test-task-5"; |
| 124 | + let taskExecutionCount = 0; |
| 125 | + const task = async () => { |
| 126 | + taskExecutionCount++; |
| 127 | + }; |
| 128 | + const runInMs = 200; |
| 129 | + |
| 130 | + // Act |
| 131 | + await taskService.createOneTimeTask(taskId, task, runInMs); |
| 132 | + await taskService.abortOneTimeTask(taskId); |
| 133 | + await taskService.createOneTimeTask(taskId, task, 100); |
| 134 | + |
| 135 | + // Wait for second task execution |
| 136 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 137 | + |
| 138 | + // Assert |
| 139 | + expect(taskExecutionCount).toBe(1); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments