This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-477
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-477 by this push:
new 9c34ab1 WIP.
9c34ab1 is described below
commit 9c34ab1ac1f9c32a62e44e8ea8be68cf7ee8960e
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Feb 18 11:50:56 2022 +0300
WIP.
---
.../dialogflow/NCDialogFlowManagerSpec.scala | 2 +-
...lientImplSpec.scala => NCModelClientSpec.scala} | 20 +++---
.../internal/impl/NCModelSlowModelSpec.scala | 83 ++++++++++++++++++++++
.../internal/impl/scan/NCTestModelJava.java | 3 +-
.../compiler/functions/NCIDLFunctionsModel.scala | 2 +-
.../apache/nlpcraft/nlp/util/NCTestEntity.scala | 4 +-
.../apache/nlpcraft/nlp/util/NCTestPipeline.scala | 2 +-
.../apache/nlpcraft/nlp/util/NCTestRequest.scala | 3 +-
8 files changed, 99 insertions(+), 20 deletions(-)
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/dialogflow/NCDialogFlowManagerSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/dialogflow/NCDialogFlowManagerSpec.scala
index cf0e371..01d2ef7 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/dialogflow/NCDialogFlowManagerSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/dialogflow/NCDialogFlowManagerSpec.scala
@@ -17,9 +17,9 @@
package org.apache.nlpcraft.internal.dialogflow
+import org.apache.nlpcraft.*
import org.apache.nlpcraft.internal.util.NCUtils
import org.apache.nlpcraft.nlp.util.NCTestRequest
-import org.apache.nlpcraft.*
import org.junit.jupiter.api.*
import java.util
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientImplSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
similarity index 85%
rename from
nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientImplSpec.scala
rename to
nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
index fee87e9..a1e0f78 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientImplSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
@@ -18,7 +18,6 @@
package org.apache.nlpcraft.internal.impl
import org.apache.nlpcraft.*
-import
org.apache.nlpcraft.internal.impl.scan.NCTestModelScala.NCTestModelScalaObj
import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser
import
org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer
import org.apache.nlpcraft.nlp.util.NCTestModelAdapter
@@ -26,8 +25,9 @@ import org.apache.nlpcraft.nlp.util.opennlp.*
import org.junit.jupiter.api.Test
import scala.jdk.CollectionConverters.*
+import scala.util.Using
-class NCModelClientImplSpec:
+class NCModelClientSpec:
/**
*
*/
@@ -35,6 +35,7 @@ class NCModelClientImplSpec:
def test(): Unit =
val mdl =
new NCTestModelAdapter():
+ // TODO: doesn't work.
//@NCIntent("intent=ls term(act)={has(ent_groups, 'act')}
term(loc)={# == 'ls:loc'}*")
@NCIntent("intent=ls term(act)={# == 'ls:on'} term(loc)={# ==
'ls:loc'}*")
def onMatch(@NCIntentTerm("act") act: NCEntity,
@NCIntentTerm("loc") locs: List[NCEntity]): NCResult =
@@ -52,14 +53,9 @@ class NCModelClientImplSpec:
)
)
- val client = new NCModelClientImpl(mdl)
-
- val res = client.askSync("Lights on at second floor kitchen", null,
"userId")
-
- println(s"Intent: ${res.getIntentId}")
- println(s"Body: ${res.getBody}")
-
- client.close()
-
-
+ Using.resource(new NCModelClient(mdl)) { client =>
+ val res = client.askSync("Lights on at second floor kitchen",
null, "userId")
+ println(s"Intent: ${res.getIntentId}")
+ println(s"Body: ${res.getBody}")
+ }
\ No newline at end of file
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelSlowModelSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelSlowModelSpec.scala
new file mode 100644
index 0000000..9e70d4d
--- /dev/null
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelSlowModelSpec.scala
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.internal.impl
+
+import org.apache.nlpcraft.*
+import org.apache.nlpcraft.nlp.util.NCTestModelAdapter
+import org.apache.nlpcraft.nlp.util.opennlp.*
+import org.junit.jupiter.api.*
+
+import java.util.List as JList
+import java.util.concurrent.*
+import scala.jdk.CollectionConverters.*
+import scala.util.Using
+
+/**
+ *
+ */
+class NCModelSlowModelSpec:
+ /**
+ *
+ * @param delayMs
+ * @param iterCnt
+ * @return
+ */
+ private def mkSlowModel(delayMs: Long, iterCnt: Int): NCModel =
+ val pipeline = EN_PIPELINE.clone()
+
+ pipeline.getEntityParsers.clear()
+
+ def mkSlowParser(i: Int) =
+ new NCEntityParser:
+ override def parse(req: NCRequest, cfg: NCModelConfig, toks:
JList[NCToken]): JList[NCEntity] =
+ println(s"Parser called: $i")
+ Thread.sleep(delayMs)
+ java.util.Collections.emptyList()
+
+ (0 until iterCnt).foreach(i =>
pipeline.getEntityParsers.add(mkSlowParser(i)))
+
+ new NCTestModelAdapter:
+ override val getPipeline: NCModelPipeline = pipeline
+ /**
+ *
+ */
+ @Test
+ def testCancel(): Unit =
+ Using.resource(new NCModelClient(mkSlowModel(1, 10000))) { client =>
+ val fut = client.ask("any", null, "userId")
+
+ Thread.sleep(20)
+ require(fut.cancel(true))
+ Thread.sleep(20)
+
+ Assertions.assertThrows(classOf[CancellationException], () =>
fut.get)
+ }
+ /**
+ *
+ */
+ @Test
+ def testTimeout(): Unit =
+ Using.resource(new NCModelClient(mkSlowModel(1, 10000))) { client =>
+ val fut = client.ask("any", null, "userId")
+
+ Thread.sleep(20)
+ Thread.sleep(20)
+
+ try Assertions.assertThrows(classOf[TimeoutException], () =>
fut.get(1, TimeUnit.MILLISECONDS))
+ finally fut.cancel(true)
+ }
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
index 96560a6..1f2763e 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
@@ -17,15 +17,14 @@
package org.apache.nlpcraft.internal.impl.scan;
+import org.apache.nlpcraft.NCEntity;
import org.apache.nlpcraft.NCIntent;
-import org.apache.nlpcraft.NCIntentRef;
import org.apache.nlpcraft.NCIntentSample;
import org.apache.nlpcraft.NCIntentSampleRef;
import org.apache.nlpcraft.NCIntentTerm;
import org.apache.nlpcraft.NCModel;
import org.apache.nlpcraft.NCModelAdapter;
import org.apache.nlpcraft.NCResult;
-import org.apache.nlpcraft.NCEntity;
import org.apache.nlpcraft.nlp.util.opennlp.NCTestConfigJava;
import java.util.List;
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctionsModel.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctionsModel.scala
index af53525..3f9dd12 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctionsModel.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctionsModel.scala
@@ -18,8 +18,8 @@
package org.apache.nlpcraft.internal.intent.compiler.functions
import org.apache.nlpcraft.internal.intent.compiler.functions.NCIDLFunctions.*
-import org.junit.jupiter.api.Test
import org.apache.nlpcraft.nlp.util.opennlp.*
+import org.junit.jupiter.api.Test
import scala.language.implicitConversions
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestEntity.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestEntity.scala
index 0640a80..945c667 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestEntity.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestEntity.scala
@@ -19,9 +19,9 @@ package org.apache.nlpcraft.nlp.util
import org.apache.nlpcraft.*
import org.apache.nlpcraft.nlp.util.NCTestPipeline.*
+
+import java.util.{List as JList, Set as JSet}
import scala.jdk.CollectionConverters.*
-import java.util.Set as JSet
-import java.util.List as JList
/**
* Entity test implementation.
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestPipeline.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestPipeline.scala
index 395dc47..5a27e9e 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestPipeline.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestPipeline.scala
@@ -17,9 +17,9 @@
package org.apache.nlpcraft.nlp.util
-import org.apache.nlpcraft.nlp.util.NCTestPipeline.*
import org.apache.nlpcraft.*
import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
+import org.apache.nlpcraft.nlp.util.NCTestPipeline.*
import java.util.{Optional, ArrayList as JList}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestRequest.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestRequest.scala
index 29d08c8..89c81d9 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestRequest.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestRequest.scala
@@ -19,9 +19,10 @@ package org.apache.nlpcraft.nlp.util
import org.apache.nlpcraft.*
import org.apache.nlpcraft.nlp.util.NCTestPipeline.*
-import scala.jdk.CollectionConverters.*
+
import java.util
import java.util.Map as JMap
+import scala.jdk.CollectionConverters.*
/**
* Request test implementation.