Skip to content

Commit 27c0cfc

Browse files
Vladimir Kotalahornace
authored andcommitted
add test for xref timeout
1 parent 00e4c74 commit 27c0cfc

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,8 @@ public static void returnAnalyzers() {
574574
* @throws IOException If an exception occurs while collecting the data
575575
* @throws InterruptedException if a timeout occurs
576576
*/
577-
public void populateDocument(Document doc, File file, String path,
578-
AbstractAnalyzer fa, Writer xrefOut) throws IOException,
579-
InterruptedException {
577+
public void populateDocument(Document doc, File file, String path, AbstractAnalyzer fa, Writer xrefOut)
578+
throws IOException, InterruptedException {
580579

581580
String date = DateTools.timeToString(file.lastModified(),
582581
DateTools.Resolution.MILLISECOND);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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) 2021, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.analysis;
24+
25+
import org.apache.lucene.document.Document;
26+
import org.junit.jupiter.api.Test;
27+
import org.opengrok.indexer.analysis.plain.PlainAnalyzer;
28+
import org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory;
29+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
30+
31+
import java.io.ByteArrayInputStream;
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.io.OutputStreamWriter;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
41+
public class PlainAnalyzerTest {
42+
43+
private static StreamSource getStreamSource(final byte[] bytes) {
44+
return new StreamSource() {
45+
@Override
46+
public InputStream getStream() throws IOException {
47+
return new ByteArrayInputStream(bytes);
48+
}
49+
};
50+
}
51+
52+
@Test
53+
void testXrefTimeout() {
54+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
55+
long timeoutOriginal = env.getXrefTimeout();
56+
int timeout = 3;
57+
env.setXrefTimeout(timeout);
58+
assertEquals(timeout, env.getXrefTimeout());
59+
60+
TestablePlainAnalyzer analyzer = new TestablePlainAnalyzer();
61+
Exception exception = assertThrows(InterruptedException.class, () -> analyzer.analyze(new Document(),
62+
getStreamSource("hi".getBytes()), new OutputStreamWriter(new ByteArrayOutputStream())));
63+
assertTrue(analyzer.writeXrefCalled);
64+
assertTrue(exception.getMessage().contains("failed to generate xref"));
65+
66+
env.setXrefTimeout(timeoutOriginal);
67+
}
68+
69+
private static class TestablePlainAnalyzer extends PlainAnalyzer {
70+
boolean writeXrefCalled;
71+
72+
public TestablePlainAnalyzer() {
73+
super(PlainAnalyzerFactory.DEFAULT_INSTANCE);
74+
}
75+
76+
@Override
77+
public Xrefer writeXref(WriteXrefArgs args) throws IOException {
78+
writeXrefCalled = true;
79+
try {
80+
Thread.sleep(RuntimeEnvironment.getInstance().getXrefTimeout() * 2 * 1000);
81+
} catch (InterruptedException e) {
82+
throw new RuntimeException("failed to sleep");
83+
}
84+
return newXref(args.getIn());
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)