Skip to content

Commit 0a01de0

Browse files
committed
add callback item test
1 parent 2260f2f commit 0a01de0

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

platform-api/src/main/java/com/flow/platform/api/domain/CmdCallbackQueueItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class CmdCallbackQueueItem extends Jsonable {
3131

3232
private final Cmd cmd;
3333

34-
private Integer retryTimes = 0;
34+
private Integer retryTimes = 1;
3535

3636
// priority default 1
3737
private Integer priority = 1;

platform-api/src/test/java/com/flow/platform/api/test/service/JobServiceTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.flow.platform.api.test.TestBase;
3333
import com.flow.platform.api.util.CommonUtil;
3434
import com.flow.platform.core.exception.IllegalStatusException;
35+
import com.flow.platform.core.queue.PlatformQueue;
36+
import com.flow.platform.core.queue.PriorityMessage;
3537
import com.flow.platform.core.util.ThreadUtil;
3638
import com.flow.platform.domain.Cmd;
3739
import com.flow.platform.domain.CmdResult;
@@ -42,10 +44,14 @@
4244
import java.io.IOException;
4345
import java.time.ZonedDateTime;
4446
import java.util.List;
47+
import java.util.concurrent.CountDownLatch;
48+
import java.util.concurrent.TimeUnit;
49+
import java.util.concurrent.atomic.AtomicInteger;
4550
import org.junit.Assert;
4651
import org.junit.Before;
4752
import org.junit.Test;
4853
import org.springframework.beans.factory.annotation.Autowired;
54+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4955

5056
/**
5157
* @author yh@firim
@@ -55,6 +61,9 @@ public class JobServiceTest extends TestBase {
5561
@Autowired
5662
private JobNodeService jobNodeService;
5763

64+
@Autowired
65+
private PlatformQueue<PriorityMessage> cmdCallbackQueue;
66+
5867
@Before
5968
public void init() {
6069
stubDemo();
@@ -301,4 +310,25 @@ public void should_get_latest_job_by_node_path() throws IOException {
301310
Assert.assertEquals(1, jobs.size());
302311
Assert.assertEquals("2", jobs.get(0).getNumber().toString());
303312
}
313+
314+
@Test
315+
public void should_cmd_enqueue_limit_times_success() throws InterruptedException {
316+
Cmd cmd = new Cmd("default", "test", CmdType.RUN_SHELL, "echo 1");
317+
CountDownLatch countDownLatch = new CountDownLatch(5);
318+
AtomicInteger atomicInteger = new AtomicInteger(0);
319+
320+
// register new queue to get item info
321+
cmdCallbackQueue.register(message -> {
322+
CmdCallbackQueueItem item = CmdCallbackQueueItem.parse(message.getBody(), CmdCallbackQueueItem.class);
323+
atomicInteger.set(item.getRetryTimes());
324+
countDownLatch.countDown();
325+
});
326+
327+
// when: enter queue one not found job id
328+
jobService.enterQueue(new CmdCallbackQueueItem(CommonUtil.randomId(), cmd));
329+
countDownLatch.await(6, TimeUnit.SECONDS);
330+
331+
// then: should try 5 times
332+
Assert.assertEquals(5, atomicInteger.get());
333+
}
304334
}

0 commit comments

Comments
 (0)