This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-472 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-472 by this push: new e7d2c3c WIP. e7d2c3c is described below commit e7d2c3c8887ecf23e563591957bee5ed9d49c2b6 Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Tue Jan 11 18:28:55 2022 +0300 WIP. --- .../nlpcraft/internal/NCRequestProcessorSpec.scala | 72 +++++++++++++--------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/NCRequestProcessorSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/NCRequestProcessorSpec.scala index eb2ee23..dff8e54 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/NCRequestProcessorSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/NCRequestProcessorSpec.scala @@ -17,63 +17,77 @@ package org.apache.nlpcraft.internal -import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnPorterStemmer -import org.apache.nlpcraft.nlp.entity.parser.semantic.* -import org.apache.nlpcraft.nlp.util.* import org.apache.nlpcraft.* import org.apache.nlpcraft.nlp.entity.parser.nlp.impl.NCNlpEntityParserImpl -import org.junit.jupiter.api.Test +import org.apache.nlpcraft.nlp.entity.parser.semantic.* +import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnPorterStemmer +import org.apache.nlpcraft.nlp.util.* +import org.junit.jupiter.api.function.Executable +import org.junit.jupiter.api.* import java.util import java.util.List as JList +import java.util.concurrent.* +import scala.concurrent.CancellationException import scala.jdk.CollectionConverters.* /** * */ class NCRequestProcessorSpec: - private def test(txt: String, variantCnt: Int, elements: NCSemanticElement*): Unit = - val cfg = NCTestConfig.EN.clone() + @Test + def test(): Unit = + def test(txt: String, variantCnt: Int, elements: NCSemanticElement*): Unit = + val cfg = NCTestConfig.EN.clone() - val parser = new NCSemanticEntityParser(new NCEnPorterStemmer, cfg.getTokenParser, elements.asJava) - cfg.getEntityParsers.clear() - cfg.getEntityParsers.add(parser) + val parser = new NCSemanticEntityParser(new NCEnPorterStemmer, cfg.getTokenParser, elements.asJava) + cfg.getEntityParsers.clear() + cfg.getEntityParsers.add(parser) - val res = new NCRequestProcessor(new NCModelAdapter(cfg)).prepare(txt, null, "userId", () => ()) + val res = new NCRequestProcessor(new NCModelAdapter(cfg)).prepare(txt, null, "userId", () => ()) - println(s"Variants count: ${res.variants.size}") + println(s"Variants count: ${res.variants.size}") - for ((v, idx) <- res.variants.zipWithIndex) - println(s"Variant: $idx") - NCTestUtils.printEntities(txt, v.getEntities.asScala.toSeq) + for ((v, idx) <- res.variants.zipWithIndex) + println(s"Variant: $idx") + NCTestUtils.printEntities(txt, v.getEntities.asScala.toSeq) - require(res.variants.size == variantCnt) + require(res.variants.size == variantCnt) - @Test - def test(): Unit = import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticTestElement as Elem test("t1 t2", 4, Elem("t1", "t2"), Elem("t2", "t1")) test("t1 t2", 2, Elem("t1", "t2"), Elem("t2")) - @Test - def testCancel(): Unit = + private def mkSlowModel(delayMs: Long): NCModel = + val cfg = NCTestConfig.EN.clone() + + cfg.getEntityParsers.clear() + val slowParser = new NCEntityParser: override def parse(req: NCRequest, cfg: NCModelConfig, toks: JList[NCToken]): JList[NCEntity] = - Thread.sleep(1000) + Thread.sleep(delayMs) java.util.Collections.emptyList() - val cfg = NCTestConfig.EN.clone() + cfg.getEntityParsers.add(slowParser) - cfg.getEntityParsers.clear() - 0 to 3 foreach(_ => cfg.getEntityParsers.add(slowParser)) + new NCModelAdapter(cfg) - val err = new NCException("Test exception.") + @Test + def testCancel(): Unit = + val fut = new NCRequestProcessor(mkSlowModel(2000)).ask("any", null, "userId") + + Thread.sleep(10) + + require(fut.cancel(true)) + + Assertions.assertThrows(classOf[CancellationException], () => fut.get) + + @Test + def testTimeout(): Unit = + val fut = new NCRequestProcessor(mkSlowModel(2000)).ask("any", null, "userId") - try - new NCRequestProcessor(new NCModelAdapter(cfg)).prepare("any", null, "userId", () => throw err) + Thread.sleep(10) - require(false) - catch - case e: NCException => require(e == err) + Assertions.assertThrows(classOf[TimeoutException], () => fut.get(1, TimeUnit.MILLISECONDS)) \ No newline at end of file