Skip to content

Commit

Permalink
optimize test cases cost time
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Dec 12, 2023
1 parent 34577ef commit c20a64e
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disjob worker configuration
disjob.worker:
group: default
group: app-test
timing-wheel-tick-ms: 100
timing-wheel-ring-size: 60
maximum-pool-size: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.Serializable;
import java.util.List;
import java.util.function.Consumer;

/**
* Page query response
Expand Down Expand Up @@ -57,6 +58,12 @@ public int getTotalPages() {
return computeTotalPages(request.getPageSize(), total);
}

public void forEachRow(Consumer<T> action) {
if (rows != null) {
rows.forEach(action);
}
}

public static int computeTotalPages(int pageSize, long total) {
return (int) ((total + pageSize - 1) / pageSize);
}
Expand Down
2 changes: 1 addition & 1 deletion disjob-samples/conf-worker/application-worker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disjob worker configuration
disjob.worker:
group: default
group: app-test
timing-wheel-tick-ms: 100
timing-wheel-ring-size: 60
maximum-pool-size: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ disjob:
max-count: 5
backoff-period: 3000
worker:
group: default
group: app-test
timing-wheel-tick-ms: 100
timing-wheel-ring-size: 60
maximum-pool-size: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ public List<String> matchGroup(String term) {
}

public PageResponse<SchedGroupResponse> queryForPage(SchedGroupPageRequest pageRequest) {
return pageRequest.query(
PageResponse<SchedGroupResponse> page = pageRequest.query(
schedGroupMapper::queryPageCount,
schedGroupMapper::queryPageRecords,
SchedGroupConverter.INSTANCE::convert
);

page.forEachRow(SchedGroupResponse::maskToken);
return page;
}

// ------------------------------------------------------------close
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cn.ponfee.disjob.common.base.ToJsonString;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
Expand Down Expand Up @@ -39,4 +40,18 @@ public class SchedGroupResponse extends ToJsonString implements Serializable {
private Date createdAt;
private String updatedBy;
private String createdBy;

public void maskToken() {
this.supervisorToken = mask(supervisorToken);
this.workerToken = mask(workerToken);
this.userToken = mask(userToken);
}

private static String mask(String str) {
if (StringUtils.isEmpty(str)) {
return str;
}
return "*****";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void testJoin() throws InterruptedException {
public void testStop() throws InterruptedException {
MyThread1 thread = new MyThread1();
thread.start();
Thread.sleep(2000);
Thread.sleep(500);
Assertions.assertFalse(Threads.isStopped(thread));
Threads.stopThread(thread, 1000, 2, 500);
Threads.stopThread(thread, 1000, 2, 200);
Thread.sleep(100);
Assertions.assertTrue(Threads.isStopped(thread));
thread.join();
Expand All @@ -51,10 +51,10 @@ public void testStop() throws InterruptedException {
public void testInterrupt() throws InterruptedException {
MyThread2 thread = new MyThread2();
thread.start();
Thread.sleep(2000);
Thread.sleep(1000);
Assertions.assertFalse(Threads.isStopped(thread));
thread.interrupt();
Thread.sleep(1000);
Thread.sleep(500);
Assertions.assertFalse(Threads.isStopped(thread));
// RateLimiter不会interrupt
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import cn.ponfee.disjob.common.util.UuidUtils;
import cn.ponfee.disjob.supervisor.SpringBootTestBase;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Assertions;
Expand All @@ -26,13 +25,13 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* Redis lock test
Expand All @@ -42,8 +41,7 @@
public class RedisLockTest extends SpringBootTestBase<StringRedisTemplate> {

private static final String NAME = RandomStringUtils.randomAlphabetic(3);
private static final int ROUND = 5;
private static final int RATIO = 7;
private static final int ROUND = 1;

private RedisLockFactory factory;

Expand All @@ -55,24 +53,24 @@ protected void beforeEach() {
@Test
public void test0() throws InterruptedException {
String key = "test:" + UuidUtils.uuid32();
RedisLock redisLock = factory.create(key, 10000);
RedisLock redisLock = factory.create(key, 5000);
Assertions.assertTrue(redisLock.tryLock());

Thread.sleep(55);
Long ttl1 = bean.getExpire("lock:" + key, TimeUnit.MILLISECONDS);
System.out.println("ttl1: " + ttl1);
Assertions.assertTrue(ttl1 > 9000 && ttl1 < 10000);
Assertions.assertTrue(ttl1 > 4000 && ttl1 < 5000);

Thread.sleep(5000);
Thread.sleep(1000);
Long ttl2 = bean.getExpire("lock:" + key, TimeUnit.MILLISECONDS);
System.out.println("ttl2: " + ttl2);
Assertions.assertTrue(ttl2 > 4000 && ttl2 < 5000);
Assertions.assertTrue(ttl2 > 3000 && ttl2 < 4000);

Assertions.assertTrue(redisLock.tryLock());
Thread.sleep(50);
Long ttl3 = bean.getExpire("lock:" + key, TimeUnit.MILLISECONDS);
System.out.println("ttl3: " + ttl3);
Assertions.assertTrue(ttl3 > 9000 && ttl3 < 10000);
Assertions.assertTrue(ttl3 > 4000 && ttl3 < 5000);

Assertions.assertTrue(redisLock.isLocked());
Assertions.assertTrue(redisLock.isHeldByCurrentThread());
Expand Down Expand Up @@ -105,10 +103,8 @@ public void test1() throws IOException, InterruptedException {
List<Thread> threads = new ArrayList<>();
System.out.println("\n=========================START========================");
for (int i = 0; i < ROUND && (line = reader.readLine()) != null; i++) {
if (ThreadLocalRandom.current().nextInt(RATIO) == 0) {
final String line0 = line;
threads.add(new Thread(() -> printer.output(NAME + "-" + num.getAndIncrement() + "\t" + line0 + "\n")));
}
final String line0 = line;
threads.add(new Thread(() -> printer.output(NAME + "-" + num.getAndIncrement() + "\t" + line0 + "\n")));
}
reader.close();
for (Thread thread : threads) {
Expand All @@ -129,12 +125,10 @@ public void test2() throws IOException, InterruptedException {
List<Thread> threads = new ArrayList<>();
System.out.println("\n=========================START========================");
for (int i = 0; i < ROUND && (line = reader.readLine()) != null; i++) {
if (ThreadLocalRandom.current().nextInt(RATIO) == 0) {
final String _line = line;
threads.add(new Thread(
() -> new Printer(lock).output(NAME + "-" + num.getAndIncrement() + "\t" + _line + "\n")
));
}
final String _line = line;
threads.add(new Thread(
() -> new Printer(lock).output(NAME + "-" + num.getAndIncrement() + "\t" + _line + "\n")
));
}
reader.close();
for (Thread thread : threads) {
Expand All @@ -155,11 +149,9 @@ public void test3() throws IOException, InterruptedException {
System.out.println("\n=========================START========================");
for (int i = 0; i < ROUND && (line = reader.readLine()) != null; i++) {
final String line0 = line;
if (ThreadLocalRandom.current().nextInt(RATIO) == 0) {
threads.add(new Thread(
() -> new Printer(factory.create("test:lock:3", 30000)).output(NAME + "-" + num.getAndIncrement() + "\t" + line0 + "\n")
));
}
threads.add(new Thread(
() -> new Printer(factory.create("test:lock:3", 30000)).output(NAME + "-" + num.getAndIncrement() + "\t" + line0 + "\n")
));
}
reader.close();
for (Thread thread : threads) {
Expand All @@ -174,19 +166,11 @@ public void test3() throws IOException, InterruptedException {
@Test
public void test4() throws IOException {
Printer printer = new Printer(factory.create("test:lock:4", 30000));
AtomicInteger num = new AtomicInteger(RATIO);
System.out.println("\n=========================START========================");
List<Map<Integer, String>> lines = Files.readLines(file(), StandardCharsets.UTF_8)
.subList(0, ROUND)
.stream()
.filter(e -> ThreadLocalRandom.current().nextInt(3) == 0)
.map(line -> ImmutableMap.of(num.getAndIncrement(), line))
.collect(Collectors.toList());

execute(lines, map -> {
Entry<Integer, String> line = map.entrySet().iterator().next();
printer.output(NAME + "-" + line.getKey() + "\t" + line.getValue() + "\n");
}, ForkJoinPool.commonPool());
List<String> lines = Files.readLines(file(), StandardCharsets.UTF_8)
.subList(0, ROUND);

execute(lines, line -> printer.output(NAME + "-" + line + "\n"), ForkJoinPool.commonPool());
System.out.println("=========================END========================\n");
}

Expand All @@ -199,7 +183,7 @@ public void test4() throws IOException {
*/
@Test
public void testTryLockWithTimeout() throws InterruptedException {
int expire = 2000;
int expire = 1000;
String lockKey = "test:lock:" + UuidUtils.uuid32();
String actualKey = "lock:" + lockKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testQueueOfferAsync() throws InterruptedException {
t.start();
t.interrupt();
//Thread.interrupted();
Thread.sleep(2000);
Thread.sleep(500);
System.out.println("end---");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void testNumberSplit() {
System.out.println(Numbers.partition(47, 2));
System.out.println(Numbers.partition(256, 2));
System.out.println(Numbers.partition(256, 4));
for (int i = 0; i < 1000000; i++) {
for (int i = 0; i < 10000; i++) {
int number = ThreadLocalRandom.current().nextInt(100000) + 1, size = ThreadLocalRandom.current().nextInt(31) + 1;
List<Tuple2<Integer, Integer>> split = Numbers.partition(number, size);
Assertions.assertTrue(Collects.getFirst(split).a == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testMD5() throws ClassNotFoundException {
Assertions.assertEquals(String.class, Class.forName("java.lang.String"));
}

static long round = 1_000_000_000L;
static long round = 1_000_000L;

@Test
public void testNexFloat() {
Expand Down Expand Up @@ -188,7 +188,7 @@ public void testThreadPool() throws InterruptedException {
System.out.println("------------b");
});
System.out.println("------------1");
Thread.sleep(4000);
Thread.sleep(1000);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void testFormat() {
public void testRandom() {
double min = 1.0D, max = 0.0D;
Random random = new Random();
for (int i = 0; i < 100000000; i++) {
for (int i = 0; i < 1000000; i++) {
double r = random.nextDouble();
if (r < min) {
min = r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class ScheduledExecutorTest {
@Test
public void testSubmit() throws InterruptedException {
ScheduledThreadPoolExecutor registryScheduledExecutor = new ScheduledThreadPoolExecutor(1, ThreadPoolExecutors.DISCARD);
registryScheduledExecutor.scheduleWithFixedDelay(() -> System.out.println("scheduled"), 2, 1, TimeUnit.SECONDS);
registryScheduledExecutor.scheduleWithFixedDelay(() -> System.out.println("scheduled"), 500, 200, TimeUnit.MILLISECONDS);


System.out.println(new Date());
registryScheduledExecutor.submit(() -> System.out.println("submit"));
registryScheduledExecutor.execute(() -> System.out.println("execute"));
System.out.println(new Date());

Thread.sleep(5000);
Thread.sleep(1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public void testHashMapCapt() {

@Test
public void testTimeSecond() throws InterruptedException {
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 5; i++) {
Assertions.assertEquals((int) ((System.currentTimeMillis() % 60000) / 1000), Calendar.getInstance().get(Calendar.SECOND));
Thread.sleep(ThreadLocalRandom.current().nextLong(200));
Thread.sleep(ThreadLocalRandom.current().nextLong(50));
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ public void testTimingWheel() {
long hour = TimeUnit.HOURS.toMillis(10);
System.out.println("hour=" + hour);

for (int i = 0; i < 100000; i++) {
for (int i = 0; i < 1000; i++) {
long triggerTime = System.currentTimeMillis() + 5000 + ThreadLocalRandom.current().nextLong(hour);
timingWheel.offer(CommonTest.createExecuteTaskParam(Operations.TRIGGER, 0, 0, 1L, 0, triggerTime, JobType.GENERAL, RouteStrategy.ROUND_ROBIN, 1, "jobHandler", new Worker("default", "workerId", "host", 1)));
}
Expand Down
5 changes: 5 additions & 0 deletions disjob-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit c20a64e

Please sign in to comment.