1
1
/**
2
2
* Copyright 2009-2020 the original author or authors.
3
- *
3
+ * <p>
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* 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>
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -46,8 +46,8 @@ public class FuzzTester {
46
46
47
47
private final LockProvider lockProvider ;
48
48
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 ));
51
51
52
52
private final Logger logger = LoggerFactory .getLogger (getClass ());
53
53
@@ -59,11 +59,17 @@ public void doFuzzTest() throws InterruptedException, ExecutionException {
59
59
ExecutorService executor = Executors .newFixedThreadPool (THREADS );
60
60
int [] iterations = range (0 , THREADS ).map (i -> ITERATIONS ).toArray ();
61
61
iterations [0 ] = SHORT_ITERATION ; // short task to simulate MySql issues
62
+ Job job1 = new Job (config1 );
63
+ Job job2 = new Job (config2 );
62
64
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
+ );
64
69
waitForIt (executor .invokeAll (tasks ));
65
70
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 );
67
73
}
68
74
69
75
private void waitForIt (List <Future <Void >> futures ) throws InterruptedException , ExecutionException {
@@ -72,16 +78,16 @@ private void waitForIt(List<Future<Void>> futures) throws InterruptedException,
72
78
}
73
79
}
74
80
75
- protected Void task (int iterations ) {
81
+ protected Void task (int iterations , Job job ) {
76
82
try {
77
83
for (int i = 0 ; i < iterations ; ) {
78
- Optional <SimpleLock > lock = lockProvider .lock (config );
84
+ Optional <SimpleLock > lock = lockProvider .lock (job . getLockConfiguration () );
79
85
if (lock .isPresent ()) {
80
- int n = counter ;
86
+ int n = job . getCounter () ;
81
87
if (shouldLog ()) logger .debug ("action=getLock value={} i={}" , n , i );
82
88
sleep ();
83
89
if (shouldLog ()) logger .debug ("action=setCounter value={} i={}" , n + 1 , i );
84
- counter = n + 1 ;
90
+ job . setCounter ( n + 1 ) ;
85
91
i ++;
86
92
lock .get ().unlock ();
87
93
}
@@ -106,4 +112,26 @@ private void sleep() {
106
112
}
107
113
}
108
114
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
+ }
109
135
}
136
+
137
+
0 commit comments