Skip to content

Commit 0ac38e2

Browse files
committed
Fix
1 parent 7777f53 commit 0ac38e2

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/main/java/com/thealgorithms/scheduling/SpeculativeExecutionScheduling.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@ public final class SpeculativeExecutionScheduling {
1919
static class Task {
2020
String name;
2121
boolean completed;
22+
long startTime;
2223

2324
Task(String name) {
2425
this.name = name;
2526
this.completed = false;
27+
this.startTime = -1;
28+
}
29+
30+
void start() {
31+
this.startTime = System.currentTimeMillis();
2632
}
2733

2834
void complete() {
2935
this.completed = true;
3036
}
37+
38+
boolean hasStarted() {
39+
return this.startTime != -1;
40+
}
3141
}
3242

3343
private final Map<String, List<Task>> taskGroups;
@@ -48,7 +58,7 @@ public void addTask(String groupName, String taskName) {
4858
}
4959

5060
/**
51-
* Executes the tasks in the specified group.
61+
* Executes the tasks in the specified group by assigning a start time.
5262
*
5363
* @param groupName the name of the group
5464
* @return the name of the task that completed successfully
@@ -60,8 +70,11 @@ public String executeTasks(String groupName) {
6070
}
6171
for (Task task : tasks) {
6272
if (!task.completed) {
73+
if (!task.hasStarted()) {
74+
task.start();
75+
}
6376
task.complete();
64-
return task.name;
77+
return task.name + " started at " + task.startTime;
6578
}
6679
}
6780
return null;

src/test/java/com/thealgorithms/scheduling/SpeculativeExecutionSchedulingTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
@@ -18,28 +19,48 @@ public void setup() {
1819
@Test
1920
public void testAddAndExecuteTask() {
2021
scheduler.addTask("Group1", "Task1");
21-
assertEquals("Task1", scheduler.executeTasks("Group1"));
22+
23+
String result = scheduler.executeTasks("Group1");
24+
String[] parts = result.split(" started at ");
25+
26+
// Validate task name and ensure start time is a valid timestamp
27+
assertEquals("Task1", parts[0]);
28+
assertTrue(Long.parseLong(parts[1]) > 0, "Start time should be greater than 0");
2229
}
2330

2431
@Test
2532
public void testMultipleTasksInGroup() {
2633
scheduler.addTask("Group1", "Task1");
2734
scheduler.addTask("Group1", "Task2");
28-
assertEquals("Task1", scheduler.executeTasks("Group1"));
29-
assertEquals("Task2", scheduler.executeTasks("Group1"));
35+
36+
// Execute the first task
37+
String result1 = scheduler.executeTasks("Group1");
38+
String[] parts1 = result1.split(" started at ");
39+
assertEquals("Task1", parts1[0]);
40+
assertTrue(Long.parseLong(parts1[1]) > 0, "Start time should be greater than 0");
41+
42+
// Execute the second task
43+
String result2 = scheduler.executeTasks("Group1");
44+
String[] parts2 = result2.split(" started at ");
45+
assertEquals("Task2", parts2[0]);
46+
assertTrue(Long.parseLong(parts2[1]) > 0, "Start time should be greater than 0");
3047
}
3148

3249
@Test
3350
public void testExecuteAllTasks() {
3451
scheduler.addTask("Group1", "Task1");
3552
scheduler.addTask("Group1", "Task2");
53+
3654
scheduler.executeTasks("Group1");
3755
scheduler.executeTasks("Group1");
56+
57+
// Confirm no tasks remain
3858
assertNull(scheduler.executeTasks("Group1"));
3959
}
4060

4161
@Test
4262
public void testEmptyTaskGroup() {
63+
// Confirm executing tasks on an empty group returns null
4364
assertNull(scheduler.executeTasks("Group2"));
4465
}
4566
}

0 commit comments

Comments
 (0)