21
21
import java .util .List ;
22
22
import java .util .concurrent .CopyOnWriteArrayList ;
23
23
import java .util .concurrent .CountDownLatch ;
24
+ import java .util .concurrent .LinkedBlockingDeque ;
25
+ import java .util .concurrent .ThreadPoolExecutor ;
26
+ import java .util .concurrent .TimeUnit ;
24
27
25
28
import org .junit .jupiter .api .Test ;
26
29
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
35
38
import org .springframework .data .mapping .PersonDocument ;
36
39
37
40
/**
41
+ * Unit tests for {@link EntityCallbackDiscoverer}.
42
+ *
38
43
* @author Christoph Strobl
39
44
* @author Myeonghyeon Lee
45
+ * @author Mark Paluch
40
46
*/
41
47
class EntityCallbackDiscovererUnitTests {
42
48
@@ -59,30 +65,31 @@ void shouldDiscoverCallbackTypeConcurrencyCache() throws InterruptedException {
59
65
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
60
66
61
67
EntityCallbackDiscoverer discoverer = new EntityCallbackDiscoverer (ctx );
62
-
63
- int concurrencyCount = 4000 ;
64
- CountDownLatch startLatch = new CountDownLatch (concurrencyCount );
65
- CountDownLatch doneLatch = new CountDownLatch (concurrencyCount );
68
+ int poolSize = Runtime .getRuntime ().availableProcessors ();
69
+ ThreadPoolExecutor executor = new ThreadPoolExecutor (poolSize , poolSize , 20 , TimeUnit .SECONDS ,
70
+ new LinkedBlockingDeque <>());
71
+ CountDownLatch startLatch = new CountDownLatch (poolSize );
72
+ CountDownLatch doneLatch = new CountDownLatch (poolSize );
66
73
67
74
List <Exception > exceptions = new CopyOnWriteArrayList <>();
68
- for (int i = 0 ; i < concurrencyCount ; i ++) {
69
- Thread thread = new Thread (() -> {
75
+ for (int i = 0 ; i < poolSize ; i ++) {
76
+ executor . submit (() -> {
70
77
try {
71
78
startLatch .countDown ();
72
- startLatch .await ();
79
+ startLatch .await (5 , TimeUnit . SECONDS );
73
80
74
81
discoverer .getEntityCallbacks (PersonDocument .class ,
75
- ResolvableType .forType (BeforeSaveCallback .class ));
82
+ ResolvableType .forType (BeforeSaveCallback .class ));
76
83
} catch (Exception ex ) {
77
84
exceptions .add (ex );
78
85
} finally {
79
86
doneLatch .countDown ();
80
87
}
81
88
});
82
- thread .start ();
83
89
}
84
90
85
- doneLatch .await ();
91
+ doneLatch .await (10 , TimeUnit .SECONDS );
92
+ executor .shutdownNow ();
86
93
87
94
assertThat (exceptions ).isEmpty ();
88
95
}
0 commit comments