markusthoemmes commented on a change in pull request #3707: enable concurrent
runs on ActionContainer test utility
URL:
https://github.com/apache/incubator-openwhisk/pull/3707#discussion_r191119532
##########
File path: common/scala/src/main/scala/whisk/core/containerpool/HttpUtils.scala
##########
@@ -154,4 +168,28 @@ object HttpUtils {
case Left(ConnectionError(t)) => throw new
IllegalStateException(t.getMessage)
}
}
+
+ /** A helper method to post multiple concurrent requests to a single
connection. Used for container tests. */
+ def concurrentPost(host: String, port: Int, endPoint: String, contents:
Seq[JsValue], timeout: Duration)(
+ implicit ec: ExecutionContext): Seq[(Int, Option[JsObject])] = {
+ val connection = new HttpUtils(s"$host:$port", 90.seconds, 1.MB,
contents.size)
+
+ val futureResults = contents map { content =>
+ Future {
+ connection.post(endPoint, content, retry = true) match {
+ case Right(r) => (r.statusCode,
Try(r.entity.parseJson.asJsObject).toOption)
+ case Left(NoResponseReceived()) => throw new
IllegalStateException("no response from container")
+ case Left(Timeout()) => throw new
java.util.concurrent.TimeoutException()
+ case Left(ConnectionError(t: java.net.SocketTimeoutException)) =>
+ throw new java.util.concurrent.TimeoutException()
+ case Left(ConnectionError(t)) => throw new
IllegalStateException(t.getMessage)
+ }
Review comment:
Since this is an exact copy of aboves "post + error handling", should this
be factored in a method like:
```scala
executeRequest(connection: HttpUtils, endpoint: String, content: JsValue):
(Int, Option[JsObject] = {
connection.post(endpoint, content, retry = true) match {
case Right(r) => (r.statusCode,
Try(r.entity.parseJson.asJsObject).toOption)
case Left(NoResponseReceived()) => throw new IllegalStateException("no
response from container")
case Left(Timeout()) => throw new
java.util.concurrent.TimeoutException()
case Left(ConnectionError(t: java.net.SocketTimeoutException)) =>
throw new java.util.concurrent.TimeoutException()
case Left(ConnectionError(t)) => throw new
IllegalStateException(t.getMessage)
}
}
```
which could then be reused in both cases:
```scala
def post(host: String, port: Int, endPoint: String, content: JsValue): (Int,
Option[JsObject]) = {
val connection = new HttpUtils(s"$host:$port", 90.seconds, 1.MB)
val response = executeRequest(connection, endPoint, content)
connection.close()
response
}
...
def concurrentPost(host: String, port: Int, endPoint: String, contents:
Seq[JsValue], timeout: Duration)(
implicit ec: ExecutionContext): Seq[(Int, Option[JsObject])] = {
val connection = new HttpUtils(s"$host:$port", 90.seconds, 1.MB,
contents.size)
val futureResults = contents.map(content =>
Future(executeRequest(connection, endPoint, content))
val results = Await.result(Future.sequence(futureResults), timeout)
connection.close()
results
}
```
WDYT?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services