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