diff --git a/dotCMS/src/main/java/com/dotcms/jobs/business/queue/PostgresJobQueue.java b/dotCMS/src/main/java/com/dotcms/jobs/business/queue/PostgresJobQueue.java index 4ba43368ab17..4b43c30e2d25 100644 --- a/dotCMS/src/main/java/com/dotcms/jobs/business/queue/PostgresJobQueue.java +++ b/dotCMS/src/main/java/com/dotcms/jobs/business/queue/PostgresJobQueue.java @@ -170,14 +170,14 @@ public class PostgresJobQueue implements JobQueue { * Jackson mapper configuration and lazy initialized instance. */ private final Lazy objectMapper = Lazy.of(() -> { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.enable(SerializationFeature.INDENT_OUTPUT); - objectMapper.registerModule(new Jdk8Module()); - objectMapper.registerModule(new GuavaModule()); - objectMapper.registerModule(new JavaTimeModule()); - objectMapper.registerModule(new VersioningModule()); - objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); - return objectMapper; + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new GuavaModule()); + mapper.registerModule(new JavaTimeModule()); + mapper.registerModule(new VersioningModule()); + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + return mapper; }); @Override diff --git a/dotcms-integration/src/test/java/com/dotcms/jobs/business/api/JobQueueManagerAPIIntegrationTest.java b/dotcms-integration/src/test/java/com/dotcms/jobs/business/api/JobQueueManagerAPIIntegrationTest.java index e71675a524b6..56a6f9e852d4 100644 --- a/dotcms-integration/src/test/java/com/dotcms/jobs/business/api/JobQueueManagerAPIIntegrationTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/jobs/business/api/JobQueueManagerAPIIntegrationTest.java @@ -186,8 +186,11 @@ void test_CancelJob() throws Exception { Map parameters = new HashMap<>(); String jobId = jobQueueManagerAPI.createJob("cancellableQueue", parameters); - // Wait for the job to start - Thread.sleep(1000); + Awaitility.await().atMost(5, TimeUnit.SECONDS) + .until(() -> { + Job job = jobQueueManagerAPI.getJob(jobId); + return job.state() == JobState.RUNNING; + }); // Cancel the job jobQueueManagerAPI.cancelJob(jobId); @@ -385,7 +388,7 @@ void test_CombinedScenarios() throws Exception { }); // Wait a bit before cancelling the job - Thread.sleep(500); + Awaitility.await().pollDelay(500, TimeUnit.MILLISECONDS).until(() -> true); jobQueueManagerAPI.cancelJob(cancelJobId); // Wait for all jobs to complete (or timeout after 30 seconds) @@ -430,12 +433,9 @@ public void process(Job job) { for (int i = 0; i <= 10; i++) { float progress = i / 10.0f; tracker.updateProgress(progress); - try { - Thread.sleep(500); // Simulate work being done - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } + + // Simulate work being done + Awaitility.await().pollDelay(500, TimeUnit.MILLISECONDS).until(() -> true); } } @@ -495,20 +495,12 @@ private static class CancellableJobProcessor implements JobProcessor, Cancellabl @Override public void process(Job job) { - while (!canceled.get()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + Awaitility.await().atMost(10, TimeUnit.SECONDS) + .until(canceled::get); + + // Simulate some additional work after cancellation + Awaitility.await().pollDelay(1, TimeUnit.SECONDS).until(() -> true); } @Override @@ -532,11 +524,7 @@ private static class TestJobProcessor implements JobProcessor { @Override public void process(Job job) { // Simulate some work - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + Awaitility.await().pollDelay(1, TimeUnit.SECONDS).until(() -> true); } @Override diff --git a/dotcms-integration/src/test/java/com/dotcms/jobs/business/queue/PostgresJobQueueIntegrationTest.java b/dotcms-integration/src/test/java/com/dotcms/jobs/business/queue/PostgresJobQueueIntegrationTest.java index 797553ca31da..890354fd0285 100644 --- a/dotcms-integration/src/test/java/com/dotcms/jobs/business/queue/PostgresJobQueueIntegrationTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/jobs/business/queue/PostgresJobQueueIntegrationTest.java @@ -29,7 +29,9 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -206,7 +208,10 @@ void test_nextJob() throws Exception { assertEquals(JobState.PENDING, nextJob.state()); // Simulate some processing time - Thread.sleep(1000); + Awaitility.await().atMost(5, TimeUnit.SECONDS) + .pollInterval(100, TimeUnit.MILLISECONDS).until(() -> { + return true; + }); // Mark job as completed Job completedJob = nextJob.markAsCompleted(null); @@ -324,9 +329,13 @@ void test_getUpdatedJobsSince() throws JobQueueException, InterruptedException { String job1Id = jobQueue.createJob(queueName, new HashMap<>()); String job2Id = jobQueue.createJob(queueName, new HashMap<>()); - Thread.sleep(100); // Ensure some time passes + Awaitility.await().atMost(1, TimeUnit.SECONDS) + .pollInterval(50, TimeUnit.MILLISECONDS) + .until(() -> true);// Ensure some time passes LocalDateTime checkpointTime = LocalDateTime.now(); - Thread.sleep(100); // Ensure some more time passes + Awaitility.await().atMost(1, TimeUnit.SECONDS) + .pollInterval(50, TimeUnit.MILLISECONDS) + .until(() -> true);// Ensure some more time passes // Update job1 and create a new job after the checkpoint Job job1 = jobQueue.getJob(job1Id);