]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9525 enforce CeWorker#getOrdinal returns int >= 0
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 7 Jul 2017 14:28:21 +0000 (16:28 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 17 Jul 2017 08:52:47 +0000 (10:52 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorker.java
server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java

index 96bb7df9243e561be3bfdad5b3feda20a5022709..9a7ffff97765ca28da13e3d5a9da56a38943cd7e 100644 (file)
@@ -37,7 +37,7 @@ public interface CeWorker extends Callable<CeWorker.Result> {
   }
 
   /**
-   * Position of the current CeWorker among all the running workers.
+   * Position of the current CeWorker among all the running workers, starts with 0.
    */
   int getOrdinal();
 
index 4dbada7fce402196bad8e72795c5e07054aab709..ba5e447ac7f6d3b9e1531ae2f70db3fb31f29c5d 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.ce.queue.InternalCeQueue;
 import org.sonar.core.util.logs.Profiler;
 import org.sonar.db.ce.CeActivityDto;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.String.format;
 import static org.sonar.ce.taskprocessor.CeWorker.Result.NO_TASK;
 import static org.sonar.ce.taskprocessor.CeWorker.Result.TASK_PROCESSED;
@@ -47,13 +48,18 @@ public class CeWorkerImpl implements CeWorker {
 
   public CeWorkerImpl(int ordinal, String uuid,
     InternalCeQueue queue, CeLogging ceLogging, CeTaskProcessorRepository taskProcessorRepository) {
-    this.ordinal = ordinal;
+    this.ordinal = checkOrdinal(ordinal);
     this.uuid = uuid;
     this.queue = queue;
     this.ceLogging = ceLogging;
     this.taskProcessorRepository = taskProcessorRepository;
   }
 
+  private static int checkOrdinal(int ordinal) {
+    checkArgument(ordinal >= 0, "Ordinal must be >= 0");
+    return ordinal;
+  }
+
   @Override
   public Result call() throws Exception {
     return withCustomizedThreadName(this::findAndProcessTask);
index 59b3607ed30dcc5267e8c69a3495dedea65d9aec..026c987ecd9551948613e71c805343e52746712c 100644 (file)
@@ -27,6 +27,7 @@ import javax.annotation.Nullable;
 import org.apache.commons.lang.RandomStringUtils;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
 import org.mockito.InOrder;
 import org.mockito.Mockito;
@@ -56,6 +57,8 @@ public class CeWorkerImplTest {
   public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
   @Rule
   public LogTester logTester = new LogTester();
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
 
   private InternalCeQueue queue = mock(InternalCeQueue.class);
   private ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
@@ -66,6 +69,14 @@ public class CeWorkerImplTest {
   private CeWorker underTest = new CeWorkerImpl(randomOrdinal, workerUuid, queue, ceLogging, taskProcessorRepository);
   private InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
 
+  @Test
+  public void constructor_throws_IAE_if_ordinal_is_less_than_zero() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Ordinal must be >= 0");
+
+    new CeWorkerImpl(-1 - new Random().nextInt(20), workerUuid, queue, ceLogging, taskProcessorRepository);
+  }
+
   @Test
   public void getUUID_must_return_the_uuid_of_constructor() {
     String uuid = UUID.randomUUID().toString();