Skip to content

Commit 841f088

Browse files
committed
Fuzz test with two locks
1 parent 4676ce6 commit 841f088

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/TransactionalFuzzTester.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.javacrumbs.shedlock.test.support.jdbc;
1717

18+
import net.javacrumbs.shedlock.core.LockConfiguration;
1819
import net.javacrumbs.shedlock.core.LockProvider;
1920
import net.javacrumbs.shedlock.test.support.FuzzTester;
2021
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -28,9 +29,9 @@ public class TransactionalFuzzTester {
2829
public static void fuzzTestShouldWorkWithTransaction(LockProvider lockProvider, DataSource dataSource) throws ExecutionException, InterruptedException {
2930
new FuzzTester(lockProvider) {
3031
@Override
31-
protected Void task(int iterations) {
32+
protected Void task(int iterations, Job job) {
3233
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
33-
return transactionTemplate.execute(status -> super.task(iterations));
34+
return transactionTemplate.execute(status -> super.task(iterations, job));
3435
}
3536

3637
@Override

shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/FuzzTester.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2009-2020 the original author or authors.
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -46,8 +46,8 @@ public class FuzzTester {
4646

4747
private final LockProvider lockProvider;
4848

49-
private int counter;
50-
private final LockConfiguration config = new LockConfiguration("lock", Duration.of(5, ChronoUnit.MINUTES), Duration.of(5, ChronoUnit.MILLIS));
49+
private static final LockConfiguration config1 = new LockConfiguration("lock1", Duration.of(5, ChronoUnit.MINUTES), Duration.of(5, ChronoUnit.MILLIS));
50+
private static final LockConfiguration config2 = new LockConfiguration("lock2", Duration.of(5, ChronoUnit.MINUTES), Duration.of(5, ChronoUnit.MILLIS));
5151

5252
private final Logger logger = LoggerFactory.getLogger(getClass());
5353

@@ -59,11 +59,17 @@ public void doFuzzTest() throws InterruptedException, ExecutionException {
5959
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
6060
int[] iterations = range(0, THREADS).map(i -> ITERATIONS).toArray();
6161
iterations[0] = SHORT_ITERATION; // short task to simulate MySql issues
62+
Job job1 = new Job(config1);
63+
Job job2 = new Job(config2);
6264

63-
List<Callable<Void>> tasks = range(0, THREADS).mapToObj(i -> (Callable<Void>) () -> this.task(iterations[i])).collect(toList());
65+
66+
List<Callable<Void>> tasks = range(0, THREADS).mapToObj(i -> (Callable<Void>) () ->
67+
this.task(iterations[i], i % 2 == 0 ? job1 : job2)).collect(toList()
68+
);
6469
waitForIt(executor.invokeAll(tasks));
6570

66-
assertThat(counter).isEqualTo((THREADS - 1) * ITERATIONS + SHORT_ITERATION);
71+
assertThat(job1.getCounter()).isEqualTo((THREADS / 2 - 1) * ITERATIONS + SHORT_ITERATION);
72+
assertThat(job2.getCounter()).isEqualTo(THREADS / 2 * ITERATIONS);
6773
}
6874

6975
private void waitForIt(List<Future<Void>> futures) throws InterruptedException, ExecutionException {
@@ -72,16 +78,16 @@ private void waitForIt(List<Future<Void>> futures) throws InterruptedException,
7278
}
7379
}
7480

75-
protected Void task(int iterations) {
81+
protected Void task(int iterations, Job job) {
7682
try {
7783
for (int i = 0; i < iterations; ) {
78-
Optional<SimpleLock> lock = lockProvider.lock(config);
84+
Optional<SimpleLock> lock = lockProvider.lock(job.getLockConfiguration());
7985
if (lock.isPresent()) {
80-
int n = counter;
86+
int n = job.getCounter();
8187
if (shouldLog()) logger.debug("action=getLock value={} i={}", n, i);
8288
sleep();
8389
if (shouldLog()) logger.debug("action=setCounter value={} i={}", n + 1, i);
84-
counter = n + 1;
90+
job.setCounter(n + 1);
8591
i++;
8692
lock.get().unlock();
8793
}
@@ -106,4 +112,26 @@ private void sleep() {
106112
}
107113
}
108114

115+
protected static class Job {
116+
private final LockConfiguration lockConfiguration;
117+
private int counter = 0;
118+
119+
Job(LockConfiguration lockConfiguration) {
120+
this.lockConfiguration = lockConfiguration;
121+
}
122+
123+
public LockConfiguration getLockConfiguration() {
124+
return lockConfiguration;
125+
}
126+
127+
public int getCounter() {
128+
return counter;
129+
}
130+
131+
public void setCounter(int counter) {
132+
this.counter = counter;
133+
}
134+
}
109135
}
136+
137+

0 commit comments

Comments
 (0)