cbickel closed pull request #3186: Rework some tests. URL: https://github.com/apache/incubator-openwhisk/pull/3186
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/tests/src/test/scala/common/BaseWsk.scala b/tests/src/test/scala/common/BaseWsk.scala index 775bfe03cb..655f578292 100644 --- a/tests/src/test/scala/common/BaseWsk.scala +++ b/tests/src/test/scala/common/BaseWsk.scala @@ -17,22 +17,18 @@ package common -import java.io.BufferedWriter -import java.io.File -import java.io.FileWriter +import java.io._ import java.time.Instant -import scala.concurrent.duration.DurationInt -import scala.collection.mutable.Buffer -import scala.concurrent.duration.Duration -import scala.language.postfixOps +import common.TestUtils._ import org.scalatest.Matchers - -import TestUtils._ -import spray.json.JsObject -import spray.json.JsValue -import spray.json.pimpString +import spray.json._ import whisk.core.entity.ByteSize +import whisk.utils.retry + +import scala.collection.mutable.Buffer +import scala.concurrent.duration._ +import scala.language.postfixOps case class WskProps( authKey: String = WhiskProperties.readAuthKey(WhiskProperties.getAuthFileForTesting), @@ -72,21 +68,7 @@ trait WaitFor { pollPeriod: Duration = 1 second, totalWait: Duration = 30 seconds): T = { Thread.sleep(initialWait.toMillis) - val endTime = System.currentTimeMillis() + totalWait.toMillis - while (System.currentTimeMillis() < endTime) { - val predicate = step() - predicate match { - case (t: Boolean) if t => - return predicate - case (t: Any) if t != null && !t.isInstanceOf[Boolean] => - return predicate - case _ if System.currentTimeMillis() >= endTime => - return predicate - case _ => - Thread.sleep(pollPeriod.toMillis) - } - } - null.asInstanceOf[T] + retry[T](step(), (totalWait / pollPeriod).toInt, Some(pollPeriod)) } } diff --git a/tests/src/test/scala/common/Wsk.scala b/tests/src/test/scala/common/Wsk.scala index 1192e6eebf..ea3b52c389 100644 --- a/tests/src/test/scala/common/Wsk.scala +++ b/tests/src/test/scala/common/Wsk.scala @@ -684,9 +684,8 @@ class WskActivation() extends RunWskCmd with HasActivation with WaitFor with Bas () => { val result = cli(wp.overrides ++ Seq(noun, "get", activationId, "--auth", wp.authKey), expectedExitCode = DONTCARE_EXIT) - if (result.exitCode == NOT_FOUND) { - null - } else if (result.exitCode == SUCCESS_EXIT) { + result.exitCode shouldNot be(NOT_FOUND) + if (result.exitCode == SUCCESS_EXIT) { Right(result.stdout) } else Left(s"$result") }, diff --git a/tests/src/test/scala/common/rest/WskRest.scala b/tests/src/test/scala/common/rest/WskRest.scala index 4934b8ec42..30860432ee 100644 --- a/tests/src/test/scala/common/rest/WskRest.scala +++ b/tests/src/test/scala/common/rest/WskRest.scala @@ -18,76 +18,40 @@ package common.rest import java.io.File +import java.nio.charset.StandardCharsets +import java.security.cert.X509Certificate import java.time.Instant import java.util.Base64 -import java.security.cert.X509Certificate +import javax.net.ssl._ + +import akka.http.scaladsl.model.HttpMethods._ +import akka.http.scaladsl.model.StatusCodes.{Accepted, BadRequest, NotFound, OK} +import akka.http.scaladsl.model.Uri.Path +import akka.http.scaladsl.model._ +import akka.http.scaladsl.model.headers._ +import akka.http.scaladsl.settings.ConnectionPoolSettings +import akka.http.scaladsl.{Http, HttpsConnectionContext} +import akka.stream._ +import akka.stream.scaladsl._ +import com.typesafe.sslconfig.akka.AkkaSSLConfig +import common.TestUtils._ +import common._ import org.apache.commons.io.FileUtils -import org.scalatest.Matchers -import org.scalatest.FlatSpec +import org.scalatest._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.Span.convertDurationToSpan -import scala.Left -import scala.Right -import scala.collection.JavaConversions.mapAsJavaMap -import scala.collection.mutable.Buffer -import scala.collection.immutable.Seq -import scala.concurrent.duration.Duration -import scala.concurrent.duration.DurationInt -import scala.concurrent.{Future, Promise} -import scala.language.postfixOps -import scala.util.Failure -import scala.util.Success -import scala.util.Try -import scala.util.{Failure, Success} -import akka.http.scaladsl.model.StatusCode -import akka.http.scaladsl.model.StatusCodes.Accepted -import akka.http.scaladsl.model.StatusCodes.NotFound -import akka.http.scaladsl.model.StatusCodes.BadRequest -import akka.http.scaladsl.model.StatusCodes.OK -import akka.http.scaladsl.model.HttpRequest -import akka.http.scaladsl.model.HttpMethod -import akka.http.scaladsl.model.HttpResponse -import akka.http.scaladsl.model.headers.Authorization -import akka.http.scaladsl.model.HttpEntity -import akka.http.scaladsl.model.ContentTypes -import akka.http.scaladsl.Http -import akka.http.scaladsl.model.headers.BasicHttpCredentials -import akka.http.scaladsl.model.Uri -import akka.http.scaladsl.model.Uri.Path -import akka.http.scaladsl.model.HttpMethods.DELETE -import akka.http.scaladsl.model.HttpMethods.GET -import akka.http.scaladsl.model.HttpMethods.POST -import akka.http.scaladsl.model.HttpMethods.PUT -import akka.http.scaladsl.HttpsConnectionContext -import akka.http.scaladsl.settings.ConnectionPoolSettings -import akka.stream.ActorMaterializer -import akka.stream.scaladsl.{Keep, Sink, Source} -import akka.stream.{OverflowStrategy, QueueOfferResult} -import spray.json._ import spray.json.DefaultJsonProtocol._ -import spray.json.JsObject -import spray.json.JsValue -import spray.json.pimpString -import common._ -import common.BaseDeleteFromCollection -import common.BaseListOrGetFromCollection -import common.HasActivation -import common.RunWskCmd -import common.TestUtils -import common.TestUtils.SUCCESS_EXIT -import common.TestUtils.DONTCARE_EXIT -import common.TestUtils.ANY_ERROR_EXIT -import common.TestUtils.DONTCARE_EXIT -import common.TestUtils.RunResult -import common.WaitFor -import common.WhiskProperties -import common.WskActorSystem -import common.WskProps +import spray.json._ import whisk.core.entity.ByteSize import whisk.utils.retry -import javax.net.ssl.{HostnameVerifier, KeyManager, SSLContext, SSLSession, X509TrustManager} -import com.typesafe.sslconfig.akka.AkkaSSLConfig -import java.nio.charset.StandardCharsets + +import scala.collection.JavaConversions.mapAsJavaMap +import scala.collection.immutable.Seq +import scala.collection.mutable.Buffer +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.language.postfixOps +import scala.util._ class AcceptAllHostNameVerifier extends HostnameVerifier { override def verify(s: String, sslSession: SSLSession): Boolean = true @@ -780,19 +744,18 @@ class WskRestActivation extends RunWskRestCmd with HasActivationRest with WaitFo pollPeriod: Duration = 1 second, totalWait: Duration = 30 seconds)(implicit wp: WskProps): Either[String, JsObject] = { val activation = waitfor(() => { - val result = get(Some(activationId), expectedExitCode = DONTCARE_EXIT)(wp) - if (result.statusCode == NotFound) { - null - } else result + val result = get(Some(activationId), expectedExitCode = DONTCARE_EXIT) + result.statusCode shouldNot be(NotFound) + result }, initialWait, pollPeriod, totalWait) + Try { - assert(activation.statusCode == OK) - assert(activation.getField("activationId") != "") + activation.statusCode shouldBe (OK) + activation.getField("activationId") shouldNot be("") activation.respBody } map { Right(_) } getOrElse Left(s"Cannot find activation id from '$activation'") - } override def logs(activationId: Option[String] = None, @@ -1525,7 +1488,7 @@ object RestResult { } } -class RestResult(var statusCode: StatusCode, var respData: String = "", blocking: Boolean = false) +class RestResult(val statusCode: StatusCode, val respData: String = "", blocking: Boolean = false) extends RunResult( RestResult.convertStausCodeToExitCode(statusCode, blocking), respData, diff --git a/tests/src/test/scala/whisk/core/database/test/CleanUpActivationsTest.scala b/tests/src/test/scala/whisk/core/database/test/CleanUpActivationsTest.scala index 997397fe87..43db70e50f 100644 --- a/tests/src/test/scala/whisk/core/database/test/CleanUpActivationsTest.scala +++ b/tests/src/test/scala/whisk/core/database/test/CleanUpActivationsTest.scala @@ -20,25 +20,17 @@ package whisk.core.database.test import java.io.File import java.time.Instant -import scala.concurrent.duration.DurationInt -import scala.concurrent.duration.FiniteDuration -import scala.language.implicitConversions - +import akka.http.scaladsl.model.StatusCodes +import common._ import org.junit.runner.RunWith -import org.scalatest.FlatSpec -import org.scalatest.Matchers +import org.scalatest._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner - -import akka.http.scaladsl.model.StatusCodes -import common.StreamLogging -import common.TestUtils -import common.WaitFor -import common.WhiskProperties -import common.WskActorSystem import spray.json.DefaultJsonProtocol._ -import spray.json.JsObject -import spray.json.pimpAny +import spray.json._ + +import scala.concurrent.duration._ +import scala.language.implicitConversions @RunWith(classOf[JUnitRunner]) class CleanUpActivationsTest @@ -46,7 +38,6 @@ class CleanUpActivationsTest with Matchers with ScalaFutures with WskActorSystem - with WaitFor with StreamLogging with DatabaseScriptTestUtils { diff --git a/tests/src/test/scala/whisk/core/database/test/DatabaseScriptTestUtils.scala b/tests/src/test/scala/whisk/core/database/test/DatabaseScriptTestUtils.scala index 9b227dfffe..7d15788ad4 100644 --- a/tests/src/test/scala/whisk/core/database/test/DatabaseScriptTestUtils.scala +++ b/tests/src/test/scala/whisk/core/database/test/DatabaseScriptTestUtils.scala @@ -72,9 +72,7 @@ trait DatabaseScriptTestUtils extends ScalaFutures with Matchers with WaitFor wi /** Wait for database to appear */ def waitForDatabase(dbName: String)(implicit as: ActorSystem, logging: Logging) = { val client = new ExtendedCouchDbRestClient(dbProtocol, dbHost, dbPort.toInt, dbUsername, dbPassword, dbName) - waitfor(() => { - client.getAllDocs(includeDocs = Some(true)).futureValue.isRight - }) + waitfor(() => client.getAllDocs(includeDocs = Some(true)).futureValue shouldBe 'right) client } @@ -91,14 +89,16 @@ trait DatabaseScriptTestUtils extends ScalaFutures with Matchers with WaitFor wi /** Wait for a document to appear */ def waitForDocument(client: ExtendedCouchDbRestClient, id: String) = - waitfor(() => client.getDoc(id).futureValue.isRight) + waitfor(() => client.getDoc(id).futureValue shouldBe 'right) /** Get all docs within one database */ - def getAllDocs(dbName: String)(implicit as: ActorSystem, logging: Logging) = { + def getAllDocs(dbName: String)(implicit as: ActorSystem, logging: Logging): JsObject = { val client = new ExtendedCouchDbRestClient(dbProtocol, dbHost, dbPort.toInt, dbUsername, dbPassword, dbName) - val documents = client.getAllDocs(includeDocs = Some(true)).futureValue - documents shouldBe 'right - documents.right.get + retry({ + val documents = client.getAllDocs(includeDocs = Some(true)).futureValue + documents shouldBe 'right + documents.right.get + }) } /** wait until all documents are processed by the view */ @@ -106,7 +106,7 @@ trait DatabaseScriptTestUtils extends ScalaFutures with Matchers with WaitFor wi waitfor(() => { val view = db.executeView(designDoc, viewName)().futureValue view shouldBe 'right - view.right.get.fields("rows").convertTo[List[JsObject]].length == numDocuments + view.right.get.fields("rows").convertTo[List[JsObject]].length shouldBe numDocuments }, totalWait = 2.minutes) } } diff --git a/tests/src/test/scala/whisk/core/database/test/ReplicatorTests.scala b/tests/src/test/scala/whisk/core/database/test/ReplicatorTests.scala index 727d8720e2..819f02178f 100644 --- a/tests/src/test/scala/whisk/core/database/test/ReplicatorTests.scala +++ b/tests/src/test/scala/whisk/core/database/test/ReplicatorTests.scala @@ -32,7 +32,6 @@ import org.scalatest.junit.JUnitRunner import akka.http.scaladsl.model.StatusCodes import common.StreamLogging import common.TestUtils -import common.WaitFor import common.WhiskProperties import common.WskActorSystem import spray.json._ @@ -44,7 +43,6 @@ class ReplicatorTests with Matchers with ScalaFutures with WskActorSystem - with WaitFor with StreamLogging with DatabaseScriptTestUtils { @@ -140,18 +138,15 @@ class ReplicatorTests /** Wait for a replication to finish */ def waitForReplication(dbName: String) = { - val timeout = 5.minutes - val replicationResult = waitfor(() => { + retry(() => { val replicatorDoc = replicatorClient.getDoc(dbName).futureValue replicatorDoc shouldBe 'right val state = replicatorDoc.right.get.fields.get("_replication_state") println(s"Waiting for replication, state: $state") - state.contains("completed".toJson) - }, totalWait = timeout) - - assert(replicationResult, s"replication did not finish in $timeout") + state should contain("completed".toJson) + }, 300, Some(1.second)) } /** Compares to databases to full equality */ ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services