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;
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);
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;
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);
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();