Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Fix tests failing randomly
Browse files Browse the repository at this point in the history
Tests modified in this change set fail randomly due to shared
mutable state between multiple threads.

This commit ensures that the shared state is correctly
synchronised between threads or re-initialized before each test run.
  • Loading branch information
fmbenhassine committed Jan 28, 2019
1 parent c9f72cd commit eba7265
Show file tree
Hide file tree
Showing 2 changed files with 367 additions and 361 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@
import java.util.Properties;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -55,7 +56,7 @@ public class JsrPartitionHandlerTests extends AbstractJsrTestCase {
private JsrPartitionHandler handler;
private JobRepository repository = new JobRepositorySupport();
private StepExecution stepExecution;
private int count;
private AtomicInteger count;
private BatchPropertyContext propertyContext;
private JsrStepExecutionSplitter stepSplitter;

Expand All @@ -67,12 +68,12 @@ public void setUp() throws Exception {
stepSplitter = new JsrStepExecutionSplitter(repository, false, "step1", true);
Analyzer.collectorData = "";
Analyzer.status = "";
count = 0;
count = new AtomicInteger(0);
handler = new JsrPartitionHandler();
handler.setStep(new StepSupport() {
@Override
public void execute(StepExecution stepExecution) throws JobInterruptedException {
count++;
count.incrementAndGet();
stepExecution.setStatus(org.springframework.batch.core.BatchStatus.COMPLETED);
stepExecution.setExitStatus(new ExitStatus("done"));
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public void testHardcodedNumberOfPartitions() throws Exception {
Collection<StepExecution> executions = handler.handle(stepSplitter, stepExecution);

assertEquals(3, executions.size());
assertEquals(3, count);
assertEquals(3, count.get());
}

@Test
Expand All @@ -151,7 +152,7 @@ public void testPollingPartitionsCompletion() throws Exception {
stopWatch.stop();

assertEquals(3, executions.size());
assertEquals(3, count);
assertEquals(3, count.get());
assertTrue(stopWatch.getLastTaskTimeMillis() >= 1000);
}

Expand All @@ -173,7 +174,7 @@ public PartitionPlan mapPartitions() throws Exception {
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);

assertEquals(3, executions.size());
assertEquals(3, count);
assertEquals(3, count.get());
}

@Test
Expand All @@ -194,7 +195,7 @@ public PartitionPlan mapPartitions() throws Exception {
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);

assertEquals(3, executions.size());
assertEquals(3, count);
assertEquals(3, count.get());
}

@Test
Expand All @@ -221,7 +222,7 @@ public PartitionPlan mapPartitions() throws Exception {
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);

assertEquals(3, executions.size());
assertEquals(3, count);
assertEquals(3, count.get());
assertEquals("value1", propertyContext.getStepProperties("step1:partition0").get("key1"));
assertEquals("value2", propertyContext.getStepProperties("step1:partition1").get("key1"));
}
Expand All @@ -241,7 +242,7 @@ public void testAnalyzer() throws Exception {
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);

assertEquals(2, executions.size());
assertEquals(2, count);
assertEquals(2, count.get());
assertEquals("foobar", Analyzer.collectorData);
assertEquals("COMPLETEDdone", Analyzer.status);
}
Expand Down
Loading

0 comments on commit eba7265

Please sign in to comment.