ExecutionContext.Implicits.global Scala Examples

The following examples show how to use ExecutionContext.Implicits.global. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: HttpTimeoutSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http

import java.net.{ServerSocket, URI}
import java.util.concurrent.TimeoutException

import org.scalatest.concurrent.ScalaFutures
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler}
import org.webbitserver.netty.NettyWebServer
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.test.WsTestClient
import play.api.{Configuration, Play}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.http.ws.WSHttp
import uk.gov.hmrc.play.test.TestHttpCore

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.duration.DurationInt

class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll {

  import ExecutionContext.Implicits.global

  lazy val fakeApplication =
    GuiceApplicationBuilder(configuration = Configuration("play.ws.timeout.request" -> "1000ms")).build()

  override def beforeAll() {
    super.beforeAll()
    Play.start(fakeApplication)
  }

  override def afterAll() {
    super.afterAll()
    Play.stop(fakeApplication)
  }

  WsTestClient.withClient{ client =>

    "HttpCalls" should {

      "be gracefully timeout when no response is received within the 'timeout' frame" in {
        val http = new WSHttp with TestHttpCore {
          override val wsClient = fakeApplication.injector.instanceOf[WSClient]
        }

        // get an unused port
        val ss = new ServerSocket(0)
        ss.close()
        val executor = ExecutionContext.global // fromExecutorService(ExecutionContext.global)
        val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}")
        val ws        = new NettyWebServer(executor, ss.getLocalSocketAddress, publicUri)
        try {
          //starts web server
          ws.add(
            "/test",
            new DelayedHttpHandler(executor, 2000, new StringHttpHandler("application/json", "{name:'pong'}")))
          ws.start().get()

          implicit val hc = HeaderCarrier()

          val start = System.currentTimeMillis()
          intercept[TimeoutException] {
            //make request to web server
            Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds)
          }
          val diff = (System.currentTimeMillis() - start).toInt
          // there is test execution delay around 700ms
          diff should be >= 1000
          diff should be < 2500
        } finally {
          ws.stop()
        }
      }
    }
  }
} 
Example 2
Source File: CancelFutureTask.scala    From lemon-schedule   with GNU General Public License v2.0 5 votes vote down vote up
package com.gabry.job.experiment

import java.util.concurrent.{Callable, FutureTask}

import com.gabry.job.utils.TaskClassLoader


object CancelFutureTask {
  def main(args: Array[String]): Unit = {
    //D:/MyCode/lemon-schedule/lemon-schedule-examples/target/lemon-schedule-examples-1.0-SNAPSHOT.jar
    val classLoader = new TaskClassLoader("D:/MyCode/lemon-schedule/lemon-schedule-examples/target/lemon-schedule-examples-1.0-SNAPSHOT.jar")
    classLoader.init()
    classLoader.destroy()
    import scala.concurrent._
    import ExecutionContext.Implicits.global
    class Task extends Callable[Int]{
      override def call(): Int = {
        println(s"thread [${Thread.currentThread().getName}]")
        val count = 5
        0 until count foreach { i=>
          println(s"task alive $i")
          Thread.sleep(1000)
        }
        count
      }
    }
    val futureTask = new FutureTask[Int](new Task)
    Future{
      futureTask.run()
      println(s"futureTask执行成功 ${futureTask.get()}")
    }
    Thread.sleep(3*1000)
    println("futureTask提前终止")
    futureTask.cancel(true)
    Thread.sleep(3*1000)
    println(s"futureTask执行 ${futureTask.get()}")
  }
} 
Example 3
Source File: GitHubUserConcurrent.scala    From s4ds   with Apache License 2.0 5 votes vote down vote up
import scala.io._
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util._

import org.json4s._
import org.json4s.native.JsonMethods._

object GitHubUserConcurrent {

  implicit val formats = DefaultFormats

  case class User(id:Long, userName:String)

  def fetchUserFromUrl(url:String):Future[User] = {
    val response = Future { Source.fromURL(url).mkString }
    val parsedResponse = response.map { r => parse(r) }
    parsedResponse.map(extractUser)
  }

  def extractUser(jsonResponse:JValue):User = {
    val o = jsonResponse.transformField {
      case ("login", name) => ("userName", name)
    }
    o.extract[User]
  }

  def main(args:Array[String]) {
    val names = args.toList
    val name2User = for {
      name <- names
      url = s"https://api.github.com/users/$name"
      user = fetchUserFromUrl(url)
    } yield name -> user

    name2User.foreach { case(name, user) =>
      user.onComplete {
        case Success(u) => println(s" ** Extracted for $name: $u")
        case Failure(e) => println(s" ** Error fetching $name: $e")
      }
    }

    Await.ready(Future.sequence(name2User.map { _._2 }), 1 minute)

  }

} 
Example 4
Source File: GitHubUserAuth.scala    From s4ds   with Apache License 2.0 5 votes vote down vote up
import scala.io._
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util._

import org.json4s._
import org.json4s.native.JsonMethods._

import scalaj.http._

object GitHubUserAuth {

  implicit val formats = DefaultFormats

  case class User(id:Long, userName:String)

  lazy val token:Option[String] = sys.env.get("GHTOKEN") orElse {
    println("No token found: continuing without authentication")
    None
  }

  def fetchUserFromUrl(url:String):Future[User] = {
    val baseRequest = Http(url)
    val request = token match {
      case Some(t) => baseRequest.header(
        "Authorization", s"token $t")
      case None => baseRequest
    }
    val response = Future { 
      request.asString.body
    }
    val parsedResponse = response.map { r => parse(r) }
    parsedResponse.map(extractUser)
  }

  def extractUser(jsonResponse:JValue):User = {
    val o = jsonResponse.transformField {
      case ("login", name) => ("userName", name)
    }
    o.extract[User]
  }

  def main(args:Array[String]) {
    val names = args.toList
    val name2User = for {
      name <- names
      url = s"https://api.github.com/users/$name"
      user = fetchUserFromUrl(url)
    } yield name -> user

    name2User.foreach { case(name, user) =>
      user.onComplete {
        case Success(u) => println(s" ** Extracted for $name: $u")
        case Failure(e) => println(s" ** Error fetching $name: $e")
      }
    }

    Await.ready(Future.sequence(name2User.map { _._2 }), 1 minute)

  }

} 
Example 5
Source File: Common.scala    From ReactiveMongo-Streaming   with Apache License 2.0 5 votes vote down vote up
object Common {
  import scala.concurrent._
  import scala.concurrent.duration._
  import reactivemongo.api._

  val primaryHost = Option(System getProperty "test.primaryHost").
    getOrElse("localhost:27017")

  val failoverRetries = Option(System getProperty "test.failoverRetries").
    flatMap(r => scala.util.Try(r.toInt).toOption).getOrElse(7)

  private val timeoutFactor = 1.25D
  def estTimeout(fos: FailoverStrategy): FiniteDuration =
    (1 to fos.retries).foldLeft(fos.initialDelay) { (d, i) =>
      d + (fos.initialDelay * ((timeoutFactor * fos.delayFactor(i)).toLong))
    }

  val failoverStrategy = FailoverStrategy(retries = failoverRetries)

  val timeout: FiniteDuration = {
    val maxTimeout = estTimeout(failoverStrategy)

    if (maxTimeout < 10.seconds) 10.seconds
    else maxTimeout
  }

  private val driverReg = Seq.newBuilder[AsyncDriver]
  def newDriver(): AsyncDriver = driverReg.synchronized {
    val drv = AsyncDriver()

    driverReg += drv

    drv
  }

  lazy val driver = newDriver()

  val DefaultOptions = {
    val opts = MongoConnectionOptions.default.copy(
      failoverStrategy = failoverStrategy
    )

    if (Option(System getProperty "test.enableSSL").exists(_ == "true")) {
      opts.copy(sslEnabled = true, sslAllowsInvalidCert = true)
    } else opts
  }

  lazy val connection = Await.result(
    driver.connect(List(primaryHost), DefaultOptions), timeout)

  lazy val db = {
    import ExecutionContext.Implicits.global

    val _db = for {
      d <- connection.database(
        s"rm-akkastream-${System identityHashCode getClass}"
      )
      _ <- d.drop()
    } yield d

    Await.result(_db, timeout)
  }

  def close(): Unit = {
    import ExecutionContext.Implicits.global

    driverReg.result().foreach { driver =>
      try {
        driver.close(timeout)
      } catch {
        case e: Throwable =>
          e.printStackTrace()
        
      }
    }
  }

  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run() = close()
  })
} 
Example 6
Source File: Common.scala    From ReactiveMongo-Streaming   with Apache License 2.0 5 votes vote down vote up
object Common {
  import scala.concurrent.{ Await, ExecutionContext }
  import scala.concurrent.duration._
  import reactivemongo.api.{
    FailoverStrategy,
    AsyncDriver,
    MongoConnectionOptions
  }

  val logger = org.slf4j.LoggerFactory.getLogger("tests")

  val DefaultOptions = {
    val opts = MongoConnectionOptions.default

    if (Option(System getProperty "test.enableSSL").exists(_ == "true")) {
      opts.copy(sslEnabled = true, sslAllowsInvalidCert = true)
    } else opts
  }

  val primaryHost =
    Option(System getProperty "test.primaryHost").getOrElse("localhost:27017")

  val failoverRetries = Option(System getProperty "test.failoverRetries").
    flatMap(r => scala.util.Try(r.toInt).toOption).getOrElse(7)

  private val driverReg = Seq.newBuilder[AsyncDriver]
  def newDriver(): AsyncDriver = driverReg.synchronized {
    val drv = AsyncDriver()

    driverReg += drv

    drv
  }

  lazy val driver = newDriver()
  lazy val connection = Await.result(
    driver.connect(List(primaryHost), DefaultOptions), timeout)

  val failoverStrategy = FailoverStrategy(retries = failoverRetries)

  private val timeoutFactor = 1.2D
  def estTimeout(fos: FailoverStrategy): FiniteDuration =
    (1 to fos.retries).foldLeft(fos.initialDelay) { (d, i) =>
      d + (fos.initialDelay * ((timeoutFactor * fos.delayFactor(i)).toLong))
    }

  implicit val timeout: FiniteDuration = {
    val maxTimeout = estTimeout(failoverStrategy)
    if (maxTimeout < 10.seconds) 10.seconds else maxTimeout
  }

  //val timeoutMillis = timeout.toMillis.toInt

  lazy val db = {
    import ExecutionContext.Implicits.global

    val _db = connection.database(
      "specs2-reactivemongo-iteratees", failoverStrategy
    )

    Await.result(_db.flatMap { d => d.drop.map(_ => d) }, timeout)
  }

  def close(): Unit = {
    import ExecutionContext.Implicits.global

    driverReg.result().foreach { driver =>
      try {
        driver.close(timeout)
      } catch {
        case e: Throwable =>
          e.printStackTrace()
        
      }
    }
  }

  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run() = close()
  })
} 
Example 7
Source File: Atomic.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package ch7


import org.learningconcurrency._
import ch7._




object AtomicHistoryBad extends App {
  import java.util.concurrent.atomic._
  import scala.annotation.tailrec
  import scala.concurrent._
  import ExecutionContext.Implicits.global

  val urls = new AtomicReference[List[String]](Nil)
  val clen = new AtomicInteger(0)

  def addUrl(url: String): Unit = {
    @tailrec def append(): Unit = {
      val oldUrls = urls.get
      if (!urls.compareAndSet(oldUrls, url :: oldUrls)) append()
    }
    append()
    clen.addAndGet(url.length + 1)
  }

  def getUrlArray(): Array[Char] = {
    val array = new Array[Char](clen.get)
    val urlList = urls.get
    for ((character, i) <- urlList.map(_ + "\n").flatten.zipWithIndex) {
      array(i) = character
    }
    array
  }

  Future {
    try { log(s"sending: ${getUrlArray().mkString}") }
    catch { case e: Exception => log(s"problems getting the array $e") }
  }

  Future {
    addUrl("http://scala-lang.org")
    addUrl("https://github.com/scala/scala")
    addUrl("http://www.scala-lang.org/api")
    log("done browsing")
  }

} 
Example 8
Source File: AsyncHappy.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo.threadConcurrency

import scala.concurrent._
import scala.concurrent.duration._

  def runFlatMap() = {
    task1(1) flatMap {v1 =>
      val a = task2(v1)
      val b = task3(v1)
      a flatMap { v2 =>
        b flatMap { v3 => task4(v2 + v3) }}
    }
  }

  def timeComplete(f: () => Future[Int], name: String) {
    println("Starting " + name)
    val start = System.currentTimeMillis
    val result = Await.result(f(), Duration.Inf)//Duration.Inf 无线期等待,如果为负数没有等待完成
    val time = System.currentTimeMillis - start
    println(name + " returned " + result + " in " + time + " ms.")
  }

  timeComplete(runBlocking, "runBlocking")
  timeComplete(runOnSuccess, "runOnSuccess")
  timeComplete(runFlatMap, "runFlatMap")
  
  // force everything to terminate
  System.exit(0)
} 
Example 9
Source File: AsyncUnhappy.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo.threadConcurrency

import scala.concurrent.{Await, ExecutionContext, Future, Promise}
import scala.concurrent.duration.Duration
import scala.language.postfixOps
import scala.util.{Failure, Success}



  def timeComplete(f: () => Future[Int], name: String) {
    println("Starting " + name)
    val start = System.currentTimeMillis
    val future = f()
    try {
      val result = Await.result(future, Duration.Inf)
      val time = System.currentTimeMillis - start
      println(name + " returned " + result + " in " + time + " ms.")
    } catch {
      case t: Throwable => {
        val time = System.currentTimeMillis - start
        println(name + " threw " + t.getClass.getName + ": " + t.getMessage + " after " + time + " ms.")
      }
    }
  }

  timeComplete(runBlocking, "runBlocking")
  timeComplete(runOnComplete, "runOnComplete")
  timeComplete(runFlatMap, "runFlatMap")
 // timeComplete(runAsync, "runAsync")

  // force everything to terminate
  System.exit(0)
} 
Example 10
Source File: FutureDemo.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo

import java.time.LocalTime

import scala.concurrent.Future
import java.time._
import java.time._
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent._
import ExecutionContext.Implicits.global

object FutureDemo extends App {
  Future {
    Thread.sleep(10000)
    println(s"This is the future at ${LocalTime.now}")
  }
  println(s"This is the present at ${LocalTime.now}")

  Thread.sleep(11000)

  Future { for (i <- 1 to 100) { print("A"); Thread.sleep(10) } }
  Future { for (i <- 1 to 100) { print("B"); Thread.sleep(10) } }

  Thread.sleep(2000)

  val f = Future {
    Thread.sleep(10000)
    42
  }
  println(f)

  Thread.sleep(11000)

  println(f)

  val f23 = Future {
    if (LocalTime.now.getHour > 12)
      throw new Exception("too late")
    42
  }
  Thread.sleep(1000)
  println(f23)


  val f4 = Future { Thread.sleep(10000); 42 }
  val result = Await.result(f4, 11.seconds)
  println(result)

  val f2 = Future { Thread.sleep(10000); 42 }
  Await.ready(f2, 11.seconds)
  val Some(t) = f2.value
  println(t)
} 
Example 11
Source File: GeneratedEvaluationSuite.scala    From iolap   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions.codegen._


class GeneratedEvaluationSuite extends ExpressionEvaluationSuite {
  override def checkEvaluation(
      expression: Expression,
      expected: Any,
      inputRow: Row = EmptyRow): Unit = {
    val plan = try {
      GenerateMutableProjection.generate(Alias(expression, s"Optimized($expression)")() :: Nil)()
    } catch {
      case e: Throwable =>
        val evaluated = GenerateProjection.expressionEvaluator(expression)
        fail(
          s"""
            |Code generation of $expression failed:
            |${evaluated.code.mkString("\n")}
            |$e
          """.stripMargin)
    }

    val actual = plan(inputRow).apply(0)
    if (actual != expected) {
      val input = if (inputRow == EmptyRow) "" else s", input: $inputRow"
      fail(s"Incorrect Evaluation: $expression, actual: $actual, expected: $expected$input")
    }
  }


  test("multithreaded eval") {
    import scala.concurrent._
    import ExecutionContext.Implicits.global
    import scala.concurrent.duration._

    val futures = (1 to 20).map { _ =>
      future {
        GeneratePredicate.generate(EqualTo(Literal(1), Literal(1)))
        GenerateProjection.generate(EqualTo(Literal(1), Literal(1)) :: Nil)
        GenerateMutableProjection.generate(EqualTo(Literal(1), Literal(1)) :: Nil)
        GenerateOrdering.generate(Add(Literal(1), Literal(1)).asc :: Nil)
      }
    }

    futures.foreach(Await.result(_, 10.seconds))
  }
} 
Example 12
Source File: StpPeriodicLogger.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.tools.data.sparql

import akka.actor.ActorRef
import akka.stream.{Attributes, FlowShape, Inlet, Outlet}
import akka.pattern._
import akka.util.Timeout
import akka.stream.stage._
import cmwell.tools.data.ingester.Ingester.IngestEvent
import cmwell.tools.data.sparql.InfotonReporter.{RequestDownloadStats, RequestIngestStats, ResponseDownloadStats, ResponseIngestStats}
import cmwell.tools.data.utils.logging.DataToolsLogging

import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
import scala.concurrent.duration._

object StpPeriodicLogger {
  def apply(infotonReporter: ActorRef, logFrequency: FiniteDuration) =
    new StpPeriodicLogger(infotonReporter, logFrequency)
}

class StpPeriodicLogger(infotonReporter: ActorRef, logFrequency: FiniteDuration)
  extends GraphStage[FlowShape[IngestEvent, IngestEvent]] with DataToolsLogging {

  val in = Inlet[IngestEvent]("StpPeriodicLogger.in")
  val out = Outlet[IngestEvent]("StpPeriodicLogger.out")
  override val shape = FlowShape.of(in, out)

  override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new TimerGraphStageLogic(shape) {

    override def preStart(): Unit = schedulePeriodically(None, logFrequency)

    setHandler(in, new InHandler {
      override def onPush(): Unit = push(out, grab(in))
    })

    setHandler(out, new OutHandler {
      override def onPull(): Unit = pull(in)
    })

    override protected def onTimer(timerKey: Any): Unit = {

      implicit val timeout : akka.util.Timeout = 5.seconds

      for {
        downloadStatsMap <- (infotonReporter ? RequestDownloadStats).mapTo[ResponseDownloadStats]
        ingestStatsObj <- (infotonReporter ? RequestIngestStats).mapTo[ResponseIngestStats]
        ingestStatsOption = ingestStatsObj.stats
      } yield {
        downloadStatsMap.stats.get(SparqlTriggeredProcessor.sparqlMaterializerLabel).foreach(materialized => {
          ingestStatsOption.foreach(ingestStats => {
            logger.info(s" *** STP Agent ${materialized.label}" +
              s" *** Materialized ${materialized.receivedInfotons}, Ingested: ${ingestStats.ingestedInfotons}, Failed: ${ingestStats.failedInfotons}")
          })
        })
      }
    }

  }

} 
Example 13
Source File: DatastreamHandlerUnitSpec.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.scalatest.Inspectors
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import play.api.inject.DefaultApplicationLifecycle
import play.api.libs.json.{JsString, JsValue}
import uk.gov.hmrc.audit.HandlerResult

import scala.concurrent.duration.DurationInt
import scala.concurrent.{ExecutionContext, Future}
import ExecutionContext.Implicits.global

class DatastreamHandlerUnitSpec extends AnyWordSpecLike with Inspectors with Matchers with ScalaFutures {

  val datastreamHandler = new DatastreamHandler(
    scheme         = "http",
    host           = "localhost",
    port           = 1234,
    path           = "/some/path",
    connectTimeout = 2000.millis,
    requestTimeout = 2000.millis,
    userAgent      = "the-micro-service-name",
    materializer   = ActorMaterializer()(ActorSystem()),
    lifecycle      = new DefaultApplicationLifecycle()
    ) {
    override def sendHttpRequest(event: JsValue)(implicit ec: ExecutionContext): Future[HttpResult] =
      Future.successful(HttpResult.Response(event.as[String].toInt))
  }

  "Any Datastream response" should {
    "Return Success for any response code of 204" in {
      val result = datastreamHandler.sendEvent(JsString("204")).futureValue
      result shouldBe HandlerResult.Success
    }

    "Return Failure for any response code of 3XX or 401-412 or 414-499 or 5XX" in {
      forAll((300 to 399) ++ (401 to 412) ++ (414 to 499) ++ (500 to 599)) { code =>
        val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue
        result shouldBe HandlerResult.Failure
      }
    }

    "Return Rejected for any response code of 400 or 413" in {
      forAll(Seq(400, 413)) { code =>
        val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue
        result shouldBe HandlerResult.Rejected
      }
    }
  }
} 
Example 14
Source File: TaskTests.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.util

import utest._

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.duration.Duration

object TaskTests extends TestSuite {

  val tests = Tests {
    'tailRecM {
      import ExecutionContext.Implicits.global

      def countTo(i: Int): Task[Int] =
        Task.tailRecM(0) {
          case x if x >= i => Task.delay(Right(i))
          case toosmall => Task.delay(Left(toosmall + 1))
        }
      countTo(500000).map(_ == 500000).future().map(assert(_))
    }
  }
} 
Example 15
Source File: SSLTests.scala    From lolhttp   with Apache License 2.0 5 votes vote down vote up
package lol.http

import scala.concurrent.{ ExecutionContext }
import ExecutionContext.Implicits.global

class SSLTests extends Tests {

  val App: Service = { request => Ok("Well done") }

  test("SSL over self signed certificate") {
    implicit val trustAll = SSL.trustAll
    withServer(Server.listen(ssl = Some(SSL.selfSigned))(App)) { server =>
      contentString(Get(s"https://localhost:${server.port}/")) should be ("Well done")
    }
  }

  test("insecure connection rejected") {
    withServer(Server.listen(ssl = Some(SSL.selfSigned))(App)) { server =>
      an [Exception] should be thrownBy contentString(Get(s"https://localhost:${server.port}/"))
    }
  }

} 
Example 16
Source File: HttpPatchSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import akka.actor.ActorSystem
import com.typesafe.config.Config
import org.mockito.ArgumentCaptor
import org.mockito.Matchers.{any, eq => is}
import org.mockito.Mockito._
import org.scalatestplus.mockito.MockitoSugar
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{Json, Writes}
import uk.gov.hmrc.http.hooks.{HookData, HttpHook}

import scala.concurrent.{ExecutionContext, Future}

import uk.gov.hmrc.http.HttpReads.Implicits._

class HttpPatchSpec extends AnyWordSpecLike with Matchers with CommonHttpBehaviour {
  import ExecutionContext.Implicits.global

  class StubbedHttpPatch(doPatchResult: Future[HttpResponse], doPatchWithHeaderResult: Future[HttpResponse])
      extends HttpPatch
      with ConnectionTracingCapturing
      with MockitoSugar {
    val testHook1                                   = mock[HttpHook]
    val testHook2                                   = mock[HttpHook]
    val hooks                                       = Seq(testHook1, testHook2)
    override def configuration: Option[Config]      = None
    override protected def actorSystem: ActorSystem = ActorSystem("test-actor-system")

    def doPatch[A](url: String, body: A, headers: Seq[(String, String)])(implicit rds: Writes[A], hc: HeaderCarrier, ec: ExecutionContext) =
      doPatchResult
  }

  "HttpPatch" should {
    val testObject = TestRequestClass("a", 1)
    "be able to return plain responses" in {
      val response  = HttpResponse(200, testBody)
      val testPatch = new StubbedHttpPatch(Future.successful(response), Future.successful(response))
      testPatch.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")).futureValue shouldBe response
    }
    "be able to return objects deserialised from JSON" in {
      val response= Future.successful(HttpResponse(200, """{"foo":"t","bar":10}"""))
      val testPatch = new StubbedHttpPatch(response, response)
      testPatch.PATCH[TestRequestClass, TestClass](url, testObject, Seq("header" -> "foo")).futureValue should be(TestClass("t", 10))
    }

    behave like anErrorMappingHttpCall(
      "PATCH",
      (url, responseF) => new StubbedHttpPatch(responseF, responseF).PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")))
    behave like aTracingHttpCall("PATCH", "PATCH", new StubbedHttpPatch(defaultHttpResponse, defaultHttpResponse)) {
      _.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo"))
    }

    "Invoke any hooks provided" in {
      val dummyResponse       = HttpResponse(200, testBody)
      val dummyResponseFuture = Future.successful(dummyResponse)
      val testPatch           = new StubbedHttpPatch(dummyResponseFuture, dummyResponseFuture)
      val testJson            = Json.stringify(trcreads.writes(testObject))

      testPatch.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")).futureValue

      val respArgCaptor1 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]])
      val respArgCaptor2 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]])

      verify(testPatch.testHook1)
        .apply(is(url), is("PATCH"), is(Some(HookData.FromString(testJson))), respArgCaptor1.capture())(any(), any())
      verify(testPatch.testHook2)
        .apply(is(url), is("PATCH"), is(Some(HookData.FromString(testJson))), respArgCaptor2.capture())(any(), any())

      // verifying directly without ArgumentCaptor didn't work as Futures were different instances
      // e.g. Future.successful(5) != Future.successful(5)
      respArgCaptor1.getValue.futureValue shouldBe dummyResponse
      respArgCaptor2.getValue.futureValue shouldBe dummyResponse
    }
  }
} 
Example 17
Source File: HttpDeleteSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import akka.actor.ActorSystem
import com.typesafe.config.Config
import org.mockito.ArgumentCaptor
import org.mockito.Matchers.{any, eq => is}
import org.mockito.Mockito._
import org.scalatest.concurrent.PatienceConfiguration.{Interval, Timeout}
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.mockito.MockitoSugar
import uk.gov.hmrc.http.hooks.HttpHook

import scala.concurrent.{ExecutionContext, Future}

import uk.gov.hmrc.http.HttpReads.Implicits._

class HttpDeleteSpec extends AnyWordSpecLike with Matchers with MockitoSugar with CommonHttpBehaviour {

  import ExecutionContext.Implicits.global

  class StubbedHttpDelete(doDeleteResult: Future[HttpResponse], doDeleteWithHeaderResult: Future[HttpResponse]) extends HttpDelete with ConnectionTracingCapturing {
    val testHook1                                   = mock[HttpHook]
    val testHook2                                   = mock[HttpHook]
    val hooks                                       = Seq(testHook1, testHook2)
    override def configuration: Option[Config]      = None
    override protected def actorSystem: ActorSystem = ActorSystem("test-actor-system")

    def appName: String = ???

    def doDelete(url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier, ec: ExecutionContext) =
      doDeleteResult
  }

  "HttpDelete" should {
    "be able to return plain responses" in {
      val response   = HttpResponse(200, testBody)
      val testDelete = new StubbedHttpDelete(Future.successful(response), Future.successful(response))
      testDelete.DELETE[HttpResponse](url, Seq("foo" -> "bar")).futureValue shouldBe response
    }

    "be able to return objects deserialised from JSON" in {
      val testDelete = new StubbedHttpDelete(Future.successful(HttpResponse(200, """{"foo":"t","bar":10}""")),
        Future.successful(HttpResponse(200, """{"foo":"t","bar":10}""")))
      testDelete
        .DELETE[TestClass](url, Seq("foo" -> "bar"))
        .futureValue(Timeout(Span(2, Seconds)), Interval(Span(15, Millis))) shouldBe TestClass("t", 10)
    }

    behave like anErrorMappingHttpCall("DELETE", (url, responseF) => new StubbedHttpDelete(responseF, responseF).DELETE[HttpResponse](url, Seq("foo" -> "bar")))
    behave like aTracingHttpCall("DELETE", "DELETE", new StubbedHttpDelete(defaultHttpResponse, defaultHttpResponse)) { _.DELETE[HttpResponse](url, Seq("foo" -> "bar")) }

    "Invoke any hooks provided" in {
      val dummyResponse       = HttpResponse(200, testBody)
      val dummyResponseFuture = Future.successful(dummyResponse)
      val dummyHeader         = Future.successful(dummyResponse)
      val testDelete          = new StubbedHttpDelete(dummyResponseFuture, dummyHeader)

      testDelete.DELETE[HttpResponse](url, Seq("header" -> "foo")).futureValue

      val respArgCaptor1 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]])
      val respArgCaptor2 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]])

      verify(testDelete.testHook1).apply(is(url), is("DELETE"), is(None), respArgCaptor1.capture())(any(), any())
      verify(testDelete.testHook2).apply(is(url), is("DELETE"), is(None), respArgCaptor2.capture())(any(), any())

      // verifying directly without ArgumentCaptor didn't work as Futures were different instances
      // e.g. Future.successful(5) != Future.successful(5)
      respArgCaptor1.getValue.futureValue shouldBe dummyResponse
      respArgCaptor2.getValue.futureValue shouldBe dummyResponse
    }
  }
} 
Example 18
Source File: StreamingContextUtils.scala    From sscheck   with Apache License 2.0 5 votes vote down vote up
package es.ucm.fdi.sscheck.spark.streaming

import scala.concurrent._
import scala.concurrent.duration._
import scala.language.postfixOps
import ExecutionContext.Implicits.global
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.scheduler.{StreamingListener, StreamingListenerReceiverStarted, StreamingListenerBatchCompleted}

// Inline alternative implementation based on SyncVar
object StreamingContextUtils {
  
  def awaitForNBatchesCompleted(numBatches : Int, atMost : scala.concurrent.duration.Duration = 30 seconds)
                               (ssc : StreamingContext) : Unit = {
    val onBatchCompletedSyncVar = new SyncVar[Unit]
    ssc.addStreamingListener(new StreamingListener {
      override def onBatchCompleted(batchCompleted: StreamingListenerBatchCompleted) : Unit =  {  
        if (! onBatchCompletedSyncVar.isSet) {
          // note only this threads makes puts, so no problem with concurrency
          onBatchCompletedSyncVar.put(())
        }
      }
    })
    val waitingForBatches = Future {
      for (_ <- 1 to numBatches) {
        onBatchCompletedSyncVar.take()
      }  
    }
    Await.result(waitingForBatches, atMost)
  }
} 
Example 19
Source File: AsyncGuavaTests.scala    From freestyle   with Apache License 2.0 5 votes vote down vote up
package freestyle.async
package guava

import java.util.concurrent.{Callable, Executors}

import com.google.common.util.concurrent.{ListenableFuture, ListeningExecutorService, MoreExecutors}
import org.scalatest._

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}

class AsyncGuavaTests extends WordSpec with Matchers with Implicits {

  import ExecutionContext.Implicits.global
  import implicits._

  val exception: Throwable = new RuntimeException("Test exception")

  val service: ListeningExecutorService =
    MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10))

  def failedFuture[T]: ListenableFuture[T] =
    service.submit(new Callable[T] {
      override def call(): T = throw exception
    })

  def successfulFuture[T](value: T): ListenableFuture[T] =
    service.submit(new Callable[T] {
      override def call(): T = value
    })

  val foo = "Bar"

  "Guava ListenableFuture Freestyle integration" should {

    "transform guava ListenableFutures into scala.concurrent.Future successfully" in {
      Await.result(listenableFuture2Async(successfulFuture(foo)), Duration.Inf) shouldBe foo
    }

    "recover from failed guava ListenableFutures wrapping them into scala.concurrent.Future" in {
      Await.result(listenableFuture2Async(failedFuture[String]).failed, Duration.Inf) shouldBe exception
    }

    "transform guava ListenableFuture[Void] into scala.concurrent.Future successfully through an implicit conversion" in {
      Await.result(
        listenableFuture2Async(listenableVoidToListenableUnit(successfulFuture[Void](None.orNull))),
        Duration.Inf) shouldBe ((): Unit)
    }

    "recover from failed guava ListenableFuture[Void] wrapping them into scala.concurrent.Future through an implicit conversion" in {
      Await.result(
        listenableFuture2Async(listenableVoidToListenableUnit(failedFuture[Void])).failed,
        Duration.Inf) shouldBe exception
    }

  }

} 
Example 20
Source File: PlayTests.scala    From freestyle   with Apache License 2.0 5 votes vote down vote up
package freestyle.free.http

import org.scalatest.{Matchers, WordSpec}
import javax.inject.Inject
import scala.concurrent.ExecutionContext
import cats.Monad
import freestyle.free._
import freestyle.free.implicits._
import freestyle.free.http.play.implicits._
import _root_.play.api.mvc._
import _root_.play.api.http.{ContentTypeOf, Writeable}
import ExecutionContext.Implicits.global

class PlayTests @Inject()(val controllerComponents: ControllerComponents)
    extends WordSpec
    with Matchers
    with BaseController {

  implicit def unitWr(implicit C: Codec): Writeable[Unit] =
    Writeable(data => C.encode(data.toString))

  implicit val unitCT: ContentTypeOf[Unit] = new ContentTypeOf(Option("text/plain"))

  "Play integration" should {
    import cats.instances.future._

    import algebras._
    import handlers._

    def program[F[_]: Noop]: FreeS[F, Result] =
      Noop[F].noop.map(x => Results.Ok(x))

    "FreeS programs can be used as return value in Play actions" in {
      Action.async { _ =>
        program[Noop.Op]
      } shouldBe an[Action[Result]]
    }

    "FreeS.Par programs can interact with a given request and used as returned values in Play actions" in {
      Action.async { request =>
        Noop[Noop.Op].noop.map(_ => Results.Ok(request.method))
      } shouldBe an[Action[Result]]
    }

    "FreeS sequentials programs can interact with a given request and used as returned values in Play actions" in {
      Action.async { request =>
        Noop[Noop.Op].noop.map(_ => Results.Ok(request.method)).freeS
      } shouldBe an[Action[Result]]
    }
  }
}

object algebras {
  @free
  trait Noop {
    def noop: FS[Unit]
  }
}

object handlers {
  import algebras._

  implicit def noopHandler[M[_]](
      implicit MM: Monad[M]
  ): Noop.Handler[M] = new Noop.Handler[M] {
    def noop: M[Unit] = MM.pure(())
  }
} 
Example 21
Source File: EnpointsTest.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.sttp.client

import com.softwaremill.sttp
import com.softwaremill.sttp.TryHttpURLConnectionBackend
import com.softwaremill.sttp.akkahttp.AkkaHttpBackend
import endpoints4s.algebra.client.{
  BasicAuthTestSuite,
  JsonFromCodecTestSuite,
  TextEntitiesTestSuite,
  SumTypedEntitiesTestSuite,
  EndpointsTestSuite
}
import endpoints4s.algebra.{
  BasicAuthenticationTestApi,
  EndpointsTestApi,
  SumTypedEntitiesTestApi,
  TextEntitiesTestApi
}
import endpoints4s.algebra.playjson.JsonFromPlayJsonCodecTestApi

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

class TestClient[R[_]](address: String, backend: sttp.SttpBackend[R, _])
    extends Endpoints(address, backend)
    with BasicAuthentication[R]
    with JsonEntitiesFromCodecs[R]
    with BasicAuthenticationTestApi
    with EndpointsTestApi
    with JsonFromPlayJsonCodecTestApi
    with SumTypedEntitiesTestApi
    with TextEntitiesTestApi

class EndpointsTestSync
    extends EndpointsTestSuite[TestClient[Try]]
    with BasicAuthTestSuite[TestClient[Try]]
    with JsonFromCodecTestSuite[TestClient[Try]]
    with SumTypedEntitiesTestSuite[TestClient[Try]]
    with TextEntitiesTestSuite[TestClient[Try]] {

  val backend = TryHttpURLConnectionBackend()

  val client: TestClient[Try] =
    new TestClient(s"http://localhost:$wiremockPort", backend)

  def call[Req, Resp](endpoint: client.Endpoint[Req, Resp], args: Req) = {
    Future.fromTry(endpoint(args))
  }

  def encodeUrl[A](url: client.Url[A])(a: A): String = url.encode(a)

  clientTestSuite()
  basicAuthSuite()
  jsonFromCodecTestSuite()
  sumTypedRequestsTestSuite()
  textEntitiesTestSuite()
}

class EndpointsTestAkka
    extends EndpointsTestSuite[TestClient[Future]]
    with BasicAuthTestSuite[TestClient[Future]]
    with JsonFromCodecTestSuite[TestClient[Future]]
    with SumTypedEntitiesTestSuite[TestClient[Future]]
    with TextEntitiesTestSuite[TestClient[Future]] {

  import ExecutionContext.Implicits.global

  val backend = AkkaHttpBackend()

  val client: TestClient[Future] =
    new TestClient(s"http://localhost:$wiremockPort", backend)

  def call[Req, Resp](endpoint: client.Endpoint[Req, Resp], args: Req) =
    endpoint(args)

  def encodeUrl[A](url: client.Url[A])(a: A): String = url.encode(a)

  clientTestSuite()
  basicAuthSuite()
  jsonFromCodecTestSuite()
  sumTypedRequestsTestSuite()
  textEntitiesTestSuite()

  override def afterAll(): Unit = {
    backend.close()
    Thread.sleep(1000) // See https://github.com/softwaremill/sttp/issues/269
    super.afterAll()
  }
} 
Example 22
Source File: EndpointsTest.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.play.client

import endpoints4s.algebra.client
import endpoints4s.algebra
import endpoints4s.algebra.circe
import play.api.libs.ws.WSClient
import play.api.test.WsTestClient

import scala.concurrent.{ExecutionContext, Future}

class TestClient(address: String, wsClient: WSClient)(implicit
    EC: ExecutionContext
) extends Endpoints(address, wsClient)
    with BasicAuthentication
    with JsonEntitiesFromCodecs
    with algebra.BasicAuthenticationTestApi
    with algebra.EndpointsTestApi
    with algebra.JsonFromCodecTestApi
    with algebra.TextEntitiesTestApi
    with algebra.SumTypedEntitiesTestApi
    with circe.JsonFromCirceCodecTestApi
    with circe.JsonEntitiesFromCodecs

class EndpointsTest
    extends client.EndpointsTestSuite[TestClient]
    with client.BasicAuthTestSuite[TestClient]
    with client.JsonFromCodecTestSuite[TestClient]
    with client.SumTypedEntitiesTestSuite[TestClient]
    with client.TextEntitiesTestSuite[TestClient] {

  import ExecutionContext.Implicits.global

  val wsClient = new WsTestClient.InternalWSClient("http", wiremockPort)
  val client: TestClient =
    new TestClient(s"http://localhost:$wiremockPort", wsClient)

  def call[Req, Resp](
      endpoint: client.Endpoint[Req, Resp],
      args: Req
  ): Future[Resp] = endpoint(args)

  def encodeUrl[A](url: client.Url[A])(a: A): String = url.encode(a)

  clientTestSuite()
  basicAuthSuite()
  jsonFromCodecTestSuite()
  sumTypedRequestsTestSuite()
  textEntitiesTestSuite()

  override def afterAll(): Unit = {
    wsClient.close()
    Thread.sleep(
      6000
    ) // Unfortunate hack to let WSTestClient terminate its ActorSystem. See https://github.com/playframework/playframework/blob/8b0d5afb8c353dd8cd8d9e8057136e1858ad0173/transport/client/play-ahc-ws/src/main/scala/play/api/test/WSTestClient.scala#L142
    super.afterAll()
  }

} 
Example 23
Source File: FutureEffect.scala    From Learn-Scala-Programming   with MIT License 5 votes vote down vote up
package ch06

import scala.concurrent.{ExecutionContext, Future}
import ExecutionContext.Implicits.global
import scala.util.{Failure, Success, Try}

trait FutureEffect {
  val runningForever = Future {
    while (true) Thread.sleep(1000)
  }
  val stringFuture = Future.successful("Well")
  val failure = Future.failed(new IllegalArgumentException)
  val fromTry = Future.fromTry(Try(10 / 0))

  val runningLong = Future.unit.map { _ =>
    while (math.random() > 0.001) Thread.sleep(100)
  }

  runningLong.foreach(_ => println("First callback"))
  runningLong.foreach(_ => println("Second callback"))

  runningLong.onComplete {
    case Success(value) => println(s"Success with $value")
    case Failure(ex) => println(s"Failure with $ex")
  }

  stringFuture.transform(_.length, ex => new Exception(ex))
  stringFuture.transform {
    case Success(value) => Success(value.length)
    case Failure(ex) => Failure(new Exception(ex))
  }

  stringFuture.filter(_.length > 10)

  stringFuture.collect {
    case s if s.length > 10 => s.toUpperCase
  }

  if (runningLong.isCompleted) runningLong.value

  runningForever.value match {
    case Some(Success(value)) => println(s"Finished successfully with $value")
    case Some(Failure(exception)) => println(s"Failed with $exception")
    case None => println("Still running")
  }

}

trait FishingFutureExample {
  import Effects._

  trait plain {
    val buyBait: String => Bait
    val makeBait: String => Bait
    val castLine: Bait => Line
    val hookFish: Line => Fish
    def goFishing(bestBaitForFish: Future[String]): Future[Fish] =
      bestBaitForFish.map(buyBait).map(castLine).map(hookFish)
  }

  trait flat {
    val buyBait: String => Future[Bait]
    val makeBait: String => Future[Bait]
    val castLine: Bait => Future[Line]
    val hookFish: Line => Future[Fish]

    def goFishing(bestBaitForFish: Future[String]): Future[Fish] = for {
      baitName <- bestBaitForFish
      bait <- buyBait(baitName).fallbackTo(makeBait(baitName))
      line <- castLine(bait)
      fish <- hookFish(line)
    } yield fish

  }

} 
Example 24
Source File: TestUtil.scala    From mleap   with Apache License 2.0 5 votes vote down vote up
package ml.combust.mleap.grpc.server

import java.io.File
import java.net.URI

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory
import io.grpc.{ManagedChannel, Server}
import io.grpc.inprocess.{InProcessChannelBuilder, InProcessServerBuilder}
import ml.combust.mleap.executor.MleapExecutor
import ml.combust.mleap.grpc.GrpcClient
import ml.combust.mleap.pb.MleapGrpc
import ml.combust.mleap.pb.MleapGrpc.MleapStub
import ml.combust.mleap.runtime.frame.DefaultLeapFrame
import ml.combust.mleap.runtime.serialization.FrameReader

import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
import scala.util.Try

object TestUtil {

  lazy val lrUri: URI = URI.create(getClass.getClassLoader.getResource("models/airbnb.model.lr.zip").toURI.toString)

  lazy val frame: Try[DefaultLeapFrame] =
    FrameReader().read(new File(getClass.getClassLoader.getResource("leap_frame/frame.airbnb.json").getFile))

  lazy val uniqueServerName : String = "in-process server for " + getClass

  def createServer(system: ActorSystem) : Server = {
    val config = new GrpcServerConfig(ConfigFactory.load().getConfig("ml.combust.mleap.grpc.server.default"))
    val ssd = MleapGrpc.bindService(new GrpcServer(MleapExecutor(system), config)(global, ActorMaterializer.create(system)), global)
    val builder = InProcessServerBuilder.forName(uniqueServerName)
    builder.directExecutor().addService(ssd).intercept(new ErrorInterceptor)
    val server = builder.build
    server.start()
    server
  }

  def createClient(channel: ManagedChannel): GrpcClient = new GrpcClient(new MleapStub(channel))

  def inProcessChannel : ManagedChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor.build

} 
Example 25
Source File: NonBlockingMutexSpec.scala    From wookiee   with Apache License 2.0 5 votes vote down vote up
package com.webtrends.harness.libs.concurrent

import scala.language.reflectiveCalls

import org.specs2.mutable._
import java.util.concurrent.atomic.AtomicInteger
import scala.concurrent.{ ExecutionContext, Promise, Future, Await }
import scala.concurrent.duration.{ Duration, SECONDS }

object NonBlockingMutexSpec extends Specification {

  val waitTime = Duration(2, SECONDS)

  trait Tester {
    def run(body: => Unit): Unit
  }

  class MutexTester extends Tester {
    val mutex = new NonBlockingMutex()
    def run(body: => Unit) = mutex.exclusive(body)
  }

  class NaiveTester extends Tester {
    def run(body: => Unit) = body
  }

  def countOrderingErrors(runs: Int, tester: Tester)(implicit ec: ExecutionContext): Future[Int] = {
    val result = Promise[Int]()
    val runCount = new AtomicInteger(0)
    val orderingErrors = new AtomicInteger(0)

    for (i <- 0 until runs) {
      tester.run {
        val observedRunCount = runCount.getAndIncrement()

        // We see observedRunCount != i then this task was run out of order
        if (observedRunCount != i) {
          orderingErrors.incrementAndGet() // Record the error
        }
        // If this is the last task, complete our result promise
        if ((observedRunCount + 1) >= runs) {
          result.success(orderingErrors.get)
        }
      }
    }
    result.future
  }

  "NonBlockingMutex" should {

    "run a single operation" in {
      val p = Promise[Int]()
      val mutex = new NonBlockingMutex()
      mutex.exclusive { p.success(1) }
      Await.result(p.future, waitTime) must_== (1)
    }

    "run two operations" in {
      val p1 = Promise[Unit]()
      val p2 = Promise[Unit]()
      val mutex = new NonBlockingMutex()
      mutex.exclusive { p1.success(()) }
      mutex.exclusive { p2.success(()) }
      Await.result(p1.future, waitTime) must_== (())
      Await.result(p2.future, waitTime) must_== (())
    }

    "run code in order" in {
      import ExecutionContext.Implicits.global

      def percentageOfRunsWithOrderingErrors(runSize: Int, tester: Tester): Int = {
        val results: Seq[Future[Int]] = for (i <- 0 until 9) yield {
          countOrderingErrors(runSize, tester)
        }
        Await.result(Future.sequence(results), waitTime).filter(_ > 0).size * 10
      }

      // Iteratively increase the run size until we get observable errors 90% of the time
      // We want a high error rate because we want to then use the MutexTester
      // on the same run size and know that it is fixing up some problems. If the run size
      // is too small then the MutexTester probably isn't doing anything. We use
      // dynamic run sizing because the actual size that produces errors will vary
      // depending on the environment in which this test is run.
      var runSize = 8 // This usually reaches 8192 on my dev machine with 10 simultaneous queues
      var errorPercentage = 0
      while (errorPercentage < 90 && runSize < 1000000) {
        runSize = runSize << 1
        errorPercentage = percentageOfRunsWithOrderingErrors(runSize, new NaiveTester())
      }
      //println(s"Got $errorPercentage% ordering errors on run size of $runSize")

      // Now show that this run length works fine with the MutexTester
      percentageOfRunsWithOrderingErrors(runSize, new MutexTester()) must_== 0
    }

  }

} 
Example 26
Source File: package.scala    From Principles-of-Reactive-Programming   with GNU General Public License v3.0 5 votes vote down vote up
import scala.language.postfixOps
import scala.io.StdIn
import scala.util._
import scala.util.control.NonFatal
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.async.Async.{async, await}


    def apply() = new CancellationTokenSource {
      val p = Promise[Unit]()
      val cancellationToken = new CancellationToken {
        def isCancelled = p.future.value != None
      }
      def unsubscribe() {
        p.trySuccess(())
      }
    }
  }
} 
Example 27
Source File: Search.scala    From Principles-of-Reactive-Programming   with GNU General Public License v3.0 5 votes vote down vote up
package suggestions
package search

import org.json4s._
import scala.concurrent.{ ExecutionContext, Future, Promise }
import ExecutionContext.Implicits.global
import scala.language.postfixOps
import scala.collection._
import scala.collection.JavaConverters._
import scala.util.Try
import scala.async.Async._

import rx.lang.scala.Observable
import observablex.{SchedulerEx, ObservableEx}
import ObservableEx._

import dispatch._
import org.json4s.native._
import retrofit.http.{GET, Query}
import retrofit.Callback
import retrofit.client.Response
import retrofit.{RetrofitError, Callback, RestAdapter}
import com.google.gson.annotations.SerializedName

object Search {

  
  trait WikipediaService {
    @GET("/w/api.php?action=opensearch&format=json&limit=15")
    def suggestions(@Query("search") term: String, callback: Callback[Array[AnyRef]]): Unit

    @GET("/w/api.php?action=parse&format=json&prop=text&section=0")
    def page(@Query("page") term: String, callback: Callback[Page]): Unit
  }

  val restAdapter = new RestAdapter.Builder().setEndpoint("https://en.wikipedia.org").build()

  val service = restAdapter.create(classOf[WikipediaService])

  def callbackFuture[T]: (Callback[T], Future[T]) = {
    val p = Promise[T]()
    val cb = new Callback[T] {
      def success(t: T, response: Response) = {
        p success t
      }
      def failure(error: RetrofitError) = {
        p failure error
      }
    }

    (cb, p.future)
  }

  def wikipediaSuggestionRetrofit(term: String): Future[List[String]] = {
    async {
      val (cb, f) = callbackFuture[Array[AnyRef]]
      service.suggestions(term, cb)
      val result = await { f }
      val arraylist = result(1).asInstanceOf[java.util.List[String]]

      arraylist.asScala.toList
    }
  }

  def wikipediaPageRetrofit(term: String): Future[String] = {
    async {
      val (cb, f) = callbackFuture[Page]
      service.page(term, cb)
      val result = await { f }
      result.parse.text.all
    }
  }

  def wikipediaSuggestion(term: String): Future[List[String]] = wikipediaSuggestionRetrofit(term)

  def wikipediaPage(term: String): Future[String] = wikipediaPageRetrofit(term)

}