26
26
import java .util .concurrent .CountDownLatch ;
27
27
import java .util .concurrent .ExecutorService ;
28
28
import java .util .concurrent .Executors ;
29
- import java .util .concurrent .TimeUnit ;
30
29
30
+ import org .apache .jackrabbit .oak .commons .concurrent .ExecutorCloser ;
31
31
import org .apache .jackrabbit .oak .plugins .index .lucene .LuceneIndexDefinition ;
32
32
import org .apache .jackrabbit .oak .plugins .index .lucene .LuceneIndexWriterFactory ;
33
33
import org .apache .jackrabbit .oak .plugins .index .lucene .directory .DefaultDirectoryFactory ;
@@ -44,54 +44,53 @@ public class ConcurrentMultiplexingIndexWriterTest {
44
44
@ Rule
45
45
public TemporaryFolder folder = new TemporaryFolder (new File ("target" ));
46
46
47
- private NodeState root = INITIAL_CONTENT ;
48
- private NodeBuilder builder = EMPTY_NODE .builder ();
49
- private LuceneIndexDefinition defn = new LuceneIndexDefinition (root , builder .getNodeState (), "/foo" );
50
- private MountInfoProvider mip = Mounts .newBuilder ()
47
+ private final NodeState root = INITIAL_CONTENT ;
48
+ private final NodeBuilder builder = EMPTY_NODE .builder ();
49
+ private final LuceneIndexDefinition defn = new LuceneIndexDefinition (root , builder .getNodeState (), "/foo" );
50
+ private final MountInfoProvider mip = Mounts .newBuilder ()
51
51
.mount ("foo" , "/libs" , "/apps" )
52
52
.readOnlyMount ("ro" , "/ro-tree" )
53
53
.build ();
54
- private LuceneIndexWriterConfig writerConfig = new LuceneIndexWriterConfig ();
54
+ private final LuceneIndexWriterConfig writerConfig = new LuceneIndexWriterConfig ();
55
55
56
56
@ Test
57
57
public void concurrentWrite () throws Exception {
58
- LuceneIndexWriterFactory factory = newDirectoryFactory ();
59
- final LuceneIndexWriter writer = factory .newInstance (defn , builder , null , true );
60
-
61
58
int THREAD_COUNT = 100 ;
62
59
final int LOOP_COUNT = 10 ;
63
60
ExecutorService executorService = Executors .newFixedThreadPool (THREAD_COUNT );
64
61
CountDownLatch startLatch = new CountDownLatch (1 );
65
62
CountDownLatch doneLatch = new CountDownLatch (THREAD_COUNT );
66
63
Exception [] firstException = new Exception [1 ];
67
-
68
- for (int i = 0 ; i < THREAD_COUNT ; i ++) {
69
- executorService .submit (() -> {
70
- try {
71
- // wait for the signal
72
- startLatch .await ();
73
- for (int j = 0 ; j < LOOP_COUNT ; j ++) {
74
- writer .updateDocument ("/libs/config" , newDoc ("/libs/config" ));
64
+
65
+ try (LuceneIndexWriterFactory factory = newDirectoryFactory ()) {
66
+ LuceneIndexWriter writer = factory .newInstance (defn , builder , null , true );
67
+ for (int i = 0 ; i < THREAD_COUNT ; i ++) {
68
+ executorService .submit (() -> {
69
+ try {
70
+ // wait for the signal
71
+ startLatch .await ();
72
+ for (int j = 0 ; j < LOOP_COUNT ; j ++) {
73
+ writer .updateDocument ("/libs/config" , newDoc ("/libs/config" ));
74
+ }
75
+ } catch (InterruptedException e ) {
76
+ Thread .currentThread ().interrupt ();
77
+ } catch (Exception e ) {
78
+ firstException [0 ] = e ;
79
+ } finally {
80
+ doneLatch .countDown ();
75
81
}
76
- } catch (InterruptedException e ) {
77
- Thread .currentThread ().interrupt ();
78
- } catch (Exception e ) {
79
- firstException [0 ] = e ;
80
- } finally {
81
- doneLatch .countDown ();
82
- }
83
- });
82
+ });
83
+ }
84
+ // signal
85
+ startLatch .countDown ();
86
+ doneLatch .await ();
87
+ writer .close (0 );
84
88
}
85
- // signal
86
- startLatch .countDown ();
87
- doneLatch .await ();
88
- executorService .shutdown ();
89
- executorService .awaitTermination (10 , TimeUnit .SECONDS );
89
+ new ExecutorCloser (executorService ).close ();
90
90
if (firstException [0 ] != null ) {
91
91
throw firstException [0 ];
92
92
}
93
- writer .close (0 );
94
- }
93
+ }
95
94
96
95
private LuceneIndexWriterFactory newDirectoryFactory (){
97
96
return newDirectoryFactory (mip );
0 commit comments