scala.concurrent.ExecutionContext.global Scala Examples

The following examples show how to use scala.concurrent.ExecutionContext.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: ServerSubscriberStressTest.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.grpc.adapter.server.rs

import com.daml.grpc.adapter.TestExecutionSequencerFactory
import org.reactivestreams.tck.flow.support.HelperPublisher
import org.scalatest.concurrent.AsyncTimeLimitedTests
import org.scalatest.time.Span
import org.scalatest.time.SpanSugar._
import org.scalatest.{Assertion, AsyncWordSpec, BeforeAndAfterEach, Matchers}

import scala.concurrent.ExecutionContext.global
import scala.concurrent.Future

class ServerSubscriberStressTest
    extends AsyncWordSpec
    with BeforeAndAfterEach
    with Matchers
    with AsyncTimeLimitedTests {

  private val elemCount = 10000
  private val testRunCount = 50
  private val expectedElemRange = 0.until(elemCount)

  var serverCallStreamObserver: MockServerCallStreamObserver[Int] = _
  var sut: ServerSubscriber[Int] = _
  var helperPublisher: HelperPublisher[Int] = _

  override protected def beforeEach(): Unit = {
    serverCallStreamObserver = new MockServerCallStreamObserver[Int]
    val executor = TestExecutionSequencerFactory.instance.getExecutionSequencer
    sut = new ServerSubscriber[Int](serverCallStreamObserver, executor)
    helperPublisher = new HelperPublisher[Int](0, elemCount, i => i, global)
  }

  "ServerSubscriber" should {

    for (i <- 1.to(testRunCount)) {

      s"work with $elemCount elements when they are requested one by one (test run #$i)" in {
        helperPublisher.subscribe(sut)
        expectedElemRange.foreach(_ => serverCallStreamObserver.demandResponse())
        verifyExpectedElementsArrivedInOrder()
      }

    }
    for (i <- 1.to(testRunCount)) {

      s"work with $elemCount elements when they are requested in bulk (isReady stays true) (test run #$i)" in {
        helperPublisher.subscribe(sut)
        serverCallStreamObserver.demandResponse(elemCount)
        verifyExpectedElementsArrivedInOrder()
      }
    }
  }

  private def verifyExpectedElementsArrivedInOrder(): Future[Assertion] = {
    serverCallStreamObserver.elementsWhenCompleted.map { receivedElements =>
      receivedElements should contain theSameElementsInOrderAs expectedElemRange
    }
  }

  override def timeLimit: Span = 10.seconds
} 
Example 2
Source File: FTracingSpec.scala    From opencensus-scala   with Apache License 2.0 5 votes vote down vote up
package io.opencensus.scala.doobie

import cats.effect.{ContextShift, IO}
import io.opencensus.scala.Tracing
import io.opencensus.scala.http.testSuite.MockTracing
import io.opencensus.trace.{BlankSpan, Status}
import org.scalatest.{OptionValues, Outcome}

import scala.concurrent.ExecutionContext.global
import scala.util.Try
import org.scalatest.flatspec
import org.scalatest.matchers.should.Matchers

class FTracingSpec
    extends flatspec.FixtureAnyFlatSpec
    with Matchers
    with OptionValues {

  implicit val cs: ContextShift[IO] = IO.contextShift(global)

  case class TestInput(fTracing: FTracing[IO], mock: MockTracing)
  override protected def withFixture(test: OneArgTest): Outcome =
    test(clientTracingWithMock())

  override type FixtureParam = TestInput

  behavior of "FTracingSpec"

  it should "start with the correct name" in { f =>
    f.fTracing.traceF(IO(()), "testSpan", None).unsafeRunSync()
    f.mock.startedSpans should have size 1
    f.mock.startedSpans.head.name shouldBe "testSpan"
  }

  it should "trace with parent Span" in { f =>
    val parentSpan = BlankSpan.INSTANCE

    f.fTracing.traceF(IO(()), "testSpan", Some(parentSpan)).unsafeRunSync()
    f.mock.startedSpans should have size 1
    f.mock.startedSpans.head.parentContext.value shouldBe parentSpan.getContext
  }

  it should "stop after normal exit" in { f =>
    f.fTracing.traceF(IO(()), "testSpan", None).unsafeRunSync()
    f.mock.endedSpans should have size 1
    f.mock.endedSpans.head._2.value.getCanonicalCode shouldBe Status.OK.getCanonicalCode
  }

  it should "stop after error" in { f =>
    Try(
      f.fTracing
        .traceF(IO.raiseError(new Exception("TEST")), "testSpan", None)
        .unsafeRunSync()
    )
    f.mock.endedSpans should have size 1
    f.mock.endedSpans.head._2.value.getCanonicalCode shouldBe Status.INTERNAL.getCanonicalCode
  }

  def clientTracingWithMock() = {
    val mockTracing = new MockTracing
    val fTracing = new FTracing[IO] {
      override protected val tracing: Tracing = mockTracing
    }
    TestInput(fTracing, mockTracing)
  }
} 
Example 3
Source File: Http4sClientEndpointsJsonSchemaTest.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.http4s.client

import endpoints4s.algebra
import endpoints4s.algebra.client

import cats.effect.Sync
import org.http4s.client.Client
import cats.effect.IO
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.global
import org.http4s.client.asynchttpclient.AsyncHttpClient
import cats.effect.ContextShift
import endpoints4s.algebra.circe
import org.http4s.Uri

class TestJsonSchemaClient[F[_]: Sync](host: Uri, client: Client[F])
    extends Endpoints[F](host, client)
    with BasicAuthentication
    with JsonEntitiesFromCodecs
    with algebra.BasicAuthenticationTestApi
    with algebra.EndpointsTestApi
    with algebra.JsonFromCodecTestApi
    with algebra.SumTypedEntitiesTestApi
    with circe.JsonFromCirceCodecTestApi
    with circe.JsonEntitiesFromCodecs

class Http4sClientEndpointsJsonSchemaTest
    extends client.EndpointsTestSuite[TestJsonSchemaClient[IO]]
    with client.BasicAuthTestSuite[TestJsonSchemaClient[IO]]
    with client.JsonFromCodecTestSuite[TestJsonSchemaClient[IO]]
    with client.SumTypedEntitiesTestSuite[TestJsonSchemaClient[IO]] {

  implicit val ctx: ContextShift[IO] = IO.contextShift(global)

  val (ahc, shutdown) =
    AsyncHttpClient.allocate[IO]().unsafeRunSync()

  val client = new TestJsonSchemaClient[IO](
    Uri.unsafeFromString(s"http://localhost:$wiremockPort"),
    ahc
  )

  def call[Req, Resp](
      endpoint: client.Endpoint[Req, Resp],
      args: Req
  ): Future[Resp] = {
    Thread.sleep(50)
    val eventualResponse = endpoint(args)
    Thread.sleep(50)
    eventualResponse.unsafeToFuture()
  }

  def encodeUrl[A](url: client.Url[A])(a: A): String =
    url.encodeUrl(a).toOption.get.renderString

  clientTestSuite()
  basicAuthSuite()
  jsonFromCodecTestSuite()

  override def afterAll(): Unit = {
    shutdown.unsafeRunSync()
    super.afterAll()
  }

} 
Example 4
Source File: LinebackerSpec.scala    From linebacker   with MIT License 5 votes vote down vote up
package io.chrisdavenport.linebacker

import org.specs2._
import cats.effect._
import cats.implicits._
import java.lang.Thread
import scala.concurrent.ExecutionContext
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.{Executors, ThreadFactory}
import _root_.io.chrisdavenport.linebacker.contexts.{Executors => E}
import scala.concurrent.ExecutionContext.global

class LinebackerSpec extends Spec {
  override def is = s2"""
  Threads Run On Linebacker $runsOnLinebacker
  Threads Afterwards Run on Provided EC $runsOffLinebackerAfterwards
  """

  def runsOnLinebacker = {
    val testRun = E
      .unbound[IO]
      .map(Linebacker.fromExecutorService[IO])
      .use { implicit linebacker =>
        implicit val cs = IO.contextShift(global)
        Linebacker[IO].blockContextShift(IO(Thread.currentThread().getName))
      }

    testRun.unsafeRunSync must_=== "linebacker-thread-0"
  }

  def runsOffLinebackerAfterwards = {
    val executor = Executors.newCachedThreadPool(new ThreadFactory {
      private val counter = new AtomicLong(0L)

      def newThread(r: Runnable) = {
        val th = new Thread(r)
        th.setName("test-ec-" + counter.getAndIncrement.toString)
        th.setDaemon(true)
        th
      }
    })
    implicit val ec = ExecutionContext
      .fromExecutorService(executor)

    implicit val linebacker = Linebacker.fromExecutionContext[IO](global) // Block Onto Global
    implicit val cs = IO.contextShift(ec) // Should return to custom

    val testRun = Linebacker[IO].blockContextShift(IO.unit) *>
      IO(Thread.currentThread().getName) <*
      IO(executor.shutdownNow)

    testRun.unsafeRunSync must_=== "test-ec-0"
  }
} 
Example 5
Source File: catzQueueSpec.scala    From interop-cats   with Apache License 2.0 5 votes vote down vote up
package zio.interop

import cats.effect.{ Effect, Concurrent, ContextShift, IO => CIO }
import cats.implicits._
import zio.test.Assertion._
import zio.test._
import zio.test.interop.catz.test._
import zio.{ Runtime, ZEnv }
import zio.internal.Platform

import scala.concurrent.ExecutionContext.global

object catzQueueSpec extends DefaultRunnableSpec {

  def boundedQueueTest[F[+_]](implicit F: Concurrent[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q  <- Queue.bounded[F, Int](1)
      _  <- q.offer(1)
      r1 <- q.takeAll
      _  <- q.offer(2)
      r2 <- q.takeAll
    } yield assert(r1)(equalTo(List(1))) && assert(r2)(equalTo(List(2)))

  def droppingQueueTest[F[+_]](implicit F: Concurrent[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q <- Queue.dropping[F, Int](2)
      _ <- q.offerAll(List(1, 2, 3))
      r <- q.takeAll
    } yield assert(r)(equalTo(List(1, 2)))

  def slidingQueueTest[F[+_]](implicit F: Concurrent[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q <- Queue.sliding[F, Int](2)
      _ <- q.offerAll(List(1, 2, 3, 4))
      r <- q.takeAll
    } yield assert(r)(equalTo(List(3, 4)))

  def unboundedQueueTest[F[+_]](implicit F: Concurrent[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q        <- Queue.unbounded[F, Int]
      expected = Range.inclusive(0, 100)
      _        <- q.offerAll(expected)
      actual   <- q.takeAll
    } yield assert(actual)(equalTo(expected.toList))

  def contramapQueueTest[F[+_]](implicit F: Concurrent[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q        <- Queue.unbounded[F, String]
      q1       = q.contramap((i: Int) => i.toString)
      data     = Range.inclusive(0, 100)
      _        <- q1.offerAll(data)
      actual   <- q1.takeAll
      expected = data.map(_.toString)
    } yield assert(actual)(equalTo(expected.toList))

  def mapMQueueTest[F[+_]](implicit F: Effect[F], R: Runtime[ZEnv]): F[TestResult] =
    for {
      q        <- Queue.unbounded[F, Int]
      q1       = q.mapM(i => F.pure(i.toString))
      data     = Range.inclusive(0, 100)
      _        <- q1.offerAll(data)
      actual   <- q1.takeAll
      expected = data.map(_.toString)
    } yield assert(actual)(equalTo(expected.toList))

  def spec = suite("catzQueueSpec")(
    testF("can use a bounded queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      boundedQueueTest[CIO]
    },
    testF("can use a dropping queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      droppingQueueTest[CIO]
    },
    testF("can use a sliding queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      slidingQueueTest[CIO]
    },
    testF("can use an unbounded queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      unboundedQueueTest[CIO]
    },
    testF("can contramap a queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      contramapQueueTest[CIO]
    },
    testF("can mapM a queue from Cats Effect IO") {
      implicit val r: Runtime[ZEnv]     = Runtime.unsafeFromLayer(ZEnv.live, Platform.default)
      implicit val c: ContextShift[CIO] = CIO.contextShift(global)
      mapMQueueTest[CIO]
    }
  )
} 
Example 6
Source File: Statics.scala    From event-sourcing-kafka-streams   with MIT License 5 votes vote down vote up
package org.amitayh.invoices.web

import cats.effect.{ContextShift, Sync}
import org.http4s.dsl.Http4sDsl
import org.http4s.{HttpRoutes, StaticFile}

import scala.concurrent.ExecutionContext.global

class Statics[F[_]: Sync: ContextShift] extends Http4sDsl[F] {

  val service: HttpRoutes[F] = HttpRoutes.of[F] {
    case request @ GET -> fileName =>
      StaticFile
        .fromResource(
          name = s"/statics$fileName",
          blockingExecutionContext = global,
          req = Some(request),
          preferGzipped = true)
        .getOrElseF(NotFound())
  }

}

object Statics {
  def apply[F[_]: Sync: ContextShift]: Statics[F] = new Statics[F]
} 
Example 7
Source File: ListProjector.scala    From event-sourcing-kafka-streams   with MIT License 5 votes vote down vote up
package org.amitayh.invoices.projector

import java.util.UUID

import cats.effect.concurrent.Deferred
import cats.effect.{ContextShift, IO}
import cats.syntax.apply._
import org.amitayh.invoices.common.Config
import org.amitayh.invoices.common.domain.InvoiceSnapshot
import org.amitayh.invoices.common.serde.AvroSerde.SnapshotSerde
import org.amitayh.invoices.common.serde.UuidSerde
import org.amitayh.invoices.dao.{InvoiceList, InvoiceRecord, MySqlInvoiceList}
import org.amitayh.invoices.streamprocessor.StreamProcessorApp
import org.apache.kafka.streams.kstream.{Consumed, ForeachAction, KeyValueMapper}
import org.apache.kafka.streams.{KeyValue, StreamsBuilder, Topology}

import scala.concurrent.ExecutionContext.global

object ListProjector extends StreamProcessorApp {

  override def appId: String = "invoices.processor.list-projector"

  override def topology: Topology = ListProjectorTopology.create.unsafeRunSync()

}

object ListProjectorTopology {
  implicit val contextShift: ContextShift[IO] = IO.contextShift(global)

  def create: IO[Topology] = for {
    deferred <- Deferred[IO, Topology]
    _ <- MySqlInvoiceList.resource[IO].use { invoiceList =>
      buildTopology(invoiceList).flatMap(deferred.complete) *> IO.never
    }.start
    topology <- deferred.get
  } yield topology

  private def buildTopology(invoiceList: InvoiceList[IO]): IO[Topology] = IO {
    val builder = new StreamsBuilder

    val snapshots = builder.stream(
      Config.Topics.Snapshots.name,
      Consumed.`with`(UuidSerde, SnapshotSerde))

    snapshots
      .map[UUID, InvoiceRecord](ToRecord)
      .foreach(new SaveInvoiceRecord(invoiceList))

    builder.build()
  }
}

object ToRecord extends KeyValueMapper[UUID, InvoiceSnapshot, KeyValue[UUID, InvoiceRecord]] {
  override def apply(id: UUID, snapshot: InvoiceSnapshot): KeyValue[UUID, InvoiceRecord] =
    KeyValue.pair(id, InvoiceRecord(id, snapshot))
}

class SaveInvoiceRecord(invoicesList: InvoiceList[IO])
  extends ForeachAction[UUID, InvoiceRecord] {

  override def apply(id: UUID, value: InvoiceRecord): Unit =
    invoicesList.save(value).unsafeRunSync()

}