xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexerMainTest.java (revision 9a303f1948b99409943e5b2e00a139a726dbc8d4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2017, 2019, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.indexer.index;
25 
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.opengrok.indexer.configuration.RuntimeEnvironment;
30 import org.opengrok.indexer.history.HistoryGuru;
31 import org.opengrok.indexer.util.TestRepository;
32 
33 import java.io.IOException;
34 import java.net.URISyntaxException;
35 
36 import static org.junit.jupiter.api.Assertions.assertFalse;
37 
38 public class IndexerMainTest {
39     private TestRepository repository;
40 
41     @BeforeEach
setUp()42     public void setUp() throws IOException, URISyntaxException {
43         repository = new TestRepository();
44         // For these tests we need Mercurial repository with renamed files.
45         repository.create(HistoryGuru.class.getResource("/repositories"));
46     }
47 
48     @AfterEach
tearDown()49     public void tearDown() {
50         repository.destroy();
51     }
52 
checkNumberOfThreads()53     private void checkNumberOfThreads() {
54         /*
55          * There should not be any threads in the renamed pool now.
56          * We need to check it like this since the test framework tears
57          * down the threads at the end of the test case run so any
58          * hangs due to executors not being shut down would not be visible.
59          */
60         ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
61         Thread[] threads = new Thread[mainGroup.activeCount()];
62         mainGroup.enumerate(threads);
63         for (Thread thread : threads) {
64             if (thread == null || thread.getName() == null) {
65                 continue;
66             }
67             assertFalse(thread.getName().contains("renamed-handling"));
68         }
69     }
70 
71     /**
72      * Test cleanup of renamed thread pool after indexing with -H.
73      */
74     @Test
testMainWithH()75     public void testMainWithH() {
76         System.out.println("Generate index by using command line options with -H");
77         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
78         String[] argv = {"-S", "-H", "-s", repository.getSourceRoot(),
79                 "-d", repository.getDataRoot(), "-v", "-c", env.getCtags()};
80         Indexer.main(argv);
81         checkNumberOfThreads();
82     }
83 
84     /**
85      * Test cleanup of renamed thread pool after indexing without -H.
86      */
87     @Test
testMainWithoutH()88     public void testMainWithoutH() {
89         System.out.println("Generate index by using command line options without -H");
90         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
91         String[] argv = {"-S", "-P", "-s", repository.getSourceRoot(),
92                 "-d", repository.getDataRoot(), "-v", "-c", env.getCtags()};
93         Indexer.main(argv);
94         checkNumberOfThreads();
95     }
96 }
97