Skip to content

Commit 8617b1f

Browse files
committed
Fuzz test added
1 parent f4967a3 commit 8617b1f

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

shedlock-provider-mongo/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
<version>2.0.10</version>
3232
<scope>test</scope>
3333
</dependency>
34+
35+
<dependency>
36+
<groupId>de.flapdoodle.embed</groupId>
37+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
38+
<version>1.50.5</version>
39+
<scope>test</scope>
40+
</dependency>
3441
</dependencies>
3542

3643
</project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.javacrumbs.shedlock.provider.mongo;
17+
18+
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
19+
import net.javacrumbs.shedlock.core.LockConfiguration;
20+
import net.javacrumbs.shedlock.core.LockProvider;
21+
import net.javacrumbs.shedlock.core.SimpleLock;
22+
import org.junit.AfterClass;
23+
import org.junit.Before;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
27+
import java.io.IOException;
28+
import java.net.UnknownHostException;
29+
import java.time.Instant;
30+
import java.time.temporal.ChronoUnit;
31+
import java.util.List;
32+
import java.util.Optional;
33+
import java.util.concurrent.Callable;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.ExecutorService;
36+
import java.util.concurrent.Executors;
37+
import java.util.concurrent.Future;
38+
39+
import static java.util.stream.Collectors.toList;
40+
import static java.util.stream.IntStream.range;
41+
import static org.assertj.core.api.Assertions.assertThat;
42+
43+
public class FuzzTest {
44+
private static MongodForTestsFactory mongoFactory;
45+
46+
47+
private static final int THREADS = 4;
48+
private static final int ITERATIONS = 100;
49+
50+
private LockProvider lockProvider;
51+
52+
private int counter;
53+
private final LockConfiguration config = new LockConfiguration("lock", Instant.now().plus(5, ChronoUnit.MINUTES));
54+
55+
@Before
56+
public void createLockProvider() throws UnknownHostException {
57+
lockProvider = new MongoLockProvider(mongoFactory.newMongo(), "test", "test");
58+
}
59+
60+
61+
@Test
62+
public void fuzzTest() throws InterruptedException, ExecutionException {
63+
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
64+
65+
List<Callable<Void>> tasks = range(0, THREADS).mapToObj(i -> (Callable<Void>) this::task).collect(toList());
66+
waitForIt(executor.invokeAll(tasks));
67+
68+
assertThat(counter).isEqualTo(THREADS * ITERATIONS);
69+
}
70+
71+
private void waitForIt(List<Future<Void>> futures) throws InterruptedException, ExecutionException {
72+
for (Future<Void> f : futures) {
73+
f.get();
74+
}
75+
}
76+
77+
private Void task() {
78+
for (int i = 0; i < ITERATIONS; ) {
79+
Optional<SimpleLock> lock = lockProvider.lock(config);
80+
if (lock.isPresent()) {
81+
int n = counter;
82+
sleep();
83+
counter = n + 1;
84+
i++;
85+
lock.get().unlock();
86+
}
87+
}
88+
return null;
89+
}
90+
91+
@BeforeClass
92+
public static void startMongo() throws IOException {
93+
mongoFactory = new MongodForTestsFactory();
94+
}
95+
96+
@AfterClass
97+
public static void stopMongo() throws IOException {
98+
mongoFactory.shutdown();
99+
}
100+
101+
private void sleep() {
102+
try {
103+
Thread.sleep(1);
104+
} catch (InterruptedException e) {
105+
throw new RuntimeException(e);
106+
}
107+
}
108+
109+
}

0 commit comments

Comments
 (0)