scala.concurrent.duration.DurationInt Scala Examples
The following examples show how to use scala.concurrent.duration.DurationInt.
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: KafkaTopicInfo.scala From matcher with MIT License | 7 votes |
package tools import java.io.File import akka.actor.ActorSystem import com.typesafe.config.ConfigFactory import com.wavesplatform.dex.queue.KafkaMatcherQueue.eventDeserializer import com.wavesplatform.dex.queue.{QueueEvent, QueueEventWithMeta} import com.wavesplatform.dex.settings.toConfigOps import org.apache.kafka.clients.consumer.KafkaConsumer import org.apache.kafka.common.TopicPartition import org.apache.kafka.common.serialization.StringDeserializer import scala.collection.JavaConverters._ import scala.concurrent.duration.DurationInt object KafkaTopicInfo extends App { implicit val system: ActorSystem = ActorSystem() val configFile = new File(args(0)) val topic = args(1) val from = args(2).toLong val max = args(3).toInt println(s"""configFile: ${configFile.getAbsolutePath} |topic: $topic |from: $from |max: $max""".stripMargin) val requestTimeout = java.time.Duration.ofNanos(5.seconds.toNanos) val config = ConfigFactory .parseString("""waves.dex.events-queue.kafka.consumer.client { | client.id = "kafka-topics-info" | enable.auto.commit = false | auto.offset.reset = earliest |} | |""".stripMargin) .withFallback { ConfigFactory .parseFile(configFile) .withFallback(ConfigFactory.defaultApplication()) .withFallback(ConfigFactory.defaultReference()) .resolve() .getConfig("waves.dex.events-queue.kafka") } val consumer = new KafkaConsumer[String, QueueEvent]( config.getConfig("waves.dex.events-queue.kafka.consumer.client").toProperties, new StringDeserializer, eventDeserializer ) try { val topicPartition = new TopicPartition(topic, 0) val topicPartitions = java.util.Collections.singletonList(topicPartition) consumer.assign(topicPartitions) { val r = consumer.partitionsFor(topic, requestTimeout) println(s"Partitions:\n${r.asScala.mkString("\n")}") } { val r = consumer.endOffsets(topicPartitions, requestTimeout) println(s"End offsets for $topicPartition: ${r.asScala.mkString(", ")}") } consumer.seek(topicPartition, from) val pollDuriation = java.time.Duration.ofNanos(1.seconds.toNanos) val lastOffset = from + max var continue = true while (continue) { println(s"Reading from Kafka") val xs = consumer.poll(pollDuriation).asScala.toVector xs.foreach { msg => println(QueueEventWithMeta(msg.offset(), msg.timestamp(), msg.value())) } xs.lastOption.foreach { x => if (x.offset() == lastOffset) continue = false } } } finally { consumer.close() } }
Example 2
Source File: InfiniteRetries.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.perf import akka.actor.ActorSystem import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} trait InfiniteRetries { protected def retry[T](action: => Future[T], delay: FiniteDuration = 10.millis)( implicit system: ActorSystem): Future[T] = { implicit val ec: ExecutionContext = system.dispatcher action.transformWith { case Success(v) => Future.successful(v) case Failure(t) => val p = Promise[T]() system.scheduler.scheduleOnce( delay, () => retry[T](action, delay).onComplete { case Success(s) => p.success(s) case Failure(throwable) => p.failure(throwable) } ) p.future } } }
Example 3
Source File: CommandConfiguration.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.configuration import scala.concurrent.duration.{DurationInt, FiniteDuration} final case class CommandConfiguration( inputBufferSize: Int, maxParallelSubmissions: Int, maxCommandsInFlight: Int, limitMaxCommandsInFlight: Boolean, retentionPeriod: FiniteDuration ) object CommandConfiguration { lazy val default: CommandConfiguration = CommandConfiguration( inputBufferSize = 512, maxParallelSubmissions = 512, maxCommandsInFlight = 256, limitMaxCommandsInFlight = true, retentionPeriod = 24.hours ) }
Example 4
Source File: IndexerConfig.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.indexer import com.daml.ledger.participant.state.v1.ParticipantId import com.daml.platform.configuration.IndexConfiguration import com.daml.platform.indexer.IndexerConfig._ import scala.concurrent.duration.{DurationInt, FiniteDuration} case class IndexerConfig( participantId: ParticipantId, jdbcUrl: String, startupMode: IndexerStartupMode, restartDelay: FiniteDuration = DefaultRestartDelay, eventsPageSize: Int = IndexConfiguration.DefaultEventsPageSize, allowExistingSchema: Boolean = false, ) object IndexerConfig { val DefaultRestartDelay: FiniteDuration = 10.seconds }
Example 5
Source File: FlywayMigrations.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.store import com.daml.logging.{ContextualizedLogger, LoggingContext} import com.daml.platform.configuration.ServerRole import com.daml.platform.store.FlywayMigrations._ import com.daml.platform.store.dao.HikariConnection import com.daml.resources.ResourceOwner import com.zaxxer.hikari.HikariDataSource import org.flywaydb.core.Flyway import org.flywaydb.core.api.MigrationVersion import org.flywaydb.core.api.configuration.FluentConfiguration import scala.concurrent.duration.DurationInt import scala.concurrent.{ExecutionContext, Future} class FlywayMigrations(jdbcUrl: String)(implicit logCtx: LoggingContext) { private val logger = ContextualizedLogger.get(this.getClass) private val dbType = DbType.jdbcType(jdbcUrl) def validate()(implicit executionContext: ExecutionContext): Future[Unit] = dataSource.use { ds => Future { val flyway = configurationBase(dbType).dataSource(ds).load() logger.info("Running Flyway validation...") flyway.validate() logger.info("Flyway schema validation finished successfully.") } } def migrate(allowExistingSchema: Boolean = false)( implicit executionContext: ExecutionContext ): Future[Unit] = dataSource.use { ds => Future { val flyway = configurationBase(dbType) .dataSource(ds) .baselineOnMigrate(allowExistingSchema) .baselineVersion(MigrationVersion.fromVersion("0")) .load() logger.info("Running Flyway migration...") val stepsTaken = flyway.migrate() logger.info(s"Flyway schema migration finished successfully, applying $stepsTaken steps.") } } def reset()(implicit executionContext: ExecutionContext): Future[Unit] = dataSource.use { ds => Future { val flyway = configurationBase(dbType).dataSource(ds).load() logger.info("Running Flyway clean...") flyway.clean() logger.info("Flyway schema clean finished successfully.") } } private def dataSource: ResourceOwner[HikariDataSource] = HikariConnection.owner( serverRole = ServerRole.IndexMigrations, jdbcUrl = jdbcUrl, minimumIdle = 2, maxPoolSize = 2, connectionTimeout = 250.millis, metrics = None, ) } object FlywayMigrations { def configurationBase(dbType: DbType): FluentConfiguration = Flyway.configure().locations("classpath:db/migration/" + dbType.name) }
Example 6
Source File: PostgresIT.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.persistence import com.codahale.metrics.{MetricRegistry, SharedMetricRegistries} import com.daml.dec.DirectExecutionContext import com.daml.logging.LoggingContext.newLoggingContext import com.daml.metrics.Metrics import com.daml.platform.configuration.ServerRole import com.daml.platform.store.FlywayMigrations import com.daml.platform.store.dao.{HikariJdbcConnectionProvider, JdbcConnectionProvider} import com.daml.resources.Resource import com.daml.testing.postgresql.PostgresAroundAll import org.scalatest.{AsyncWordSpec, BeforeAndAfterAll, Matchers} import scala.concurrent.Await import scala.concurrent.duration.DurationInt class PostgresIT extends AsyncWordSpec with Matchers with PostgresAroundAll with BeforeAndAfterAll { private var connectionProviderResource: Resource[JdbcConnectionProvider] = _ private var connectionProvider: JdbcConnectionProvider = _ private val metrics = new Metrics(SharedMetricRegistries.getOrCreate("PostgresIT")) override def beforeAll(): Unit = { super.beforeAll() newLoggingContext { implicit logCtx => connectionProviderResource = HikariJdbcConnectionProvider .owner( ServerRole.Testing(getClass), postgresDatabase.url, maxConnections = 4, new MetricRegistry, ) .acquire()(DirectExecutionContext) connectionProvider = Await.result(connectionProviderResource.asFuture, 10.seconds) } } override protected def afterAll(): Unit = { Await.result(connectionProviderResource.release(), 10.seconds) super.afterAll() } "Postgres" when { "running queries using Hikari" should { "be accessible" in { connectionProvider.runSQL(metrics.test.db) { conn => val resultSet = conn.createStatement().executeQuery("SELECT 1") resultSet.next() val result = resultSet.getInt(1) result shouldEqual 1 } } } } "Flyway" should { "execute initialisation script" in { newLoggingContext { implicit logCtx => new FlywayMigrations(postgresDatabase.url).migrate()(DirectExecutionContext) }.map { _ => connectionProvider.runSQL(metrics.test.db) { conn => def checkTableExists(table: String) = { val resultSet = conn.createStatement().executeQuery(s"SELECT * from $table") resultSet.next shouldEqual false } def checkTableDoesNotExist(table: String) = { val resultSet = conn.createStatement().executeQuery(s"SELECT to_regclass('$table')") resultSet.next shouldEqual true Option(resultSet.getString(1)) shouldEqual Option.empty[String] resultSet.wasNull() shouldEqual true } checkTableExists("parameters") checkTableExists("configuration_entries") checkTableExists("participant_command_completions") checkTableExists("participant_command_submissions") checkTableExists("participant_contract_witnesses") checkTableExists("participant_contracts") checkTableExists("participant_events") checkTableExists("parties") checkTableExists("party_entries") checkTableExists("packages") checkTableExists("package_entries") checkTableDoesNotExist("participant_event_flat_transaction_witnesses") checkTableDoesNotExist("participant_event_transaction_tree_witnesses") } } } } }
Example 7
Source File: ResetServiceIT.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.services.reset import com.daml.platform.sandbox.services.SandboxFixture import scala.concurrent.Await import scala.concurrent.duration.DurationInt import scala.ref.WeakReference final class ResetServiceIT extends ResetServiceITBase with SandboxFixture { "ResetService" when { "state is reset" should { "clear out all garbage" in { val state = new WeakReference(Await.result(server.sandboxState, 5.seconds)) for { lid <- fetchLedgerId() _ <- reset(lid) } yield { System.gc() state.get should be(None) } } } } }
Example 8
Source File: SandboxNextFixture.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandboxnext import com.daml.ledger.participant.state.v1.SeedService import com.daml.ledger.api.testing.utils.{OwnedResource, Resource, SuiteResource} import com.daml.platform.sandbox.AbstractSandboxFixture import com.daml.platform.sandbox.config.SandboxConfig import com.daml.platform.sandbox.services.GrpcClientResource import com.daml.ports.Port import com.daml.resources.ResourceOwner import io.grpc.Channel import org.scalatest.Suite import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt trait SandboxNextFixture extends AbstractSandboxFixture with SuiteResource[(Port, Channel)] { self: Suite => override protected def config: SandboxConfig = super.config.copy( seeding = Some(SeedService.Seeding.Weak), ) override protected def serverPort: Port = suiteResource.value._1 override protected def channel: Channel = suiteResource.value._2 override protected lazy val suiteResource: Resource[(Port, Channel)] = { implicit val ec: ExecutionContext = system.dispatcher new OwnedResource[(Port, Channel)]( for { jdbcUrl <- database .fold[ResourceOwner[Option[String]]](ResourceOwner.successful(None))(_.map(info => Some(info.jdbcUrl))) port <- new Runner(config.copy(jdbcUrl = jdbcUrl)) channel <- GrpcClientResource.owner(port) } yield (port, channel), acquisitionTimeout = 1.minute, releaseTimeout = 1.minute, ) } }
Example 9
Source File: ExpiringStreamServiceCallAuthTests.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.auth import java.time.Duration import com.daml.grpc.{GrpcException, GrpcStatus} import com.daml.platform.sandbox.services.SubmitAndWaitDummyCommand import com.daml.platform.testing.StreamConsumer import com.daml.timer.Delayed import io.grpc.Status import io.grpc.stub.StreamObserver import scala.concurrent.duration.DurationInt import scala.concurrent.{Future, Promise} import scala.util.control.NonFatal trait ExpiringStreamServiceCallAuthTests[T] extends ReadOnlyServiceCallAuthTests with SubmitAndWaitDummyCommand { protected def stream: Option[String] => StreamObserver[T] => Unit private def expectExpiration(token: String): Future[Unit] = { val promise = Promise[Unit]() stream(Option(token))(new StreamObserver[T] { @volatile private[this] var gotSomething = false def onNext(value: T): Unit = { gotSomething = true } def onError(t: Throwable): Unit = { t match { case GrpcException(GrpcStatus(Status.Code.PERMISSION_DENIED, _), _) if gotSomething => val _ = promise.trySuccess(()) case NonFatal(e) => val _ = promise.tryFailure(e) } } def onCompleted(): Unit = { val _ = promise.tryFailure(new RuntimeException("stream completed before token expiration")) } }) promise.future } private def canActAsMainActorExpiresInFiveSeconds = toHeader(expiringIn(Duration.ofSeconds(5), readWriteToken(mainActor))) private def canReadAsMainActorExpiresInFiveSeconds = toHeader(expiringIn(Duration.ofSeconds(5), readOnlyToken(mainActor))) it should "break a stream in flight upon read-only token expiration" in { val _ = Delayed.Future.by(10.seconds)(submitAndWait()) expectExpiration(canReadAsMainActorExpiresInFiveSeconds).map(_ => succeed) } it should "break a stream in flight upon read/write token expiration" in { val _ = Delayed.Future.by(10.seconds)(submitAndWait()) expectExpiration(canActAsMainActorExpiresInFiveSeconds).map(_ => succeed) } override def serviceCallWithToken(token: Option[String]): Future[Any] = submitAndWait().flatMap(_ => new StreamConsumer[T](stream(token)).first()) }
Example 10
Source File: SandboxFixture.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.services import com.daml.ledger.api.testing.utils.{OwnedResource, Resource, SuiteResource} import com.daml.platform.sandbox.{AbstractSandboxFixture, SandboxServer} import com.daml.ports.Port import com.daml.resources.ResourceOwner import io.grpc.Channel import org.scalatest.Suite import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt trait SandboxFixture extends AbstractSandboxFixture with SuiteResource[(SandboxServer, Channel)] { self: Suite => protected def server: SandboxServer = suiteResource.value._1 override protected def serverPort: Port = server.port override protected def channel: Channel = suiteResource.value._2 override protected lazy val suiteResource: Resource[(SandboxServer, Channel)] = { implicit val ec: ExecutionContext = system.dispatcher new OwnedResource[(SandboxServer, Channel)]( for { jdbcUrl <- database .fold[ResourceOwner[Option[String]]](ResourceOwner.successful(None))(_.map(info => Some(info.jdbcUrl))) server <- SandboxServer.owner(config.copy(jdbcUrl = jdbcUrl)) channel <- GrpcClientResource.owner(server.port) } yield (server, channel), acquisitionTimeout = 1.minute, releaseTimeout = 1.minute, ) } }
Example 11
Source File: JdbcLedgerDaoBackend.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.store.dao import com.codahale.metrics.MetricRegistry import com.daml.ledger.api.domain.LedgerId import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll import com.daml.logging.LoggingContext import com.daml.logging.LoggingContext.newLoggingContext import com.daml.metrics.Metrics import com.daml.platform.configuration.ServerRole import com.daml.platform.store.dao.events.LfValueTranslation import com.daml.platform.store.{DbType, FlywayMigrations} import com.daml.resources.{Resource, ResourceOwner} import org.scalatest.Suite import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, ExecutionContext} private[dao] trait JdbcLedgerDaoBackend extends AkkaBeforeAndAfterAll { this: Suite => protected def dbType: DbType protected def jdbcUrl: String protected def daoOwner(implicit logCtx: LoggingContext): ResourceOwner[LedgerDao] = JdbcLedgerDao .writeOwner( serverRole = ServerRole.Testing(getClass), jdbcUrl = jdbcUrl, eventsPageSize = 100, metrics = new Metrics(new MetricRegistry), lfValueTranslationCache = LfValueTranslation.Cache.none, ) protected final var ledgerDao: LedgerDao = _ // `dbDispatcher` and `ledgerDao` depend on the `postgresFixture` which is in turn initialized `beforeAll` private var resource: Resource[LedgerDao] = _ override protected def beforeAll(): Unit = { super.beforeAll() implicit val executionContext: ExecutionContext = system.dispatcher resource = newLoggingContext { implicit logCtx => for { _ <- Resource.fromFuture(new FlywayMigrations(jdbcUrl).migrate()) dao <- daoOwner.acquire() _ <- Resource.fromFuture(dao.initializeLedger(LedgerId("test-ledger"))) } yield dao } ledgerDao = Await.result(resource.asFuture, 10.seconds) } override protected def afterAll(): Unit = { Await.result(resource.release(), 10.seconds) super.afterAll() } }
Example 12
Source File: GrpcHealthService.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.server.api.services.grpc import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.Source import com.daml.grpc.adapter.ExecutionSequencerFactory import com.daml.dec.DirectExecutionContext import com.daml.ledger.api.health.HealthChecks import com.daml.platform.api.grpc.GrpcApiService import com.daml.platform.server.api.DropRepeated import com.daml.platform.server.api.services.grpc.GrpcHealthService._ import io.grpc.health.v1.health.{ HealthAkkaGrpc, HealthCheckRequest, HealthCheckResponse, HealthGrpc } import io.grpc.{ServerServiceDefinition, Status, StatusException} import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} class GrpcHealthService( healthChecks: HealthChecks, maximumWatchFrequency: FiniteDuration = 1.second, )( implicit protected val esf: ExecutionSequencerFactory, protected val mat: Materializer, executionContext: ExecutionContext, ) extends HealthAkkaGrpc with GrpcApiService { override def bindService(): ServerServiceDefinition = HealthGrpc.bindService(this, DirectExecutionContext) override def check(request: HealthCheckRequest): Future[HealthCheckResponse] = Future.fromTry(matchResponse(serviceFrom(request))) override def watchSource(request: HealthCheckRequest): Source[HealthCheckResponse, NotUsed] = Source .fromIterator(() => Iterator.continually(matchResponse(serviceFrom(request)).get)) .throttle(1, per = maximumWatchFrequency) .via(DropRepeated()) private def matchResponse(componentName: Option[String]): Try[HealthCheckResponse] = if (!componentName.forall(healthChecks.hasComponent)) Failure(new StatusException(Status.NOT_FOUND)) else if (healthChecks.isHealthy(componentName)) Success(servingResponse) else Success(notServingResponse) } object GrpcHealthService { private[grpc] val servingResponse = HealthCheckResponse(HealthCheckResponse.ServingStatus.SERVING) private[grpc] val notServingResponse = HealthCheckResponse(HealthCheckResponse.ServingStatus.NOT_SERVING) private def serviceFrom(request: HealthCheckRequest): Option[String] = { Option(request.service).filter(_.nonEmpty) } }
Example 13
Source File: Eventually.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testtool.infrastructure import com.daml.timer.RetryStrategy import scala.concurrent.duration.{Duration, DurationInt} import scala.concurrent.{ExecutionContext, Future} object Eventually { def eventually[A]( runAssertion: => Future[A], attempts: Int = 10, firstWaitTime: Duration = 10.millis, )(implicit ec: ExecutionContext): Future[A] = RetryStrategy.exponentialBackoff(attempts, firstWaitTime) { (_, _) => runAssertion } }
Example 14
Source File: ParticipantSession.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testtool.infrastructure.participant import com.daml.ledger.api.testtool.infrastructure.LedgerServices import com.daml.ledger.api.v1.ledger_identity_service.GetLedgerIdentityRequest import com.daml.ledger.api.v1.transaction_service.GetLedgerEndRequest import com.daml.timer.RetryStrategy import io.grpc.ManagedChannel import io.netty.channel.nio.NioEventLoopGroup import org.slf4j.LoggerFactory import scala.concurrent.duration.{DurationInt, SECONDS} import scala.concurrent.{ExecutionContext, Future} private[participant] final class ParticipantSession( val config: ParticipantSessionConfiguration, channel: ManagedChannel, eventLoopGroup: NioEventLoopGroup, )(implicit val executionContext: ExecutionContext) { private[this] val logger = LoggerFactory.getLogger(classOf[ParticipantSession]) private[this] val services: LedgerServices = new LedgerServices(channel) // The ledger identifier is retrieved only once when the participant session is created // Changing the ledger identifier during the execution of a session can result in unexpected consequences // The test tool is designed to run tests in an isolated environment but changing the // global state of the ledger breaks this assumption, no matter what private[this] val ledgerIdF = RetryStrategy.exponentialBackoff(10, 10.millis) { (attempt, wait) => logger.debug(s"Fetching ledgerId to create context (attempt #$attempt, next one in $wait)...") services.identity.getLedgerIdentity(new GetLedgerIdentityRequest).map(_.ledgerId) } private[testtool] def createTestContext( endpointId: String, applicationId: String, identifierSuffix: String, ): Future[ParticipantTestContext] = for { ledgerId <- ledgerIdF end <- services.transaction.getLedgerEnd(new GetLedgerEndRequest(ledgerId)).map(_.getOffset) } yield new ParticipantTestContext( ledgerId, endpointId, applicationId, identifierSuffix, end, services, config.partyAllocation, ) private[testtool] def close(): Unit = { logger.info(s"Disconnecting from participant at ${config.host}:${config.port}...") channel.shutdownNow() if (!channel.awaitTermination(10L, SECONDS)) { sys.error("Channel shutdown stuck. Unable to recover. Terminating.") } logger.info(s"Connection to participant at ${config.host}:${config.port} shut down.") if (!eventLoopGroup .shutdownGracefully(0, 0, SECONDS) .await(10L, SECONDS)) { sys.error("Unable to shutdown event loop. Unable to recover. Terminating.") } logger.info(s"Connection to participant at ${config.host}:${config.port} closed.") } }
Example 15
Source File: MultiFixtureBase.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testing.utils import java.util.concurrent.{Executors, ScheduledExecutorService, TimeUnit} import com.daml.dec.DirectExecutionContext import org.scalatest._ import org.scalatest.concurrent.{AsyncTimeLimitedTests, ScaledTimeSpans} import org.scalatest.exceptions.TestCanceledException import org.scalatest.time.Span import scala.collection.immutable.Iterable import scala.concurrent.duration.DurationInt import scala.concurrent.{Future, Promise, TimeoutException} import scala.util.control.{NoStackTrace, NonFatal} trait MultiFixtureBase[FixtureId, TestContext] extends Assertions with BeforeAndAfterAll with ScaledTimeSpans with AsyncTimeLimitedTests { self: AsyncTestSuite => private var es: ScheduledExecutorService = _ override protected def beforeAll(): Unit = { super.beforeAll() es = Executors.newScheduledThreadPool(1) } override protected def afterAll(): Unit = { es.shutdownNow() super.afterAll() } protected class TestFixture(val id: FixtureId, createContext: () => TestContext) { def context(): TestContext = createContext() } def timeLimit: Span = scaled(30.seconds) object TestFixture { def apply(id: FixtureId, createContext: () => TestContext): TestFixture = new TestFixture(id, createContext) def unapply(testFixture: TestFixture): Option[(FixtureId, TestContext)] = Some((testFixture.id, testFixture.context())) } protected def fixtures: Iterable[TestFixture] protected def allFixtures(runTest: TestContext => Future[Assertion]): Future[Assertion] = forAllFixtures(fixture => runTest(fixture.context)) protected def forAllFixtures(runTest: TestFixture => Future[Assertion]): Future[Assertion] = { forAllMatchingFixtures { case f => runTest(f) } } protected def forAllMatchingFixtures( runTest: PartialFunction[TestFixture, Future[Assertion]]): Future[Assertion] = { if (parallelExecution) { val results = fixtures.map( fixture => if (runTest.isDefinedAt(fixture)) runTestAgainstFixture(fixture, runTest) else Future.successful(succeed)) Future.sequence(results).map(foldAssertions) } else { fixtures.foldLeft(Future.successful(succeed)) { case (resultSoFar, thisFixture) => resultSoFar.flatMap { case Succeeded => runTestAgainstFixture(thisFixture, runTest) case other => Future.successful(other) } } } } }
Example 16
Source File: OwnedResource.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testing.utils import com.daml.resources import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{Await, ExecutionContext} import scala.reflect.ClassTag final class OwnedResource[T: ClassTag]( owner: resources.ResourceOwner[T], acquisitionTimeout: FiniteDuration = 30.seconds, releaseTimeout: FiniteDuration = 30.seconds, )(implicit executionContext: ExecutionContext) extends ManagedResource[T] { private var resource: resources.Resource[T] = _ override def construct(): T = { resource = owner.acquire() Await.result(resource.asFuture, acquisitionTimeout) } override def destruct(value: T): Unit = { Await.result(resource.release(), releaseTimeout) } }
Example 17
Source File: AkkaBeforeAndAfterAll.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testing.utils import java.util.concurrent.Executors import akka.actor.ActorSystem import akka.stream.Materializer import com.daml.grpc.adapter.{AkkaExecutionSequencerPool, ExecutionSequencerFactory} import com.google.common.util.concurrent.ThreadFactoryBuilder import org.scalatest.{BeforeAndAfterAll, Suite} import org.slf4j.LoggerFactory import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, ExecutionContext} trait AkkaBeforeAndAfterAll extends BeforeAndAfterAll { self: Suite => private val logger = LoggerFactory.getLogger(getClass) protected def actorSystemName: String = this.getClass.getSimpleName private implicit lazy val executionContext: ExecutionContext = ExecutionContext.fromExecutorService( Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(s"$actorSystemName-thread-pool-worker-%d") .setUncaughtExceptionHandler((thread, _) => logger.error(s"got an uncaught exception on thread: ${thread.getName}")) .build())) protected implicit lazy val system: ActorSystem = ActorSystem(actorSystemName, defaultExecutionContext = Some(executionContext)) protected implicit lazy val materializer: Materializer = Materializer(system) protected implicit lazy val executionSequencerFactory: ExecutionSequencerFactory = new AkkaExecutionSequencerPool(poolName = actorSystemName, actorCount = 1) override protected def afterAll(): Unit = { executionSequencerFactory.close() materializer.shutdown() Await.result(system.terminate(), 30.seconds) super.afterAll() } }
Example 18
Source File: AkkaExecutionSequencerPool.scala From daml with Apache License 2.0 | 5 votes |
// 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 import java.util.concurrent.atomic.AtomicInteger import akka.Done import akka.actor.ActorSystem import scala.collection.breakOut import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{Await, ExecutionContext, Future} class AkkaExecutionSequencerPool( poolName: String, actorCount: Int = AkkaExecutionSequencerPool.defaultActorCount, terminationTimeout: FiniteDuration = 30.seconds, )(implicit system: ActorSystem) extends ExecutionSequencerFactory { require(actorCount > 0) private val counter = new AtomicInteger() private val pool = Array.fill(actorCount)( AkkaExecutionSequencer(s"$poolName-${counter.getAndIncrement()}", terminationTimeout)) override def getExecutionSequencer: ExecutionSequencer = pool(counter.getAndIncrement() % actorCount) override def close(): Unit = Await.result(closeAsync(), terminationTimeout) def closeAsync(): Future[Unit] = { implicit val ec: ExecutionContext = system.dispatcher val eventuallyClosed: Future[Seq[Done]] = Future.sequence(pool.map(_.closeAsync)(breakOut)) Future.firstCompletedOf( Seq( system.whenTerminated.map(_ => ()), // Cut it short if the ActorSystem stops. eventuallyClosed.map(_ => ()), ) ) } } object AkkaExecutionSequencerPool { private val defaultActorCount: Int = Runtime.getRuntime.availableProcessors() * 8 }
Example 19
Source File: RetryStrategy.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.timer import scala.concurrent.duration.{Duration, DurationInt} import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NonFatal object RetryStrategy { def constant(attempts: Option[Int] = None, waitTime: Duration)( predicate: PartialFunction[Throwable, Boolean] ): RetryStrategy = new RetryStrategy(attempts, waitTime, waitTime, identity, predicate) } final class RetryStrategy private ( attempts: Option[Int], firstWaitTime: Duration, waitTimeCap: Duration, progression: Duration => Duration, predicate: PartialFunction[Throwable, Boolean]) { private def clip(t: Duration): Duration = t.min(waitTimeCap).max(0.millis) def apply[A](run: (Int, Duration) => Future[A])(implicit ec: ExecutionContext): Future[A] = { def go(attempt: Int, wait: Duration): Future[A] = { run(attempt, wait) .recoverWith { case NonFatal(throwable) if attempts.exists(attempt > _) => Future.failed(throwable) case NonFatal(throwable) if predicate.lift(throwable).getOrElse(false) => Delayed.Future.by(wait)(go(attempt + 1, clip(progression(wait)))) case NonFatal(throwable) => Future.failed(throwable) } } go(1, clip(firstWaitTime)) } }
Example 20
Source File: ProgramResource.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.resources import java.util.concurrent.{Executors, TimeUnit} import com.daml.logging.ContextualizedLogger import com.daml.logging.LoggingContext.newLoggingContext import com.daml.resources.ProgramResource._ import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{Await, ExecutionContext} import scala.util.Try import scala.util.control.{NoStackTrace, NonFatal} class ProgramResource[T]( owner: => ResourceOwner[T], tearDownTimeout: FiniteDuration = 10.seconds, ) { private val logger = ContextualizedLogger.get(getClass) private val executorService = Executors.newCachedThreadPool() def run(): Unit = { newLoggingContext { implicit logCtx => val resource = { implicit val executionContext: ExecutionContext = ExecutionContext.fromExecutor(executorService) Try(owner.acquire()).fold(Resource.failed, identity) } def stop(): Unit = { Await.result(resource.release(), tearDownTimeout) executorService.shutdown() executorService.awaitTermination(tearDownTimeout.toMillis, TimeUnit.MILLISECONDS) () } sys.runtime.addShutdownHook(new Thread(() => { try { stop() } catch { case NonFatal(exception) => logger.error("Failed to stop successfully.", exception) } })) // On failure, shut down immediately. resource.asFuture.failed.foreach { exception => exception match { // The error is suppressed; we don't need to print anything more. case _: SuppressedStartupException => case _: StartupException => logger.error( s"Shutting down because of an initialization error.\n${exception.getMessage}") case NonFatal(_) => logger.error("Shutting down because of an initialization error.", exception) } sys.exit(1) // `stop` will be triggered by the shutdown hook. }(ExecutionContext.global) // Run on the global execution context to avoid deadlock. } } } object ProgramResource { trait StartupException extends NoStackTrace { self: Exception => } trait SuppressedStartupException { self: Exception => } }
Example 21
Source File: ChaosSetup.scala From eventuate-chaos with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.chaos import akka.actor.ActorSystem import akka.actor.Props import akka.pattern.BackoffSupervisor import com.rbmhtechnology.eventuate.ReplicationConnection import com.rbmhtechnology.eventuate.ReplicationEndpoint import com.typesafe.config.ConfigFactory import scala.concurrent.duration.DurationInt trait ChaosSetup extends App { def getSystem: ActorSystem def getEndpoint(implicit system: ActorSystem): ReplicationEndpoint protected def baseConfig(hostname: String) = ConfigFactory.parseString( s""" |akka.actor.provider = "akka.remote.RemoteActorRefProvider" |akka.remote.enabled-transports = ["akka.remote.netty.tcp"] |akka.remote.netty.tcp.hostname = "$hostname" |akka.remote.netty.tcp.port = 2552 |akka.test.single-expect-default = 10s |akka.loglevel = "INFO" |eventuate.log.write-batch-size = 16 |eventuate.log.read-timeout = 3s |eventuate.log.retry-delay = 3s |akka.remote.netty.tcp.maximum-frame-size = 1024000b """.stripMargin) protected def quote(str: String) = "\"" + str + "\"" protected def supervised(props: Props, name: String): Props = BackoffSupervisor.props(props, name, 1.second, 30.seconds, 0.1) def name = { if (args == null || args.length < 1) { Console.err.println("no <nodename> specified") sys.exit(1) } else { args(0) } } def hostname = sys.env.getOrElse("HOSTNAME", s"$name.eventuate-chaos.docker") // replication connection to other node(s) def connections = args.drop(1).map { conn => conn.split(":") match { case Array(host, port) => ReplicationConnection(host, port.toInt) case Array(host) => ReplicationConnection(host, 2552) } }.toSet }
Example 22
Source File: IntegrationSpec.scala From Scala-Programming-Projects with MIT License | 5 votes |
import org.scalatestplus.play.PlaySpec import org.scalatestplus.play.guice.GuiceOneServerPerSuite import play.api.libs.ws.WSClient import scala.concurrent.duration.DurationInt import scala.concurrent.Await import org.scalatest._ import Matchers._ class IntegrationSpec extends PlaySpec with GuiceOneServerPerSuite { "Application" should { val wsClient = app.injector.instanceOf[WSClient] val myPublicAddress = s"localhost:$port" "work from within a browser" in { val testURL = s"http://$myPublicAddress/" val response = Await.result(wsClient.url(testURL).get(), 1 seconds) response.body should include ("<title>Shopping Page</title>") } } }
Example 23
Source File: ActorMetricsSpec.scala From prometheus-akka with Apache License 2.0 | 5 votes |
package com.workday.prometheus.akka import scala.concurrent.{Await, Future} import scala.concurrent.duration.DurationInt import akka.actor._ import akka.monitor.instrumentation.CellInfo import akka.testkit.TestProbe class ActorMetricsSpec extends TestKitBaseSpec("ActorMetricsSpec") { import ActorMetricsTestActor._ "the actor metrics" should { "respect the configured include and exclude filters" in { val trackedActor = createTestActor("tracked-actor") val nonTrackedActor = createTestActor("non-tracked-actor") val excludedTrackedActor = createTestActor("tracked-explicitly-excluded-actor") actorMetricsRecorderOf(trackedActor) should not be empty actorMetricsRecorderOf(nonTrackedActor) shouldBe empty actorMetricsRecorderOf(excludedTrackedActor) shouldBe empty val metrics = actorMetricsRecorderOf(trackedActor).get metrics.actorName shouldEqual "actormetricsspec_user_tracked_actor" metrics.messages.get shouldEqual 1.0 } "handle concurrent metric getOrElseUpdate calls" in { implicit val ec = system.dispatcher val e = Entity("fake-actor-name", MetricsConfig.Actor) val futures = (1 to 100).map{ _ => Future(ActorMetrics.metricsFor(e)) } val future = Future.sequence(futures) val metrics = Await.result(future, 10.seconds) metrics.fold(metrics.head) { (compare, metric) => metric shouldEqual compare compare } } } def actorMetricsRecorderOf(ref: ActorRef): Option[ActorMetrics] = { val name = CellInfo.cellName(system, ref) val entity = Entity(name, MetricsConfig.Actor) if (ActorMetrics.hasMetricsFor(entity)) { ActorMetrics.metricsFor(entity) } else { None } } def createTestActor(name: String): ActorRef = { val actor = system.actorOf(Props[ActorMetricsTestActor], name) val initialiseListener = TestProbe() // Ensure that the router has been created before returning. actor.tell(Ping, initialiseListener.ref) initialiseListener.expectMsg(Pong) actor } }
Example 24
Source File: RouterMetricsSpec.scala From prometheus-akka with Apache License 2.0 | 5 votes |
package com.workday.prometheus.akka import scala.concurrent.{Await, Future} import scala.concurrent.duration.DurationInt import akka.actor._ import akka.monitor.instrumentation.CellInfo import akka.routing._ import akka.testkit.TestProbe class RouterMetricsSpec extends TestKitBaseSpec("RouterMetricsSpec") { import RouterMetricsTestActor._ "the router metrics" should { "respect the configured include and exclude filters" in { val trackedRouter = createTestPoolRouter("tracked-pool-router") val nonTrackedRouter = createTestPoolRouter("non-tracked-pool-router") val excludedTrackedRouter = createTestPoolRouter("tracked-explicitly-excluded-pool-router") routerMetricsRecorderOf(trackedRouter) should not be empty routerMetricsRecorderOf(nonTrackedRouter) shouldBe empty routerMetricsRecorderOf(excludedTrackedRouter) shouldBe empty val metrics = routerMetricsRecorderOf(trackedRouter).get metrics.actorName shouldEqual "routermetricsspec_user_tracked_pool_router" metrics.messages.get shouldEqual 1.0 } "handle concurrent metric getOrElseUpdate calls" in { implicit val ec = system.dispatcher val e = Entity("fake-actor-name", MetricsConfig.Actor) val futures = (1 to 100).map{ _ => Future(ActorMetrics.metricsFor(e)) } val future = Future.sequence(futures) val metrics = Await.result(future, 10.seconds) metrics.fold(metrics.head) { (compare, metric) => metric shouldEqual compare compare } } } def routerMetricsRecorderOf(ref: ActorRef): Option[RouterMetrics] = { val name = CellInfo.cellName(system, ref) val entity = Entity(name, MetricsConfig.Router) if (RouterMetrics.hasMetricsFor(entity)) { RouterMetrics.metricsFor(entity) } else { None } } def createTestPoolRouter(routerName: String): ActorRef = { val router = system.actorOf(RoundRobinPool(5).props(Props[RouterMetricsTestActor]), routerName) val initialiseListener = TestProbe() // Ensure that the router has been created before returning. router.tell(Ping, initialiseListener.ref) initialiseListener.expectMsg(Pong) router } }
Example 25
package com.wavesplatform.dex.time import java.net.{InetAddress, SocketTimeoutException} import java.util.concurrent.RejectedExecutionException import java.util.concurrent.atomic.AtomicBoolean import com.wavesplatform.dex.domain.utils.ScorexLogging import monix.eval.Task import monix.execution.schedulers.SchedulerService import monix.execution.{ExecutionModel, Scheduler} import org.apache.commons.net.ntp.NTPUDPClient import scala.concurrent.duration.DurationInt class NTP(ntpServer: String) extends Time with ScorexLogging with AutoCloseable { private val offsetPanicThreshold = 1000000L private val ExpirationTimeout = 60.seconds private val RetryDelay = 10.seconds private val ResponseTimeout = 10.seconds private val duringShutdown = new AtomicBoolean(false) private implicit val scheduler: SchedulerService = Scheduler.singleThread( name = "time-impl", daemonic = false, executionModel = ExecutionModel.AlwaysAsyncExecution, reporter = { case _: RejectedExecutionException if duringShutdown.get() => // ignore case e: Throwable => log.error("An uncaught error", e) } ) private val client = new NTPUDPClient() client.setDefaultTimeout(ResponseTimeout.toMillis.toInt) @volatile private var offset = 0L private val updateTask: Task[Unit] = { def newOffsetTask: Task[Option[(InetAddress, java.lang.Long)]] = Task { try { val info = client.getTime(InetAddress.getByName(ntpServer)) info.computeDetails() Option(info.getOffset).map { offset => val r = if (Math.abs(offset) > offsetPanicThreshold) throw new Exception("Offset is suspiciously large") else offset (info.getAddress, r) } } catch { case _: SocketTimeoutException => None case t: Throwable => log.warn("Problems with NTP: ", t) None } } newOffsetTask.flatMap { case None if !scheduler.isShutdown => updateTask.delayExecution(RetryDelay) case Some((server, newOffset)) if !scheduler.isShutdown => log.trace(s"Adjusting time with $newOffset milliseconds, source: ${server.getHostAddress}.") offset = newOffset updateTask.delayExecution(ExpirationTimeout) case _ => Task.unit } } def correctedTime(): Long = System.currentTimeMillis() + offset private var txTime: Long = 0 def getTimestamp(): Long = { txTime = Math.max(correctedTime(), txTime + 1) txTime } private val taskHandle = updateTask.runAsync { case Left(e) => log.error(s"Error executing task", e) case _ => } override def close(): Unit = if (duringShutdown.compareAndSet(false, true)) { log.info("Shutting down Time") taskHandle.cancel() if (client.isOpen) client.close() scheduler.shutdown() } }
Example 26
Source File: JwtUtils.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.auth import java.security import com.wavesplatform.dex.api.ws.protocol.WsAddressSubscribe.JwtPayload import com.wavesplatform.dex.domain.account.{AddressScheme, KeyPair, PublicKey} import com.wavesplatform.dex.domain.bytes.ByteStr import pdi.jwt.{JwtAlgorithm, JwtJson} import play.api.libs.json.{JsObject, Json} import scala.concurrent.duration.{DurationInt, FiniteDuration} trait JwtUtils { def mkJwt(authServiceKeyPair: security.KeyPair, payload: JwtPayload): String = mkJwt(authServiceKeyPair, Json.toJsObject(payload)) def mkJwt(authServiceKeyPrivateKey: security.PrivateKey, payload: JsObject): String = JwtJson.encode(payload, authServiceKeyPrivateKey, JwtAlgorithm.RS256) def mkJwt(authServiceKeyPair: security.KeyPair, payload: JsObject): String = JwtJson.encode(payload, authServiceKeyPair.getPrivate, JwtAlgorithm.RS256) def mkJwtSignedPayload(clientKeyPair: KeyPair, networkByte: Byte = AddressScheme.current.chainId, lifetime: FiniteDuration = 1.hour): JwtPayload = mkJwtNotSignedPayload(clientKeyPair, networkByte, lifetime).signed(clientKeyPair) def mkJwtNotSignedPayload(clientPublicKey: PublicKey, networkByte: Byte = AddressScheme.current.chainId, lifetime: FiniteDuration = 1.hour): JwtPayload = { val exp = System.currentTimeMillis() / 1000 + lifetime.toSeconds JwtPayload( signature = ByteStr(Array.emptyByteArray), publicKey = clientPublicKey, networkByte = networkByte.toChar.toString, clientId = "test", firstTokenExpirationInSeconds = exp, activeTokenExpirationInSeconds = exp, scope = List("general") ) } } object JwtUtils extends JwtUtils
Example 27
Source File: AskActorSpec.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors import akka.actor.ActorRef import akka.testkit.TestProbe import com.wavesplatform.dex.test.matchers.DiffMatcherWithImplicits import com.wavesplatform.dex.time.SystemTime import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, Future, TimeoutException} class AskActorSpec extends AnyFreeSpec with Matchers with SystemTime with MatcherSpecLike with DiffMatcherWithImplicits { private val defaultTimeout = 5.seconds private val defaultResponse = "foo" "AskActor" - { "happy path" in test { (ref, future) => ref ! defaultResponse val actual = Await.result(future, defaultTimeout) actual should matchTo(defaultResponse) } "timeout" in test { (_, future) => Await.result(future.failed, defaultTimeout) shouldBe a[TimeoutException] } "unexpected response type" in test { (ref, future) => ref ! 100500 Await.result(future.failed, defaultTimeout) shouldBe a[IllegalArgumentException] } } private def test(f: (ActorRef, Future[String]) => Unit): Unit = { val (ref, future) = AskActor.mk[String](100.millis) val p = TestProbe() p.watch(ref) f(ref, future) p.expectTerminated(ref, defaultTimeout) } override protected def actorSystemName: String = "AskActorSpec" }
Example 28
Source File: WsSuiteBase.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.it import com.softwaremill.diffx.{Derived, Diff} import com.wavesplatform.dex.api.ws.connection.WsConnection import com.wavesplatform.dex.api.ws.entities.WsFullOrder import com.wavesplatform.dex.api.ws.protocol.{WsError, WsServerMessage} import com.wavesplatform.dex.it.api.websockets.HasWebSockets import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.reflect.ClassTag trait WsSuiteBase extends MatcherSuiteBase with HasWebSockets { protected implicit val wsErrorDiff: Diff[WsError] = Derived[Diff[WsError]].ignore[WsError, Long](_.timestamp) protected implicit val wsCompleteOrderDiff: Diff[WsFullOrder] = Derived[Diff[WsFullOrder]].ignore[WsFullOrder, Long](_.timestamp).ignore[WsFullOrder, Long](_.eventTimestamp) final implicit class WsConnectionOps(val self: WsConnection) { def receiveAtLeastN[T <: WsServerMessage: ClassTag](n: Int): List[T] = { val r = eventually { val xs = self.collectMessages[T] xs.size should be >= n xs } Thread.sleep(200) // Waiting for additional messages r } def receiveNoMessages(duration: FiniteDuration = 1.second): Unit = { val sizeBefore = self.messages.size Thread.sleep(duration.toMillis) self.messages.size shouldBe sizeBefore } def receiveNoMessagesOf[T <: WsServerMessage: ClassTag](duration: FiniteDuration = 1.second): Unit = { val sizeBefore = self.collectMessages[T].size Thread.sleep(duration.toMillis) self.collectMessages[T].size shouldBe sizeBefore } } }
Example 29
package com.wavesplatform import com.wavesplatform.dex.domain.account.{KeyPair, PublicKey} import com.wavesplatform.dex.domain.asset.AssetPair import com.wavesplatform.dex.domain.order.{Order, OrderType} import com.wavesplatform.dex.waves.WavesFeeConstants._ import com.wavesplatform.it.api.MatcherCommand import org.scalacheck.Gen import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{Await, Future} import scala.util.Random import scala.util.control.NonFatal package object it { def executeCommands(xs: Seq[MatcherCommand], ignoreErrors: Boolean = true, timeout: FiniteDuration = 3.minutes): Int = { Await.result(Future.sequence(xs.map(executeCommand(_, ignoreErrors))), timeout).sum } private def executeCommand(x: MatcherCommand, ignoreErrors: Boolean): Future[Int] = try x match { case MatcherCommand.Place(api, order) => api.tryPlace(order).map(_.fold(_ => 0, _ => 1)) case MatcherCommand.Cancel(api, owner, order) => api.tryCancel(owner, order).map(_.fold(_ => 0, _ => 1)) } catch { case NonFatal(e) => if (ignoreErrors) Future.successful(0) else Future.failed(e) } def orderGen(matcher: PublicKey, trader: KeyPair, assetPairs: Seq[AssetPair], types: Seq[OrderType] = Seq(OrderType.BUY, OrderType.SELL)): Gen[Order] = { val ts = System.currentTimeMillis() for { assetPair <- Gen.oneOf(assetPairs) tpe <- Gen.oneOf(types) amount <- Gen.choose(10, 100) price <- Gen.choose(10, 100) orderVersion <- Gen.choose[Byte](1, 3) expirationDiff <- Gen.choose(600000, 6000000) } yield { if (tpe == OrderType.BUY) Order.buy( trader, matcher, assetPair, amount, price * Order.PriceConstant, ts, ts + expirationDiff, matcherFee, orderVersion ) else Order.sell( trader, matcher, assetPair, amount, price * Order.PriceConstant, ts, ts + expirationDiff, matcherFee, orderVersion ) } } def choose[T](xs: IndexedSeq[T]): T = xs(Random.nextInt(xs.size)) }
Example 30
Source File: WsPingPongExternalTestSuite.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.it.sync.api.ws import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.dex.api.ws.protocol.WsError import scala.concurrent.Await import scala.concurrent.duration.DurationInt class WsPingPongExternalTestSuite extends WsPingPongBaseSuite { protected val maxConnectionLifetime = 6.seconds override protected lazy val wsStreamUri: String = getWsStreamUri(dex1) override protected val dexInitialSuiteConfig: Config = ConfigFactory .parseString( s"""waves.dex.web-sockets.external-client-handler { | max-connection-lifetime = $maxConnectionLifetime | health-check = { | ping-interval = $pingInterval | pong-timeout = $pongTimeout | } | } |""".stripMargin ) .withFallback(jwtPublicKeyConfig) "Web socket connection should be closed " - { s"by max-connection-lifetime = $maxConnectionLifetime" in { val wsac = mkWsAddressConnection(alice, dex1) val connectionLifetime = Await.result(wsac.connectionLifetime, maxConnectionLifetime + delta) connectionLifetime should (be >= maxConnectionLifetime and be <= maxConnectionLifetime + delta) wsac.pings.size should be >= 5 wsac.isClosed shouldBe true wsac.collectMessages[WsError].head should matchTo( WsError( timestamp = 0L, // ignored code = 109077767, // WsConnectionMaxLifetimeExceeded message = "WebSocket has reached max allowed lifetime" ) ) } } }
Example 31
Source File: DatabaseBackwardCompatTestSuite.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.it.sync.compat import com.wavesplatform.dex.api.http.entities.HttpOrderStatus.Status import com.wavesplatform.dex.domain.order.Order import com.wavesplatform.dex.it.docker.DexContainer import com.wavesplatform.it.orderGen import com.wavesplatform.it.tags.DexMultipleVersions import org.scalacheck.Gen import org.testcontainers.containers.BindMode import scala.concurrent.duration.DurationInt @DexMultipleVersions class DatabaseBackwardCompatTestSuite extends BackwardCompatSuiteBase { "Database backward compatibility test" in { val assetPairs = List(ethWavesPair, wavesUsdPair) val twoAccountsOrdersGen = Gen.oneOf( orderGen(matcher, alice, assetPairs), orderGen(matcher, bob, assetPairs) ) val orders = Gen.containerOfN[Vector, Order](200, twoAccountsOrdersGen).sample.get orders.foreach(dex2.api.place) dex2.api.waitForOrder(orders.last)(_.status != Status.NotFound) dex2.stopWithoutRemove() dex1.start() orders.groupBy(_.assetPair).valuesIterator.foreach { orders => dex1.api.waitForOrder(orders.last)(_.status != Status.NotFound) } Thread.sleep(3.seconds.toMillis) // An additional time to wait the concurrent processing val state1 = state(dex1.api, orders) val state2 = state(dex2.api, orders) state1 should matchTo(state2) } override protected def beforeAll(): Unit = { super.beforeAll() val containerDataDir = DexContainer.containerPath("data") List(dex1, dex2).foreach { _.underlying.configure { _.withFileSystemBind(localLogsDir.resolve("db").toString, containerDataDir, BindMode.READ_WRITE) } } dex2.start() } }
Example 32
Source File: MatcherSuiteBase.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.it import java.nio.charset.StandardCharsets import java.util.concurrent.ThreadLocalRandom import cats.instances.FutureInstances import com.wavesplatform.dex.asset.DoubleOps import com.wavesplatform.dex.domain.account.KeyPair import com.wavesplatform.dex.domain.asset.Asset import com.wavesplatform.dex.domain.bytes.ByteStr import com.wavesplatform.dex.domain.utils.ScorexLogging import com.wavesplatform.dex.it.api.BaseContainersKit import com.wavesplatform.dex.it.api.node.HasWavesNode import com.wavesplatform.dex.it.config.{GenesisConfig, PredefinedAccounts, PredefinedAssets} import com.wavesplatform.dex.it.dex.HasDex import com.wavesplatform.dex.it.matchers.ItMatchers import com.wavesplatform.dex.it.test.InformativeTestStart import com.wavesplatform.dex.it.waves.{MkWavesEntities, ToWavesJConversions} import com.wavesplatform.dex.test.matchers.DiffMatcherWithImplicits import com.wavesplatform.dex.waves.WavesFeeConstants import com.wavesplatform.it.api.ApiExtensions import org.scalatest.concurrent.Eventually import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, CancelAfterFailure} import scala.concurrent.duration.DurationInt trait MatcherSuiteBase extends AnyFreeSpec with Matchers with CancelAfterFailure with BeforeAndAfterAll with BeforeAndAfterEach with Eventually with BaseContainersKit with HasDex with HasWavesNode with MkWavesEntities with ApiExtensions with ItMatchers with DoubleOps with WavesFeeConstants with PredefinedAssets with PredefinedAccounts with DiffMatcherWithImplicits with InformativeTestStart with FutureInstances with ToWavesJConversions with ScorexLogging { GenesisConfig.setupAddressScheme() override protected val moduleName: String = "dex-it" override implicit def patienceConfig: PatienceConfig = super.patienceConfig.copy(timeout = 30.seconds, interval = 1.second) override protected def beforeAll(): Unit = { log.debug(s"Perform beforeAll") kafkaServer.foreach { _ => createKafkaTopic(dexRunConfig.getString("waves.dex.events-queue.kafka.topic")) } wavesNode1.start() dex1.start() } override protected def afterAll(): Unit = { log.debug(s"Perform afterAll") stopBaseContainers() super.afterAll() } def createAccountWithBalance(balances: (Long, Asset)*): KeyPair = { val account = KeyPair(ByteStr(s"account-test-${ThreadLocalRandom.current().nextInt()}".getBytes(StandardCharsets.UTF_8))) balances.foreach { case (balance, asset) => assert( wavesNode1.api.balance(alice, asset) >= balance, s"Alice doesn't have enough balance in ${asset.toString} to make a transfer" ) broadcastAndAwait(mkTransfer(alice, account.toAddress, balance, asset)) } account } }
Example 33
Source File: IntegrationSuiteBase.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.grpc.integration import com.wavesplatform.dex.asset.DoubleOps import com.wavesplatform.dex.domain.utils.ScorexLogging import com.wavesplatform.dex.it.api.BaseContainersKit import com.wavesplatform.dex.it.api.node.{HasWavesNode, NodeApiExtensions} import com.wavesplatform.dex.it.config.{GenesisConfig, PredefinedAccounts, PredefinedAssets} import com.wavesplatform.dex.it.test.InformativeTestStart import com.wavesplatform.dex.it.waves.{MkWavesEntities, ToWavesJConversions} import com.wavesplatform.dex.test.matchers.DiffMatcherWithImplicits import com.wavesplatform.dex.waves.WavesFeeConstants import org.scalatest.concurrent.Eventually import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} import scala.concurrent.duration.DurationInt trait IntegrationSuiteBase extends AnyFreeSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with Eventually with BaseContainersKit with HasWavesNode with MkWavesEntities with WavesFeeConstants with NodeApiExtensions with PredefinedAssets with PredefinedAccounts with DoubleOps with DiffMatcherWithImplicits with InformativeTestStart with ToWavesJConversions with ScorexLogging { GenesisConfig.setupAddressScheme() override protected val moduleName: String = "waves-integration-it" override implicit def patienceConfig: PatienceConfig = super.patienceConfig.copy(timeout = 30.seconds, interval = 1.second) override protected def beforeAll(): Unit = { log.debug(s"Perform beforeAll") wavesNode1.start() } override protected def afterAll(): Unit = { log.debug(s"Perform afterAll") stopBaseContainers() super.afterAll() } }
Example 34
Source File: WsConnection.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.load.ws import akka.Done import akka.actor.{ActorRef, ActorSystem, Status} import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage, WebSocketRequest} import akka.stream.scaladsl.{Flow, Sink, Source} import akka.stream.{CompletionStrategy, Materializer, OverflowStrategy} import com.wavesplatform.dex.api.ws.connection.TestWsHandlerActor import com.wavesplatform.dex.api.ws.protocol.{WsClientMessage, WsMessage, WsServerMessage} import com.wavesplatform.dex.domain.utils.ScorexLogging import play.api.libs.json.Json import scala.concurrent.Future import scala.concurrent.duration.DurationInt import scala.util.{Failure, Success, Try} class WsConnection(uri: String, receive: WsServerMessage => Option[WsClientMessage])(implicit system: ActorSystem) extends ScorexLogging { import system.dispatcher private implicit val materializer = Materializer(system) private val wsHandlerRef = system.actorOf(TestWsHandlerActor.props(keepAlive = true)) log.info(s"Connecting to Matcher WS API: $uri") protected def stringifyClientMessage(cm: WsClientMessage): TextMessage.Strict = WsMessage.toStrictTextMessage(cm)(WsClientMessage.wsClientMessageWrites) // To server private val source: Source[TextMessage.Strict, ActorRef] = { val completionMatcher: PartialFunction[Any, CompletionStrategy] = { case akka.actor.Status.Success(_) => CompletionStrategy.draining } val failureMatcher: PartialFunction[Any, Throwable] = { case Status.Failure(cause) => cause } Source .actorRef[WsClientMessage](completionMatcher, failureMatcher, 10, OverflowStrategy.fail) .map(stringifyClientMessage) .mapMaterializedValue { source => wsHandlerRef.tell(TestWsHandlerActor.AssignSourceRef, source) source } } // To client private val sink: Sink[Message, Future[Done]] = Sink.foreach { case tm: TextMessage => // TODO move to tests for { strictText <- tm.toStrict(1.second).map(_.getStrictText) clientMessage <- { log.trace(s"Got $strictText") Try { Json.parse(strictText).as[WsServerMessage] } match { case Failure(exception) => Future.failed(exception) case Success(x) => Future.successful { receive(x).foreach(wsHandlerRef ! _) } } } } yield clientMessage case bm: BinaryMessage => bm.dataStream.runWith(Sink.ignore) Future.failed { new IllegalArgumentException("Binary messages are not supported") } } private val flow: Flow[Message, TextMessage.Strict, Future[Done]] = Flow.fromSinkAndSourceCoupled(sink, source).watchTermination() { case (_, f) => f.onComplete { case Success(_) => log.info(s"WebSocket connection to $uri successfully closed") case Failure(e) => log.error(s"WebSocket connection to $uri closed with an error", e) }(materializer.executionContext) f } val (connectionResponse, closed) = Http().singleWebSocketRequest(WebSocketRequest(uri), flow) def send(message: WsClientMessage): Unit = wsHandlerRef ! TestWsHandlerActor.SendToServer(message) def isClosed: Boolean = closed.isCompleted def close(): Future[Done] = { if (!isClosed) wsHandlerRef ! TestWsHandlerActor.CloseConnection closed } }
Example 35
Source File: Philosopher.scala From didactic-computing-machine with GNU Affero General Public License v3.0 | 5 votes |
package DiningPhilosophers import DiningPhilosophers.ForkMessages._ import DiningPhilosophers.PhilosopherMessages._ import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props} import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration.DurationInt import scala.concurrent.duration.FiniteDuration import scala.concurrent.ExecutionContext.Implicits.global class Philosopher(val leftFork: ActorRef, val rightFork: ActorRef) extends Actor with ActorLogging { def name = self.path.name private val eatingTime = 2500.millis private val thinkingTime = 5000.millis private val retryTime = 10.millis def thinkFor(duration: FiniteDuration) = { context.system.scheduler.scheduleOnce(duration, self, Eat) context.become(thinking) } def thinking: Receive = { case Eat => log.info(s"Philosopher ${self.path.name} wants to eat") leftFork ! Take rightFork ! Take context.become(hungry) } def hungry: Receive = { case ForkBeingUsed => handleForkBeingUsed() case ForkTaken => log.info(s"Philosopher ${self.path.name} found one fork to be taken by other philosopher") context.become(waitingForOtherFork) } def waitingForOtherFork: Receive = { case ForkBeingUsed => handleForkBeingUsed() case ForkTaken => log.info(s"Philosopher ${self.path.name} starts to eat") context.system.scheduler.scheduleOnce(eatingTime, self, Think) context.become(eating) } def eating: Receive = { case Think => log.info(s"Philosopher ${self.path.name} starts to think") leftFork ! Put rightFork ! Put thinkFor(thinkingTime) } def handleForkBeingUsed(): Unit = { log.info(s"Philosopher ${self.path.name} found one fork to be in use") leftFork ! Put rightFork ! Put thinkFor(retryTime) } def receive = { case Think => log.info(s"Philosopher ${self.path.name} started thinking") thinkFor(thinkingTime) } }
Example 36
Source File: FeyGenericActorTest.scala From incubator-retired-iota with Apache License 2.0 | 5 votes |
package org.apache.iota.fey import akka.actor.ActorRef import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.duration.FiniteDuration class FeyGenericActorTest(override val params: Map[String,String] = Map.empty, override val backoff: FiniteDuration = 1.minutes, override val connectTo: Map[String,ActorRef] = Map.empty, override val schedulerTimeInterval: FiniteDuration = 2.seconds, override val orchestrationName: String = "", override val orchestrationID: String = "", override val autoScale: Boolean = false) extends FeyGenericActor { var count = 0 var started = false var processed = false var executing = false var stopped = false var restarted = false override def onStart(): Unit = { started = true } override def processMessage[T](message: T, sender: ActorRef): Unit = { processed = true log.info(s"Processing message ${message.toString}") propagateMessage(s"PROPAGATING FROM ${self.path.name} - Message: ${message.toString}") startBackoff() } override def execute(): Unit = { log.info(s"Executing action in ${self.path.name}") executing = true } override def customReceive: Receive = { case "TEST_CUSTOM" => count+=1 } override def onStop(): Unit = { log.info(s"Actor ${self.path.name} stopped.") stopped = true } override def onRestart(reason: Throwable): Unit = { restarted = true } }
Example 37
Source File: JsonReceiverSpec.scala From incubator-retired-iota with Apache License 2.0 | 5 votes |
package org.apache.iota.fey import java.nio.file.{Files, Paths} import akka.actor.ActorRef import akka.testkit.{EventFilter, TestProbe} import ch.qos.logback.classic.Level import scala.concurrent.duration.{DurationInt, FiniteDuration} class JsonReceiverSpec extends BaseAkkaSpec with LoggingTest{ class ReceiverTest(verifyActor: ActorRef) extends JsonReceiver{ override def execute(): Unit = { verifyActor ! "EXECUTED" Thread.sleep(500) } override def exceptionOnRun(e: Exception): Unit = { verifyActor ! "INTERRUPTED" } } val verifyTB = TestProbe("RECEIVER-TEST") val receiver = new ReceiverTest(verifyTB.ref) "Executing validJson in JsonReceiver" should { "return false when json schema is not right" in { receiver.validJson(getJSValueFromString(Utils_JSONTest.test_json_schema_invalid)) should be(false) } "log message to Error" in { ("Incorrect JSON schema \n/ensembles/0 \n\tErrors: Property command missing") should beLoggedAt(Level.ERROR) } "return true when Json schema is valid" in { receiver.validJson(getJSValueFromString(Utils_JSONTest.create_json_test)) should be(true) } } "Executing checkForLocation in JsonReceiver" should { "log message at Debug level" in { receiver.checkForLocation(getJSValueFromString(Utils_JSONTest.test_json_schema_invalid)) "Location not defined in JSON" should beLoggedAt(Level.DEBUG) } "download jar dynamically from URL" in { receiver.checkForLocation(getJSValueFromString(Utils_JSONTest.location_test)) Files.exists(Paths.get(s"${CONFIG.DYNAMIC_JAR_REPO}/fey-stream.jar")) should be(true) } } var watchThread: Thread = _ "Start a Thread with the JSON receiver" should { "Start Thread" in { watchThread = new Thread(receiver, "TESTING-RECEIVERS-IN-THREAD") watchThread.setDaemon(true) watchThread.start() TestProbe().isThreadRunning("TESTING-RECEIVERS-IN-THREAD") should be(true) } "execute execute() method inside run" in { verifyTB.expectMsgAllOf(600.milliseconds,"EXECUTED","EXECUTED") } } "Interrupting the receiver Thread" should { "Throw Interrupted exception" in { EventFilter[InterruptedException]() intercept { watchThread.interrupt() watchThread.join() } } "execute exceptionOnRun method" in { verifyTB.receiveWhile(1200.milliseconds) { case "EXECUTED" => } verifyTB.expectMsg("INTERRUPTED") } } }
Example 38
Source File: WatchServiceReceiverSpec.scala From incubator-retired-iota with Apache License 2.0 | 5 votes |
package org.apache.iota.fey import java.nio.file.{Files, Paths} import java.nio.charset.StandardCharsets import akka.testkit.{EventFilter, TestProbe} import scala.concurrent.duration.{DurationInt, FiniteDuration} import java.io.File import ch.qos.logback.classic.Level class WatchServiceReceiverSpec extends BaseAkkaSpec{ val watcherTB = TestProbe("WATCH-SERVICE") var watchFileTask:WatchServiceReceiver = _ val watchTestDir = s"${CONFIG.JSON_REPOSITORY}/watchtest" "Creating WatchServiceReceiver" should { "process initial files in the JSON repository" in { CONFIG.JSON_EXTENSION = "json.not" watchFileTask = new WatchServiceReceiver(watcherTB.ref) watcherTB.expectMsgAllClassOf(classOf[JsonReceiverActor.JSON_RECEIVED]) CONFIG.JSON_EXTENSION = "json.test" } } var watchThread: Thread = _ "Start a Thread with WatchServiceReceiver" should { "Start Thread" in { watchThread = new Thread(watchFileTask, "TESTING-WATCHER-IN-THREAD") watchThread.setDaemon(true) watchThread.start() TestProbe().isThreadRunning("TESTING-WATCHER-IN-THREAD") should be(true) } } "Start watching directory" should { "Starting receiving CREATED event" taggedAs(SlowTest) in { watchFileTask.watch(Paths.get(watchTestDir)) Files.write(Paths.get(s"$watchTestDir/watched.json.test"), Utils_JSONTest.create_json_test.getBytes(StandardCharsets.UTF_8)) watcherTB.expectMsgAllClassOf(20.seconds, classOf[JsonReceiverActor.JSON_RECEIVED]) } "Starting receiving UPDATE event" taggedAs(SlowTest) in { Files.write(Paths.get(s"$watchTestDir/watched-update.json.test"), Utils_JSONTest.delete_json_test.getBytes(StandardCharsets.UTF_8)) Thread.sleep(200) Files.write(Paths.get(s"$watchTestDir/watched-update.json.test"), Utils_JSONTest.create_json_test.getBytes(StandardCharsets.UTF_8)) watcherTB.expectMsgAllClassOf(20.seconds, classOf[JsonReceiverActor.JSON_RECEIVED]) } } "processJson" should { "log to warn level when json has invalid schema" in { Files.write(Paths.get(s"$watchTestDir/watched-invalid.json.test"), Utils_JSONTest.test_json_schema_invalid.getBytes(StandardCharsets.UTF_8)) watchFileTask.processJson(s"$watchTestDir/watched-invalid.json.test",new File(s"$watchTestDir/watched-invalid.json.test")) s"File $watchTestDir/watched-invalid.json.test not processed. Incorrect JSON schema" should beLoggedAt(Level.WARN) } } "interrupt watchservice" should{ "interrupt thread" in { watchThread.interrupt() } } }
Example 39
Source File: BaseAkkaSpec.scala From incubator-retired-iota with Apache License 2.0 | 5 votes |
package org.apache.iota.fey import java.nio.file.Paths import akka.actor.{ActorIdentity, ActorRef, ActorSystem, Identify, Props} import akka.testkit.{EventFilter, TestEvent, TestProbe} import com.typesafe.config.ConfigFactory import org.scalatest.BeforeAndAfterAll import play.api.libs.json._ import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.Await class BaseAkkaSpec extends BaseSpec with BeforeAndAfterAll with LoggingTest{ //Load default configuration for Fey when running tests resetCapturedLogs() CONFIG.loadUserConfiguration(Paths.get(TestSetup.configTest.toURI()).toFile().getAbsolutePath) TestSetup.setup() val systemName = "FEY-TEST" implicit val system = ActorSystem(systemName, ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]""")) system.eventStream.publish(TestEvent.Mute(EventFilter.debug())) system.eventStream.publish(TestEvent.Mute(EventFilter.info())) system.eventStream.publish(TestEvent.Mute(EventFilter.warning())) system.eventStream.publish(TestEvent.Mute(EventFilter.error())) val globalIdentifierName = "GLOBAL-IDENTIFIER" val globalIdentifierRef = system.actorOf(Props[IdentifyFeyActors],globalIdentifierName) override protected def afterAll(): Unit = { //Force reload of GenericActor's jar Utils.loadedJars.remove("fey-test-actor.jar") Monitor.events.removeAllNodes() Await.ready(system.terminate(), 20.seconds) } implicit class TestProbeOps(probe: TestProbe) { def expectActor(path: String, max: FiniteDuration = 3.seconds): ActorRef = { probe.within(max) { var actor = null: ActorRef probe.awaitAssert { (probe.system actorSelection path).tell(Identify(path), probe.ref) probe.expectMsgPF(100 milliseconds) { case ActorIdentity(`path`, Some(ref)) => actor = ref } } actor } } def expectActorInSystem(path: String, lookInSystem: ActorSystem, max: FiniteDuration = 3.seconds): ActorRef = { probe.within(max) { var actor = null: ActorRef probe.awaitAssert { (lookInSystem actorSelection path).tell(Identify(path), probe.ref) probe.expectMsgPF(100 milliseconds) { case ActorIdentity(`path`, Some(ref)) => actor = ref } } actor } } def verifyActorTermination(actor: ActorRef)(implicit system: ActorSystem): Unit = { val watcher = TestProbe() watcher.watch(actor) watcher.expectTerminated(actor) } def notExpectActor(path: String, max: FiniteDuration = 3.seconds): Unit = { probe.within(max) { probe.awaitAssert { (probe.system actorSelection path).tell(Identify(path), probe.ref) probe.expectMsgPF(100 milliseconds) { case ActorIdentity(`path`, None) => } } } } def isThreadRunning(threadName: String): Boolean = { Thread.getAllStackTraces.keySet().toArray .map(_.asInstanceOf[Thread]) .find(_.getName == threadName) match { case Some(thread) => if(thread.isAlive) true else false case None => false } } } //Utils Functions def getJSValueFromString(json: String): JsValue = { Json.parse(json) } }
Example 40
Source File: FeyGenericActorReceiverTest.scala From incubator-retired-iota with Apache License 2.0 | 5 votes |
package org.apache.iota.fey import akka.actor.ActorRef import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.duration.FiniteDuration class FeyGenericActorReceiverTest(override val params: Map[String,String] = Map.empty, override val backoff: FiniteDuration = 1.minutes, override val connectTo: Map[String,ActorRef] = Map.empty, override val schedulerTimeInterval: FiniteDuration = 2.seconds, override val orchestrationName: String = "", override val orchestrationID: String = "", override val autoScale: Boolean = false) extends FeyGenericActorReceiver { override def customReceive:Receive = { case "PROPAGATE" => propagateMessage("PROPAGATE-CALLED") case x => log.debug(s"Message not treated: $x") } override def getJSONString[T](input: T): String = { input match{ case "VALID_JSON" => Utils_JSONTest.create_json_test case "INVALID_JSON" => Utils_JSONTest.test_json_schema_invalid case "JSON_LOCATION" => Utils_JSONTest.location_test_2 } } var count = 0 var started = false var executing = false var stopped = false var restarted = false override def onStart(): Unit = { started = true } override def execute(): Unit = { log.info(s"Executing action in ${self.path.name}") executing = true } override def onStop(): Unit = { log.info(s"Actor ${self.path.name} stopped.") stopped = true } override def onRestart(reason: Throwable): Unit = { restarted = true } }
Example 41
Source File: PersistOnEventWithRecoverySpecLeveldb.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate import java.util.UUID import akka.actor.Actor import akka.actor.ActorRef import akka.actor.Props import akka.testkit.TestProbe import com.rbmhtechnology.eventuate.ReplicationIntegrationSpec.replicationConnection import com.rbmhtechnology.eventuate.utilities._ import org.apache.commons.io.FileUtils import org.scalatest.Matchers import org.scalatest.WordSpec import scala.concurrent.duration.DurationInt object PersistOnEventWithRecoverySpecLeveldb { class OnBEmitRandomActor(val eventLog: ActorRef, probe: TestProbe) extends EventsourcedActor with PersistOnEvent { override def id = getClass.getName override def onCommand = Actor.emptyBehavior override def onEvent = { case "A" => case "B" => persistOnEvent(UUID.randomUUID().toString) case uuid: String => probe.ref ! uuid } } def persistOnEventProbe(locationA1: Location, log: ActorRef) = { val probe = locationA1.probe locationA1.system.actorOf(Props(new OnBEmitRandomActor(log, probe))) probe } val noMsgTimeout = 100.millis } class PersistOnEventWithRecoverySpecLeveldb extends WordSpec with Matchers with MultiLocationSpecLeveldb { import RecoverySpecLeveldb._ import PersistOnEventWithRecoverySpecLeveldb._ override val logFactory: String => Props = id => SingleLocationSpecLeveldb.TestEventLog.props(id, batching = true) "An EventsourcedActor with PersistOnEvent" must { "not re-attempt persistence on successful write after reordering of events through disaster recovery" in { val locationB = location("B", customConfig = RecoverySpecLeveldb.config) def newLocationA = location("A", customConfig = RecoverySpecLeveldb.config) val locationA1 = newLocationA val endpointB = locationB.endpoint(Set("L1"), Set(replicationConnection(locationA1.port))) def newEndpointA(l: Location, activate: Boolean) = l.endpoint(Set("L1"), Set(replicationConnection(locationB.port)), activate = activate) val endpointA1 = newEndpointA(locationA1, activate = true) val targetA = endpointA1.target("L1") val logDirA = logDirectory(targetA) val targetB = endpointB.target("L1") val a1Probe = persistOnEventProbe(locationA1, targetA.log) write(targetA, List("A")) write(targetB, List("B")) val event = a1Probe.expectMsgClass(classOf[String]) assertConvergence(Set("A", "B", event), endpointA1, endpointB) locationA1.terminate().await FileUtils.deleteDirectory(logDirA) val locationA2 = newLocationA val endpointA2 = newEndpointA(locationA2, activate = false) endpointA2.recover().await val a2Probe = persistOnEventProbe(locationA2, endpointA2.logs("L1")) a2Probe.expectMsg(event) a2Probe.expectNoMsg(noMsgTimeout) assertConvergence(Set("A", "B", event), endpointA2, endpointB) } } }
Example 42
Source File: ConsulCoordinationSpec.scala From constructr-consul with Apache License 2.0 | 5 votes |
package com.tecsisa.constructr.coordination.consul import akka.Done import akka.actor.{ ActorSystem, AddressFromURIString } import akka.testkit.{ TestDuration, TestProbe } import com.typesafe.config.ConfigFactory import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } import scala.concurrent.duration.{ Duration, DurationInt, FiniteDuration } import scala.concurrent.{ Await, Awaitable } import scala.util.Random object ConsulCoordinationSpec { private val coordinationHost = { val dockerHostPattern = """tcp://(\S+):\d{1,5}""".r sys.env .get("DOCKER_HOST") .collect { case dockerHostPattern(address) => address } .getOrElse("127.0.0.1") } } class ConsulCoordinationSpec extends WordSpec with Matchers with BeforeAndAfterAll { import ConsulCoordinationSpec._ private implicit val system = { val config = ConfigFactory .parseString(s"constructr.coordination.host = $coordinationHost") .withFallback(ConfigFactory.load()) ActorSystem("default", config) } private val address1 = AddressFromURIString("akka.tcp://default@a:2552") private val address2 = AddressFromURIString("akka.tcp://default@b:2552") "ConsulCoordination" should { "correctly interact with consul" in { val coordination = new ConsulCoordination(randomString(), system) // Getting nodes resultOf(coordination.getNodes()) shouldBe 'empty // Lock (ttl >= 10s) resultOf(coordination.lock(address1, 10.seconds)) shouldBe true resultOf(coordination.lock(address1, 10.seconds)) shouldBe true resultOf(coordination.lock(address2, 10.seconds)) shouldBe false // Add self resultOf(coordination.addSelf(address1, 10.seconds)) shouldBe Done resultOf(coordination.getNodes()) shouldBe Set(address1) // Refresh resultOf(coordination.refresh(address1, 10.seconds)) shouldBe Done resultOf(coordination.getNodes()) shouldBe Set(address1) val probe = TestProbe() import probe._ awaitAssert( resultOf(coordination.getNodes()) shouldBe 'empty, 25.seconds // Wait until open sessions expire ) } } override protected def afterAll() = { Await.ready(system.terminate(), Duration.Inf) super.afterAll() } private def resultOf[A](awaitable: Awaitable[A], max: FiniteDuration = 3.seconds.dilated) = Await.result(awaitable, max) private def randomString() = math.abs(Random.nextInt).toString }
Example 43
Source File: PlanResultInterpreter.scala From rug with GNU General Public License v3.0 | 5 votes |
package com.atomist.rug.runtime.plans import com.atomist.rug.spi.Handlers.Status.{Failure, Success} import com.atomist.rug.spi.Handlers._ import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.DurationInt object PlanResultInterpreter { def interpret(planResult: PlanResult): Response = { if (hasLogFailure(planResult.log)) { Response(Failure) } else { Response(Success) } } @tailrec def hasLogFailure(log: Seq[PlanLogEvent]): Boolean = { log.headOption match { case Some(head) => head match { case _: PlanLogError => true case result: InstructionResult if result.response.status == Failure => true case result: NestedPlanRun => val planResult = result.planResult hasLogFailure(log.tail ++ planResult.log) case _ => hasLogFailure(log.tail) } case None => false } } }
Example 44
Source File: PlanResultLogger.scala From rug with GNU General Public License v3.0 | 5 votes |
package com.atomist.rug.runtime.plans import com.atomist.rug.spi.Handlers.Status.Failure import com.atomist.rug.spi.Handlers._ import org.slf4j.Logger import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.DurationInt class PlanResultLogger(val logger: Logger) { def log(planResult: PlanResult): Unit = { logEvents(planResult.log) } @tailrec private def logEvents(log: Seq[PlanLogEvent]): Unit = { log.headOption match { case Some(head) => val remainingEvents = head match { case logError: PlanLogError => logger.error("Error running plan.", logError.error) log.tail case result: InstructionResult if result.response.status == Failure => logger.error("Failure running plan.", result) log.tail case result: NestedPlanRun => val planResult = result.planResult log.tail ++ planResult.log case _ => log.tail } logEvents(remainingEvents) case None => } } }
Example 45
Source File: RemoteTranslationsDemo.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.guide.views.ext.demo import io.udash.css.CssView import io.udash.web.guide.components.BootstrapUtils import io.udash.web.guide.demos.AutoDemo import io.udash.web.guide.styles.partials.GuideStyles import scalatags.JsDom.all._ object RemoteTranslationsDemo extends AutoDemo with CssView { private val (rendered, source) = { import io.udash.i18n._ import io.udash.web.guide.Context.serverRpc import io.udash.web.guide.demos.i18n.Translations import org.scalajs.dom.ext.LocalStorage import scalatags.JsDom.all._ import scala.concurrent.duration.DurationInt implicit val translationProvider: RemoteTranslationProvider = new RemoteTranslationProvider( serverRpc.demos.translations, Some(LocalStorage), 6.hours ) implicit val lang: Lang = Lang("pl") div( ul( li( "auth.loginLabel: ", Translations.auth.loginLabel.translated() ), li( "auth.passwordLabel: ", Translations.auth.passwordLabel.translated() ), li( "auth.login.buttonLabel: ", Translations.auth.login.buttonLabel.translated() ), li( "auth.login.retriesLeft: ", Translations.auth.login.retriesLeft(3).translated() ), li( "auth.login.retriesLeftOne: ", Translations.auth.login.retriesLeftOne.translated() ), li( "auth.register.buttonLabel: ", Translations.auth.register.buttonLabel.translated() ) ) ) }.withSourceCode override protected def demoWithSource(): (Modifier, Iterator[String]) = { ( div( BootstrapUtils.wellStyles, id := "rpc-translations-demo", GuideStyles.frame, GuideStyles.useBootstrap )(rendered), source.linesIterator ) } }
Example 46
Source File: DynamicRemoteTranslationsDemo.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.guide.views.ext.demo import io.udash.properties.single.Property import io.udash.web.guide.demos.AutoDemo import io.udash.web.guide.styles.partials.GuideStyles import scalatags.JsDom.all._ object DynamicRemoteTranslationsDemo extends AutoDemo { private val (rendered, source) = { import io.udash.bootstrap.utils.BootstrapStyles._ import io.udash.css.CssView._ import io.udash.i18n._ import io.udash.web.guide.Context.serverRpc import io.udash.web.guide.demos.i18n.Translations import org.scalajs.dom.Event import org.scalajs.dom.ext.LocalStorage import scalatags.JsDom.all._ import scala.concurrent.duration.DurationInt implicit val translationProvider: RemoteTranslationProvider = new RemoteTranslationProvider( serverRpc.demos.translations, Some(LocalStorage), 6.hours ) implicit val lang: Property[Lang] = Property(Lang("en")) div( button( Button.btn, Button.color(Color.Primary) )(id := "enButton", onclick := ((_: Event) => lang.set(Lang("en"))))("EN"), " ", button( Button.btn, Button.color(Color.Primary) )(id := "plButton", onclick := ((_: Event) => lang.set(Lang("pl"))))("PL"), div(Card.card, Card.body, Background.color(Color.Light), Spacing.margin( side = Side.Top, size = SpacingSize.Normal ))(ul( li( "auth.loginLabel: ", Translations.auth.loginLabel.translatedDynamic() ), li( "auth.passwordLabel: ", Translations.auth.passwordLabel.translatedDynamic() ), li( "auth.login.buttonLabel: ", Translations.auth.login.buttonLabel.translatedDynamic() ), li( "auth.login.retriesLeft: ", Translations.auth.login.retriesLeft(3).translatedDynamic() ), li( "auth.login.retriesLeftOne: ", Translations.auth.login.retriesLeftOne.translatedDynamic() ), li( "auth.register.buttonLabel: ", Translations.auth.register.buttonLabel.translatedDynamic() ) )) ) }.withSourceCode override protected def demoWithSource(): (Modifier, Iterator[String]) = { import io.udash.css.CssView._ ( div( id := "dynamic-rpc-translations-demo", GuideStyles.frame, GuideStyles.useBootstrap )(rendered), source.linesIterator ) } }
Example 47
Source File: PopoversDemo.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.guide.views.ext.demo.bootstrap import io.udash.css.CssView import io.udash.web.guide.demos.AutoDemo import io.udash.web.guide.styles.partials.GuideStyles import scalatags.JsDom.all._ object PopoversDemo extends AutoDemo with CssView { private val (rendered, source) = { import io.udash._ import io.udash.bootstrap._ import BootstrapStyles._ import io.udash.bootstrap.badge.UdashBadge import io.udash.bootstrap.button.UdashButton import io.udash.bootstrap.tooltip.UdashPopover import scalatags.JsDom.all._ import scala.concurrent.duration.DurationInt val popoverContainerId = ComponentId("popover-container") val label1 = UdashBadge()(_ => Seq[Modifier]( "Popover on hover with delay", Spacing.margin(size = SpacingSize.Small) )).render UdashPopover( trigger = Seq(UdashPopover.Trigger.Hover), delay = UdashPopover.Delay(500.millis, 250.millis), title = "Popover...", content = "Content...".render, container = Some(s"#$popoverContainerId") )(label1) val label2 = UdashBadge()(_ => Seq[Modifier]( "Popover on click", Spacing.margin(size = SpacingSize.Small) )).render UdashPopover( trigger = Seq(UdashPopover.Trigger.Click), delay = UdashPopover.Delay(0.millis, 250.millis), placement = UdashPopover.Placement.Bottom, title = "Popover 2...", content = "Content...".render, container = Some(s"#$popoverContainerId") )(label2) val label3 = UdashBadge()(_ => Seq[Modifier]( "Popover with JS toggler", Spacing.margin(size = SpacingSize.Small) )).render val label3Tooltip = UdashPopover( trigger = Seq(UdashPopover.Trigger.Manual), placement = UdashPopover.Placement.Left, html = true, title = "Popover 3...", content = Seq( p("HTML content..."), ul(li("Item 1"), li("Item 2"), li("Item 3")) ).render, container = Some(s"#$popoverContainerId") )(label3) val button = UdashButton()("Toggle popover") button.listen { case _ => label3Tooltip.toggle() } div(popoverContainerId)( label1, label2, label3, button ).render }.withSourceCode override protected def demoWithSource(): (Modifier, Iterator[String]) = { (rendered.setup(_.applyTags(GuideStyles.frame)), source.linesIterator) } }
Example 48
Source File: TooltipsDemo.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.guide.views.ext.demo.bootstrap import io.udash.web.guide.demos.AutoDemo import io.udash.web.guide.styles.partials.GuideStyles import scalatags.JsDom.all._ object TooltipsDemo extends AutoDemo { private val (rendered, source) = { import io.udash._ import io.udash.bootstrap._ import BootstrapStyles._ import io.udash.bootstrap.badge.UdashBadge import io.udash.bootstrap.button.UdashButton import io.udash.bootstrap.tooltip.UdashTooltip import io.udash.css.CssView._ import scalatags.JsDom.all._ import scala.concurrent.duration.DurationInt val tooltipContainerId = ComponentId("tooltip-container") val label1 = UdashBadge()(_ => Seq[Modifier]( "Tooltip on hover with delay", Spacing.margin(size = SpacingSize.Small) )).render UdashTooltip( trigger = Seq(UdashTooltip.Trigger.Hover), delay = UdashTooltip.Delay(500.millis, 250.millis), title = "Tooltip...", container = Some(s"#$tooltipContainerId") )(label1) val label2 = UdashBadge()(_ => Seq[Modifier]( "Tooltip on click", Spacing.margin(size = SpacingSize.Small) )).render UdashTooltip( trigger = Seq(UdashTooltip.Trigger.Click), delay = UdashTooltip.Delay(0.millis, 250.millis), placement = UdashTooltip.Placement.Bottom, title = "Tooltip 2...", container = Some(s"#$tooltipContainerId") )(label2) val label3 = UdashBadge()(_ => Seq[Modifier]( "Tooltip with JS toggler", Spacing.margin(size = SpacingSize.Small) )).render val label3Tooltip = UdashTooltip( trigger = Seq(UdashTooltip.Trigger.Manual), placement = UdashTooltip.Placement.Right, title = "Tooltip 3...", container = Some(s"#$tooltipContainerId") )(label3) val button = UdashButton()("Toggle tooltip") button.listen { case _ => label3Tooltip.toggle() } div(tooltipContainerId)( label1, label2, label3, button ).render }.withSourceCode override protected def demoWithSource(): (Modifier, Iterator[String]) = { import io.udash.css.CssView._ (rendered.setup(_.applyTags(GuideStyles.frame)), source.linesIterator) } }
Example 49
Source File: GuideStyleUtils.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.guide.styles.utils import io.udash.css.{CssBase, CssStyle} import io.udash.web.commons.styles.utils.StyleConstants import scalacss.internal.Macros.Color import scalacss.internal.{AV, Attr, Length} import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.language.postfixOps object GuideStyleUtils extends CssBase { import dsl._ val relativeMiddle: CssStyle = mixin( top(50 %%), transform := "translateY(-50%)", position.relative ) def transition(property: Attr = all, duration: FiniteDuration = 250 milliseconds): CssStyle = mixin( transitionProperty := property.toString(), transitionDuration(duration), transitionTimingFunction.easeInOut ) def border(bColor: Color = StyleConstants.Colors.GreyExtra, bWidth: Length[Double] = 1.0 px, bStyle: AV = borderStyle.solid): CssStyle = mixin( borderWidth(bWidth), bStyle, borderColor(bColor) ) }
Example 50
Source File: CommonStyleUtils.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.commons.styles.utils import io.udash.css.{CssBase, CssStyle} import scalacss.internal.Macros.Color import scalacss.internal.{AV, Attr, Length} import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.language.postfixOps object CommonStyleUtils extends CssBase { import dsl._ val middle: CssStyle = mixin( top(50 %%), transform := "translateY(-50%)" ) val center: CssStyle = mixin( top(50 %%), left(50 %%), transform := "translateY(-50%) translateX(-50%)" ) val relativeMiddle: CssStyle = mixin( middle, position.relative ) val absoluteMiddle: CssStyle = mixin( middle, position.absolute ) val absoluteCenter: CssStyle = mixin( center, position.absolute ) def transition(property: Attr = all, duration: FiniteDuration = 250 milliseconds): CssStyle = style( transitionProperty := property.toString(), transitionDuration(duration), transitionTimingFunction.easeInOut ) def border(bColor: Color = StyleConstants.Colors.GreyExtra, bWidth: Length[Double] = 1.0 px, bStyle: AV = borderStyle.solid): CssStyle = style( borderWidth(bWidth), bStyle, borderColor(bColor) ) }
Example 51
Source File: HeaderStyles.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.web.homepage.styles.partials import io.udash.css.{CssBase, CssStyle} import io.udash.web.commons.styles.attributes.Attributes import io.udash.web.commons.styles.components.{HeaderButtonsStyles, HeaderNavStyles} import io.udash.web.commons.styles.utils.{CommonStyleUtils, MediaQueries, StyleConstants} import scalacss.internal.Literal import scala.concurrent.duration.DurationInt import scala.language.postfixOps object HeaderStyles extends CssBase with HeaderButtonsStyles with HeaderNavStyles { import dsl._ val header: CssStyle = style( position.absolute, top(`0`), left(`0`), width(100 %%), height(StyleConstants.Sizes.LandingPageHeaderHeight px), fontSize(1 rem), zIndex(999), &.attr(Attributes.data(Attributes.Pinned), "true")( position.fixed, height(StyleConstants.Sizes.HeaderHeightPin px), backgroundColor.black, animationName(headerAnimation), animationIterationCount.count(1), animationDuration(300 milliseconds), MediaQueries.tabletLandscape( height(StyleConstants.Sizes.HeaderHeightPin * .85 px) ), unsafeChild(s".${headerLogo.className}")( width(48 px), height(56 px), backgroundImage := "url(/assets/images/udash_logo.png)", MediaQueries.tabletPortrait( display.none ) ), unsafeChild(s".${btnMobile.className}")( CommonStyleUtils.middle ) ), MediaQueries.tabletPortrait( height(StyleConstants.Sizes.HeaderHeight * .9 px) ) ) private lazy val headerAnimation: CssStyle = keyframes( 0d -> keyframe( transform := "translateY(-100%)" ), 100d -> keyframe( transform := "translateY(0)" ) ) val headerLeft: CssStyle = style( position.relative, float.left, height(100 %%) ) lazy val headerLogo: CssStyle = style( CommonStyleUtils.relativeMiddle, display.inlineBlock, verticalAlign.middle, width(65 px), height(96 px), marginRight(25 px), backgroundImage := "url(/assets/images/udash_logo_l.png)", backgroundRepeat.noRepeat, backgroundSize := "100%", MediaQueries.tabletPortrait( display.block, width(StyleConstants.Sizes.GuideHeaderHeightMobile px), height(14 px), backgroundPosition := Literal.bottom, transform := none, top.auto ) ) lazy val btnMobile: CssStyle = style( position.relative ) }
Example 52
Source File: UsesServerRPC.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.rpc.internals import com.avsystem.commons.SharedExtensions._ import io.udash.rpc._ import io.udash.utils.{CallbacksHandler, Registration} import org.scalajs.dom import scala.concurrent.duration.{Duration, DurationInt} import scala.concurrent.{Future, Promise} import scala.scalajs.js import scala.scalajs.js.Dictionary def onCallFailure(callback: exceptionCallbacks.CallbackType): Registration = exceptionCallbacks.register(callback) private def handleException(ex: Throwable): Unit = exceptionCallbacks.fire(ex) def handleResponse(response: RpcResponse): Unit = { pendingCalls.remove(response.callId) .foreach { promise => response match { case RpcResponseSuccess(r, _) => promise.success(r) case RpcResponseException(_, exception, _) => handleException(exception) promise.failure(exception) case RpcResponseFailure(cause, error, _) => val exception = RpcFailure(cause, error) handleException(exception) promise.failure(exception) } } } override protected[rpc] def fireRemote(getterChain: List[RpcInvocation], invocation: RpcInvocation): Unit = sendRpcRequest(RpcFire(invocation, getterChain)) protected[rpc] def callRemote(callId: String, getterChain: List[RpcInvocation], invocation: RpcInvocation): Unit = sendRpcRequest(RpcCall(invocation, getterChain, callId)) private def sendRpcRequest(request: RpcRequest): Unit = connector.sendRpcRequest(request) protected class RawRemoteRPC(getterChain: List[RpcInvocation]) extends ServerRawRpc { def fire(invocation: RpcInvocation): Unit = fireRemote(getterChain, invocation) def call(invocation: RpcInvocation): Future[JsonStr] = Promise[JsonStr]().setup { promise => val callId = newCallId() callRemote(callId, getterChain, invocation) pendingCalls.put(callId, promise) dom.window.setTimeout( () => handleResponse(RpcResponseException("Request timeout", UsesServerRPC.CallTimeout(callTimeout), callId)), callTimeout.toMillis.toDouble ) }.future def get(invocation: RpcInvocation): ServerRawRpc = new RawRemoteRPC(invocation :: getterChain) } } object UsesServerRPC { case class CallTimeout(callTimeout: Duration) extends RuntimeException(s"Response missing after $callTimeout.") }
Example 53
Source File: TooltipUtils.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.bootstrap package tooltip import com.avsystem.commons.misc.{AbstractValueEnum, AbstractValueEnumCompanion, EnumCtx} import io.udash.wrappers.jquery._ import org.scalajs.dom import scala.collection.mutable import scala.concurrent.duration.{Duration, DurationInt} import scala.scalajs.js import scala.scalajs.js.| trait Tooltip extends Listenable { override final type EventType = TooltipEvent def apply( animation: Boolean = true, boundary: String | dom.Node = "scrollParent", container: Option[String | dom.Node] = None, content: js.Function1[dom.Node, String] | dom.Node = io.udash.emptyStringNode(), delay: Delay | Long = Delay(0 millis, 0 millis), html: Boolean = false, offset: Int | String = "0", placement: Placement = defaultPlacement, template: Option[String] = None, title: String | js.Function1[dom.Node, String] | dom.Node = "", trigger: Seq[Trigger] = defaultTrigger )(el: dom.Node): TooltipType = initTooltip( js.Dictionary( "animation" -> animation, "boundary" -> boundary, "container" -> container.getOrElse(false), "content" -> content, "delay" -> delay, "html" -> html, "offset" -> offset, "placement" -> placement.jsValue, "template" -> template.getOrElse(defaultTemplate), "title" -> title, "trigger" -> trigger.map(_.jsValue).mkString(" ") ) )(el) protected def initTooltip(options: js.Dictionary[Any])(el: dom.Node): TooltipType protected val defaultPlacement: Placement protected val defaultTemplate: String protected val defaultTrigger: Seq[Trigger] }
Example 54
Source File: TextArea.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.bindings.inputs import io.udash._ import org.scalajs.dom.html.TextArea import org.scalajs.dom.{Element, Event, KeyboardEvent} import scalatags.JsDom.all._ import scala.concurrent.duration.{Duration, DurationInt} def apply(value: Property[String], debounce: Duration = 20 millis)(textareaModifiers: Modifier*): InputBinding[TextArea] = new InputBinding[TextArea] { private val element = textarea( textareaModifiers, nestedInterceptor(new TextAreaModifier(value, Some(debounce))) ).render override def render: TextArea = element } private class TextAreaModifier(property: Property[String], debounce: Option[Duration]) extends TextInputsModifier(property, debounce) { override def elementValue(t: Element): String = t.asInstanceOf[TextArea].value override def setElementValue(t: Element, v: String): Unit = t.asInstanceOf[TextArea].value = v override def setElementKeyUp(t: Element, callback: KeyboardEvent => Unit): Unit = t.asInstanceOf[TextArea].onkeyup = callback override def setElementOnChange(t: Element, callback: Event => Unit): Unit = t.asInstanceOf[TextArea].onchange = callback override def setElementOnInput(t: Element, callback: Event => Unit): Unit = t.asInstanceOf[TextArea].oninput = callback override def setElementOnPaste(t: Element, callback: Event => Unit): Unit = t.asInstanceOf[TextArea].onpaste = callback } }
Example 55
Source File: ExecutionContextFactoryTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.utils.test import scala.concurrent.Await import scala.concurrent.Future import scala.concurrent.duration.DurationInt import org.junit.runner.RunWith import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import common.WskActorSystem import org.apache.openwhisk.utils.ExecutionContextFactory.FutureExtensions @RunWith(classOf[JUnitRunner]) class ExecutionContextFactoryTests extends FlatSpec with Matchers with WskActorSystem { behavior of "future extensions" it should "take first to complete" in { val f1 = Future.successful({}).withTimeout(500.millis, new Throwable("error")) Await.result(f1, 1.second) shouldBe ({}) val failure = new Throwable("error") val f2 = Future { Thread.sleep(1.second.toMillis) }.withTimeout(500.millis, failure) a[Throwable] shouldBe thrownBy { Await.result(f2, 1.seconds) } } }
Example 56
Source File: NestedSemaphoreTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.common import common.ConcurrencyHelpers import org.apache.openwhisk.utils.ExecutionContextFactory import org.junit.runner.RunWith import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import scala.concurrent.duration.DurationInt @RunWith(classOf[JUnitRunner]) class NestedSemaphoreTests extends FlatSpec with Matchers with ConcurrencyHelpers { // use an infinite thread pool to allow for maximum concurrency implicit val executionContext = ExecutionContextFactory.makeCachedThreadPoolExecutionContext() val acquireTimeout = 1.minute behavior of "NestedSemaphore" it should "allow acquire of concurrency permits before acquire of memory permits" in { val s = new NestedSemaphore[String](20) s.availablePermits shouldBe 20 val actionId = "action1" val actionConcurrency = 5 val actionMemory = 3 //use all concurrency on a single slot concurrently(5, acquireTimeout) { s.tryAcquireConcurrent(actionId, actionConcurrency, actionMemory) } should contain only true s.availablePermits shouldBe 20 - 3 //we used a single container (memory == 3) s.concurrentState(actionId).availablePermits shouldBe 0 //use up all the remaining memory (17) and concurrency slots (17 / 3 * 5 = 25) concurrently(25, acquireTimeout) { s.tryAcquireConcurrent(actionId, actionConcurrency, actionMemory) } should contain only true s.availablePermits shouldBe 2 //we used 18 (20/3 = 6, 6*3=18) s.concurrentState(actionId).availablePermits shouldBe 0 s.tryAcquireConcurrent("action1", actionConcurrency, actionMemory) shouldBe false } it should "not give away more permits even under concurrent load" in { // 100 iterations of this test (0 until 100).foreach { _ => val s = new NestedSemaphore(32) // try to acquire more permits than allowed in parallel val acquires = concurrently(64, acquireTimeout)(s.tryAcquire()) val result = Seq.fill(32)(true) ++ Seq.fill(32)(false) acquires should contain theSameElementsAs result } } }
Example 57
Source File: ForcibleSemaphoreTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.common import common.ConcurrencyHelpers import org.apache.openwhisk.utils.ExecutionContextFactory import org.junit.runner.RunWith import org.scalatest.{FlatSpec, Matchers} import org.scalatest.junit.JUnitRunner import scala.concurrent.duration.DurationInt @RunWith(classOf[JUnitRunner]) class ForcibleSemaphoreTests extends FlatSpec with Matchers with ConcurrencyHelpers { // use an infinite thread pool to allow for maximum concurrency implicit val executionContext = ExecutionContextFactory.makeCachedThreadPoolExecutionContext() behavior of "ForcableSemaphore" it should "not allow to acquire, force or release negative amounts of permits" in { val s = new ForcibleSemaphore(2) an[IllegalArgumentException] should be thrownBy s.tryAcquire(0) an[IllegalArgumentException] should be thrownBy s.tryAcquire(-1) an[IllegalArgumentException] should be thrownBy s.forceAcquire(0) an[IllegalArgumentException] should be thrownBy s.forceAcquire(-1) an[IllegalArgumentException] should be thrownBy s.release(0) an[IllegalArgumentException] should be thrownBy s.release(-1) } it should "allow to acquire the defined amount of permits only" in { val s = new ForcibleSemaphore(2) s.tryAcquire() shouldBe true // 1 permit left s.tryAcquire() shouldBe true // 0 permits left s.tryAcquire() shouldBe false val s2 = new ForcibleSemaphore(4) s2.tryAcquire(5) shouldBe false // only 4 permits available s2.tryAcquire(3) shouldBe true // 1 permit left s2.tryAcquire(2) shouldBe false // only 1 permit available s2.tryAcquire() shouldBe true } it should "allow to release permits again" in { val s = new ForcibleSemaphore(2) s.tryAcquire() shouldBe true // 1 permit left s.tryAcquire() shouldBe true // 0 permits left s.tryAcquire() shouldBe false s.release() // 1 permit left s.tryAcquire() shouldBe true s.release(2) // 1 permit left s.tryAcquire(2) shouldBe true } it should "allow to force permits, delaying the acceptance of 'usual' permits until all of forced permits are released" in { val s = new ForcibleSemaphore(2) s.tryAcquire(2) shouldBe true // 0 permits left s.forceAcquire(5) // -5 permits left s.tryAcquire() shouldBe false s.release(4) // -1 permits left s.tryAcquire() shouldBe false s.release() // 0 permits left s.tryAcquire() shouldBe false s.release() // 1 permit left s.tryAcquire() shouldBe true } it should "not give away more permits even under concurrent load" in { // 100 iterations of this test (0 until 100).foreach { _ => val s = new ForcibleSemaphore(32) // try to acquire more permits than allowed in parallel val acquires = concurrently(64, 1.minute)(s.tryAcquire()) val result = Seq.fill(32)(true) ++ Seq.fill(32)(false) acquires should contain theSameElementsAs result } } }
Example 58
Source File: MaxActionDurationTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.limits import java.io.File import scala.concurrent.duration.DurationInt import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import common.{ConcurrencyHelpers, TestHelpers, TestUtils, WskActorSystem, WskProps, WskTestHelpers} import common.rest.WskRestOperations import org.apache.openwhisk.core.entity._ import spray.json.DefaultJsonProtocol._ import spray.json._ import org.apache.openwhisk.http.Messages import org.apache.openwhisk.core.entity.TimeLimit import org.scalatest.tagobjects.Slow "node-, python, and java-action" should s"run up to the max allowed duration (${TimeLimit.MAX_DURATION})" taggedAs (Slow) in withAssetCleaner( wskprops) { (wp, assetHelper) => // When you add more runtimes, keep in mind, how many actions can be processed in parallel by the Invokers! val runtimes = Map("node" -> "helloDeadline.js", "python" -> "sleep.py", "java" -> "sleep.jar") .filter { case (_, name) => new File(TestUtils.getTestActionFilename(name)).exists() } concurrently(runtimes.toSeq, TimeLimit.MAX_DURATION + 2.minutes) { case (k, name) => println(s"Testing action kind '${k}' with action '${name}'") assetHelper.withCleaner(wsk.action, name) { (action, _) => val main = if (k == "java") Some("Sleep") else None action.create( name, Some(TestUtils.getTestActionFilename(name)), timeout = Some(TimeLimit.MAX_DURATION), main = main) } val run = wsk.action.invoke( name, Map("forceHang" -> true.toJson, "sleepTimeInMs" -> (TimeLimit.MAX_DURATION + 30.seconds).toMillis.toJson)) withActivation( wsk.activation, run, initialWait = 1.minute, pollPeriod = 1.minute, totalWait = TimeLimit.MAX_DURATION + 2.minutes) { activation => withClue("Activation result not as expected:") { activation.response.status shouldBe ActivationResponse.messageForCode(ActivationResponse.DeveloperError) activation.response.result shouldBe Some( JsObject("error" -> Messages.timedoutActivation(TimeLimit.MAX_DURATION, init = false).toJson)) activation.duration.toInt should be >= TimeLimit.MAX_DURATION.toMillis.toInt } } } } }
Example 59
Source File: CosmosDBLeakTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.cosmosdb import akka.stream.scaladsl.{Sink, Source} import io.netty.util.ResourceLeakDetector import io.netty.util.ResourceLeakDetector.Level import org.apache.openwhisk.common.TransactionId import org.apache.openwhisk.core.entity.{ BasicAuthenticationAuthKey, Identity, Namespace, Secret, Subject, UUID, WhiskAuth, WhiskNamespace } import org.junit.runner.RunWith import org.scalatest.FlatSpec import org.scalatest.junit.JUnitRunner import scala.concurrent.duration.DurationInt @RunWith(classOf[JUnitRunner]) class CosmosDBLeakTests extends FlatSpec with CosmosDBStoreBehaviorBase { behavior of s"CosmosDB leak" private var initialLevel: Level = _ override protected def beforeAll(): Unit = { RecordingLeakDetectorFactory.register() initialLevel = ResourceLeakDetector.getLevel ResourceLeakDetector.setLevel(Level.PARANOID) super.beforeAll() } override def afterAll(): Unit = { super.afterAll() ResourceLeakDetector.setLevel(initialLevel) withClue("Recorded leak count should be zero") { RecordingLeakDetectorFactory.counter.cur shouldBe 0 } } it should "not happen in performing subject query" ignore { implicit val tid: TransactionId = transid() val uuid = UUID() val ak = BasicAuthenticationAuthKey(uuid, Secret()) val ns = Namespace(aname(), uuid) val subs = Array(WhiskAuth(Subject(), Set(WhiskNamespace(ns, ak)))) subs foreach (put(authStore, _)) implicit val patienceConfig: PatienceConfig = PatienceConfig(timeout = 30.minutes) Source(1 to 500) .filter(_ => RecordingLeakDetectorFactory.counter.cur == 0) .mapAsync(5) { i => if (i % 5 == 0) println(i) queryName(ns) } .runWith(Sink.ignore) .futureValue System.gc() withClue("Recorded leak count should be zero") { RecordingLeakDetectorFactory.counter.cur shouldBe 0 } } def queryName(ns: Namespace)(implicit tid: TransactionId) = { Identity.list(authStore, List(ns.name.asString), limit = 1) } }
Example 60
Source File: DatabaseScriptTestUtils.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.test import scala.concurrent.duration.DurationInt import scala.io.Source import org.scalatest.Matchers import org.scalatest.concurrent.IntegrationPatience import org.scalatest.concurrent.ScalaFutures import akka.actor.ActorSystem import common.WaitFor import common.WhiskProperties import pureconfig._ import pureconfig.generic.auto._ import spray.json._ import spray.json.DefaultJsonProtocol._ import org.apache.openwhisk.common.Logging import org.apache.openwhisk.core.ConfigKeys import org.apache.openwhisk.core.WhiskConfig import org.apache.openwhisk.core.database.CouchDbRestClient import org.apache.openwhisk.core.database.CouchDbConfig trait DatabaseScriptTestUtils extends ScalaFutures with Matchers with WaitFor with IntegrationPatience { case class DatabaseUrl(dbProtocol: String, dbUsername: String, dbPassword: String, dbHost: String, dbPort: String) { def url = s"$dbProtocol://$dbUsername:$dbPassword@$dbHost:$dbPort" def safeUrl = s"$dbProtocol://$dbHost:$dbPort" } val python = WhiskProperties.python val config = loadConfigOrThrow[CouchDbConfig](ConfigKeys.couchdb) val dbProtocol = config.protocol val dbHost = config.host val dbPort = config.port val dbUsername = config.username val dbPassword = config.password val dbPrefix = WhiskProperties.getProperty(WhiskConfig.dbPrefix) val dbUrl = DatabaseUrl(dbProtocol, dbUsername, dbPassword, dbHost, dbPort.toString) def retry[T](task: => T) = org.apache.openwhisk.utils.retry(task, 10, Some(500.milliseconds)) def waitForView(db: CouchDbRestClient, designDoc: String, viewName: String, numDocuments: Int) = { waitfor(() => { val view = db.executeView(designDoc, viewName)().futureValue view shouldBe 'right view.right.get.fields("rows").convertTo[List[JsObject]].length == numDocuments }, totalWait = 2.minutes) } }
Example 61
Source File: ActivationStoreBehaviorBase.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.test.behavior import java.time.Instant import akka.stream.ActorMaterializer import common.{StreamLogging, WskActorSystem} import org.apache.openwhisk.common.TransactionId import org.apache.openwhisk.core.database.{ActivationStore, CacheChangeNotification, UserContext} import org.apache.openwhisk.core.database.test.behavior.ArtifactStoreTestUtil.storeAvailable import org.apache.openwhisk.core.entity._ import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers, Outcome} import scala.collection.mutable.ListBuffer import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.concurrent.duration.DurationInt import scala.language.postfixOps import scala.util.{Random, Try} trait ActivationStoreBehaviorBase extends FlatSpec with ScalaFutures with Matchers with StreamLogging with WskActorSystem with IntegrationPatience with BeforeAndAfterEach { protected implicit val materializer: ActorMaterializer = ActorMaterializer() protected implicit val notifier: Option[CacheChangeNotification] = None def context: UserContext def activationStore: ActivationStore private val docsToDelete = ListBuffer[(UserContext, ActivationId)]() def storeType: String protected def transId() = TransactionId(Random.alphanumeric.take(32).mkString) override def afterEach(): Unit = { cleanup() stream.reset() } override protected def withFixture(test: NoArgTest): Outcome = { assume(storeAvailable(storeAvailableCheck), s"$storeType not configured or available") val outcome = super.withFixture(test) if (outcome.isFailed) { println(logLines.mkString("\n")) } outcome } protected def storeAvailableCheck: Try[Any] = Try(true) //~----------------------------------------< utility methods > protected def store(activation: WhiskActivation, context: UserContext)( implicit transid: TransactionId, notifier: Option[CacheChangeNotification]): DocInfo = { val doc = activationStore.store(activation, context).futureValue docsToDelete.append((context, ActivationId(activation.docid.asString))) doc } protected def newActivation(ns: String, actionName: String, start: Long): WhiskActivation = { WhiskActivation( EntityPath(ns), EntityName(actionName), Subject(), ActivationId.generate(), Instant.ofEpochMilli(start), Instant.ofEpochMilli(start + 1000)) } def cleanup()(implicit timeout: Duration = 10 seconds): Unit = { implicit val tid: TransactionId = transId() docsToDelete.map { e => Try { Await.result(activationStore.delete(e._2, e._1), timeout) } } docsToDelete.clear() } }
Example 62
Source File: MultipleReadersSingleWriterCacheTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.test import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.Await import scala.concurrent.Future import scala.concurrent.duration.DurationInt import org.junit.runner.RunWith import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import common.StreamLogging import common.WskActorSystem import org.apache.openwhisk.common.TransactionId import org.apache.openwhisk.core.database.CacheChangeNotification import org.apache.openwhisk.core.database.MultipleReadersSingleWriterCache import org.apache.openwhisk.core.entity.CacheKey @RunWith(classOf[JUnitRunner]) class MultipleReadersSingleWriterCacheTests extends FlatSpec with Matchers with MultipleReadersSingleWriterCache[String, String] with WskActorSystem with StreamLogging { behavior of "the cache" it should "execute the callback on invalidating and updating an entry" in { val ctr = new AtomicInteger(0) val key = CacheKey("key") implicit val transId = TransactionId.testing lazy implicit val cacheUpdateNotifier = Some { new CacheChangeNotification { override def apply(key: CacheKey) = { ctr.incrementAndGet() Future.successful(()) } } } // Create an cache entry Await.ready(cacheUpdate("doc", key, Future.successful("db save successful")), 10.seconds) ctr.get shouldBe 1 // Callback should be called if entry exists Await.ready(cacheInvalidate(key, Future.successful(())), 10.seconds) ctr.get shouldBe 2 Await.ready(cacheUpdate("docdoc", key, Future.successful("update in db successful")), 10.seconds) ctr.get shouldBe 3 // Callback should be called if entry does not exist Await.ready(cacheInvalidate(CacheKey("abc"), Future.successful(())), 10.seconds) ctr.get shouldBe 4 } }
Example 63
Source File: ActionsApiWithoutDbPollingTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.controller.test import java.time.Instant import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.sprayJsonUnmarshaller import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.server.Route import org.apache.openwhisk.core.controller.WhiskActionsApi import org.apache.openwhisk.core.controller.actions.ControllerActivationConfig import org.apache.openwhisk.core.database.UserContext import org.apache.openwhisk.core.entity._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import spray.json.DefaultJsonProtocol._ import spray.json._ import scala.concurrent.duration.DurationInt behavior of "Actions API without DB Polling" val creds = WhiskAuthHelpers.newIdentity() val context = UserContext(creds) val namespace = EntityPath(creds.subject.asString) val collectionPath = s"/${EntityPath.DEFAULT}/${collection.path}" def aname() = MakeName.next("action_tests") val actionLimit = Exec.sizeLimit val parametersLimit = Parameters.sizeLimit override val controllerActivationConfig = ControllerActivationConfig(false, 2.seconds) it should "invoke a blocking action which is converted to a non-blocking due to delayed active ack" in { implicit val tid = transid() val action = WhiskAction( namespace, aname(), jsDefault("??"), limits = ActionLimits( TimeLimit(controllerActivationConfig.maxWaitForBlockingActivation - 1.second), MemoryLimit(), LogLimit())) put(entityStore, action) val start = Instant.now Post(s"$collectionPath/${action.name}?blocking=true") ~> Route.seal(routes(creds)) ~> check { // status should be accepted because there is no active ack response and // db polling will fail since there is no record of the activation // as a result, the api handler will convert this to a non-blocking request status should be(Accepted) val duration = Instant.now.toEpochMilli - start.toEpochMilli val response = responseAs[JsObject] response.fields.size shouldBe 1 response.fields("activationId") should not be None headers should contain(RawHeader(ActivationIdHeader, response.fields("activationId").convertTo[String])) // all blocking requests wait up to the specified blocking timeout regadless of the action time limit duration should be >= controllerActivationConfig.maxWaitForBlockingActivation.toMillis } } it should "invoke a blocking action which completes with an activation id only" in { implicit val tid = transid() val action = WhiskAction(namespace, aname(), jsDefault("??")) put(entityStore, action) try { // do not store the activation in the db, instead register it as the response to generate on active ack loadBalancer.whiskActivationStub = Some((1.milliseconds, Left(activationIdFactory.make()))) val start = Instant.now Post(s"$collectionPath/${action.name}?blocking=true") ~> Route.seal(routes(creds)) ~> check { // status should be accepted because the test is simulating a response which only has // an activation id response from the invoker; unlike the previous test, here the invoker // does respond to complete the api handler's waiting promise status should be(Accepted) val duration = Instant.now.toEpochMilli - start.toEpochMilli val response = responseAs[JsObject] response.fields.size shouldBe 1 response.fields("activationId") should not be None headers should contain(RawHeader(ActivationIdHeader, response.fields("activationId").convertTo[String])) duration should be <= 1.second.toMillis } } finally { loadBalancer.whiskActivationStub = None } } }
Example 64
Source File: RateThrottleTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.controller.test import scala.concurrent.duration.DurationInt import org.junit.runner.RunWith import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import common.StreamLogging import org.apache.openwhisk.common.TransactionId import org.apache.openwhisk.core.entitlement._ import org.apache.openwhisk.core.entity.UserLimits @RunWith(classOf[JUnitRunner]) class RateThrottleTests extends FlatSpec with Matchers with StreamLogging { implicit val transid = TransactionId.testing val subject = WhiskAuthHelpers.newIdentity() behavior of "Rate Throttle" it should "throttle when rate exceeds allowed threshold" in { new RateThrottler("test", _ => 0).check(subject).ok shouldBe false val rt = new RateThrottler("test", _ => 1) rt.check(subject).ok shouldBe true rt.check(subject).ok shouldBe false rt.check(subject).ok shouldBe false Thread.sleep(1.minute.toMillis) rt.check(subject).ok shouldBe true } it should "check against an alternative limit if passed in" in { val withLimits = subject.copy(limits = UserLimits(invocationsPerMinute = Some(5))) val rt = new RateThrottler("test", u => u.limits.invocationsPerMinute.getOrElse(1)) rt.check(withLimits).ok shouldBe true // 1 rt.check(withLimits).ok shouldBe true // 2 rt.check(withLimits).ok shouldBe true // 3 rt.check(withLimits).ok shouldBe true // 4 rt.check(withLimits).ok shouldBe true // 5 rt.check(withLimits).ok shouldBe false } }
Example 65
Source File: ActionsApiWithDbPollingTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.controller.test import java.time.Instant import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.sprayJsonUnmarshaller import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.server.Route import org.apache.openwhisk.core.controller.WhiskActionsApi import org.apache.openwhisk.core.controller.actions.ControllerActivationConfig import org.apache.openwhisk.core.database.UserContext import org.apache.openwhisk.core.entity._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import spray.json.DefaultJsonProtocol._ import spray.json._ import scala.concurrent.duration.DurationInt behavior of "Actions API with DB Polling" val creds = WhiskAuthHelpers.newIdentity() val context = UserContext(creds) val namespace = EntityPath(creds.subject.asString) val collectionPath = s"/${EntityPath.DEFAULT}/${collection.path}" def aname() = MakeName.next("action_tests") val actionLimit = Exec.sizeLimit val parametersLimit = Parameters.sizeLimit override val controllerActivationConfig = ControllerActivationConfig(true, 60.seconds) it should "invoke a blocking action and retrieve result via db polling" in { implicit val tid = transid() val action = WhiskAction(namespace, aname(), jsDefault("??")) val activation = WhiskActivation( action.namespace, action.name, creds.subject, activationIdFactory.make(), start = Instant.now, end = Instant.now, response = ActivationResponse.success(Some(JsObject("test" -> "yes".toJson))), logs = ActivationLogs(Vector("first line", "second line"))) put(entityStore, action) // storing the activation in the db will allow the db polling to retrieve it // the test harness makes sure the activation id observed by the test matches // the one generated by the api handler storeActivation(activation, false, false, context) try { Post(s"$collectionPath/${action.name}?blocking=true") ~> Route.seal(routes(creds)) ~> check { status should be(OK) val response = responseAs[JsObject] response should be(activation.withoutLogs.toExtendedJson()) } // repeat invoke, get only result back Post(s"$collectionPath/${action.name}?blocking=true&result=true") ~> Route.seal(routes(creds)) ~> check { status should be(OK) val response = responseAs[JsObject] response should be(activation.resultAsJson) headers should contain(RawHeader(ActivationIdHeader, activation.activationId.asString)) } } finally { deleteActivation(ActivationId(activation.docid.asString), context) } } it should "invoke a blocking action and return error response when activation fails" in { implicit val tid = transid() val action = WhiskAction(namespace, aname(), jsDefault("??")) val activation = WhiskActivation( action.namespace, action.name, creds.subject, activationIdFactory.make(), start = Instant.now, end = Instant.now, response = ActivationResponse.whiskError("test")) put(entityStore, action) // storing the activation in the db will allow the db polling to retrieve it // the test harness makes sure the activation id observed by the test matches // the one generated by the api handler storeActivation(activation, false, false, context) try { Post(s"$collectionPath/${action.name}?blocking=true") ~> Route.seal(routes(creds)) ~> check { status should be(InternalServerError) val response = responseAs[JsObject] response should be(activation.withoutLogs.toExtendedJson()) headers should contain(RawHeader(ActivationIdHeader, response.fields("activationId").convertTo[String])) } } finally { deleteActivation(ActivationId(activation.docid.asString), context) } } }
Example 66
Source File: WskActorSystem.scala From openwhisk with Apache License 2.0 | 5 votes |
package common import scala.concurrent.Await import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt import akka.actor.ActorSystem import akka.http.scaladsl.Http import org.scalatest.BeforeAndAfterAll import org.scalatest.Suite trait WskActorSystem extends BeforeAndAfterAll { self: Suite => implicit val actorSystem: ActorSystem = ActorSystem() implicit def executionContext: ExecutionContext = actorSystem.dispatcher override def afterAll() = { try { Await.result(Http().shutdownAllConnectionPools(), 30.seconds) } finally { actorSystem.terminate() Await.result(actorSystem.whenTerminated, 30.seconds) } super.afterAll() } }
Example 67
Source File: WskUnicodeTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package system.basic import java.io.File import io.restassured.RestAssured import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import scala.concurrent.duration.DurationInt import common._ import common.rest.WskRestOperations import spray.json._ import system.rest.RestUtil @RunWith(classOf[JUnitRunner]) class WskUnicodeTests extends TestHelpers with WskTestHelpers with JsHelpers with WskActorSystem with RestUtil { implicit val wskprops: common.WskProps = WskProps() val wsk: WskOperations = new WskRestOperations val activationMaxDuration = 2.minutes val activationPollDuration = 3.minutes import WskUnicodeTests._ val actionKinds: Iterable[Kind] = { val response = RestAssured.given.config(sslconfig).get(getServiceURL) response.statusCode should be(200) val mf = response.body.asString.parseJson.asJsObject.fields("runtimes").asJsObject mf.fields.values.map(_.convertTo[Vector[Kind]]).flatten.filter(!_.deprecated) } println(s"Kinds to test: ${actionKinds.map(_.kind).mkString(", ")}") def main(kind: String): Option[String] = { if (kind.startsWith("java")) { Some("Unicode") } else if (kind.contains("dotnet")) { Some("Apache.OpenWhisk.UnicodeTests.Dotnet::Apache.OpenWhisk.UnicodeTests.Dotnet.Unicode::Main") } else None } def getFileLocation(kind: String): Option[String] = { // the test file is either named kind.txt or kind.bin // one of the two must exist otherwise, fail the test. val prefix = "unicode.tests" + File.separator + kind.replace(":", "-") val txt = new File(TestUtils.getTestActionFilename(s"$prefix.txt")) val bin = new File(TestUtils.getTestActionFilename(s"$prefix.bin")) if (txt.exists) Some(txt.toString) else if (bin.exists) Some(bin.toString) else { println(s"WARNING: did not find text or binary action for kind $kind, skipping it") None } } // tolerate missing files rather than throw an exception actionKinds.map(k => (k.kind, getFileLocation(k.kind))).collect { case (actionKind, file @ Some(_)) => s"$actionKind action" should "Ensure that UTF-8 in supported in source files, input params, logs, and output results" in withAssetCleaner( wskprops) { (wp, assetHelper) => val name = s"unicodeGalore.${actionKind.replace(":", "")}" assetHelper.withCleaner(wsk.action, name) { (action, _) => action .create(name, file, main = main(actionKind), kind = Some(actionKind), timeout = Some(activationMaxDuration)) } withActivation( wsk.activation, wsk.action.invoke(name, parameters = Map("delimiter" -> JsString("❄"))), totalWait = activationPollDuration) { activation => val response = activation.response response.result.get.fields.get("error") shouldBe empty response.result.get.fields.get("winter") should be(Some(JsString("❄ ☃ ❄"))) activation.logs.toList.flatten.mkString(" ") should include("❄ ☃ ❄") } } } } protected[basic] object WskUnicodeTests extends DefaultJsonProtocol { case class Kind(kind: String, deprecated: Boolean) implicit val serdes: RootJsonFormat[Kind] = jsonFormat2(Kind) }
Example 68
Source File: MessagingProvider.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.connector import akka.actor.ActorSystem import scala.concurrent.duration.DurationInt import scala.concurrent.duration.FiniteDuration import org.apache.openwhisk.common.Logging import org.apache.openwhisk.core.WhiskConfig import org.apache.openwhisk.core.entity.ByteSize import org.apache.openwhisk.spi.Spi import scala.util.Try trait MessagingProvider extends Spi { def getConsumer( config: WhiskConfig, groupId: String, topic: String, maxPeek: Int = Int.MaxValue, maxPollInterval: FiniteDuration = 5.minutes)(implicit logging: Logging, actorSystem: ActorSystem): MessageConsumer def getProducer(config: WhiskConfig, maxRequestSize: Option[ByteSize] = None)( implicit logging: Logging, actorSystem: ActorSystem): MessageProducer def ensureTopic(config: WhiskConfig, topic: String, topicConfig: String, maxMessageBytes: Option[ByteSize] = None)( implicit logging: Logging): Try[Unit] }
Example 69
Source File: RemoteCacheInvalidation.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database import java.nio.charset.StandardCharsets import scala.concurrent.Future import scala.concurrent.duration.DurationInt import scala.util.Failure import scala.util.Success import scala.util.Try import akka.actor.ActorSystem import akka.actor.Props import spray.json._ import org.apache.openwhisk.common.Logging import org.apache.openwhisk.core.WhiskConfig import org.apache.openwhisk.core.connector.Message import org.apache.openwhisk.core.connector.MessageFeed import org.apache.openwhisk.core.connector.MessagingProvider import org.apache.openwhisk.core.entity.CacheKey import org.apache.openwhisk.core.entity.ControllerInstanceId import org.apache.openwhisk.core.entity.WhiskAction import org.apache.openwhisk.core.entity.WhiskActionMetaData import org.apache.openwhisk.core.entity.WhiskPackage import org.apache.openwhisk.core.entity.WhiskRule import org.apache.openwhisk.core.entity.WhiskTrigger import org.apache.openwhisk.spi.SpiLoader case class CacheInvalidationMessage(key: CacheKey, instanceId: String) extends Message { override def serialize = CacheInvalidationMessage.serdes.write(this).compactPrint } object CacheInvalidationMessage extends DefaultJsonProtocol { def parse(msg: String) = Try(serdes.read(msg.parseJson)) implicit val serdes = jsonFormat(CacheInvalidationMessage.apply _, "key", "instanceId") } class RemoteCacheInvalidation(config: WhiskConfig, component: String, instance: ControllerInstanceId)( implicit logging: Logging, as: ActorSystem) { import RemoteCacheInvalidation._ implicit private val ec = as.dispatchers.lookup("dispatchers.kafka-dispatcher") private val instanceId = s"$component${instance.asString}" private val msgProvider = SpiLoader.get[MessagingProvider] private val cacheInvalidationConsumer = msgProvider.getConsumer(config, s"$cacheInvalidationTopic$instanceId", cacheInvalidationTopic, maxPeek = 128) private val cacheInvalidationProducer = msgProvider.getProducer(config) def notifyOtherInstancesAboutInvalidation(key: CacheKey): Future[Unit] = { cacheInvalidationProducer.send(cacheInvalidationTopic, CacheInvalidationMessage(key, instanceId)).map(_ => ()) } private val invalidationFeed = as.actorOf(Props { new MessageFeed( "cacheInvalidation", logging, cacheInvalidationConsumer, cacheInvalidationConsumer.maxPeek, 1.second, removeFromLocalCache) }) def invalidateWhiskActionMetaData(key: CacheKey) = WhiskActionMetaData.removeId(key) private def removeFromLocalCache(bytes: Array[Byte]): Future[Unit] = Future { val raw = new String(bytes, StandardCharsets.UTF_8) CacheInvalidationMessage.parse(raw) match { case Success(msg: CacheInvalidationMessage) => { if (msg.instanceId != instanceId) { WhiskActionMetaData.removeId(msg.key) WhiskAction.removeId(msg.key) WhiskPackage.removeId(msg.key) WhiskRule.removeId(msg.key) WhiskTrigger.removeId(msg.key) } } case Failure(t) => logging.error(this, s"failed processing message: $raw with $t") } invalidationFeed ! MessageFeed.Processed } } object RemoteCacheInvalidation { val cacheInvalidationTopic = "cacheInvalidation" }
Example 70
Source File: Main.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import kamon.Kamon import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, ExecutionContextExecutor, Future} object Main { def main(args: Array[String]): Unit = { Kamon.init() implicit val system: ActorSystem = ActorSystem("events-actor-system") implicit val materializer: ActorMaterializer = ActorMaterializer() val binding = OpenWhiskEvents.start(system.settings.config) addShutdownHook(binding) } private def addShutdownHook(binding: Future[Http.ServerBinding])(implicit actorSystem: ActorSystem, materializer: ActorMaterializer): Unit = { implicit val ec: ExecutionContextExecutor = actorSystem.dispatcher sys.addShutdownHook { Await.result(binding.map(_.unbind()), 30.seconds) Await.result(actorSystem.whenTerminated, 30.seconds) } } }
Example 71
Source File: ApiTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.http.scaladsl.model.headers.HttpEncodings._ import akka.http.scaladsl.model.headers.{`Accept-Encoding`, `Content-Encoding`, HttpEncoding, HttpEncodings} import akka.http.scaladsl.model.{HttpCharsets, HttpEntity, HttpResponse} import akka.http.scaladsl.testkit.ScalatestRouteTest import kamon.prometheus.PrometheusReporter import org.apache.openwhisk.core.monitoring.metrics.OpenWhiskEvents.MetricConfig import org.junit.runner.RunWith import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner import org.scalatest.matchers.Matcher import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import pureconfig.loadConfigOrThrow import io.prometheus.client.CollectorRegistry import pureconfig.generic.auto._ import scala.concurrent.duration.DurationInt @RunWith(classOf[JUnitRunner]) class ApiTests extends FlatSpec with Matchers with ScalatestRouteTest with EventsTestHelper with ScalaFutures with BeforeAndAfterAll { implicit val timeoutConfig = PatienceConfig(1.minute) private var api: PrometheusEventsApi = _ private var consumer: EventConsumer = _ override protected def beforeAll(): Unit = { super.beforeAll() CollectorRegistry.defaultRegistry.clear() val metricConfig = loadConfigOrThrow[MetricConfig](system.settings.config, "user-events") val mericRecorder = PrometheusRecorder(new PrometheusReporter, metricConfig) consumer = createConsumer(56754, system.settings.config, mericRecorder) api = new PrometheusEventsApi(consumer, createExporter()) } protected override def afterAll(): Unit = { consumer.shutdown().futureValue super.afterAll() } behavior of "EventsApi" it should "respond ping request" in { Get("/ping") ~> api.routes ~> check { //Due to retries using a random port does not immediately result in failure handled shouldBe true } } it should "respond metrics request" in { Get("/metrics") ~> `Accept-Encoding`(gzip) ~> api.routes ~> check { contentType.charsetOption shouldBe Some(HttpCharsets.`UTF-8`) contentType.mediaType.params("version") shouldBe "0.0.4" response should haveContentEncoding(gzip) } } private def haveContentEncoding(encoding: HttpEncoding): Matcher[HttpResponse] = be(encoding) compose { (_: HttpResponse).header[`Content-Encoding`].map(_.encodings.head).getOrElse(HttpEncodings.identity) } private def createExporter(): PrometheusExporter = () => HttpEntity(PrometheusExporter.textV4, "foo".getBytes) }
Example 72
Source File: KafkaSpecBase.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.kafka.testkit.scaladsl.{EmbeddedKafkaLike, ScalatestKafkaSpec} import akka.stream.ActorMaterializer import net.manub.embeddedkafka.EmbeddedKafka import org.scalatest._ import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} import scala.concurrent.duration.{DurationInt, FiniteDuration} abstract class KafkaSpecBase extends ScalatestKafkaSpec(6065) with Matchers with ScalaFutures with FlatSpecLike with EmbeddedKafka with EmbeddedKafkaLike with IntegrationPatience with Eventually with EventsTestHelper { this: Suite => implicit val timeoutConfig: PatienceConfig = PatienceConfig(1.minute) implicit val materializer: ActorMaterializer = ActorMaterializer() override val sleepAfterProduce: FiniteDuration = 10.seconds override protected val topicCreationTimeout = 60.seconds }
Example 73
Source File: StreamStreamDataGenerator.scala From structured-streaming-application with Apache License 2.0 | 5 votes |
package knolx.kafka import java.util.Properties import akka.actor.ActorSystem import knolx.Config._ import knolx.KnolXLogger import knolx.spark.Stock import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.serialization.StringSerializer import org.json4s.NoTypeHints import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.write import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.DurationInt import scala.util.Random object StreamStreamDataGenerator extends App with KnolXLogger { val system = ActorSystem("DataStreamer") val props = new Properties() props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer) props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) val producer = new KafkaProducer[String, String](props) val companyNames = List("kirloskar", "bajaj", "amul", "dlf", "ebay") val orderTypes = List("buy", "sell") val numberOfSharesList = List(1, 2, 3, 4, 5, 6, 7, 8, 9) val randomCompanyNames = Random.shuffle(companyNames).drop(Random.shuffle((1 to 3).toList).head) implicit val formats = Serialization.formats(NoTypeHints) info("Streaming companies listed into Kafka...") system.scheduler.schedule(0 seconds, 20 seconds) { randomCompanyNames.foreach { name => producer.send(new ProducerRecord[String, String](companiesTopic, name)) } } info("Streaming stocks data into Kafka...") system.scheduler.schedule(0 seconds, 5 seconds) { companyNames.foreach { name => val stock = Stock(name, Random.shuffle(numberOfSharesList).head, Random.shuffle(orderTypes).head) producer.send(new ProducerRecord[String, String](stocksTopic, write(stock))) } } }
Example 74
Source File: DataStreamer.scala From structured-streaming-application with Apache License 2.0 | 5 votes |
package knolx.kafka import java.util.Properties import akka.actor.ActorSystem import knolx.Config.{bootstrapServer, topic} import knolx.KnolXLogger import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.serialization.StringSerializer import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.DurationInt import scala.util.Random object DataStreamer extends App with KnolXLogger { val system = ActorSystem("DataStreamer") val props = new Properties() props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer) props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) val producer = new KafkaProducer[String, String](props) val someWords = List("about", "above", "after", "again", "against") info("Streaming data into Kafka...") system.scheduler.schedule(0 seconds, 200 milliseconds) { Random.shuffle(someWords).headOption.foreach { word => producer.send(new ProducerRecord[String, String](topic, word)) } } }
Example 75
Source File: StreamStaticDataGenerator.scala From structured-streaming-application with Apache License 2.0 | 5 votes |
package knolx.kafka import java.util.Properties import akka.actor.ActorSystem import knolx.Config.{bootstrapServer, topic} import knolx.KnolXLogger import knolx.spark.Stock import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.serialization.StringSerializer import org.json4s.NoTypeHints import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.write import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.DurationInt import scala.util.Random object StreamStaticDataGenerator extends App with KnolXLogger { val system = ActorSystem("DataStreamer") val props = new Properties() props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer) props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) val producer = new KafkaProducer[String, String](props) val companyNames = List("kirloskar", "bajaj", "amul", "dlf", "ebay") val orderTypes = List("buy", "sell") val numberOfSharesList = List(1, 2, 3, 4, 5, 6, 7, 8, 9) implicit val formats = Serialization.formats(NoTypeHints) info("Streaming data into Kafka...") system.scheduler.schedule(0 seconds, 5 seconds) { companyNames.foreach { name => val stock = Stock(name, Random.shuffle(numberOfSharesList).head, Random.shuffle(orderTypes).head) producer.send(new ProducerRecord[String, String](topic, write(stock))) } } }
Example 76
Source File: MultiDataStreamer.scala From structured-streaming-application with Apache License 2.0 | 5 votes |
package knolx.kafka import java.util.Properties import akka.actor.ActorSystem import knolx.Config.{bootstrapServer, topic} import knolx.KnolXLogger import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.serialization.StringSerializer import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.DurationInt import scala.language.postfixOps import scala.util.Random object MultiDataStreamer extends App with KnolXLogger { val system = ActorSystem("DataStreamer") val props = new Properties() props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer) props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName) val producer = new KafkaProducer[String, String](props) info("Streaming data into Kafka...") system.scheduler.schedule(0 seconds, 3000 milliseconds) { (1 to Random.nextInt(100)).foreach { id => producer.send(new ProducerRecord[String, String](topic,s"device$id", (Math.random * 2 + 1).toString)) } } }
Example 77
Source File: CounterTest.scala From endpoints4s with MIT License | 5 votes |
package quickstart import java.net.ServerSocket import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes} import akka.http.scaladsl.server.Directives._ import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.DurationInt import org.scalatest.freespec.AsyncFreeSpec class CounterTest extends AsyncFreeSpec with BeforeAndAfterAll { implicit val actorSystem: ActorSystem = ActorSystem() val routes = CounterServer.routes ~ DocumentationServer.routes val interface = "0.0.0.0" val port = findOpenPort() val server = Http().bindAndHandle(routes, interface, port) override protected def afterAll(): Unit = { Await.result( Await.result(server, 10.seconds).terminate(3.seconds), 15.seconds ) Await.result(actorSystem.terminate(), 5.seconds) super.afterAll() } "CounterServer" - { "Query counter value" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/current-value")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) assert(entity.data.utf8String == "{\"value\":0}") } } "Increment counter value" in { val request = HttpRequest( method = HttpMethods.POST, uri = uri("/increment"), entity = HttpEntity(ContentTypes.`application/json`, "{\"step\":1}") ) for { response <- Http().singleRequest(request) } yield { assert(response.status == StatusCodes.OK) } } "Query API documentation" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/documentation.json")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) } } } def findOpenPort(): Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } def uri(suffix: String) = s"http://$interface:$port$suffix" }
Example 78
Source File: QueriesService.scala From endpoints4s with MIT License | 5 votes |
package cqrs.queries import java.util.UUID import akka.actor.Scheduler import play.api.libs.ws.WSClient import scala.concurrent.ExecutionContext.Implicits.global import cqrs.commands.{CommandsEndpoints, MeterCreated, RecordAdded, StoredEvent} import scala.collection.immutable.SortedMap import scala.concurrent.duration.DurationInt import scala.concurrent.Future import scala.concurrent.stm.{Ref, atomic} private def applyEvent(state: State, storedEvent: StoredEvent): State = storedEvent match { case StoredEvent(t, MeterCreated(id, label)) => state.copy( lastEventTimestamp = Some(t), meters = state.meters + (id -> Meter(id, label, SortedMap.empty)) ) case StoredEvent(t, RecordAdded(id, date, value)) => val meter = state.meters(id) val updatedMeter = meter.copy(timeSeries = meter.timeSeries + (date -> value)) state.copy( lastEventTimestamp = Some(t), meters = state.meters + (id -> updatedMeter) ) } }
Example 79
Source File: ChunkedEntitiesDocs.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.play.server import akka.stream.scaladsl.Source import endpoints4s.algebra import endpoints4s.algebra.JsonStreamingExample import scala.concurrent.duration.DurationInt trait ChunkedEntitiesDocs extends algebra.ChunkedEntitiesDocs with ChunkedEntities { //#implementation import akka.stream.scaladsl.FileIO import java.nio.file.Paths val logoHandler = logo.implementedBy { _ => FileIO.fromPath(Paths.get("/foo/bar/logo.png")).map(_.toArray) } //#implementation } //#json-streaming import endpoints4s.play.server class JsonStreamingExampleServer(val playComponents: server.PlayComponents) extends JsonStreamingExample with server.Endpoints with server.ChunkedJsonEntities with server.JsonEntitiesFromSchemas { val routes = routesFromEndpoints( ticks.implementedBy(_ => Source.tick(0.seconds, 1.second, ())) ) } //#json-streaming
Example 80
Source File: ChunkedEntitiesDocs.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.akkahttp.server import akka.http.scaladsl.server.Route import endpoints4s.algebra import endpoints4s.algebra.JsonStreamingExample trait ChunkedEntitiesDocs extends algebra.ChunkedEntitiesDocs with ChunkedEntities { //#implementation import java.nio.file.Paths import akka.stream.scaladsl.FileIO val logoRoute: Route = logo.implementedBy { _ => FileIO.fromPath(Paths.get("/foo/bar/logo.png")).map(_.toArray) } //#implementation } import scala.concurrent.duration.DurationInt //#json-streaming import akka.stream.scaladsl.Source import endpoints4s.akkahttp.server object JsonStreamingExampleServer extends JsonStreamingExample with server.Endpoints with server.ChunkedJsonEntities with server.JsonEntitiesFromSchemas { val routes = ticks.implementedBy(_ => Source.tick(0.seconds, 1.second, ())) } //#json-streaming
Example 81
Source File: BlockActorTest.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.networking import akka.actor.ActorSystem import akka.testkit.{ImplicitSender, TestActorRef, TestKit, TestProbe} import org.bitcoins.core.crypto.DoubleSha256Digest import org.bitcoins.core.protocol.blockchain.BlockHeader import org.bitcoins.core.util.{BitcoinSLogger, BitcoinSUtil} import org.bitcoins.spvnode.messages.BlockMessage import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FlatSpecLike, MustMatchers} import scala.concurrent.duration.DurationInt class BlockActorTest extends TestKit(ActorSystem("BlockActorTest")) with FlatSpecLike with MustMatchers with ImplicitSender with BeforeAndAfter with BeforeAndAfterAll with BitcoinSLogger { def blockActor = TestActorRef(BlockActor.props,self) val blockHash = DoubleSha256Digest(BitcoinSUtil.flipEndianness("00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206")) "BlockActor" must "be able to send a GetBlocksMessage then receive that block back" in { blockActor ! blockHash val blockMsg = expectMsgType[BlockMessage](10.seconds) blockMsg.block.blockHeader.hash must be (blockHash) } it must "be able to request a block from it's block header" in { val blockHeader = BlockHeader("0100000043497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000bac8b0fa927c0ac8234287e33c5f74d38d354820e24756ad709d7038fc5f31f020e7494dffff001d03e4b672") blockActor ! blockHeader val blockMsg = expectMsgType[BlockMessage](10.seconds) blockMsg.block.blockHeader.hash must be (blockHash) } override def afterAll = { TestKit.shutdownActorSystem(system) } }
Example 82
Source File: RpcUtil.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.rpc.util import java.net.ServerSocket import akka.actor.ActorSystem import org.bitcoins.rpc.client.common.BitcoindRpcClient import scala.annotation.tailrec import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration.DurationInt import scala.util.{Failure, Random, Success, Try} abstract class RpcUtil extends AsyncUtil { def awaitServerShutdown( server: BitcoindRpcClient, duration: FiniteDuration = 300.milliseconds, maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = { retryUntilSatisfiedF(() => server.isStoppedF, duration, maxTries) } @tailrec final def randomPort: Int = { val MAX = 65535 // max tcp port number val MIN = 1025 // lowest port not requiring sudo val port = Math.abs(Random.nextInt(MAX - MIN) + (MIN + 1)) val attempt = Try { val socket = new ServerSocket(port) socket.close() socket.getLocalPort } attempt match { case Success(value) => value case Failure(_) => randomPort } } } object RpcUtil extends RpcUtil
Example 83
Source File: JpaSessionImplSpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.internal.javadsl.persistence.jpa import javax.persistence.EntityManager import javax.persistence.EntityTransaction import javax.persistence.Persistence import com.google.common.collect.ImmutableMap import com.lightbend.lagom.javadsl.persistence.jpa.TestJpaEntity import org.scalatest.matchers.BePropertyMatchResult import org.scalatest.matchers.BePropertyMatcher import scala.compat.java8.FunctionConverters._ import scala.compat.java8.FutureConverters._ import scala.concurrent.duration.DurationInt import scala.concurrent.Await import scala.concurrent.Future class JpaSessionImplSpec extends JpaPersistenceSpec { private val open = BePropertyMatcher[EntityManager] { entityManager => BePropertyMatchResult(entityManager.isOpen, "open") } private val active = BePropertyMatcher[EntityTransaction] { entityTransaction => BePropertyMatchResult(entityTransaction.isActive, "active") } // Convenience for converting between Scala and Java 8 private def withTransaction[T](block: EntityManager => T): Future[T] = jpa.withTransaction(block.asJava).toScala "JpaSessionImpl" must { "provide an open EntityManager and close it when the block completes" in { val entityManager = Await.result(withTransaction { entityManager => entityManager shouldBe open entityManager }, 65.seconds) entityManager should not be null entityManager should not be open } "provide an active EntityTransaction and complete it when the block completes" in { val entityTransaction = Await.result(withTransaction { entityManager => val transaction = entityManager.getTransaction transaction shouldBe active transaction }, 10.seconds) entityTransaction should not be null entityTransaction should not be active } "support saving and reading entities" in { Persistence.generateSchema("default", ImmutableMap.of("hibernate.hbm2ddl.auto", "update")) val entity = new TestJpaEntity("1", "test saving and reading entities") entity.getId shouldBe null Await.ready(withTransaction(_.persist(entity)), 10.seconds) // Note that the retrieval runs in a new transaction val retrievedEntity = Await.result( withTransaction { _.createQuery("SELECT test FROM TestJpaEntity test WHERE parentId = :parentId", classOf[TestJpaEntity]) .setParameter("parentId", "1") .getSingleResult }, 10.seconds ) retrievedEntity.getId should not be null retrievedEntity.getParentId should equal("1") retrievedEntity.getElement should equal("test saving and reading entities") } } }
Example 84
Source File: TestUtils.scala From scala-play-realworld-example-app with MIT License | 5 votes |
package commons_test.test_helpers import commons.services.ActionRunner import slick.dbio.DBIO import scala.concurrent.duration.{Duration, DurationInt} import scala.concurrent.{Await, Future} object TestUtils { val config: Map[String, String] = Map( "play.evolutions.enabled" -> "true", "play.evolutions.autoApply" -> "true", "slick.dbs.default.profile" -> "slick.jdbc.H2Profile$", "slick.dbs.default.db.driver" -> "org.h2.Driver", "slick.dbs.default.db.url" -> "jdbc:h2:mem:play;DATABASE_TO_UPPER=false", "slick.dbs.default.db.user" -> "user", "slick.dbs.default.db.password" -> "" ) def runAndAwaitResult[T](action: DBIO[T])(implicit actionRunner: ActionRunner, duration: Duration = new DurationInt(1).minute): T = { val future: Future[T] = actionRunner.runTransactionally(action) Await.result(future, duration) } }
Example 85
Source File: ShipmentViewEndpointSpec.scala From ddd-leaven-akka-v2 with MIT License | 5 votes |
package ecommerce.shipping.app import akka.http.scaladsl.model.StatusCodes.NotFound import akka.http.scaladsl.server._ import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} import akka.testkit.TestDuration import com.typesafe.config.ConfigFactory import ecommerce.sales.view.ViewTestSupport import ecommerce.shipping.view.{ShipmentDao, ShipmentView} import ecommerce.shipping.{ShippingSerializationHintsProvider, ShippingStatus} import org.json4s.Formats import org.scalatest.{BeforeAndAfter, Matchers, WordSpecLike} import pl.newicom.dddd.serialization.JsonSerHints._ import pl.newicom.dddd.utils.UUIDSupport.uuid7 import scala.concurrent.duration.DurationInt class ShipmentViewEndpointSpec extends WordSpecLike with Matchers with ScalatestRouteTest with ViewTestSupport with BeforeAndAfter { override lazy val config = ConfigFactory.load implicit val formats: Formats = new ShippingSerializationHintsProvider().hints() implicit val routeTimeout = RouteTestTimeout(3.seconds dilated) lazy val dao = new ShipmentDao val shipmentId = uuid7 before { viewStore.run { dao.createOrUpdate(ShipmentView(shipmentId, "order-1", ShippingStatus.Delivered)) }.futureValue } after { viewStore.run { dao.remove(shipmentId) }.futureValue } "Shipment view endpoint" should { def response = responseAs[String] val route: Route = new ShipmentViewEndpoint().route(viewStore) "respond to /shipment/all with all shipments" in { Get("/shipment/all") ~> route ~> check { response should include (shipmentId) } } "respond to /shipment/{shipmentId} with requested shipment" in { Get(s"/shipment/$shipmentId") ~> route ~> check { response should include (shipmentId) } } "respond to /shipment/{shipmentId} with NotFound if shipment unknown" in { Get(s"/shipment/invalid") ~> route ~> check { status shouldBe NotFound } } } def ensureSchemaDropped = dao.ensureSchemaDropped def ensureSchemaCreated = dao.ensureSchemaCreated }
Example 86
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
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.Play import play.api.test.FakeApplication 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 import scala.concurrent.duration.DurationInt import scala.concurrent.ExecutionContext.Implicits.global class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll { lazy val fakeApplication = FakeApplication(additionalConfiguration = Map("ws.timeout.request" -> "1000")) override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } "HttpCalls" should { "be gracefully timeout when no response is received within the 'timeout' frame" in { val http = new WSHttp with TestHttpCore // get an unused port val ss = new ServerSocket(0) ss.close() val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}") val ws = new NettyWebServer(global, ss.getLocalSocketAddress, publicUri) try { //starts web server ws.add( "/test", new DelayedHttpHandler(global, 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 87
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
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 88
Source File: Consumer.scala From akka-cluster-load-balancing with MIT License | 5 votes |
package kamkor.actor import akka.actor.{ Actor, Props, UnboundedStash, ActorLogging } import scala.concurrent.duration.DurationInt class Consumer(val processingTimeMillis: Int) extends Actor with UnboundedStash with ActorLogging { import context.dispatcher def receive: Receive = { case data: Array[Int] => { context.become(processing, discardOld = false) context.system.scheduler.scheduleOnce(processingTimeMillis.millis, self, "endProcessing") } } def processing: Receive = { case data: Array[Int] => stash() case "endProcessing" => { log.debug("endProcessing") // for unit test unstashAll() context.unbecome() } } } object Consumer { def props(processingTimeMillis: Int): Props = Props(new Consumer(processingTimeMillis)) }
Example 89
Source File: ClusterListener.scala From akka-cluster-load-balancing with MIT License | 5 votes |
package kamkor.actor import scala.collection.immutable.HashSet import scala.concurrent.duration.DurationInt import akka.actor.{ Actor, Props } import akka.cluster.Cluster import akka.cluster.ClusterEvent.MemberUp import akka.cluster.metrics.{ ClusterMetricsChanged, ClusterMetricsExtension, NodeMetrics } import akka.cluster.metrics.StandardMetrics.HeapMemory import kamkor.{ ConsumerApp } import kamkor.metrics.{ ClusterHeapMetrics, MetricsLogger } class ClusterListener(metricsIntervalSeconds: Int) extends Actor { import context.dispatcher context.system.scheduler.schedule( metricsIntervalSeconds.seconds, metricsIntervalSeconds.seconds, self, "logConsumersHeapUse") private[this] val cluster = Cluster(context.system) private[this] val metricsLogger = new MetricsLogger(name = cluster.selfAddress.port.getOrElse(0).toString()) private[this] val clusterHeapMetrics = new ClusterHeapMetrics() private var consumers: Set[String] = HashSet.empty override def preStart(): Unit = { ClusterMetricsExtension(context.system).subscribe(self) cluster.subscribe(self, classOf[MemberUp]) } override def postStop(): Unit = { ClusterMetricsExtension(context.system).unsubscribe(self) Cluster(context.system).unsubscribe(self) } def receive: Receive = { case MemberUp(m) if m.roles.contains(ConsumerApp.clusterRole) => consumers += m.address.hostPort case ClusterMetricsChanged(clusterMetrics) => clusterMetrics .filter(nm => consumers.contains(nm.address.hostPort)) .foreach(updateHeapUse(_)) case "logConsumersHeapUse" => { metricsLogger.log(clusterHeapMetrics.calculateAverages) clusterHeapMetrics.clear() } } private[this] def updateHeapUse(nodeMetrics: NodeMetrics) { nodeMetrics match { case HeapMemory(address, timestamp, used, committed, max) => { val usedMB = Math.round(used.doubleValue / 1024 / 1024) clusterHeapMetrics.update(address.hostPort, usedMB) } case _ => // no heap info } } } object ClusterListener { def props(metricsIntervalSeconds: Int): Props = Props(new ClusterListener(metricsIntervalSeconds: Int)) }
Example 90
Source File: ConsumerSpec.scala From akka-cluster-load-balancing with MIT License | 5 votes |
package kamkor.actor import scala.concurrent.duration.DurationInt import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } import com.typesafe.config.ConfigFactory import akka.actor.ActorSystem import akka.testkit.{ EventFilter, ImplicitSender, TestKit } class ConsumerSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll { def this() = this( ActorSystem("ClusterSystem", ConfigFactory.parseString(""" akka.loggers = ["akka.testkit.TestEventListener"] akka.loglevel = "DEBUG" """))) override def afterAll: Unit = TestKit.shutdownActorSystem(system) "A Customer actor that processes 1 message for 200 millis" must { "log endedProcessing with debug level 5 times within 1-1.3 seconds" in { val consumer = system.actorOf(Consumer.props(processingTimeMillis = 200)) val data: Array[Int] = Array(0, 1, 2) // akka scheduling is not 100% accurate http://doc.akka.io/docs/akka/snapshot/scala/scheduler.html within(999.millis, 1300.millis) { EventFilter.debug(pattern = "endProcessing", occurrences = 5) intercept { for (_ <- 0 until 5) { consumer ! data } } } } } }
Example 91
Source File: ProgressRoutes.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.web.routes import akka.NotUsed import akka.actor.ActorRef import akka.http.scaladsl.coding.Gzip import akka.http.scaladsl.marshalling.sse.EventStreamMarshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.model.sse.ServerSentEvent import akka.http.scaladsl.model.ws.TextMessage._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.stream.scaladsl._ import com.olegych.scastie.api._ import com.olegych.scastie.balancer._ import play.api.libs.json.Json import scala.concurrent.Future import scala.concurrent.duration.DurationInt class ProgressRoutes(progressActor: ActorRef) { val routes: Route = encodeResponseWith(Gzip)( concat( snippetIdStart("progress-sse") { sid => complete { progressSource(sid).map { progress => ServerSentEvent(Json.stringify(Json.toJson(progress))) } } }, snippetIdStart("progress-ws")( sid => handleWebSocketMessages(webSocket(sid)) ) ) ) private def progressSource( snippetId: SnippetId ): Source[SnippetProgress, NotUsed] = { Source .fromFuture((progressActor ? SubscribeProgress(snippetId))(1.second).mapTo[Source[SnippetProgress, NotUsed]]) .flatMapConcat(identity) } private def webSocket(snippetId: SnippetId): Flow[ws.Message, ws.Message, _] = { def flow: Flow[String, SnippetProgress, NotUsed] = { val in = Flow[String].to(Sink.ignore) val out = progressSource(snippetId) Flow.fromSinkAndSource(in, out) } Flow[ws.Message] .mapAsync(1) { case Strict(c) => Future.successful(c) case e => Future.failed(new Exception(e.toString)) } .via(flow) .map( progress => ws.TextMessage.Strict(Json.stringify(Json.toJson(progress))) ) } }
Example 92
Source File: ScalaLangRoutes.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.web.routes import com.olegych.scastie.api._ import com.olegych.scastie.web.oauth2._ import com.olegych.scastie.balancer._ import akka.util.Timeout import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.model.StatusCodes.Created import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Directives._ import akka.pattern.ask import scala.concurrent.duration.DurationInt // temporary route for the scala-lang frontpage class ScalaLangRoutes( dispatchActor: ActorRef, userDirectives: UserDirectives )(implicit system: ActorSystem) { import system.dispatcher import userDirectives.optionalLogin implicit val timeout: Timeout = Timeout(5.seconds) // format: off val routes: Route = post( extractClientIP(remoteAddress => optionalLogin(user => path("scala-lang")( entity(as[String]) { code => val inputs = InputsWithIpAndUser( Inputs.default.copy(code = code), UserTrace( remoteAddress.toString, None ) ) complete( (dispatchActor ? RunSnippet(inputs)) .mapTo[SnippetId] .map( snippetId => ( Created, snippetId.url ) ) ) } ) ) ) ) }
Example 93
Source File: ScalaJsRoutes.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.web.routes import com.olegych.scastie.api._ import akka.util.Timeout import akka.pattern.ask import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.coding.Gzip import scala.concurrent.duration.DurationInt //not used anymore class ScalaJsRoutes(dispatchActor: ActorRef)(implicit system: ActorSystem) { import system.dispatcher implicit val timeout: Timeout = Timeout(1.seconds) val routes: Route = encodeResponseWith(Gzip)( concat( snippetIdEnd(Shared.scalaJsHttpPathPrefix, ScalaTarget.Js.targetFilename)( sid => complete( (dispatchActor ? FetchScalaJs(sid)) .mapTo[Option[FetchResultScalaJs]] .map(_.map(_.content)) ) ), snippetIdEnd(Shared.scalaJsHttpPathPrefix, ScalaTarget.Js.sourceFilename)( sid => complete( (dispatchActor ? FetchScalaSource(sid)) .mapTo[Option[FetchResultScalaSource]] .map(_.map(_.content)) ) ), snippetIdEnd(Shared.scalaJsHttpPathPrefix, ScalaTarget.Js.sourceMapFilename)( sid => complete( (dispatchActor ? FetchScalaJsSourceMap(sid)) .mapTo[Option[FetchResultScalaJsSourceMap]] .map(_.map(_.content)) ) ) ) ) }
Example 94
Source File: StatusRoutes.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.web.routes import akka.NotUsed import akka.actor.ActorRef import akka.http.scaladsl.marshalling.sse.EventStreamMarshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.model.sse.ServerSentEvent import akka.http.scaladsl.model.ws.TextMessage._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.{Route, _} import akka.pattern.ask import akka.stream.scaladsl._ import com.olegych.scastie.api._ import com.olegych.scastie.balancer._ import com.olegych.scastie.web.oauth2.UserDirectives import play.api.libs.json.Json import scala.concurrent.duration.DurationInt import scala.concurrent.{ExecutionContext, Future} class StatusRoutes(statusActor: ActorRef, userDirectives: UserDirectives)(implicit ec: ExecutionContext) { val isAdminUser: Directive1[Boolean] = userDirectives.optionalLogin.map( user => user.exists(_.isAdmin) ) val routes: Route = isAdminUser { isAdmin => concat( path("status-sse")( complete( statusSource(isAdmin).map { progress => ServerSentEvent( Json.stringify(Json.toJson(progress)) ) } ) ), path("status-ws")( handleWebSocketMessages(webSocketProgress(isAdmin)) ) ) } private def statusSource(isAdmin: Boolean) = { def hideTask(progress: StatusProgress): StatusProgress = if (isAdmin) progress else progress match { case StatusProgress.Sbt(runners) => // Hide the task Queue for non admin users, // they will only see the runner count StatusProgress.Sbt( runners.map(_.copy(tasks = Vector())) ) case _ => progress } Source .fromFuture((statusActor ? SubscribeStatus)(2.seconds).mapTo[Source[StatusProgress, NotUsed]]) .flatMapConcat(s => s.map(hideTask)) } private def webSocketProgress( isAdmin: Boolean ): Flow[ws.Message, ws.Message, _] = { def flow: Flow[String, StatusProgress, NotUsed] = { val in = Flow[String].to(Sink.ignore) val out = statusSource(isAdmin) Flow.fromSinkAndSource(in, out) } Flow[ws.Message] .mapAsync(1) { case Strict(c) => Future.successful(c) case e => Future.failed(new Exception(e.toString)) } .via(flow) .map( progress => ws.TextMessage.Strict(Json.stringify(Json.toJson(progress))) ) } }
Example 95
Source File: DownloadRoutes.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.web.routes import com.olegych.scastie.balancer.DownloadSnippet import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.actor.ActorRef import akka.pattern.ask import java.nio.file.Path import akka.util.Timeout import scala.concurrent.duration.DurationInt class DownloadRoutes(dispatchActor: ActorRef) { implicit val timeout = Timeout(5.seconds) val routes: Route = get { snippetIdStart("download")( sid => onSuccess((dispatchActor ? DownloadSnippet(sid)).mapTo[Option[Path]]) { case Some(path) => getFromFile(path.toFile) case None => throw new Exception( s"Can't serve project ${sid.base64UUID} to user ${sid.user.getOrElse("anon")}" ) } ) } }
Example 96
Source File: RestApiServer.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie package web import api._ import balancer._ import akka.pattern.ask import akka.actor.ActorRef import akka.util.Timeout import akka.http.scaladsl.model.RemoteAddress import scala.concurrent.{Future, ExecutionContext} import scala.concurrent.duration.DurationInt class RestApiServer( dispatchActor: ActorRef, ip: RemoteAddress, maybeUser: Option[User] )(implicit executionContext: ExecutionContext) extends RestApi { implicit val timeout: Timeout = Timeout(20.seconds) private def wrap(inputs: Inputs): InputsWithIpAndUser = InputsWithIpAndUser(inputs, UserTrace(ip.toString, maybeUser)) def run(inputs: Inputs): Future[SnippetId] = { dispatchActor .ask(RunSnippet(wrap(inputs))) .mapTo[SnippetId] } def format(formatRequest: FormatRequest): Future[FormatResponse] = { dispatchActor .ask(formatRequest) .mapTo[FormatResponse] } def save(inputs: Inputs): Future[SnippetId] = { dispatchActor .ask(SaveSnippet(wrap(inputs))) .mapTo[SnippetId] } def update(editInputs: EditInputs): Future[Option[SnippetId]] = { import editInputs._ if (snippetId.isOwnedBy(maybeUser)) { dispatchActor .ask(UpdateSnippet(snippetId, wrap(inputs))) .mapTo[Option[SnippetId]] } else { Future.successful(None) } } def delete(snippetId: SnippetId): Future[Boolean] = { if (snippetId.isOwnedBy(maybeUser)) { dispatchActor .ask(DeleteSnippet(snippetId)) .mapTo[Unit] .map(_ => true) } else { Future.successful(false) } } def fork(editInputs: EditInputs): Future[Option[SnippetId]] = { import editInputs._ dispatchActor .ask(ForkSnippet(snippetId, wrap(inputs))) .mapTo[Option[SnippetId]] } def fetch(snippetId: SnippetId): Future[Option[FetchResult]] = { dispatchActor .ask(FetchSnippet(snippetId)) .mapTo[Option[FetchResult]] } def fetchOld(id: Int): Future[Option[FetchResult]] = { dispatchActor .ask(FetchOldSnippet(id)) .mapTo[Option[FetchResult]] } def fetchUser(): Future[Option[User]] = { Future.successful(maybeUser) } def fetchUserSnippets(): Future[List[SnippetSummary]] = { maybeUser match { case Some(user) => dispatchActor .ask(FetchUserSnippets(user)) .mapTo[List[SnippetSummary]] case _ => Future.successful(Nil) } } }
Example 97
Source File: NexusDocker.scala From coursier with Apache License 2.0 | 5 votes |
package coursier.test import com.spotify.docker.client.DefaultDockerClient import com.spotify.docker.client.messages.{ContainerConfig, HostConfig, PortBinding} import coursier.cache.internal.FileUtil import scala.annotation.tailrec import scala.collection.JavaConverters._ import scala.concurrent.duration.{Duration, DurationInt} import scala.util.Try final case class NexusDocker(base: String, shutdown: () => Unit) object NexusDocker { def apply( image: String, basePath: String, // can't find a way to get back a randomly assigned port (even following https://github.com/spotify/docker-client/issues/625) // so that one has to be specified hostPort: Int, timeout: Duration = 2.minutes ): NexusDocker = { val addr = s"localhost:$hostPort" def log(s: String): Unit = Console.err.println(s"[$image @ $addr] $s") val docker = DefaultDockerClient.fromEnv().build() docker.pull(image) val portBindings = Map("8081" -> Seq(PortBinding.of("0.0.0.0", hostPort)).asJava) val hostConfig = HostConfig.builder().portBindings(portBindings.asJava).build() val containerConfig = ContainerConfig.builder() .hostConfig(hostConfig) .image(image) .exposedPorts(portBindings.keys.toSeq: _*) .build() var idOpt = Option.empty[String] def shutdown(): Unit = for (id <- idOpt) { Try(docker.killContainer(id)) docker.removeContainer(id) docker.close() } try { val creation = docker.createContainer(containerConfig) val id = creation.id() idOpt = Some(id) log(s"starting container $id") docker.startContainer(id) val base: String = s"http://localhost:$hostPort/$basePath" log(s"waiting for nexus server to be up-and-running") val retryDuration = 2.seconds @tailrec def loop(retry: Int): Unit = if (retry > 0) { val url = new java.net.URL(base) try { FileUtil.readFully(url.openStream()) log("nexus up") } catch { case e: java.io.IOException => log(s"Caught $e, retrying in $retryDuration") Thread.sleep(retryDuration.toMillis) loop(retry - 1) } } else throw new Exception(s"Timeout when waiting for container for $image to be up-and-running") val retryCount = if (timeout.isFinite) (timeout / retryDuration).ceil.toInt else Int.MaxValue loop(retryCount) NexusDocker(base, () => shutdown()) } catch { case t: Throwable => shutdown() throw t } } }
Example 98
Source File: CentralNexus3ProxyTests.scala From coursier with Apache License 2.0 | 5 votes |
package coursier.test import coursier.maven.MavenRepository import scala.concurrent.duration.DurationInt object CentralNexus3ProxyTests extends CentralTests { val repo = NexusDocker( "sonatype/nexus3:3.3.1", "repository/maven-central/", // 400 error without the trailing '/' 9082, timeout = 3.minutes // !!! ) override def utestAfterAll(): Unit = repo.shutdown() override def central = MavenRepository(repo.base.stripSuffix("/")) .withVersionsCheckHasModule(false) }
Example 99
Source File: FallbackRefreshDisplay.scala From coursier with Apache License 2.0 | 5 votes |
package coursier.cache.loggers import java.io.Writer import coursier.cache.loggers.RefreshInfo.{CheckUpdateInfo, DownloadInfo} import scala.concurrent.duration.{Duration, DurationInt} class FallbackRefreshDisplay(quiet: Boolean = false) extends RefreshDisplay { private var previous = Set.empty[String] @volatile private var lastInstantOpt = Option.empty[Long] private def describe(info: RefreshInfo): String = info match { case downloadInfo: DownloadInfo => val pctOpt = downloadInfo.fraction.map(100.0 * _) if (downloadInfo.length.isEmpty && downloadInfo.downloaded == 0L) "" else s"(${pctOpt.map(pct => f"$pct%.2f %%, ").mkString}${downloadInfo.downloaded}${downloadInfo.length.map(" / " + _).mkString})" case _: CheckUpdateInfo => "Checking for updates" } val refreshInterval: Duration = 1.second override def newEntry(out: Writer, url: String, info: RefreshInfo): Unit = { lastInstantOpt = Some(System.currentTimeMillis()) if (!quiet) { val msg = info match { case _: DownloadInfo => s"Downloading $url\n" case _: CheckUpdateInfo => s"Checking $url\n" } out.write(msg) out.flush() } } override def removeEntry(out: Writer, url: String, info: RefreshInfo): Unit = { lastInstantOpt = Some(System.currentTimeMillis()) if (!quiet) { val prefix = if (info.watching) "(watching) " else "" val msg = info match { case _: DownloadInfo => s"Downloaded $url\n" case _: CheckUpdateInfo => s"Checked $url\n" } out.write(prefix + msg) out.flush() } } def update( out: Writer, done: Seq[(String, RefreshInfo)], downloads: Seq[(String, RefreshInfo)], changed: Boolean ): Unit = { val now = System.currentTimeMillis() // displaying updates if last message is more than 5 s old if (lastInstantOpt.exists(now > _ + 5000L)) { val downloads0 = downloads.filter { case (url, _) => previous(url) } if (downloads0.nonEmpty) { out.write("Still downloading:\n") for ((url, info) <- downloads0) { assert(info != null, s"Incoherent state ($url)") out.write(s"$url ${describe(info)}\n") } out.write("\n") out.flush() lastInstantOpt = Some(now) } } previous = previous ++ downloads.map(_._1) } override def stop(out: Writer): Unit = { previous = Set.empty lastInstantOpt = None } }
Example 100
Source File: PubSubThrottlerSpec.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra.journal import scala.concurrent.duration.DurationInt import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import akka.actor.{ ActorSystem, Props } import akka.testkit.{ TestKit, TestProbe } class PubSubThrottlerSpec extends TestKit(ActorSystem("CassandraConfigCheckerSpec")) with AnyWordSpecLike with Matchers { "PubSubThrottler" should { "eat up duplicate messages that arrive within the same [interval] window" in { val delegate = TestProbe() val throttler = system.actorOf(Props(new PubSubThrottler(delegate.ref, 5.seconds))) throttler ! "hello" throttler ! "hello" throttler ! "hello" delegate.within(2.seconds) { delegate.expectMsg("hello") } // Only first "hello" makes it through during the first interval. delegate.expectNoMessage(2.seconds) // Eventually, the interval will roll over and forward ONE further hello. delegate.expectMsg(10.seconds, "hello") delegate.expectNoMessage(2.seconds) throttler ! "hello" delegate.within(2.seconds) { delegate.expectMsg("hello") } } "allow differing messages to pass through within the same [interval] window" in { val delegate = TestProbe() val throttler = system.actorOf(Props(new PubSubThrottler(delegate.ref, 5.seconds))) throttler ! "hello" throttler ! "world" delegate.within(2.seconds) { delegate.expectMsg("hello") delegate.expectMsg("world") } } } }
Example 101
Source File: DatastreamHandlerUnitSpec.scala From play-auditing with Apache License 2.0 | 5 votes |
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 102
Source File: MapInitAndLastTests.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.streams.test import akka.stream._ import akka.stream.scaladsl.{GraphDSL, RunnableGraph, Source} import akka.stream.testkit.scaladsl.{TestSink, TestSource} import akka.stream.testkit.TestPublisher.{Probe => SrcProbe} import akka.stream.testkit.TestSubscriber.{Probe => SnkProbe} import cmwell.util.stream.MapInitAndLast import scala.concurrent.duration.DurationInt class MapInitAndLastTests extends StreamSpec { def generateGraph[In](): (SrcProbe[In],SnkProbe[(In,Boolean)]) = { val src = TestSource.probe[In] val snk = TestSink.probe[(In,Boolean)] RunnableGraph.fromGraph(GraphDSL.create(src, snk)((a, b) => (a, b)) { implicit b => { (s1, s2) => { import GraphDSL.Implicits._ val mial = b.add(new MapInitAndLast[In, (In,Boolean)](_ -> false, _ -> true)) s1 ~> mial ~> s2 ClosedShape } } }).run() } describe("MapInitAndLast Stage"){ it("should buffer a single element"){ val (src,snk) = generateGraph[Int]() snk.request(99) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendComplete() snk.expectNext((1,true)) snk.expectComplete() } it("should treat last element differently") { val (src,snk) = generateGraph[Int]() snk.request(99) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendNext(2) snk.expectNext((1,false)) src.sendNext(3) snk.expectNext((2,false)) src.sendComplete() snk.expectNext((3,true)) snk.expectComplete() } it("should propagate back-pressure"){ val (src,snk) = generateGraph[Int]() snk.ensureSubscription() src.sendNext(1) snk.expectNoMessage(300.millis) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendComplete() snk.expectNoMessage(300.millis) snk.request(1) snk.expectNext((1,false)) snk.request(1) snk.expectNext((1,true)) snk.expectComplete() } } }
Example 103
Source File: StreamSpec.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.streams.test import akka.actor.ActorSystem import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.scalatest.{BeforeAndAfterAll, FunSpec, Matchers} import scala.concurrent.duration.DurationInt import scala.concurrent.Await trait StreamSpec extends FunSpec with Matchers with BeforeAndAfterAll { protected implicit val system = { def config = ConfigFactory.load() ActorSystem("default", config) } protected implicit val mat = ActorMaterializer() override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 104
Source File: ServicesRoutesTests.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.it import com.typesafe.scalalogging.LazyLogging import org.scalatest.{AsyncFunSpec, Matchers} import scala.concurrent.duration.DurationInt class ServicesRoutesTests extends AsyncFunSpec with Matchers with Helpers with LazyLogging { import cmwell.util.http.SimpleResponse.Implicits.UTF8StringHandler private def putTextualFileInfoton(path: String, payload: String) = { Http.post(path, payload, Some("text/plain"), headers = tokenHeader :+ "X-CM-Well-Type" -> "File").flatMap { _ => spinCheck(100.millis, true)(Http.get(path))(_.status == 200) } } private def refreshCache() = Http.get(cmw / "_services-cache", List("op" -> "refresh")) describe("Services Routes") { val ingestServiceInfotonsAndRefreshCache = { val totalServices = 2 val data = """@prefix nn: <cmwell://meta/nn#> . |@prefix sys: <cmwell://meta/sys#> . |@prefix o: <cmwell://meta/services/> . | |o:PermID a sys:Redirection ; | nn:route "/permid/" ; | nn:sourcePattern "/permid/(.*)" ; | nn:replacement "/graph.link/PID-$1" . | | |o:TempRoute a sys:Redirection ; | nn:route "/temp/" ; | nn:sourcePattern "/temp/(.*)" ; | nn:replacement "/graph.link/temp/$1" . | | """.stripMargin Http.post(_in, data, Some("text/rdf+turtle;charset=UTF-8"), List("format" -> "ttl"), tokenHeader).flatMap { _ => spinCheck(100.millis, true)(Http.get(cmw / "meta" / "services", List("op" -> "stream")))(_.payload.trim().lines.size == totalServices) }.flatMap(_ => refreshCache()) } it("Should get an Infoton via Service Route of Redirection type") { // testing redirection: GET /permid/XYZ should return content of /graph.link/PID-XYZ val payload = "Hello World 789" val actualPath = cmw / "graph.link" / "PID-789" val ingestTargetInfoton = putTextualFileInfoton(actualPath, payload) (ingestTargetInfoton zip ingestServiceInfotonsAndRefreshCache).flatMap { _ => Http.get(cmw / "permid" / "789").map(_.payload should be(payload)) } } it("Should remove a service route") { val tempRouteServiceInfotonPath = cmw / "meta" / "services" / "TempRoute" val virtualPath = cmw / "temp" / "1234" val actualPath = cmw / "graph.link" / "temp" / "1234" val ingestTargetInfoton = putTextualFileInfoton(actualPath, payload = "Foo Bar") (ingestTargetInfoton zip ingestServiceInfotonsAndRefreshCache).flatMap { _ => Http.get(virtualPath).map(_.status should be(200)) }.flatMap { _ => Http.delete(tempRouteServiceInfotonPath, headers = tokenHeader).flatMap { _ => spinCheck(100.millis, true)(Http.get(cmw / "meta" / "services", List("op" -> "stream")))(!_.payload.contains("TempRoute")) } }.flatMap { _ => refreshCache() }.flatMap { _ => Http.get(virtualPath).map(_.status should be(404)) } } } }
Example 105
Source File: FileInfotonTests.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.it import java.nio.charset.StandardCharsets import com.typesafe.scalalogging.LazyLogging import org.scalatest.{AsyncFunSpec, Matchers, TryValues} import play.api.libs.json._ import scala.concurrent.duration.DurationInt import scala.io.Source class FileInfotonTests extends AsyncFunSpec with Matchers with TryValues with Helpers with LazyLogging { describe("file infoton") { val path = cmt / "InfoFile4" val fileStr = Source.fromURL(this.getClass.getResource("/article.txt")).mkString val j = Json.obj("Offcourse" -> Seq("I can do it"),"I'm" -> Seq("a spellbinder")) val f0 = Http.post(path, fileStr, Some("text/plain;charset=UTF-8"), Nil, ("X-CM-WELL-TYPE" -> "FILE") :: tokenHeader).map { res => withClue(res){ Json.parse(res.payload) should be(jsonSuccess) } } val f1 = f0.flatMap {_ => spinCheck(100.millis, true)(Http.get(path)){res => new String(res.payload, StandardCharsets.UTF_8) == fileStr && res.contentType.takeWhile(_ != ';') == "text/plain"} .map { res => withClue(res) { new String(res.payload, StandardCharsets.UTF_8) should be(fileStr) res.contentType.takeWhile(_ != ';') should be("text/plain") } }} val f2 = f1.flatMap(_ => Http.post(path, Json.stringify(j), None, Nil, ("X-CM-WELL-TYPE" -> "FILE_MD") :: tokenHeader)).map {res => withClue(res) { Json.parse(res.payload) should be(jsonSuccess) } } val f3 = f2.flatMap(_ => spinCheck(100.millis, true)(Http.get(path, List("format" -> "json"))){ res => val jsonResult = Json.parse(res.payload).transform(fieldsSorter andThen (__ \ 'fields).json.pick) jsonResult match { case JsSuccess(value, _) => value == j case JsError(_) => false } }.map{ res => withClue(res) { Json .parse(res.payload) .transform(fieldsSorter andThen (__ \ 'fields).json.pick) .get shouldEqual j } } ) val f4 = f3.flatMap(_ => Http.delete(uri = path, headers = tokenHeader).map { res => withClue(res) { Json.parse(res.payload) should be(jsonSuccess) } }) val lenna = cmt / "lenna" val f5 = { val lennaInputStream = this.getClass.getResource("/Lenna.png").openStream() Http.post(lenna / "Lenna.png", () => lennaInputStream, Some("image/png"), Nil, ("X-CM-WELL-TYPE" -> "FILE") :: tokenHeader).transform { res => // first, close the stream lennaInputStream.close() withClue(res)(res.map { r => Json.parse(r.payload) should be(jsonSuccess) }) } } val f6 = spinCheck(100.millis,true,1.minute)(Http.get(lenna,List("op" -> "search","qp" -> "content.mimeType:image/png", "format" -> "json"))){ res => res.status match { case 503 => Recoverable case 200 => { val j = Json.parse(res.payload) \ "results" (j \ "total": @unchecked) match { case JsDefined(JsNumber(n)) => n.intValue == 1 } } case _ => UnRecoverable } }.map { res => withClue(res) { val j = Json.parse(res.payload) \ "results" (j \ "infotons": @unchecked) match { case JsDefined(JsArray(arr)) => (arr.head \ "system" \ "path": @unchecked) match { case JsDefined(JsString(lennaPath)) => lennaPath shouldEqual "/cmt/cm/test/lenna/Lenna.png" } } } } it("should put File infoton")(f0) it("should get previously inserted file with text/plain mimetype")(f1) it("should put file infoton metadata")(f2) it("should get file infoton metadata")(f3) it("should delete file infoton")(f4) it("should upload Lenna.png image")(f5) it("should search by content.mimeType")(f6) } }
Example 106
Source File: CrawlerStreamSpec.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.crawler import akka.actor.ActorSystem import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import scala.concurrent.duration.DurationInt import scala.concurrent.Await trait CrawlerStreamSpec extends FlatSpec with Matchers with BeforeAndAfterAll { protected implicit val system = { def config = ConfigFactory.load() ActorSystem("default", config) } protected implicit val mat = ActorMaterializer() override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 107
Source File: ProbeStatement.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.context import scala.concurrent.duration.DurationInt import scala.reflect.api.Types import scala.reflect.macros.whitebox.{ Context => MacroContext } import scala.util.Failure import scala.util.Success import io.getquill._ import io.getquill.util.Cache import io.getquill.util.MacroContextExt._ import io.getquill.util.LoadObject import io.getquill.idiom.Idiom object ProbeStatement { private val cache = new Cache[Types#Type, Context[Idiom, NamingStrategy]] def apply(statement: String, c: MacroContext) = { import c.universe.{ Try => _, _ } def resolveContext(tpe: Type) = tpe match { case tpe if tpe <:< c.weakTypeOf[QueryProbing] => LoadObject[Context[Idiom, NamingStrategy]](c)(tpe) match { case Success(context) => Some(context) case Failure(ex) => c.error( s"Can't load the context of type '$tpe' for a compile-time query probing. " + s"Make sure that context creation happens in a separate compilation unit. " + s"For more information please refer to the documentation http://getquill.io/#quotation-query-probing. " + s"Reason: '$ex'" ) None } case _ => None } val tpe = c.prefix.tree.tpe cache .getOrElseUpdate(tpe, resolveContext(tpe), 30.seconds) .map(_.probe(statement)) .foreach { case Success(_) => case Failure(ex) => c.error(s"Query probing failed. Reason: '$ex'") } } }
Example 108
Source File: CacheSpec.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.util import java.io.Closeable import scala.concurrent.duration.DurationInt import io.getquill.Spec class CacheSpec extends Spec { class Value extends Closeable { var closes = 0 override def close = closes += 1 } "caches hits" in { val cache = new Cache[Int, Value] val value = new Value var calls = 0 def _value = { calls += 1 Some(value) } cache.getOrElseUpdate(1, _value, 1.second) mustEqual Some(value) cache.getOrElseUpdate(1, _value, 1.second) mustEqual Some(value) calls mustEqual 1 } "caches misses" in { val cache = new Cache[Int, Value] var calls = 0 def _value = { calls += 1 None } cache.getOrElseUpdate(1, _value, 1.second) mustEqual None cache.getOrElseUpdate(1, _value, 1.second) mustEqual None calls mustEqual 1 } "expires keys" in { val cache = new Cache[Int, Value] val value = new Value var calls = 0 def _value = { calls += 1 Some(value) } cache.getOrElseUpdate(1, _value, -1.milliseconds) cache.getOrElseUpdate(2, None, -1.milliseconds) calls mustEqual 1 value.closes mustEqual 1 cache.getOrElseUpdate(1, _value, 1.second) calls mustEqual 2 value.closes mustEqual 1 } }
Example 109
Source File: BaseAkkaSpec.scala From reactive-application-development-scala with Apache License 2.0 | 5 votes |
package com.rarebooks.library import akka.actor.{ ActorIdentity, ActorRef, ActorSystem, Identify } import akka.testkit.{ EventFilter, TestEvent, TestProbe } import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.{ DurationInt, FiniteDuration } abstract class BaseAkkaSpec(actorSystemName: String) extends BaseSpec with BeforeAndAfterAll { implicit class TestProbeOps(probe: TestProbe) { def expectActor(path: String, max: FiniteDuration = 3.seconds): ActorRef = { probe.within(max) { var actor = null: ActorRef probe.awaitAssert { (probe.system actorSelection path).tell(Identify(path), probe.ref) probe.expectMsgPF(100 milliseconds) { case ActorIdentity(`path`, Some(ref)) => actor = ref } } actor } } } implicit val system = ActorSystem(actorSystemName) system.eventStream.publish(TestEvent.Mute(EventFilter.debug())) system.eventStream.publish(TestEvent.Mute(EventFilter.info())) system.eventStream.publish(TestEvent.Mute(EventFilter.warning())) system.eventStream.publish(TestEvent.Mute(EventFilter.error())) override protected def afterAll(): Unit = { Await.ready(system.terminate(), 20.seconds) } }
Example 110
Source File: Integration.scala From helm with Apache License 2.0 | 5 votes |
package helm package http4s import scala.concurrent.duration.DurationInt import cats.effect.IO import cats.implicits._ import com.github.dockerjava.core.DefaultDockerClientConfig import com.github.dockerjava.netty.NettyDockerCmdExecFactory import com.whisk.docker._ import com.whisk.docker.impl.dockerjava.{Docker, DockerJavaExecutorFactory} import com.whisk.docker.scalatest._ import journal.Logger import org.http4s._ import org.http4s.client.blaze._ import org.scalacheck._ import org.scalatest._ import org.scalatest.enablers.CheckerAsserting import org.scalatest.prop._ // This is how we use docker-kit. Nothing specific to helm in this trait. trait DefaultDockerKit extends DockerKit { override implicit val dockerFactory: DockerFactory = new DockerJavaExecutorFactory( new Docker(DefaultDockerClientConfig.createDefaultConfigBuilder().build(), factory = new NettyDockerCmdExecFactory())) lazy val dockerHost: String = { // i'm expecting protocol://ip:port sys.env.get("DOCKER_HOST").flatMap { url => val parts = url.split(":") if (parts.length == 3) Some(parts(1).substring(2)) else None }.getOrElse("127.0.0.1") } } trait DockerConsulService extends DefaultDockerKit { private[this] val logger = Logger[DockerConsulService] override implicit val dockerFactory: DockerFactory = new DockerJavaExecutorFactory( new Docker(DefaultDockerClientConfig.createDefaultConfigBuilder().build(), factory = new NettyDockerCmdExecFactory())) val ConsulPort = 18512 val consulContainer = DockerContainer("consul:0.7.0", name = Some("consul")) .withPorts(8500 -> Some(ConsulPort)) .withLogLineReceiver(LogLineReceiver(true, s => logger.debug(s"consul: $s"))) .withReadyChecker(DockerReadyChecker.LogLineContains("agent: Synced")) abstract override def dockerContainers: List[DockerContainer] = consulContainer :: super.dockerContainers } class IntegrationSpec extends FlatSpec with Matchers with Checkers with BeforeAndAfterAll with DockerConsulService with DockerTestKit { val client = Http1Client[IO]().unsafeRunSync val baseUrl: Uri = Uri.fromString(s"http://${dockerHost}:${ConsulPort}").valueOr(throw _) val interpreter = new Http4sConsulClient(baseUrl, client) "consul" should "work" in check { (k: String, v: Array[Byte]) => scala.concurrent.Await.result(dockerContainers.head.isReady(), 20.seconds) helm.run(interpreter, ConsulOp.kvSet(k, v)).unsafeRunSync // Equality comparison for Option[Array[Byte]] doesn't work properly. Since we expect the value to always be Some making a custom matcher doesn't seem worthwhile, so call .get on the Option // See https://github.com/scalatest/scalatest/issues/491 helm.run(interpreter, ConsulOp.kvGetRaw(k, None, None)).unsafeRunSync.value.get should be (v) helm.run(interpreter, ConsulOp.kvListKeys("")).unsafeRunSync should contain (k) helm.run(interpreter, ConsulOp.kvDelete(k)).unsafeRunSync helm.run(interpreter, ConsulOp.kvListKeys("")).unsafeRunSync should not contain (k) true }(implicitly, implicitly, Arbitrary(Gen.alphaStr suchThat(_.size > 0)), implicitly, implicitly, Arbitrary.arbContainer[Array, Byte], implicitly, implicitly, implicitly[CheckerAsserting[EntityDecoder[IO, Array[Byte]]]], implicitly, implicitly) }
Example 111
Source File: AddService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.add import javax.ws.rs.{Consumes, POST, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.add.AddActor._ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.parameters.RequestBody import io.swagger.v3.oas.annotations.responses.ApiResponse import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/add") class AddService(addActor: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val requestFormat: RootJsonFormat[AddRequest] = jsonFormat1(AddRequest) implicit val responseFormat: RootJsonFormat[AddResponse] = jsonFormat1(AddResponse) val route: Route = add @POST @Consumes(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Add integers", description = "Add integers", requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[AddRequest])))), responses = Array( new ApiResponse(responseCode = "200", description = "Add response", content = Array(new Content(schema = new Schema(implementation = classOf[AddResponse])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def add: Route = path("add") { post { entity(as[AddRequest]) { request => complete { (addActor ? request).mapTo[AddResponse] } } } } }
Example 112
Source File: AddOptionService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.addoption import javax.ws.rs.{Consumes, POST, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.addoption.AddOptionActor._ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.parameters.RequestBody import io.swagger.v3.oas.annotations.responses.ApiResponse import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/addOption") class AddOptionService(addActor: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val requestFormat: RootJsonFormat[AddOptionRequest] = jsonFormat2(AddOptionRequest) implicit val responseFormat: RootJsonFormat[AddOptionResponse] = jsonFormat1(AddOptionResponse) val route: Route = addOption @POST @Consumes(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Add integers", description = "Add integers", requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionRequest])))), responses = Array( new ApiResponse(responseCode = "200", description = "Add response", content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionResponse])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def addOption: Route = path("addOption") { post { entity(as[AddOptionRequest]) { request => complete { (addActor ? request).mapTo[AddOptionResponse] } } } } }
Example 113
Source File: HelloService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.hello import javax.ws.rs.{GET, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.hello.HelloActor._ import io.swagger.v3.oas.annotations.enums.ParameterIn import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.{Operation, Parameter} import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/hello") class HelloService(hello: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val greetingFormat: RootJsonFormat[Greeting] = jsonFormat1(Greeting) val route: Route = getHello ~ getHelloSegment @GET @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Return Hello greeting (anonymous)", description = "Return Hello greeting for anonymous request", responses = Array( new ApiResponse(responseCode = "200", description = "Hello response", content = Array(new Content(schema = new Schema(implementation = classOf[Greeting])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def getHello: Route = path("hello") { get { complete { (hello ? AnonymousHello).mapTo[Greeting] } } } @GET @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Return Hello greeting", description = "Return Hello greeting for named user", parameters = Array(new Parameter(name = "name", in = ParameterIn.PATH, description = "user name")), responses = Array( new ApiResponse(responseCode = "200", description = "Hello response", content = Array(new Content(schema = new Schema(implementation = classOf[Greeting])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def getHelloSegment: Route = path("hello" / Segment) { name => get { complete { (hello ? Hello(name)).mapTo[Greeting] } } } }
Example 114
Source File: RespondeeTests.scala From streamee with Apache License 2.0 | 5 votes |
package io.moia.streamee import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AsyncWordSpec import scala.concurrent.duration.DurationInt final class RespondeeTests extends AsyncWordSpec with AkkaSuite with Matchers { import Respondee._ "A Respondee" should { "fail its promise with a TimeoutException if not receiving a Response in time" in { val timeout = 100.milliseconds val (_, response) = Respondee.spawn[Int](timeout) response.future.failed.map { case ResponseTimeoutException(t) => t shouldBe timeout } } "successfully complete its promise with the received Response" in { val (respondee, response) = Respondee.spawn[Int](1.second) respondee ! Response(42) response.future.map(_ shouldBe 42) } } }
Example 115
Source File: AkkaSuite.scala From streamee with Apache License 2.0 | 5 votes |
package io.moia.streamee import akka.actor.{ ActorSystem, Scheduler } import org.scalatest.{ BeforeAndAfterAll, Suite } import scala.concurrent.Await import scala.concurrent.duration.DurationInt trait AkkaSuite extends Suite with BeforeAndAfterAll { protected implicit val system: ActorSystem = ActorSystem() protected implicit val scheduler: Scheduler = system.scheduler override protected def afterAll(): Unit = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 116
Source File: Time.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.utils import java.net.{InetAddress, SocketTimeoutException} import monix.eval.Task import monix.execution.ExecutionModel import monix.execution.schedulers.SchedulerService import org.apache.commons.net.ntp.NTPUDPClient import scala.concurrent.duration.DurationInt trait Time { def correctedTime(): Long def getTimestamp(): Long } class NTP(ntpServer: String) extends Time with ScorexLogging with AutoCloseable { log.info("Initializing time") private val offsetPanicThreshold = 1000000L private val ExpirationTimeout = 60.seconds private val RetryDelay = 10.seconds private val ResponseTimeout = 10.seconds private implicit val scheduler: SchedulerService = Schedulers.singleThread(name = "time-impl", reporter = log.error("Error in NTP", _), ExecutionModel.AlwaysAsyncExecution) private val client = new NTPUDPClient() client.setDefaultTimeout(ResponseTimeout.toMillis.toInt) @volatile private var offset = 0L private val updateTask: Task[Unit] = { def newOffsetTask: Task[Option[(InetAddress, java.lang.Long)]] = Task { try { client.open() val info = client.getTime(InetAddress.getByName(ntpServer)) info.computeDetails() Option(info.getOffset).map { offset => val r = if (Math.abs(offset) > offsetPanicThreshold) throw new Exception("Offset is suspiciously large") else offset (info.getAddress, r) } } catch { case _: SocketTimeoutException => None case t: Throwable => log.warn("Problems with NTP: ", t) None } finally { client.close() } } newOffsetTask.flatMap { case None if !scheduler.isShutdown => updateTask.delayExecution(RetryDelay) case Some((server, newOffset)) if !scheduler.isShutdown => log.trace(s"Adjusting time with $newOffset milliseconds, source: ${server.getHostAddress}.") offset = newOffset updateTask.delayExecution(ExpirationTimeout) case _ => Task.unit } } def correctedTime(): Long = System.currentTimeMillis() + offset private var txTime: Long = 0 def getTimestamp(): Long = { txTime = Math.max(correctedTime(), txTime + 1) txTime } private val taskHandle = updateTask.runAsyncLogErr override def close(): Unit = { log.info("Shutting down Time") taskHandle.cancel() scheduler.shutdown() } }
Example 117
Source File: ClientSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.util.concurrent.ConcurrentHashMap import com.wavesplatform.{TransactionGen, Version} import io.netty.buffer.{ByteBuf, Unpooled} import io.netty.channel.Channel import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.group.ChannelGroup import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.duration.DurationInt import scala.util.Random class ClientSpec extends FreeSpec with Matchers with MockFactory with TransactionGen { private val clientHandshake = new Handshake( applicationName = "wavesI", applicationVersion = Version.VersionTuple, nodeName = "test", nodeNonce = Random.nextInt(), declaredAddress = None ) private val serverHandshake = clientHandshake.copy(nodeNonce = Random.nextInt()) "should send only a local handshake on connection" in { val channel = createEmbeddedChannel(mock[ChannelGroup]) val sentClientHandshakeBuff = channel.readOutbound[ByteBuf]() Handshake.decode(sentClientHandshakeBuff) shouldBe clientHandshake channel.outboundMessages() shouldBe empty } "should add a server's channel to all channels after the handshake only" in { var channelWasAdded = false val allChannels = mock[ChannelGroup] (allChannels.add _).expects(*).onCall { _: Channel => channelWasAdded = true true } val channel = createEmbeddedChannel(allChannels) // skip the client's handshake channel.readOutbound[ByteBuf]() channelWasAdded shouldBe false val replyServerHandshakeBuff = Unpooled.buffer() serverHandshake.encode(replyServerHandshakeBuff) channel.writeInbound(replyServerHandshakeBuff) channelWasAdded shouldBe true } private def createEmbeddedChannel(allChannels: ChannelGroup) = new EmbeddedChannel( new HandshakeDecoder(PeerDatabase.NoOp), new HandshakeTimeoutHandler(1.minute), new HandshakeHandler.Client( handshake = clientHandshake, establishedConnections = new ConcurrentHashMap(), peerConnections = new ConcurrentHashMap(), peerDatabase = PeerDatabase.NoOp, allChannels = allChannels ) ) }
Example 118
Source File: BrokenConnectionDetectorSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import com.wavesplatform.TransactionGen import io.netty.channel.embedded.EmbeddedChannel import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} import scala.concurrent.duration.DurationInt class BrokenConnectionDetectorSpec extends FreeSpec with Matchers with MockFactory with PropertyChecks with TransactionGen { "should not close an active connection until the timeout" in { val handler = new BrokenConnectionDetector(400.millis) val ch = new EmbeddedChannel(handler) ch.writeInbound("foo") Thread.sleep(200) ch.runPendingTasks() ch.isActive shouldBe true } "should not close a connection when messages are keep going" in { val handler = new BrokenConnectionDetector(100.millis) val ch = new EmbeddedChannel(handler) (1 to 3).foreach { _ => ch.writeInbound("bar") Thread.sleep(50) ch.runPendingTasks() } ch.isActive shouldBe true } "should close a broken connection" in { val handler = new BrokenConnectionDetector(200.millis) val ch = new EmbeddedChannel(handler) ch.writeInbound("bar") Thread.sleep(250) ch.runPendingTasks() ch.isActive shouldBe false } }
Example 119
Source File: ObservedLoadingCacheSpecification.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.utils import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicLong import com.google.common.base.Ticker import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache} import com.wavesplatform.utils.ObservedLoadingCacheSpecification.FakeTicker import monix.execution.Ack import monix.reactive.Observer import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import scala.jdk.CollectionConverters._ import scala.concurrent.Future import scala.concurrent.duration.DurationInt class ObservedLoadingCacheSpecification extends FreeSpec with Matchers with MockFactory { private val ExpiringTime = 10.minutes "notifies" - { "on refresh" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.refresh("foo") } "on put" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.put("foo", 10) } "on putAll" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() (changes.onNext _).expects("bar").returning(Future.successful(Ack.Continue)).once() loadingCache.putAll(Map[String, Integer]("foo" -> 10, "bar" -> 11).asJava) } "on invalidate" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.invalidate("foo") } "on invalidateAll" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() (changes.onNext _).expects("bar").returning(Future.successful(Ack.Continue)).once() loadingCache.invalidateAll(Seq("foo", "bar").asJava) } } "don't notify" - { "on cache expiration" in test { (loadingCache, changes, ticker) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.put("foo", 1) ticker.advance(ExpiringTime.toMillis + 100, TimeUnit.MILLISECONDS) } } private def test(f: (LoadingCache[String, Integer], Observer[String], FakeTicker) => Unit): Unit = { val changes = mock[Observer[String]] val ticker = new FakeTicker() val delegate = CacheBuilder .newBuilder() .expireAfterWrite(ExpiringTime.toMillis, TimeUnit.MILLISECONDS) .ticker(ticker) .build(new CacheLoader[String, Integer] { override def load(key: String): Integer = key.length }) val loadingCache = new ObservedLoadingCache(delegate, changes) f(loadingCache, changes, ticker) } } private object ObservedLoadingCacheSpecification { // see https://github.com/google/guava/blob/master/guava-testlib/src/com/google/common/testing/FakeTicker.java class FakeTicker extends Ticker { private val nanos = new AtomicLong() private var autoIncrementStepNanos = 0L def advance(time: Long, timeUnit: TimeUnit): FakeTicker = advance(timeUnit.toNanos(time)) def advance(nanoseconds: Long): FakeTicker = { nanos.addAndGet(nanoseconds) this } def setAutoIncrementStep(autoIncrementStep: Long, timeUnit: TimeUnit): FakeTicker = { require(autoIncrementStep >= 0, "May not auto-increment by a negative amount") this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep) this } override def read: Long = nanos.getAndAdd(autoIncrementStepNanos) } }
Example 120
Source File: ZeromqConnectionTests.scala From almond with BSD 3-Clause "New" or "Revised" License | 5 votes |
package almond.channels.zeromq import java.nio.charset.StandardCharsets import almond.channels.{Channel, ConnectionParameters, Message} import almond.logger.LoggerContext import cats.effect.IO import utest._ import scala.concurrent.duration.DurationInt object ZeromqConnectionTests extends TestSuite { val tests = Tests { 'simple - { val logCtx = LoggerContext.nop val params = ConnectionParameters.randomLocal() val kernelThreads = ZeromqThreads.create("test-kernel") val serverThreads = ZeromqThreads.create("test-server") val msg0 = Message( Nil, "header".getBytes(StandardCharsets.UTF_8), "parent_header".getBytes(StandardCharsets.UTF_8), "metadata".getBytes(StandardCharsets.UTF_8), "content".getBytes(StandardCharsets.UTF_8) ) val t = for { kernel <- params.channels(bind = true, kernelThreads, logCtx) server <- params.channels(bind = false, serverThreads, logCtx) _ <- kernel.open _ <- server.open _ <- server.send(Channel.Requests, msg0) resp <- kernel.tryRead(Seq(Channel.Requests), 1.second).flatMap { case Some(r) => IO.pure(r) case None => IO.raiseError(new Exception("no message")) } _ = assert(resp._1 == Channel.Requests) _ = assert(resp._2.copy(idents = Nil) == msg0) // TODO Enforce this is run via bracketing _ <- kernel.close _ <- server.close } yield () t.unsafeRunSync() } } }
Example 121
Source File: UserRepositorySpec.scala From gabbler with Apache License 2.0 | 5 votes |
package de.heikoseeberger.gabbler.user import akka.actor.ActorSystem import akka.persistence.inmemory.query.scaladsl.InMemoryReadJournal import akka.persistence.query.PersistenceQuery import akka.stream.ActorMaterializer import akka.testkit.TestProbe import org.scalatest.{ AsyncWordSpec, BeforeAndAfterAll, Matchers } import scala.concurrent.Await import scala.concurrent.duration.DurationInt class UserRepositorySpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll { import UserRepository._ private implicit val system = ActorSystem() private implicit val mat = ActorMaterializer() private val readJournal = PersistenceQuery(system) .readJournalFor[InMemoryReadJournal](InMemoryReadJournal.Identifier) private val user = User(0, "jsnow", "Jon Snow", "[email protected]") "UserRepository" should { "correctly handle getting, adding and removing users" in { import user._ val userRepository = system.actorOf(UserRepository(readJournal)) val sender = TestProbe() implicit val senderRef = sender.ref userRepository ! GetUsers sender.expectMsg(Users(Set.empty)) userRepository ! AddUser(username, nickname, email) val userAdded = sender.expectMsg(UserAdded(user)) userRepository ! GetUsers sender.expectMsg(Users(Set(user))) userRepository ! AddUser(username, "Jon Targaryen", "[email protected]") sender.expectMsg(UsernameTaken(username)) userRepository ! RemoveUser(id) val userRemoved = sender.expectMsg(UserRemoved(user)) userRepository ! GetUsers sender.expectMsg(Users(Set.empty)) userRepository ! RemoveUser(id) sender.expectMsg(IdUnknown(id)) userRepository ! GetUserEvents(0) val userEvents = sender.expectMsgPF(hint = "source of user events") { case UserEvents(e) => e } userEvents .take(2) .runFold(Vector.empty[(Long, UserEvent)])(_ :+ _) .map( _ should contain inOrder ( (1, userAdded), // The first event has seqNo 1! (2, userRemoved) ) ) } } override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 122
Source File: GenCodecSupportSpec.scala From akka-http-json with Apache License 2.0 | 5 votes |
package de.heikoseeberger.akkahttpavsystemgencodec import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.ContentTypes.`application/json` import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller } import akka.stream.Materialzer import com.avsystem.commons.serialization.GenCodec import org.scalatest.{ AsyncWordSpec, BeforeAndAfterAll, Matchers } import scala.concurrent.Await import scala.concurrent.duration.DurationInt object GenCodecSupportSpec { final object Foo { implicit val codec: GenCodec[Foo] = GenCodec.materialize[Foo] } final case class Foo(bar: String) { require(bar == "bar", "bar must be 'bar'!") } } final class GenCodecSupportSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll { import GenCodecSupport._ import GenCodecSupportSpec._ private implicit val system = ActorSystem() private implicit val mat = Materialzer() "GenCodecSupport" should { "enable marshalling and unmarshalling of case classes" in { val foo = Foo("bar") Marshal(foo) .to[RequestEntity] .flatMap(Unmarshal(_).to[Foo]) .map(_ shouldBe foo) } "provide proper error messages for requirement errors" in { val entity = HttpEntity(MediaTypes.`application/json`, """{ "bar": "baz" }""") Unmarshal(entity) .to[Foo] .failed .map(_ should have message "requirement failed: bar must be 'bar'!") } "fail with NoContentException when unmarshalling empty entities" in { val entity = HttpEntity.empty(`application/json`) Unmarshal(entity) .to[Foo] .failed .map(_ shouldBe Unmarshaller.NoContentException) } "fail with UnsupportedContentTypeException when Content-Type is not `application/json`" in { val entity = HttpEntity("""{ "bar": "bar" }""") Unmarshal(entity) .to[Foo] .failed .map(_ shouldBe UnsupportedContentTypeException(`application/json`)) } "allow unmarshalling with passed in Content-Types" in { val foo = Foo("bar") val `application/json-home` = MediaType.applicationWithFixedCharset("json-home", HttpCharsets.`UTF-8`, "json-home") final object CustomGenCodecSupport extends GenCodecSupport { override def unmarshallerContentTypes = List(`application/json`, `application/json-home`) } import CustomGenCodecSupport._ val entity = HttpEntity(`application/json-home`, """{ "bar": "bar" }""") Unmarshal(entity).to[Foo].map(_ shouldBe foo) } } override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 123
Source File: JsoniterScalaSupportSpec.scala From akka-http-json with Apache License 2.0 | 5 votes |
package de.heikoseeberger.akkahttpjsoniterscala import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.ContentTypes.{ `application/json`, `text/plain(UTF-8)` } import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller } import akka.stream.scaladsl.{ Sink, Source } import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec import com.github.plokhotnyuk.jsoniter_scala.macros._ import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.DurationInt import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AsyncWordSpec object JsoniterScalaSupportSpec { final case class Foo(bar: String) { require(bar startsWith "bar", "bar must start with 'bar'!") } } final class JsoniterScalaSupportSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll { import JsoniterScalaSupport._ import JsoniterScalaSupportSpec._ private implicit val system: ActorSystem = ActorSystem() private implicit val codec: JsonValueCodec[Foo] = JsonCodecMaker.make[Foo](CodecMakerConfig) "JsoniterScalaSupport" should { "should enable marshalling and unmarshalling" in { val foo = Foo("bar") Marshal(foo) .to[RequestEntity] .flatMap(Unmarshal(_).to[Foo]) .map(_ shouldBe foo) } "enable streamed marshalling and unmarshalling for json arrays" in { val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList Marshal(Source(foos)) .to[RequestEntity] .flatMap(entity => Unmarshal(entity).to[SourceOf[Foo]]) .flatMap(_.runWith(Sink.seq)) .map(_ shouldBe foos) } "provide proper error messages for requirement errors" in { val entity = HttpEntity(MediaTypes.`application/json`, """{ "bar": "baz" }""") Unmarshal(entity) .to[Foo] .failed .map(_ should have message "requirement failed: bar must start with 'bar'!") } "fail with NoContentException when unmarshalling empty entities" in { val entity = HttpEntity.empty(`application/json`) Unmarshal(entity) .to[Foo] .failed .map(_ shouldBe Unmarshaller.NoContentException) } "fail with UnsupportedContentTypeException when Content-Type is not `application/json`" in { val entity = HttpEntity("""{ "bar": "bar" }""") Unmarshal(entity) .to[Foo] .failed .map( _ shouldBe UnsupportedContentTypeException(Some(`text/plain(UTF-8)`), `application/json`) ) } "allow unmarshalling with passed in Content-Types" in { val foo = Foo("bar") val `application/json-home` = MediaType.applicationWithFixedCharset("json-home", HttpCharsets.`UTF-8`, "json-home") final object CustomJsoniterScalaSupport extends JsoniterScalaSupport { override def unmarshallerContentTypes: List[ContentTypeRange] = List(`application/json`, `application/json-home`) } import CustomJsoniterScalaSupport._ val entity = HttpEntity(`application/json-home`, """{ "bar": "bar" }""") Unmarshal(entity).to[Foo].map(_ shouldBe foo) } } override protected def afterAll(): Unit = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 124
Source File: UpickleSupportSpec.scala From akka-http-json with Apache License 2.0 | 5 votes |
package de.heikoseeberger.akkahttpupickle import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.ContentTypes.{ `application/json`, `text/plain(UTF-8)` } import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller } import akka.stream.scaladsl.{ Sink, Source } import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.DurationInt import upickle.default.{ ReadWriter, macroRW } import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AsyncWordSpec object UpickleSupportSpec { final object Foo { implicit val rw: ReadWriter[Foo] = macroRW } final case class Foo(bar: String) { require(bar startsWith "bar", "bar must start with 'bar'!") } } final class UpickleSupportSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll { import UpickleSupport._ import UpickleSupportSpec._ private implicit val system = ActorSystem() "UpickleSupport" should { "enable marshalling and unmarshalling of case classes" in { val foo = Foo("bar") Marshal(foo) .to[RequestEntity] .flatMap(Unmarshal(_).to[Foo]) .map(_ shouldBe foo) } "enable streamed marshalling and unmarshalling for json arrays" in { val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList Marshal(Source(foos)) .to[RequestEntity] .flatMap(entity => Unmarshal(entity).to[SourceOf[Foo]]) .flatMap(_.runWith(Sink.seq)) .map(_ shouldBe foos) } "provide proper error messages for requirement errors" in { val entity = HttpEntity(MediaTypes.`application/json`, """{ "bar": "baz" }""") Unmarshal(entity) .to[Foo] .failed .map(_ should have message "requirement failed: bar must start with 'bar'!") } "fail with NoContentException when unmarshalling empty entities" in { val entity = HttpEntity.empty(`application/json`) Unmarshal(entity) .to[Foo] .failed .map(_ shouldBe Unmarshaller.NoContentException) } "fail with UnsupportedContentTypeException when Content-Type is not `application/json`" in { val entity = HttpEntity("""{ "bar": "bar" }""") Unmarshal(entity) .to[Foo] .failed .map( _ shouldBe UnsupportedContentTypeException(Some(`text/plain(UTF-8)`), `application/json`) ) } "allow unmarshalling with passed in Content-Types" in { val foo = Foo("bar") val `application/json-home` = MediaType.applicationWithFixedCharset("json-home", HttpCharsets.`UTF-8`, "json-home") final object CustomUpickleSupport extends UpickleSupport { override def unmarshallerContentTypes = List(`application/json`, `application/json-home`) } import CustomUpickleSupport._ val entity = HttpEntity(`application/json-home`, """{ "bar": "bar" }""") Unmarshal(entity).to[Foo].map(_ shouldBe foo) } } override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 125
Source File: UpickleCustomizationSupportSpec.scala From akka-http-json with Apache License 2.0 | 5 votes |
package de.heikoseeberger.akkahttpupickle import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.ContentTypes.{ `application/json`, `text/plain(UTF-8)` } import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller } import akka.stream.scaladsl.{ Sink, Source } import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AsyncWordSpec import upickle.AttributeTagged import upickle.core.Visitor import scala.concurrent.Await import scala.concurrent.duration.DurationInt final class UpickleCustomizationSupportSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll { private implicit val system = ActorSystem() object FooApi extends AttributeTagged { override implicit val IntWriter: FooApi.Writer[Int] = new Writer[Int] { override def write0[V](out: Visitor[_, V], v: Int): V = out.visitString("foo", -1) } } object UpickleFoo extends UpickleCustomizationSupport { override type Api = FooApi.type override def api: FooApi.type = FooApi } import UpickleFoo._ "UpickleCustomizationSupport" should { "support custom configuration" in { Marshal(123) .to[RequestEntity] .flatMap(Unmarshal(_).to[String]) .map(_ shouldBe "foo") } } override protected def afterAll() = { Await.ready(system.terminate(), 42.seconds) super.afterAll() } }
Example 126
Source File: EtcdCoordinationSpec.scala From constructr with Apache License 2.0 | 5 votes |
package de.heikoseeberger.constructr.coordination.etcd import akka.Done import akka.actor.{ ActorSystem, AddressFromURIString } import akka.testkit.{ TestDuration, TestProbe } import com.typesafe.config.ConfigFactory import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } import scala.concurrent.duration.{ Duration, DurationInt, FiniteDuration } import scala.concurrent.{ Await, Awaitable } import scala.util.Random object EtcdCoordinationSpec { private val coordinationHost = { val dockerHostPattern = """tcp://(\S+):\d{1,5}""".r sys.env .get("DOCKER_HOST") .collect { case dockerHostPattern(address) => address } .getOrElse("127.0.0.1") } } class EtcdCoordinationSpec extends WordSpec with Matchers with BeforeAndAfterAll { import EtcdCoordinationSpec._ private implicit val system = { val config = ConfigFactory .parseString(s"constructr.coordination.host = $coordinationHost") .withFallback(ConfigFactory.load()) ActorSystem("default", config) } private val address = AddressFromURIString("akka.tcp://default@a:2552") private val address2 = AddressFromURIString("akka.tcp://default@b:2552") "EtcdCoordination" should { "correctly interact with etcd" in { val coordination = new EtcdCoordination(randomString(), system) resultOf(coordination.getNodes()) shouldBe 'empty resultOf(coordination.lock(address, 10.seconds.dilated)) shouldBe true resultOf(coordination.lock(address, 10.seconds.dilated)) shouldBe true resultOf(coordination.lock(address2, 10.seconds.dilated)) shouldBe false resultOf(coordination.addSelf(address, 10.seconds.dilated)) shouldBe Done resultOf(coordination.getNodes()) shouldBe Set(address) resultOf(coordination.refresh(address, 1.second.dilated)) shouldBe Done resultOf(coordination.getNodes()) shouldBe Set(address) val probe = TestProbe() probe.within(5.seconds.dilated) { // 2 seconds should be enough, but who knows hows ... probe.awaitAssert { resultOf(coordination.getNodes()) shouldBe 'empty } } } } override protected def afterAll() = { Await.ready(system.terminate(), Duration.Inf) super.afterAll() } private def resultOf[A](awaitable: Awaitable[A], max: FiniteDuration = 3.seconds.dilated) = Await.result(awaitable, max) private def randomString() = math.abs(Random.nextInt).toString }
Example 127
Source File: ClientSpec.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.helpers import java.util.UUID import akka.actor.ActorSystem import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.{KillSwitches, Materializer, SharedKillSwitch} import akka.util.Timeout import akka.util.Timeout.durationToTimeout import com.danielasfregola.twitter4s.entities.streaming.StreamingMessage import com.danielasfregola.twitter4s.http.clients.authentication.AuthenticationClient import com.danielasfregola.twitter4s.http.clients.rest.RestClient import com.danielasfregola.twitter4s.http.clients.streaming.StreamingClient import scala.concurrent.Future import scala.concurrent.duration.DurationInt trait ClientSpec extends Spec { abstract class AuthenticationClientSpecContext extends RequestDSL with SpecContext { protected val authenticationClient = new AuthenticationClient(consumerToken) { override def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])( implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val requestStartTime = System.currentTimeMillis val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR t <- unmarshal[T](requestStartTime, f)(request, response, materializer) } yield t } } } abstract class RestClientSpecContext extends RequestDSL with SpecContext { protected val restClient = new RestClient(consumerToken, accessToken) { override def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])( implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val requestStartTime = System.currentTimeMillis val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR t <- unmarshal[T](requestStartTime, f)(request, response, materializer) } yield t } } } abstract class StreamingClientSpecContext extends RequestDSL with SpecContext { def dummyProcessing: PartialFunction[StreamingMessage, Unit] = { case _ => } val killSwitch = KillSwitches.shared(s"test-twitter4s-${UUID.randomUUID}") protected val streamingClient = new StreamingClient(consumerToken, accessToken) { override def processStreamRequest[T <: StreamingMessage: Manifest]( request: HttpRequest )( f: PartialFunction[T, Unit], errorHandler: PartialFunction[Throwable, Unit] )( implicit system: ActorSystem, materializer: Materializer ): Future[SharedKillSwitch] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR _ <- Future.successful(processBody(response, killSwitch)(f)(manifest[T], request, materializer)) } yield killSwitch } } } }
Example 128
Source File: ExperimentVariantEventLevelDBServiceTest.scala From izanami with Apache License 2.0 | 5 votes |
package specs.leveldb.abtesting import java.io.File import domains.abtesting.events.impl.ExperimentVariantEventLevelDBService import domains.abtesting.AbstractExperimentServiceTest import domains.abtesting.events.ExperimentVariantEventService import env.{DbDomainConfig, DbDomainConfigDetails, LevelDbConfig} import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll} import test.FakeApplicationLifecycle import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, Future} import scala.util.Random class ExperimentVariantEventLevelDBServiceTest extends AbstractExperimentServiceTest("LevelDb") with BeforeAndAfter with BeforeAndAfterAll { private val lifecycle: FakeApplicationLifecycle = new FakeApplicationLifecycle() override def dataStore(name: String): ExperimentVariantEventService.Service = ExperimentVariantEventLevelDBService(s"./target/leveldb-test/data-${Random.nextInt(1000)}") override protected def afterAll(): Unit = { super.afterAll() Await.result(Future.traverse(lifecycle.hooks) { _.apply() }, 5.seconds) import scala.reflect.io.Directory val directory = new Directory(new File("./target/leveldb-test/")) directory.deleteRecursively() } }
Example 129
Source File: LevelDBJsonDataStoreTest.scala From izanami with Apache License 2.0 | 5 votes |
package specs.leveldb.store import java.io.File import env.{DbDomainConfig, DbDomainConfigDetails, InMemory, LevelDbConfig} import org.scalatest.BeforeAndAfterAll import store.AbstractJsonDataStoreTest import test.FakeApplicationLifecycle import scala.concurrent.{Await, Future} import scala.concurrent.duration.DurationInt import scala.util.Random import store.leveldb._ import store.datastore.JsonDataStore class LevelDBJsonDataStoreTest extends AbstractJsonDataStoreTest("LevelDb") with BeforeAndAfterAll { private val lifecycle: FakeApplicationLifecycle = new FakeApplicationLifecycle() override def dataStore(name: String): JsonDataStore.Service = LevelDBJsonDataStore(s"./target/leveldb-storetest/data-${Random.nextInt(1000)}") override protected def afterAll(): Unit = { super.afterAll() Await.result(Future.traverse(lifecycle.hooks) { _.apply() }, 5.seconds) import scala.reflect.io.Directory val directory = new Directory(new File("./target/leveldb-test/")) directory.deleteRecursively() } }
Example 130
Source File: FetchWithCacheConfigClient.scala From izanami with Apache License 2.0 | 5 votes |
package izanami.configs import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import akka.event.Logging import akka.http.scaladsl.util.FastFuture import akka.stream.Materializer import akka.util.Timeout import com.google.common.cache.{Cache, CacheBuilder} import izanami.Strategy.FetchWithCacheStrategy import izanami.scaladsl._ import izanami._ import play.api.libs.json.Json import scala.concurrent.Future import scala.concurrent.duration.DurationInt import scala.util.{Failure, Success} object FetchWithCacheConfigClient { def apply( clientConfig: ClientConfig, fallback: Configs, underlyingStrategy: ConfigClient, cacheConfig: FetchWithCacheStrategy )(implicit izanamiDispatcher: IzanamiDispatcher, actorSystem: ActorSystem, materializer: Materializer): FetchWithCacheConfigClient = new FetchWithCacheConfigClient(clientConfig, fallback, underlyingStrategy, cacheConfig, underlyingStrategy.cudConfigClient) } private[configs] class FetchWithCacheConfigClient( clientConfig: ClientConfig, fallback: Configs, underlyingStrategy: ConfigClient, cacheConfig: FetchWithCacheStrategy, override val cudConfigClient: CUDConfigClient )(implicit val izanamiDispatcher: IzanamiDispatcher, actorSystem: ActorSystem, val materializer: Materializer) extends ConfigClient { import actorSystem.dispatcher implicit val timeout = Timeout(10.second) private val logger = Logging(actorSystem, this.getClass.getName) private val cache: Cache[String, Seq[Config]] = CacheBuilder .newBuilder() .maximumSize(cacheConfig.maxElement) .expireAfterWrite(cacheConfig.duration.toMillis, TimeUnit.MILLISECONDS) .build[String, Seq[Config]]() override def configs(pattern: Seq[String]): Future[Configs] = { val convertedPattern = Option(pattern).map(_.map(_.replace(".", ":")).mkString(",")).getOrElse("*") Option(cache.getIfPresent(convertedPattern)) match { case Some(configs) => FastFuture.successful(Configs(configs)) case None => val futureConfigs = underlyingStrategy.configs(convertedPattern) futureConfigs.onComplete { case Success(c) => cache.put(convertedPattern, c.configs) case Failure(e) => logger.error(e, "Error fetching configs") } futureConfigs } } override def config(key: String) = { require(key != null, "key should not be null") val convertedKey: String = key.replace(".", ":") Option(cache.getIfPresent(convertedKey)) match { case Some(configs) => FastFuture.successful(configs.find(_.id == convertedKey).map(_.value).getOrElse(Json.obj())) case None => val futureConfig: Future[Configs] = underlyingStrategy.configs(convertedKey) futureConfig.onComplete { case Success(configs) => cache.put(convertedKey, configs.configs) case Failure(e) => logger.error(e, "Error fetching features") } futureConfig .map( _.configs .find(_.id == convertedKey) .map(c => c.value) .getOrElse(Json.obj()) ) } } override def configsSource(pattern: String) = underlyingStrategy.configsSource(pattern) override def configsStream(pattern: String) = underlyingStrategy.configsStream(pattern) }
Example 131
Source File: FetchWithCacheConfigClientSpec.scala From izanami with Apache License 2.0 | 5 votes |
package izanami.configs import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.testkit.TestKit import izanami.Strategy.FetchWithCacheStrategy import izanami._ import izanami.scaladsl.{Config, Configs, IzanamiClient} import org.scalatest.BeforeAndAfterAll import org.scalatest.mockito.MockitoSugar import play.api.libs.json.Json import scala.concurrent.duration.DurationInt class FetchWithCacheConfigClientSpec extends IzanamiSpec with BeforeAndAfterAll with MockitoSugar with ConfigServer { implicit val system = ActorSystem("test") implicit val materializer = ActorMaterializer() override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } "FetchWithCacheFeatureStrategy" should { "List configs" in { runServer { ctx => //#config-fetch-cache val strategy = IzanamiClient( ClientConfig(ctx.host) ).configClient( strategy = FetchWithCacheStrategy(maxElement = 2, duration = 1.second), fallback = Configs( "test2" -> Json.obj("value" -> 2) ) ) //#config-fetch-cache val initialConfigs = Seq( Config("test", Json.obj("value" -> 1)) ) ctx.setValues(initialConfigs) val configs: Configs = strategy.configs("*").futureValue strategy.configs("*").futureValue strategy.configs("*").futureValue strategy.configs("*").futureValue configs.configs must be(initialConfigs) ctx.calls.size must be(1) configs.get("test") must be(Json.obj("value" -> 1)) configs.get("test2") must be(Json.obj("value" -> 2)) configs.get("other") must be(Json.obj()) } } "Test feature active" in { runServer { ctx => val strategy = IzanamiClient( ClientConfig(ctx.host) ).configClient( strategy = FetchWithCacheStrategy(2, 5.second), fallback = Configs( "test5" -> Json.obj("value" -> 2) ) ) val initialConfigs = Seq( Config("test1", Json.obj("value" -> 1)), Config("test2", Json.obj("value" -> 2)), Config("test3", Json.obj("value" -> 3)), Config("test4", Json.obj("value" -> 4)) ) ctx.setValues(initialConfigs) strategy.config("test1").futureValue must be(Json.obj("value" -> 1)) ctx.calls must have size 1 strategy.config("test2").futureValue must be(Json.obj("value" -> 2)) ctx.calls must have size 2 strategy.config("test1").futureValue must be(Json.obj("value" -> 1)) ctx.calls must have size 2 strategy.config("test2").futureValue must be(Json.obj("value" -> 2)) ctx.calls must have size 2 strategy.config("test3").futureValue must be(Json.obj("value" -> 3)) ctx.calls must have size 3 } } } }