/
ProcessingMessageSchedulerTest.java
132 lines (122 loc) · 4.7 KB
/
ProcessingMessageSchedulerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2020 Sonu Kumar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.sonus21.rqueue.spring.boot.tests.integration;
import static com.github.sonus21.rqueue.utils.RedisUtils.getRedisTemplate;
import static com.github.sonus21.rqueue.utils.TimeUtils.waitFor;
import static rqueue.test.TestUtils.buildMessage;
import com.github.sonus21.rqueue.core.RqueueMessage;
import com.github.sonus21.rqueue.exception.TimedOutException;
import com.github.sonus21.rqueue.producer.RqueueMessageSender;
import com.github.sonus21.rqueue.spring.boot.application.ApplicationWithCustomConfiguration;
import com.github.sonus21.rqueue.utils.QueueUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import rqueue.test.RunTestUntilFail;
import rqueue.test.TestUtils;
import rqueue.test.dto.Job;
import rqueue.test.service.ConsumedMessageService;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationWithCustomConfiguration.class)
@Slf4j
@TestPropertySource(
properties = {
"auto.start.scheduler=false",
"spring.redis.port=6383",
"mysql.db.name=test3",
"max.workers.count=120"
})
public class ProcessingMessageSchedulerTest {
static {
System.setProperty("TEST_NAME", ProcessingMessageSchedulerTest.class.getSimpleName());
}
@Autowired protected ConsumedMessageService consumedMessageService;
@Autowired protected RqueueMessageSender messageSender;
@Autowired protected RedisConnectionFactory redisConnectionFactory;
private RedisTemplate<String, RqueueMessage> redisTemplate;
@Value("${job.queue.name}")
private String jobQueueName;
@Rule
public RunTestUntilFail retry =
new RunTestUntilFail(
log,
1,
() -> {
for (Entry<String, List<RqueueMessage>> entry :
TestUtils.getMessageMap(jobQueueName, redisTemplate).entrySet()) {
log.error("FAILING Queue {}", entry.getKey());
for (RqueueMessage message : entry.getValue()) {
log.error("FAILING Queue {} Msg {}", entry.getKey(), message);
}
}
});
private int messageCount = 110;
@PostConstruct
public void init() {
redisTemplate = getRedisTemplate(redisConnectionFactory);
}
@Test
public void publishMessageIsTriggeredOnMessageRemoval()
throws InterruptedException, TimedOutException {
String processingQueueName = QueueUtils.getProcessingQueueName(jobQueueName);
long currentTime = System.currentTimeMillis();
List<Job> jobs = new ArrayList<>();
List<String> ids = new ArrayList<>();
int maxDelay = 2000;
Random random = new Random();
for (int i = 0; i < messageCount; i++) {
Job job = Job.newInstance();
jobs.add(job);
ids.add(job.getId());
int delay = random.nextInt(maxDelay);
if (random.nextBoolean()) {
delay = delay * -1;
}
redisTemplate
.opsForZSet()
.add(
processingQueueName,
buildMessage(job, jobQueueName, null, null),
currentTime + delay);
}
Thread.sleep(maxDelay);
waitFor(
() -> 0 == messageSender.getAllMessages(jobQueueName).size(),
60 * 1000L,
"messages to be consumed");
waitFor(
() -> messageCount == consumedMessageService.getMessages(ids, Job.class).size(),
"message count to be matched");
waitFor(
() -> jobs.containsAll(consumedMessageService.getMessages(ids, Job.class).values()),
"All jobs to be executed");
}
}