java.time.Clock Scala Examples

The following examples show how to use java.time.Clock. 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: JobRunServiceFixture.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package jobrun

import java.time.Clock

import dcos.metronome.model._
import mesosphere.marathon.core.task.Task

import scala.collection.concurrent.TrieMap
import scala.concurrent.duration.Duration
import scala.concurrent.{Future, Promise}

object JobRunServiceFixture {

  def simpleJobRunService(): JobRunService =
    new JobRunService {
      val specs = TrieMap.empty[JobRunId, StartedJobRun]

      override def getJobRun(jobRunId: JobRunId): Future[Option[StartedJobRun]] = {
        Future.successful(specs.get(jobRunId))
      }

      override def killJobRun(jobRunId: JobRunId): Future[StartedJobRun] = {
        specs.get(jobRunId) match {
          case Some(value) => Future.successful(value)
          case None => Future.failed(JobRunDoesNotExist(jobRunId))
        }
      }

      override def activeRuns(jobSpecId: JobId): Future[Iterable[StartedJobRun]] = {
        Future.successful(specs.values.filter(_.jobRun.jobSpec.id == jobSpecId))
      }
      override def listRuns(filter: JobRun => Boolean): Future[Iterable[StartedJobRun]] = {
        Future.successful(specs.values.filter(r => filter(r.jobRun)))
      }
      override def startJobRun(jobSpec: JobSpec, schedule: Option[ScheduleSpec] = None): Future[StartedJobRun] = {
        val startingDeadline: Option[Duration] = schedule.map(_.startingDeadline)
        val run = JobRun(
          JobRunId(jobSpec),
          jobSpec,
          JobRunStatus.Active,
          Clock.systemUTC().instant(),
          None,
          startingDeadline,
          Map.empty[Task.Id, JobRunTask]
        )
        val startedRun = StartedJobRun(run, Promise[JobResult].future)
        specs += run.id -> startedRun
        Future.successful(startedRun)
      }
    }
} 
Example 2
Source File: ResourceFSpec.scala    From nexus-iam   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.types

import java.time.{Clock, Instant, ZoneId}

import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources}
import ch.epfl.bluebrain.nexus.iam.config.AppConfig.HttpConfig
import ch.epfl.bluebrain.nexus.iam.config.Vocabulary._
import ch.epfl.bluebrain.nexus.iam.testsyntax._
import ch.epfl.bluebrain.nexus.iam.types.Identity.User
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.Printer
import io.circe.syntax._
import org.scalatest.Inspectors
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

//noinspection TypeAnnotation
class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources {

  "A ResourceMetadata" should {
    val user          = User("mysubject", "myrealm")
    val user2         = User("mysubject2", "myrealm")
    implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com")
    val clock: Clock  = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())
    val instant       = clock.instant()
    val id            = url"http://example.com/id"
    val printer       = Printer.spaces2.copy(dropNullValues = true)

    "be converted to Json correctly" when {
      "using multiple types" in {
        val json  = jsonContentOf("/resources/write-response.json")
        val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList, nxv.Realm), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
      "using a single type" in {
        val json  = jsonContentOf("/resources/write-response-singletype.json")
        val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
      "using no types" in {
        val json  = jsonContentOf("/resources/write-response-notypes.json")
        val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
    }
  }
} 
Example 3
Source File: JobHistory.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package model

import java.time.{Clock, Instant}

import mesosphere.marathon.core.task.Task

case class JobHistorySummary(
    jobSpecId: JobId,
    successCount: Long,
    failureCount: Long,
    lastSuccessAt: Option[Instant],
    lastFailureAt: Option[Instant]
)
object JobHistorySummary {
  def apply(h: JobHistory): JobHistorySummary = {
    JobHistorySummary(h.jobSpecId, h.successCount, h.failureCount, h.lastSuccessAt, h.lastFailureAt)
  }
  def empty(id: JobId): JobHistorySummary = JobHistorySummary(id, 0, 0, None, None)
}

case class JobHistory(
    jobSpecId: JobId,
    successCount: Long,
    failureCount: Long,
    lastSuccessAt: Option[Instant],
    lastFailureAt: Option[Instant],
    successfulRuns: Seq[JobRunInfo],
    failedRuns: Seq[JobRunInfo]
)

object JobHistory {
  def empty(id: JobId): JobHistory = JobHistory(id, 0, 0, None, None, Seq.empty, Seq.empty)
}

case class JobRunInfo(id: JobRunId, createdAt: Instant, finishedAt: Instant, tasks: Seq[Task.Id])
object JobRunInfo {
  def apply(run: JobRun): JobRunInfo = {
    JobRunInfo(run.id, run.createdAt, Clock.systemUTC().instant(), run.tasks.keys.to[Seq])
  }
} 
Example 4
Source File: JobRunId.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package model

import java.time.{Clock, ZoneId}
import java.time.format.DateTimeFormatter

import mesosphere.marathon.state.PathId

case class JobRunId(jobId: JobId, value: String) {
  override def toString: String = s"${jobId.path.mkString(".")}.$value"
  def toPathId: PathId = jobId.toPathId / value
}

object JobRunId {
  val idFormat: DateTimeFormatter = DateTimeFormatter
    .ofPattern("yyyyMMddHHmmss")
    .withZone(ZoneId.systemDefault())

  def apply(job: JobSpec): JobRunId = {
    val date = idFormat.format(Clock.systemUTC().instant())
    val random = scala.util.Random.alphanumeric.take(5).mkString
    JobRunId(job.id, s"$date$random")
  }

  def apply(runSpecId: PathId): JobRunId = {
    JobRunId(JobId(runSpecId.parent), runSpecId.path.last)
  }
} 
Example 5
Source File: ScheduleSpec.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package model

import java.time.{Clock, Instant, ZoneId, ZonedDateTime}

import com.wix.accord.Validator
import com.wix.accord.dsl._

import scala.concurrent.duration._

case class ScheduleSpec(
    id: String,
    cron: CronSpec,
    timeZone: ZoneId = ScheduleSpec.DefaultTimeZone,
    startingDeadline: Duration = ScheduleSpec.DefaultStartingDeadline,
    concurrencyPolicy: ConcurrencyPolicy = ScheduleSpec.DefaultConcurrencyPolicy,
    enabled: Boolean = ScheduleSpec.DefaultEnabled
) {
  def clock: Clock = ScheduleSpec.DefaultClock

  def nextExecution(after: Instant): Instant = {
    val localAfter = ZonedDateTime.ofInstant(after, timeZone)
    val localNext = cron.nextExecution(localAfter)
    localNext.toInstant
  }

  def nextExecution(): Instant = nextExecution(clock.instant())
}

object ScheduleSpec {
  val DefaultTimeZone = ZoneId.of("UTC")
  val DefaultStartingDeadline = 15.minutes
  val DefaultConcurrencyPolicy = ConcurrencyPolicy.Allow
  val DefaultEnabled = true
  val DefaultClock = Clock.systemUTC()

  implicit lazy val validScheduleSpec: Validator[ScheduleSpec] = validator[ScheduleSpec] { spec =>
    spec.startingDeadline >= 1.minute
  }
} 
Example 6
Source File: Event.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package model

import java.time.{Clock, Instant}

trait Event {
  val eventType: String
  val timestamp: Instant
}

object Event {

  trait JobSpecEvent extends Event
  case class JobSpecCreated(
      job: JobSpec,
      eventType: String = "job_created",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobSpecEvent

  case class JobSpecUpdated(
      job: JobSpec,
      eventType: String = "job_updated",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobSpecEvent

  case class JobSpecDeleted(
      job: JobSpec,
      eventType: String = "job_deleted",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobSpecEvent

  trait JobRunEvent extends Event
  case class JobRunStarted(
      jobRun: JobRun,
      eventType: String = "job_run_started",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobRunEvent

  case class JobRunUpdate(
      jobRun: JobRun,
      eventType: String = "job_run_updated",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobRunEvent

  case class JobRunFinished(
      jobRun: JobRun,
      eventType: String = "job_run_finished",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobRunEvent

  case class JobRunFailed(
      jobRun: JobRun,
      eventType: String = "job_run_failed",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends JobRunEvent

  trait ReconciliationEvent extends Event
  case class ReconciliationFinished(
      eventType: String = "job_run_failed",
      timestamp: Instant = Clock.systemUTC().instant()
  ) extends ReconciliationEvent

} 
Example 7
Source File: NotifyOfTaskStateOperationStep.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package scheduler.impl

import akka.Done
import akka.event.EventStream
import dcos.metronome.eventbus.TaskStateChangedEvent
import dcos.metronome.scheduler.TaskState
import mesosphere.marathon.core.instance.update.{InstanceChange, InstanceChangeHandler}
import java.time.Clock

import scala.concurrent.Future

class NotifyOfTaskStateOperationStep(eventBus: EventStream, clock: Clock) extends InstanceChangeHandler {
  override def name: String = "NotifyOfTaskStateOperationStep"
  override def metricName: String = "NotifyOfTaskStateOperationStep"

  override def process(instanceChange: InstanceChange): Future[Done] = {
    taskState(instanceChange).foreach { state =>
      val event = TaskStateChangedEvent(
        taskId = instanceChange.instance.appTask.taskId,
        taskState = state,
        timestamp = clock.instant()
      )
      eventBus.publish(event)
    }

    Future.successful(Done)
  }

  private[this] def taskState(instanceChange: InstanceChange): Option[TaskState] = TaskState(instanceChange.condition)

} 
Example 8
Source File: JobsModule.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome

import java.time.Clock

import akka.actor.ActorSystem
import dcos.metronome.history.JobHistoryModule
import dcos.metronome.jobinfo.JobInfoModule
import dcos.metronome.jobrun.JobRunModule
import dcos.metronome.jobspec.JobSpecModule
import dcos.metronome.queue.LaunchQueueModule
import dcos.metronome.repository.SchedulerRepositoriesModule
import dcos.metronome.scheduler.SchedulerModule
import mesosphere.marathon.MetricsModule
import mesosphere.marathon.core.base.{ActorsModule, JvmExitsCrashStrategy, LifecycleState}
import mesosphere.marathon.core.plugin.{PluginManager, PluginModule}

class JobsModule(config: JobsConfig, actorSystem: ActorSystem, clock: Clock, metricsModule: MetricsModule) {

  private[this] lazy val crashStrategy = JvmExitsCrashStrategy
  private[this] lazy val lifecycleState = LifecycleState.WatchingJVM
  private[this] lazy val pluginModule = new PluginModule(config.scallopConf, crashStrategy)
  def pluginManger: PluginManager = pluginModule.pluginManager

  val actorsModule = new ActorsModule(actorSystem)

  val schedulerRepositoriesModule = new SchedulerRepositoriesModule(
    metricsModule.metrics,
    config,
    lifecycleState,
    actorsModule,
    actorSystem,
    crashStrategy
  )

  val schedulerModule: SchedulerModule = new SchedulerModule(
    metricsModule.metrics,
    config,
    actorSystem,
    clock,
    schedulerRepositoriesModule,
    pluginModule,
    lifecycleState,
    crashStrategy,
    actorsModule
  )

  val jobRunModule = {
    val launchQueue = schedulerModule.launchQueueModule.launchQueue
    val instanceTracker = schedulerModule.instanceTrackerModule.instanceTracker
    val driverHolder = schedulerModule.schedulerDriverHolder
    new JobRunModule(
      config,
      actorSystem,
      clock,
      schedulerRepositoriesModule.jobRunRepository,
      launchQueue,
      instanceTracker,
      driverHolder,
      metricsModule.metrics,
      schedulerModule.leadershipModule
    )
  }

  val jobSpecModule = new JobSpecModule(
    config,
    actorSystem,
    clock,
    schedulerRepositoriesModule.jobSpecRepository,
    jobRunModule.jobRunService,
    metricsModule.metrics,
    schedulerModule.leadershipModule
  )

  val jobHistoryModule = new JobHistoryModule(
    config,
    actorSystem,
    clock,
    schedulerRepositoriesModule.jobHistoryRepository,
    metricsModule.metrics,
    schedulerModule.leadershipModule
  )

  val jobInfoModule =
    new JobInfoModule(jobSpecModule.jobSpecService, jobRunModule.jobRunService, jobHistoryModule.jobHistoryService)

  val queueModule = new LaunchQueueModule(schedulerModule.launchQueueModule.launchQueue)
} 
Example 9
Source File: JobHistoryModule.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package history

import java.time.Clock

import akka.actor.{ActorRef, ActorSystem}
import dcos.metronome.history.impl.{JobHistoryServiceActor, JobHistoryServiceDelegate}
import dcos.metronome.model.{JobHistory, JobId}
import dcos.metronome.repository.Repository
import mesosphere.marathon.core.leadership.LeadershipModule
import mesosphere.marathon.metrics.Metrics

class JobHistoryModule(
    config: JobHistoryConfig,
    actorSystem: ActorSystem,
    clock: Clock,
    repository: Repository[JobId, JobHistory],
    metrics: Metrics,
    leadershipModule: LeadershipModule
) {

  val jobHistoryServiceActor: ActorRef = leadershipModule.startWhenLeader(
    JobHistoryServiceActor.props(config, clock, repository, metrics),
    "JobHistoryServiceActor"
  )

  lazy val jobHistoryService: JobHistoryService = new JobHistoryServiceDelegate(jobHistoryServiceActor, config)
} 
Example 10
Source File: JobSpecModule.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package jobspec

import java.time.Clock

import akka.actor.ActorSystem
import dcos.metronome.jobrun.JobRunService
import dcos.metronome.jobspec.impl.{
  JobSpecPersistenceActor,
  JobSpecSchedulerActor,
  JobSpecServiceActor,
  JobSpecServiceDelegate
}
import dcos.metronome.model.{JobId, JobSpec}
import dcos.metronome.repository.Repository
import mesosphere.marathon.core.leadership.LeadershipModule
import mesosphere.marathon.metrics.Metrics

class JobSpecModule(
    config: JobSpecConfig,
    actorSystem: ActorSystem,
    clock: Clock,
    jobSpecRepository: Repository[JobId, JobSpec],
    runService: JobRunService,
    metrics: Metrics,
    leadershipModule: LeadershipModule
) {

  private[this] def persistenceActor(id: JobId) = JobSpecPersistenceActor.props(id, jobSpecRepository, metrics)
  private[this] def scheduleActor(jobSpec: JobSpec) = JobSpecSchedulerActor.props(jobSpec, clock, runService)

  val serviceActor = leadershipModule.startWhenLeader(
    JobSpecServiceActor.props(jobSpecRepository, persistenceActor, scheduleActor),
    "JobSpecServiceActor"
  )

  def jobSpecService: JobSpecService = new JobSpecServiceDelegate(config, serviceActor, metrics)
} 
Example 11
Source File: JobSpecSchedulerActor.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package jobspec.impl

import java.time.{Clock, Instant}
import java.util.concurrent.TimeUnit

import akka.actor._
import dcos.metronome.jobrun.JobRunService
import dcos.metronome.model.{JobSpec, ScheduleSpec}

import scala.concurrent.duration._


class JobSpecSchedulerActor(initSpec: JobSpec, clock: Clock, runService: JobRunService)
    extends Actor
    with Stash
    with ActorLogging {

  import JobSpecSchedulerActor._
  import context.dispatcher

  private[impl] var spec = initSpec
  private[impl] var nextSchedule: Option[Cancellable] = None
  private[impl] var scheduledAt: Option[Instant] = None

  override def preStart(): Unit = {
    scheduleNextRun()
  }

  override def postStop(): Unit = {
    cancelSchedule()
  }

  override def receive: Receive = {
    case StartJob(schedule) => runJob(schedule)
    case UpdateJobSpec(newSpec) => updateSpec(newSpec)
  }

  def updateSpec(newSpec: JobSpec): Unit = {
    log.info(s"JobSpec ${newSpec.id} has been updated. Reschedule.")
    spec = newSpec
    scheduledAt = None
    scheduleNextRun()
  }

  def runJob(schedule: ScheduleSpec): Unit = {
    log.info(s"Start next run of job ${spec.id}, which was scheduled for $scheduledAt")
    runService.startJobRun(spec, Some(schedule))
    scheduleNextRun()
  }

  def scheduleNextRun(): Unit = {
    val lastScheduledAt = scheduledAt
    cancelSchedule()
    // TODO: only reschedule for one specific schedule!
    spec.schedules.foreach { schedule =>
      val now = clock.instant()
      val from = lastScheduledAt.getOrElse(now)
      val nextTime = schedule.nextExecution(from)
      scheduledAt = Some(nextTime)
      // 60 secs is the smallest unit of reschedule time for cron
      val inSeconds = Math.max(java.time.Duration.between(now, nextTime).getSeconds, 60)
      nextSchedule =
        Some(context.system.scheduler.scheduleOnce(Duration(inSeconds, TimeUnit.SECONDS), self, StartJob(schedule)))
      log.info(s"Spec ${spec.id}: next run is scheduled for: $nextTime (in $inSeconds seconds)")
    }
  }

  def cancelSchedule(): Unit = {
    nextSchedule.foreach { c => if (!c.isCancelled) c.cancel() }
    nextSchedule = None
    scheduledAt = None
  }
}

object JobSpecSchedulerActor {

  case class StartJob(schedule: ScheduleSpec)
  case class UpdateJobSpec(newSpec: JobSpec)

  def props(spec: JobSpec, clock: Clock, runService: JobRunService): Props = {
    Props(new JobSpecSchedulerActor(spec, clock, runService))
  }
} 
Example 12
Source File: JobRunModule.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package jobrun

import java.time.Clock

import akka.actor.{ActorContext, ActorSystem, Props}
import dcos.metronome.jobrun.impl.{
  JobRunExecutorActor,
  JobRunPersistenceActor,
  JobRunServiceActor,
  JobRunServiceDelegate
}
import dcos.metronome.model.{JobResult, JobRun, JobRunId}
import dcos.metronome.repository.Repository
import mesosphere.marathon.MarathonSchedulerDriverHolder
import mesosphere.marathon.core.launchqueue.LaunchQueue
import mesosphere.marathon.core.leadership.LeadershipModule
import mesosphere.marathon.core.task.tracker.InstanceTracker
import mesosphere.marathon.metrics.Metrics

import scala.concurrent.Promise

class JobRunModule(
    config: JobRunConfig,
    actorSystem: ActorSystem,
    clock: Clock,
    jobRunRepository: Repository[JobRunId, JobRun],
    launchQueue: LaunchQueue,
    instanceTracker: InstanceTracker,
    driverHolder: MarathonSchedulerDriverHolder,
    metrics: Metrics,
    leadershipModule: LeadershipModule
) {

  import com.softwaremill.macwire._

  private[this] def executorFactory(jobRun: JobRun, promise: Promise[JobResult]): Props = {
    val persistenceActorFactory = (id: JobRunId, context: ActorContext) =>
      context.actorOf(JobRunPersistenceActor.props(id, jobRunRepository, metrics))
    JobRunExecutorActor.props(
      jobRun,
      promise,
      persistenceActorFactory,
      launchQueue,
      instanceTracker,
      driverHolder,
      clock
    )(actorSystem.scheduler)
  }

  val jobRunServiceActor = leadershipModule.startWhenLeader(
    JobRunServiceActor.props(clock, executorFactory, jobRunRepository, metrics),
    "JobRunService"
  )

  def jobRunService: JobRunService = wire[JobRunServiceDelegate]
} 
Example 13
Source File: SettableClock.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome

import java.time.{Clock, Instant, LocalDateTime, ZoneOffset, ZoneId, Duration}

import scala.concurrent.duration.FiniteDuration

object SettableClock {
  private val defaultJavaClock =
    Clock.fixed(LocalDateTime.of(2015, 4, 9, 12, 30, 0).toInstant(ZoneOffset.UTC), ZoneOffset.UTC)

  def ofNow() = new SettableClock(Clock.fixed(Instant.now(), ZoneOffset.UTC))
}

class SettableClock(private[this] var clock: Clock = SettableClock.defaultJavaClock) extends Clock {
  private[this] var subscribers: List[() => Unit] = Nil
  def onChange(fn: () => Unit): Unit =
    synchronized {
      subscribers = fn :: subscribers
    }

  override def getZone: ZoneId = clock.getZone

  override def instant(): Instant = clock.instant()

  override def withZone(zoneId: ZoneId): Clock = new SettableClock(clock.withZone(zoneId))

  def +=(duration: FiniteDuration): Unit = plus(duration)

  def plus(duration: FiniteDuration): this.type =
    plus(Duration.ofMillis(duration.toMillis))

  def plus(duration: Duration): this.type = {
    clock = Clock.offset(clock, duration)
    subscribers.foreach(_())
    this
  }

  def at(instant: Instant): this.type = {
    clock = Clock.fixed(instant, clock.getZone)
    subscribers.foreach(_())
    this
  }
} 
Example 14
Source File: JobSpecServiceFixture.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package jobspec.impl

import java.time.Clock

import dcos.metronome.jobspec.JobSpecService
import dcos.metronome.model.{JobId, JobSpec, ScheduleSpec}

import scala.collection.concurrent.TrieMap
import scala.concurrent.Future
import scala.util.control.NonFatal

object JobSpecServiceFixture {

  def simpleJobSpecService(testClock: Clock = Clock.systemUTC()): JobSpecService =
    new JobSpecService {
      val specs = TrieMap.empty[JobId, JobSpec]
      import Future._
      override def getJobSpec(id: JobId): Future[Option[JobSpec]] = successful(specs.get(id))

      override def createJobSpec(jobSpec: JobSpec): Future[JobSpec] = {
        specs.get(jobSpec.id) match {
          case Some(_) =>
            failed(JobSpecAlreadyExists(jobSpec.id))
          case None =>
            specs += jobSpec.id -> jobSpecWithMockedTime(jobSpec)
            successful(jobSpec)
        }
      }

      private def jobSpecWithMockedTime(jobSpec: JobSpec): JobSpec =
        jobSpec.copy(schedules =
          jobSpec.schedules.map(s =>
            new ScheduleSpec(s.id, s.cron, s.timeZone, s.startingDeadline, s.concurrencyPolicy, s.enabled) {
              override def clock: Clock = testClock
            }
          )
        )

      override def updateJobSpec(id: JobId, update: JobSpec => JobSpec): Future[JobSpec] = {
        specs.get(id) match {
          case Some(spec) =>
            try {
              val changed = update(spec)
              specs.update(id, jobSpecWithMockedTime(changed))
              successful(changed)
            } catch {
              case NonFatal(ex) => failed(ex)
            }
          case None => failed(JobSpecDoesNotExist(id))
        }
      }

      override def listJobSpecs(filter: JobSpec => Boolean): Future[Iterable[JobSpec]] = {
        successful(specs.values.filter(filter))
      }

      override def deleteJobSpec(id: JobId): Future[JobSpec] = {
        specs.get(id) match {
          case Some(spec) =>
            specs -= id
            successful(spec)
          case None => failed(JobSpecDoesNotExist(id))
        }
      }
    }
} 
Example 15
Source File: Module.scala    From Full-Stack-Scala-Starter   with Apache License 2.0 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}


class Module extends AbstractModule {

  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])
  }
} 
Example 16
Source File: JavaInstantGenerators.scala    From scalacheck-ops   with Apache License 2.0 5 votes vote down vote up
package org.scalacheck.ops.time

import java.time.{Clock, Duration, Instant}

import org.scalacheck.Gen

object JavaInstantGenerators extends JavaInstantGenerators
trait JavaInstantGenerators extends AbstractTimeGenerators {
  override type InstantType = Instant
  override type DurationType = Duration
  override type ParamsType = Clock

  override val defaultRange: Duration = Duration.ofDays(365)

  override val defaultParams: Clock = Clock.systemUTC()

  override protected[time] def now(implicit clock: Clock): Instant = Instant.now(clock)

  import JavaLocalTimeGenerators.MAX_NANOS

  override def between(start: Instant, end: Instant)(implicit params: Clock): Gen[Instant] = {
    val startSeconds = start.getEpochSecond
    val endSeconds = end.getEpochSecond
    if (startSeconds == endSeconds) {
      for {
        nanos <- Gen.choose(start.getNano, end.getNano)
      } yield Instant.ofEpochSecond(startSeconds, nanos)
    }
    else {
      for {
        seconds <- Gen.choose(startSeconds, endSeconds)
        nanos <- seconds match {
          case `startSeconds` =>
            Gen.choose(start.getNano, MAX_NANOS)
          case `endSeconds` =>
            Gen.choose(0, end.getNano)
          case _ =>
            Gen.choose(0, MAX_NANOS)
        }
      } yield Instant.ofEpochSecond(seconds, nanos)
    }
  }

  override protected[time] def addToCeil(
    instant: Instant,
    duration: Duration
  )(implicit params: Clock): Instant = {
    try instant plus duration
    catch {
      case ex: ArithmeticException => Instant.MAX
    }
  }

  override protected[time] def subtractToFloor(
    instant: Instant,
    duration: Duration
  )(implicit params: Clock): Instant = {
    try instant minus duration
    catch {
      case ex: ArithmeticException => Instant.MIN
    }
  }
} 
Example 17
Source File: JavaLocalTimeGenerators.scala    From scalacheck-ops   with Apache License 2.0 5 votes vote down vote up
package org.scalacheck.ops.time

import java.time.{Clock, Duration, LocalTime}

import org.scalacheck.Gen

trait JavaLocalTimeGenerators extends AbstractTimeGenerators {
  override type InstantType = LocalTime
  override type DurationType = Duration
  override type ParamsType = Clock

  override val defaultParams: Clock = Clock.systemUTC()

  override val defaultRange: Duration = Duration.ofHours(24)

  override protected[time] def now(implicit params: Clock): LocalTime = LocalTime.now(params)

  override def between(start: LocalTime, end: LocalTime)(implicit params: Clock): Gen[LocalTime] = {
    for {
      nanoOfDay <- Gen.choose(start.toNanoOfDay, end.toNanoOfDay)
    } yield LocalTime.ofNanoOfDay(nanoOfDay)
  }

  override protected[time] def addToCeil(instant: LocalTime, duration: Duration)
    (implicit params: Clock): LocalTime = {
    instant plus duration
  }

  override protected[time] def subtractToFloor(instant: LocalTime, duration: Duration)
    (implicit params: Clock): LocalTime = {
    instant minus duration
  }
}

object JavaLocalTimeGenerators extends JavaLocalTimeGenerators {

  final val MAX_NANOS = 999999999
} 
Example 18
Source File: ApplicationTimer.scala    From play-swagger   with Apache License 2.0 5 votes vote down vote up
package services

import java.time.{ Clock, Instant }
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future


@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {

  // This code is called when the application starts.
  private val start: Instant = clock.instant
  Logger.info(s"ApplicationTimer demo: Starting application at $start.")

  // When the application starts, register a stop hook with the
  // ApplicationLifecycle object. The code inside the stop hook will
  // be run when the application stops.
  appLifecycle.addStopHook { () ⇒
    val stop: Instant = clock.instant
    val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
    Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
    Future.successful(())
  }
} 
Example 19
Source File: Module.scala    From play-swagger   with Apache License 2.0 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ ApplicationTimer, AtomicCounter, Counter }


class Module extends AbstractModule {

  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])
  }

} 
Example 20
Source File: IndexNameFragment.scala    From stream-reactor   with Apache License 2.0 5 votes vote down vote up
package com.datamountaineer.streamreactor.connect.elastic6.indexname

import java.time.Clock
import java.time.LocalDateTime._
import java.time.format.DateTimeFormatter._

object ClockProvider {
  val ClockInstance: Clock = Clock.systemUTC()
}

sealed trait IndexNameFragment {
  def getFragment: String
}

case class TextFragment(text: String) extends IndexNameFragment {
  override def getFragment: String = text
}

case class DateTimeFragment(dateTimeFormat: String, clock: Clock = ClockProvider.ClockInstance) extends IndexNameFragment {
  override def getFragment: String = s"${now(clock).format(ofPattern(dateTimeFormat))}"
}
object DateTimeFragment {
  val OpeningChar = '{'
  val ClosingChar = '}'
} 
Example 21
Source File: StorageCache.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.cache

import java.time.{Clock, Instant}
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap

import akka.actor.ActorSystem
import cats.Monad
import cats.effect.{Effect, Timer}
import cats.implicits._
import ch.epfl.bluebrain.nexus.commons.cache.{KeyValueStore, KeyValueStoreConfig}
import ch.epfl.bluebrain.nexus.kg.RevisionedValue
import ch.epfl.bluebrain.nexus.kg.cache.Cache._
import ch.epfl.bluebrain.nexus.kg.cache.StorageProjectCache._
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.kg.storage.Storage
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri

class StorageCache[F[_]: Effect: Timer] private (projectToCache: ConcurrentHashMap[UUID, StorageProjectCache[F]])(
    implicit as: ActorSystem,
    config: KeyValueStoreConfig,
    clock: Clock
) {

  
private class StorageProjectCache[F[_]: Monad] private (store: KeyValueStore[F, AbsoluteIri, RevisionedStorage])
    extends Cache[F, AbsoluteIri, RevisionedStorage](store) {

  private implicit val ordering: Ordering[RevisionedStorage] = Ordering.by((s: RevisionedStorage) => s.rev).reverse

  private implicit def revisioned(storage: Storage)(implicit instant: Instant): RevisionedStorage =
    RevisionedValue(instant.toEpochMilli, storage)

  def get: F[List[Storage]] =
    store.values.map(_.toList.sorted.map(_.value))

  def getDefault: F[Option[Storage]] =
    get.map(_.collectFirst { case storage if storage.default => storage })

  def getBy(id: AbsoluteIri): F[Option[Storage]] =
    get(id).map(_.collectFirst { case RevisionedValue(_, storage) if storage.id == id => storage })

  def put(storage: Storage)(implicit instant: Instant): F[Unit] =
    if (storage.deprecated) store.remove(storage.id)
    else store.put(storage.id, storage)
}

private object StorageProjectCache {

  type RevisionedStorage = RevisionedValue[Storage]

  def apply[F[_]: Effect: Timer](
      project: ProjectRef
  )(implicit as: ActorSystem, config: KeyValueStoreConfig): StorageProjectCache[F] =
    new StorageProjectCache(
      KeyValueStore.distributed(s"storage-${project.id}", (_, storage) => storage.value.rev)
    )

}

object StorageCache {

  def apply[F[_]: Timer: Effect](implicit as: ActorSystem, config: KeyValueStoreConfig, clock: Clock): StorageCache[F] =
    new StorageCache(new ConcurrentHashMap[UUID, StorageProjectCache[F]]())
} 
Example 22
Source File: CompositeResolutionSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.resolve

import java.time.Clock
import java.util.UUID
import java.util.regex.Pattern.quote

import cats.instances.try_._
import cats.syntax.show._
import ch.epfl.bluebrain.nexus.commons.test.Resources
import ch.epfl.bluebrain.nexus.kg.resources.Ref
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.implicits._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.{OptionValues, TryValues}

import scala.util.Try

class CompositeResolutionSpec extends AnyWordSpecLike with Resources with Matchers with TryValues with OptionValues {

  "CompositeResolution" should {

    implicit val clock = Clock.systemUTC

    val resource1Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}"
    val resource2Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}"
    val staticResolution1 = StaticResolution[Try](
      Map(
        resource1Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )
    val staticResolution2 = StaticResolution[Try](
      Map(
        resource2Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource2Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )
    val staticResolution3 = StaticResolution[Try](
      Map(
        resource1Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )

    val compositeResolution = CompositeResolution[Try](List(staticResolution1, staticResolution2, staticResolution3))

    "return the resource from the first resolver which returns the resource" in {

      compositeResolution.resolve(Ref(resource1Uri)).success.value.value shouldEqual staticResolution1
        .resolve(Ref(resource1Uri))
        .success
        .value
        .value
      compositeResolution.resolve(Ref(resource2Uri)).success.value.value shouldEqual staticResolution2
        .resolve(Ref(resource2Uri))
        .success
        .value
        .value
    }
  }

} 
Example 23
Source File: TarFlowSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.archives

import java.nio.file.Files
import java.time.{Clock, Instant, ZoneId}

import akka.actor.ActorSystem
import akka.stream.scaladsl.FileIO
import akka.testkit.TestKit
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.storage.digestSink
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration._

class TarFlowSpec
    extends TestKit(ActorSystem("TarFlowSpec"))
    with AnyWordSpecLike
    with Matchers
    with TestHelper
    with ScalaFutures {

  private implicit val ec    = system.dispatcher
  private implicit val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault())

  override implicit def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds)

  "A TarFlow" should {

    "tar a bunch of sources" in {
      val digest =
        "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886"
      val elems = 500
      val contents =
        List.tabulate(2) { i =>
          val content = (i until (i + elems)).toList.mkString(",") + "\n"
          ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content))
        }
      val path = Files.createTempFile("test", ".tar")
      TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue
      FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest
      Files.delete(path)
    }
  }
} 
Example 24
Source File: ArchiveCacheSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.archives

import java.time.{Clock, Instant, ZoneId}

import cats.effect.{IO, Timer}
import ch.epfl.bluebrain.nexus.admin.client.types.Project
import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture
import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues
import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription}
import ch.epfl.bluebrain.nexus.kg.config.Settings
import ch.epfl.bluebrain.nexus.kg.resources.Id
import ch.epfl.bluebrain.nexus.kg.resources.syntax._
import org.scalatest.concurrent.Eventually
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration._

class ArchiveCacheSpec
    extends ActorSystemFixture("ArchiveCacheSpec", true)
    with TestHelper
    with AnyWordSpecLike
    with Matchers
    with IOOptionValues
    with Eventually {

  override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds)

  private val appConfig = Settings(system).appConfig
  private implicit val config =
    appConfig.copy(archives = appConfig.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100))
  private implicit val timer: Timer[IO] = IO.timer(system.dispatcher)

  private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue
  private implicit val clock          = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault())
  private val instant                 = clock.instant()

  def randomProject() = {
    val instant = Instant.EPOCH
    // format: off
    Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri)
    // format: on
  }

  "An archive cache" should {

    "write and read an Archive" in {
      val resId     = Id(randomProject().ref, genIri)
      val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None)
      val file1     = File(genIri, randomProject(), None, None, None)
      val archive   = Archive(resId, instant, Anonymous, Set(resource1, file1))
      val _         = cache.put(archive).value.some
      cache.get(archive.resId).value.some shouldEqual archive
    }

    "read a non existing resource" in {
      val resId = Id(randomProject().ref, genIri)
      cache.get(resId).value.ioValue shouldEqual None
    }

    "read after timeout" in {
      val resId   = Id(randomProject().ref, genIri)
      val set     = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None))
      val archive = Archive(resId, instant, Anonymous, set)
      val _       = cache.put(archive).value.some
      val time    = System.currentTimeMillis()
      cache.get(resId).value.some shouldEqual archive
      eventually {
        cache.get(resId).value.ioValue shouldEqual None
      }
      val diff = System.currentTimeMillis() - time
      diff should be > config.archives.cacheInvalidateAfter.toMillis
      diff should be < config.archives.cacheInvalidateAfter.toMillis + 300
    }
  }
} 
Example 25
Source File: TestHelper.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg

import java.time.Clock
import java.util.UUID

import akka.stream.Materializer
import akka.stream.scaladsl.Source
import akka.util.ByteString
import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness}
import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.iam.client.types.{AccessControlList, Identity, Permission, ResourceAccessControlList}
import ch.epfl.bluebrain.nexus.kg.config.Schemas.unconstrainedSchemaUri
import ch.epfl.bluebrain.nexus.kg.resources.ResourceF.Value
import ch.epfl.bluebrain.nexus.kg.resources.{Ref, ResId, ResourceF}
import ch.epfl.bluebrain.nexus.kg.storage.AkkaSource
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.Json

trait TestHelper extends EitherValues with Randomness {

  private val clock     = Clock.systemUTC()
  val read: Permission  = Permission.unsafe("resources/read")
  val write: Permission = Permission.unsafe("files/write")

  def consume(source: AkkaSource)(implicit mt: Materializer): String = {
    import org.scalatest.concurrent.ScalaFutures._
    source.runFold("")(_ ++ _.utf8String).futureValue
  }

  def produce(string: String, chunkSize: Int = 100): AkkaSource =
    Source(string.grouped(chunkSize).map(ByteString(_)).toList)

  def resourceAcls(acl: AccessControlList): ResourceAccessControlList =
    ResourceAccessControlList(
      url"http://example.com/id",
      1L,
      Set.empty,
      clock.instant(),
      Anonymous,
      clock.instant(),
      Anonymous,
      acl
    )

  def simpleV(
      id: ResId,
      value: Json,
      rev: Long = 1L,
      types: Set[AbsoluteIri] = Set.empty,
      deprecated: Boolean = false,
      schema: Ref = Ref(unconstrainedSchemaUri),
      created: Identity = Anonymous,
      updated: Identity = Anonymous
  )(implicit clock: Clock): ResourceF[Value] =
    ResourceF(
      id,
      rev,
      types,
      deprecated,
      Map.empty,
      None,
      clock.instant(),
      clock.instant(),
      created,
      updated,
      schema,
      Value(value, value.contextValue, value.toGraph(id.value).rightValue)
    )

  def simpleV(res: ResourceF[Json])(implicit clock: Clock) = ResourceF(
    res.id,
    res.rev,
    res.types,
    res.deprecated,
    Map.empty,
    None,
    clock.instant(),
    clock.instant(),
    res.createdBy,
    res.updatedBy,
    res.schema,
    Value(res.value, res.value.contextValue, res.value.toGraph(res.id.value).rightValue)
  )

  def genUUID: UUID = UUID.randomUUID()

  def genIri: AbsoluteIri = url"http://example.com/" + genUUID.toString

  private def sourceInChunks(input: String): AkkaSource =
    Source.fromIterator(() => input.grouped(10000).map(ByteString(_)))

  def genSource: AkkaSource = sourceInChunks(genString())

} 
Example 26
Source File: SparqlLinkSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.indexing

import java.time.{Clock, Instant, ZoneId}

import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding
import ch.epfl.bluebrain.nexus.kg.config.Schemas._
import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv
import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink}
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.Vocabulary._
import ch.epfl.bluebrain.nexus.rdf.implicits._
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.OptionValues
import org.scalatest.matchers.should.Matchers

class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues {

  "A SparqlLink" should {

    val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())

    val id        = url"http://example.com/id"
    val property  = url"http://example.com/friend"
    val property2 = url"http://example.com/friend2"
    val paths     = List(property, property2)

    "build SparqlExternalLink from SPARQL response" in {
      val bindings = Map(
        "s"     -> Binding("uri", id.asString),
        "paths" -> Binding("literal", s"${property.asString} ${property2.asString}")
      )
      SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths)
    }

    "build SparqlResourceLink from SPARQL response" in {
      val self    = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id"
      val project = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/"
      val author  = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me"
      val bindings = Map(
        "s"              -> Binding("uri", id.asString),
        "paths"          -> Binding("literal", s"${property.asString} ${property2.asString}"),
        "_rev"           -> Binding("literal", "1", datatype = Some(xsd.long.asString)),
        "_self"          -> Binding("uri", self.asString),
        "_project"       -> Binding("uri", project.asString),
        "types"          -> Binding("literal", s"${nxv.Resolver.asString} ${nxv.Schema.asString}"),
        "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString),
        "_createdBy"     -> Binding("uri", author.asString),
        "_updatedBy"     -> Binding("uri", author.asString),
        "_createdAy"     -> Binding("uri", author.asString),
        "_createdAt"     -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)),
        "_updatedAt"     -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)),
        "_deprecated"    -> Binding("literal", "false", datatype = Some(xsd.boolean.asString))
      )
      SparqlResourceLink(bindings).value shouldEqual
        SparqlResourceLink(
          id,
          project,
          self,
          1L,
          Set[AbsoluteIri](nxv.Schema, nxv.Resolver),
          false,
          clock.instant(),
          clock.instant(),
          author,
          author,
          unconstrainedRef,
          paths
        )
    }
  }

} 
Example 27
Source File: TaggingAdapterSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.persistence

import java.time.{Clock, Instant, ZoneId}

import akka.persistence.journal.Tagged
import cats.syntax.show._
import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.config.Schemas._
import ch.epfl.bluebrain.nexus.kg.config.Vocabulary._
import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other
import ch.epfl.bluebrain.nexus.kg.resources.Event._
import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref}
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import io.circe.Json
import org.scalatest.Inspectors
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper {

  "A TaggingAdapter" should {
    val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())

    def genJson(): Json = Json.obj("key" -> Json.fromString(genString()))

    val adapter = new TaggingAdapter()
    val orgRef  = OrganizationRef(genUUID)
    val id      = Id(ProjectRef(genUUID), nxv.projects)

    val mapping = Map(
      Set(
        s"type=${nxv.Schema.value.show}",
        s"type=${nxv.Resource.value.show}",
        s"project=${id.parent.id}",
        s"org=${orgRef.show}",
        "event"
      ) ->
        Created(id, orgRef, Ref(shaclSchemaUri), Set(nxv.Schema, nxv.Resource), genJson(), clock.instant(), Anonymous),
      Set(
        s"type=${nxv.Resolver.value.show}",
        s"type=${nxv.Resource.value.show}",
        s"project=${id.parent.id}",
        s"org=${orgRef.show}",
        "event"
      ) ->
        Updated(id, orgRef, 1L, Set(nxv.Resource, nxv.Resolver), genJson(), clock.instant(), Anonymous),
      Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") ->
        Deprecated(id, orgRef, 1L, Set(nxv.Resource), clock.instant(), Anonymous),
      Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event") ->
        TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous)
    )

    "set the appropriate tags" in {
      forAll(mapping.toList) {
        case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags)
      }
    }

    "return an empty manifest" in {
      adapter.manifest(Other(genString())) shouldEqual ""
    }
  }
}

object TaggingAdapterSpec {
  private[persistence] final case class Other(value: String)

} 
Example 28
Source File: StorageCacheSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.cache

import java.nio.file.Paths
import java.time.Clock

import akka.testkit._
import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.config.AppConfig._
import ch.epfl.bluebrain.nexus.kg.config.{AppConfig, Settings}
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.{ProjectRef}
import ch.epfl.bluebrain.nexus.kg.storage.Storage.DiskStorage
import ch.epfl.bluebrain.nexus.rdf.implicits._
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.{Inspectors, TryValues}

import scala.concurrent.duration._

//noinspection NameBooleanParameters
class StorageCacheSpec
    extends ActorSystemFixture("StorageCacheSpec", true)
    with Matchers
    with Inspectors
    with ScalaFutures
    with TryValues
    with TestHelper {

  override implicit def patienceConfig: PatienceConfig = PatienceConfig(3.seconds.dilated, 5.milliseconds)

  private implicit val clock: Clock         = Clock.systemUTC
  private implicit val appConfig: AppConfig = Settings(system).appConfig

  val ref1 = ProjectRef(genUUID)
  val ref2 = ProjectRef(genUUID)

  val time   = clock.instant()
  val lastId = url"http://example.com/lastA"
  // initialInstant.minusSeconds(1L + genInt().toLong)

  val tempStorage = DiskStorage(ref1, genIri, 1L, false, true, "alg", Paths.get("/tmp"), read, write, 1024L)

  val lastStorageProj1 = tempStorage.copy(id = lastId)
  val lastStorageProj2 = tempStorage.copy(ref = ref2, id = lastId)

  val storagesProj1: List[DiskStorage] = List.fill(5)(tempStorage.copy(id = genIri)) :+ lastStorageProj1
  val storagesProj2: List[DiskStorage] = List.fill(5)(tempStorage.copy(ref = ref2, id = genIri)) :+ lastStorageProj2

  private val cache = StorageCache[Task]

  "StorageCache" should {

    "index storages" in {
      forAll((storagesProj1 ++ storagesProj2).zipWithIndex) {
        case (storage, index) =>
          implicit val instant = time.plusSeconds(index.toLong)
          cache.put(storage).runToFuture.futureValue
          cache.get(storage.ref, storage.id).runToFuture.futureValue shouldEqual Some(storage)
      }
    }

    "get latest default storage" in {
      cache.getDefault(ref1).runToFuture.futureValue shouldEqual Some(lastStorageProj1)
      cache.getDefault(ref2).runToFuture.futureValue shouldEqual Some(lastStorageProj2)
      cache.getDefault(ProjectRef(genUUID)).runToFuture.futureValue shouldEqual None
    }

    "list storages" in {
      cache.get(ref1).runToFuture.futureValue should contain theSameElementsAs storagesProj1
      cache.get(ref2).runToFuture.futureValue should contain theSameElementsAs storagesProj2
    }

    "deprecate storage" in {
      val storage          = storagesProj1.head
      implicit val instant = time.plusSeconds(30L)
      cache.put(storage.copy(deprecated = true, rev = 2L)).runToFuture.futureValue
      cache.get(storage.ref, storage.id).runToFuture.futureValue shouldEqual None
      cache.get(ref1).runToFuture.futureValue should contain theSameElementsAs storagesProj1.filterNot(_ == storage)
    }
  }
} 
Example 29
Source File: Module.scala    From sbt-header   with Apache License 2.0 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}


class Module extends AbstractModule {

  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])
  }

} 
Example 30
Source File: ClockProviderSpec.scala    From chronoscala   with MIT License 5 votes vote down vote up
package jp.ne.opt.chronoscala

import java.time.{Clock, ZoneId}

import jp.ne.opt.chronoscala.Imports._
import org.scalatest.BeforeAndAfter
import org.scalatest.flatspec.AnyFlatSpec

class ClockProviderSpec extends AnyFlatSpec with BeforeAndAfter {

  after {
    ClockProvider.setCurrentClockSystem()
  }

  it should "set current clock" in {

    ClockProvider.setCurrentClock(Clock.fixed(Instant.ofEpochMilli(0L), ZoneId.of("UTC")))

    assert(Instant.now() == Instant.ofEpochMilli(0L))
    assert(LocalDate.now() == LocalDate.parse("1970-01-01"))
    assert(LocalDateTime.now() == LocalDateTime.parse("1970-01-01T00:00:00.000"))
    assert(LocalTime.now() == LocalTime.parse("00:00:00.000"))
    assert(ZonedDateTime.now() == ZonedDateTime.parse("1970-01-01T00:00:00.000+00:00[UTC]"))
    assert(OffsetDateTime.now() == OffsetDateTime.parse("1970-01-01T00:00:00.000+00:00"))
  }
} 
Example 31
Source File: CarbonClient.scala    From akka-http-metrics   with Apache License 2.0 5 votes vote down vote up
package fr.davit.akka.http.metrics.graphite

import java.time.{Clock, Instant}

import akka.NotUsed
import akka.actor.ActorSystem
import akka.event.Logging
import akka.stream.scaladsl.{Flow, Keep, RestartFlow, Sink, Source, Tcp}
import akka.stream.{OverflowStrategy, QueueOfferResult}
import akka.util.ByteString
import fr.davit.akka.http.metrics.core.Dimension

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

object CarbonClient {

  def apply(host: String, port: Int)(implicit system: ActorSystem): CarbonClient = new CarbonClient(host, port)
}

class CarbonClient(host: String, port: Int)(implicit system: ActorSystem) extends AutoCloseable {

  private val logger         = Logging(system.eventStream, classOf[CarbonClient])
  protected val clock: Clock = Clock.systemUTC()

  private def serialize[T](name: String, value: T, dimensions: Seq[Dimension], ts: Instant): ByteString = {
    val tags         = dimensions.map(d => d.key + "=" + d.value).toList
    val taggedMetric = (name :: tags).mkString(";")
    ByteString(s"$taggedMetric $value ${ts.getEpochSecond}\n")
  }

  // TODO read backoff from config
  private def connection: Flow[ByteString, ByteString, NotUsed] =
    RestartFlow.withBackoff(
      minBackoff = 3.seconds,
      maxBackoff = 30.seconds,
      randomFactor = 0.2, // adds 20% "noise" to vary the intervals slightly
      maxRestarts = -1 // keep retrying forever
    )(() => Tcp().outgoingConnection(host, port))

  private val queue = Source
    .queue[ByteString](19, OverflowStrategy.dropHead)
    .via(connection)
    .toMat(Sink.ignore)(Keep.left)
    .run()

  def publish[T](
      name: String,
      value: T,
      dimensions: Seq[Dimension] = Seq.empty,
      ts: Instant = Instant
        .now(clock)
  ): Unit = {
    // it's reasonable to block until the message in enqueued
    Await.result(queue.offer(serialize(name, value, dimensions, ts)), Duration.Inf) match {
      case QueueOfferResult.Enqueued    => logger.debug("Metric {} enqueued", name)
      case QueueOfferResult.Dropped     => logger.debug("Metric {} dropped", name)
      case QueueOfferResult.Failure(e)  => logger.error(e, s"Failed publishing metric $name")
      case QueueOfferResult.QueueClosed => throw new Exception("Failed publishing metric to closed carbon client")
    }
  }

  override def close(): Unit = {
    queue.complete()
    Await.result(queue.watchCompletion(), Duration.Inf)
  }
} 
Example 32
Source File: Module.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}


class Module extends AbstractModule {
  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])
  }

} 
Example 33
Source File: ApplicationTimer.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package services

import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future


@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {

  // This code is called when the application starts.
  private val start: Instant = clock.instant
  Logger.info(s"ApplicationTimer demo: Starting application at $start.")

  // When the application starts, register a stop hook with the
  // ApplicationLifecycle object. The code inside the stop hook will
  // be run when the application stops.
  appLifecycle.addStopHook { () =>
    val stop: Instant = clock.instant
    val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
    Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
    Future.successful(())
  }
} 
Example 34
Source File: Module.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}


class Module extends AbstractModule {
  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])
  }

} 
Example 35
Source File: HttpServer.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.server

import java.lang.Math._
import java.time.Clock
import java.util.concurrent.Executors._
import java.util.concurrent.ExecutorService

import bad.robot.temperature.JsonToCsv
import bad.robot.temperature.ds18b20.{SensorFile, SensorReader}
import bad.robot.temperature.rrd.Host
import bad.robot.temperature.task.TemperatureMachineThreadFactory
import fs2.Stream
import fs2.Scheduler
import fs2.StreamApp._
import cats.implicits._

import scala.concurrent.ExecutionContext
import cats.effect.IO
import org.http4s.HttpService
import org.http4s.server.blaze.BlazeBuilder
import org.http4s.server.middleware.{CORS, GZip}

object HttpServer {
  def apply(port: Int, monitored: List[Host]): HttpServer = {
    new HttpServer(port, monitored)
  }
}

class HttpServer(port: Int, monitored: List[Host]) {

  private val DefaultHttpExecutorService: ExecutorService = {
    newFixedThreadPool(max(4, Runtime.getRuntime.availableProcessors), TemperatureMachineThreadFactory("http-server"))
  }

  def asStream(temperatures: AllTemperatures, connections: Connections): Stream[IO, ExitCode] = {
    import scala.concurrent.ExecutionContext.Implicits.global // todo replace with explicit one

    for {
      scheduler <- Scheduler[IO](corePoolSize = 1)
      exitCode  <- build(temperatures, connections, scheduler).serve
    } yield exitCode
  }

  private[server] def build(temperatures: AllTemperatures, connections: Connections, scheduler: Scheduler): BlazeBuilder[IO] = {
    BlazeBuilder[IO]
      .withWebSockets(true)
      .withExecutionContext(ExecutionContext.fromExecutorService(DefaultHttpExecutorService))
      .bindHttp(port, "0.0.0.0")
      .mountService(services(scheduler, temperatures, connections), "/")
  }

  private def services(scheduler: Scheduler, temperatures: AllTemperatures, connections: Connections): HttpService[IO] = {
    GZip(
      CORS(
        TemperatureEndpoint(scheduler, SensorReader(Host.local, SensorFile.find()), temperatures, connections) <+>
          ConnectionsEndpoint(connections)(Clock.systemDefaultZone) <+>
          LogEndpoint() <+>
          ExportEndpoint(JsonFile.load, JsonToCsv.DefaultTimeFormatter) <+>
          VersionEndpoint() <+>
          StaticFiles() <+>
          StaticResources()
      )
    )
  }
} 
Example 36
Source File: ConnectionsEndpoint.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.server

import java.net.InetAddress
import java.time.Clock

import bad.robot.temperature.IpAddress._
import bad.robot.temperature._
import cats.data.NonEmptyList
import cats.effect.IO
import cats.implicits._
import org.http4s._
import org.http4s.dsl.io._

object ConnectionsEndpoint {

  private implicit val encoder = jsonEncoder[List[Connection]]

  def apply(connections: Connections, ipAddresses: => NonEmptyList[Option[InetAddress]] = currentIpAddress)(implicit clock: Clock) = HttpService[IO] {
    case GET -> Root / "connections" => {
      Ok(connections.all).map(_.putHeaders(xForwardedHost(ipAddresses)))
    }

    case GET -> Root / "connections" / "active" / "within" / LongVar(period) / "mins" => {
      Ok(connections.allWithin(period)).map(_.putHeaders(xForwardedHost(ipAddresses)))
    }
  }

  private def xForwardedHost(ipAddresses: NonEmptyList[Option[InetAddress]]): Header = {
    Header("X-Forwarded-Host", ipAddresses.map(_.fold("unknown")(_.getHostAddress)).mkString_("", ", ", ""))
  }

} 
Example 37
Source File: TemperatureEndpoint.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.server

import java.time.Clock

import bad.robot.logging.Log
import bad.robot.temperature.rrd.Host
import bad.robot.temperature.{jsonEncoder, _}
import cats.effect.IO
import io.circe._
import fs2.{Sink, _}
import org.http4s.HttpService
import org.http4s.dsl.io._
import org.http4s.headers.`X-Forwarded-For`
import org.http4s.server.websocket._
import org.http4s.websocket.WebsocketBits._

import scala.concurrent.duration._
import scala.language.postfixOps

object TemperatureEndpoint {

  private implicit val encoder = jsonEncoder[Json]
  private implicit val decoder = jsonDecoder[Measurement]

  implicit def jsonMapEncoder: Encoder[Map[Host, Measurement]] = new Encoder[Map[Host, Measurement]] {
    def apply(measurements: Map[Host, Measurement]): Json = Json.obj(
      ("measurements", Encoder[List[Measurement]].apply(measurements.values.toList))
    )
  }

  private val latestTemperatures = CurrentTemperatures(Clock.systemDefaultZone)
  
  private val sink: Sink[IO, WebSocketFrame] = _.evalMap { (ws: WebSocketFrame) => ws match {
    case Text(fromClient, _) => IO(Log.debug(s"Client sent ws data: $fromClient"))
    case frame               => IO(Log.debug(s"Unknown type sent from ws client: $frame"))
  }}
  
  
  import scala.concurrent.ExecutionContext.Implicits.global // todo replace with explicit one

  def apply(scheduler: Scheduler, sensors: TemperatureReader, allTemperatures: AllTemperatures, connections: Connections) = HttpService[IO] {

    case GET -> Root / "temperatures" / "average" => {
      Ok(encode(latestTemperatures.average))
    }

    case GET -> Root / "temperatures" / "live" / "average" => {
      val source: Stream[IO, WebSocketFrame] = scheduler.awakeEvery[IO](1 second).map { _ =>
        Text(encode(latestTemperatures.average).spaces2ps)
      }
      
      WebSocketBuilder[IO].build(source, sink)
    }

    case GET -> Root / "temperatures" => {
      Ok(encode(latestTemperatures.all))
    }

    case GET -> Root / "temperatures" / "live" => {
      val source: Stream[IO, WebSocketFrame] = scheduler.awakeEvery[IO](1 second).map { _ =>
        Text(encode(latestTemperatures.all).spaces2ps)
      }

      WebSocketBuilder[IO].build(source, sink)
    }
      
    case DELETE -> Root / "temperatures" => {
      latestTemperatures.clear()
      NoContent()
    }

    case request @ PUT -> Root / "temperature" => {
      request.decode[Measurement](measurement => {
        val result = connections.update(measurement.host, request.headers.get(`X-Forwarded-For`))

        result.toHttpResponse(_ => {
          latestTemperatures.updateWith(measurement)
          allTemperatures.put(measurement)
          NoContent()
        })
      })
    }
  }

} 
Example 38
Source File: TemperaturesTest.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.server

import java.time.{Clock, Instant, ZoneId}

import bad.robot.temperature._
import bad.robot.temperature.rrd.{Host, Seconds}
import org.specs2.mutable.Specification

class TemperaturesTest extends Specification {

  "Filter out old temperatures" >> {
    val measurement1 = Measurement(Host("lounge"), Seconds(0), List(
      SensorReading("28-00000dfg34ca", Temperature(31.1)),
      SensorReading("28-00000f33fdc3", Temperature(32.8))
    ))
    val measurement2 = Measurement(Host("bedroom"), Seconds(120), List(
      SensorReading("28-00000f3554ds", Temperature(21.1)),
      SensorReading("28-000003dd3433", Temperature(22.8))
    ))

    val temperatures = CurrentTemperatures(fixedClock(Instant.ofEpochSecond(300)))
    temperatures.updateWith(measurement1)
    temperatures.updateWith(measurement2)

    val expected = Map(
      Host("bedroom", None) -> Measurement(Host("bedroom"), Seconds(120), List(
        SensorReading("28-00000f3554ds", Temperature(21.1)),
        SensorReading("28-000003dd3433", Temperature(22.8))
      ))
    )

    temperatures.all must_== expected
  }

  "Filter out old temperatures when averaging" >> {
    val measurement1 = Measurement(Host("lounge"), Seconds(0), List(
      SensorReading("28-00000dfg34ca", Temperature(31.1)),
      SensorReading("28-00000f33fdc3", Temperature(32.8))
    ))
    val measurement2 = Measurement(Host("bedroom"), Seconds(60), List(
      SensorReading("28-00000f3554ds", Temperature(21.1)),
      SensorReading("28-000003dd3433", Temperature(22.8))
    ))

    val temperatures = CurrentTemperatures(fixedClock(Instant.ofEpochSecond(300)))
    temperatures.updateWith(measurement1)
    temperatures.updateWith(measurement2)

    val expected = Map(
      Host("bedroom", None) -> Measurement(Host("bedroom"), Seconds(60), List(
        SensorReading("Average", Temperature(21.950000000000003))
      ))
    )

    temperatures.average must_== expected
  }
  
  
  def fixedClock(instant: Instant = Instant.now) = Clock.fixed(instant, ZoneId.systemDefault())

} 
Example 39
Source File: ApplicationTimer.scala    From play-webpack-typescript-react   with MIT License 5 votes vote down vote up
package services

import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future


@Singleton
class ApplicationTimer @Inject()(clock: Clock, appLifecycle: ApplicationLifecycle) {

  // This code is called when the application starts.
  private val start: Instant = clock.instant
  Logger.info(s"ApplicationTimer demo: Starting application at $start.")

  // When the application starts, register a stop hook with the
  // ApplicationLifecycle object. The code inside the stop hook will
  // be run when the application stops.
  appLifecycle.addStopHook { () =>
    val stop: Instant     = clock.instant
    val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
    Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
    Future.successful(())
  }
} 
Example 40
Source File: Module.scala    From play-webpack-typescript-react   with MIT License 5 votes vote down vote up
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}


@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements"))
class Module extends AbstractModule {

  override def configure() = {
    // Use the system clock as the default implementation of Clock
    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    // Ask Guice to create an instance of ApplicationTimer when the
    // application starts.
    bind(classOf[ApplicationTimer]).asEagerSingleton()
    // Set AtomicCounter as the implementation for Counter.
    bind(classOf[Counter]).to(classOf[AtomicCounter])

    ()
  }

} 
Example 41
Source File: HeapDump.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.util

import java.io.File
import java.lang.management.ManagementFactory.getPlatformMXBeans
import java.time.Clock
import java.time.ZonedDateTime.now
import java.time.format.DateTimeFormatter.ISO_DATE_TIME

import com.sun.management.HotSpotDiagnosticMXBean
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Response}
import com.twitter.io.Readers
import com.twitter.util.Future
import io.fintrospect.ContentType
import io.fintrospect.formats.ResponseBuilder.HttpResponse


class HeapDump(processIdentifier: String = "", clock: Clock = Clock.systemUTC()) extends Service[Request, Response] {
  override def apply(request: Request): Future[Response] = {
    val dumpFileName = s"heapdump-$processIdentifier-${now(clock).format(ISO_DATE_TIME)}"
    val dumpFile = File.createTempFile(dumpFileName, ".hprof")
    dumpFile.delete()

    getPlatformMXBeans(classOf[HotSpotDiagnosticMXBean]).get(0).dumpHeap(dumpFile.getAbsolutePath, true)

    val response = HttpResponse(ContentType("application/x-heap-dump"))
      .withHeaders("Content-disposition" -> ("inline; filename=\"" + dumpFileName + ".hprof\""))
      .withContent(Readers.newFileReader(dumpFile, 1024)).build()

    Future(response).ensure(dumpFile.delete())
  }
} 
Example 42
Source File: ApplicationTimer.scala    From sbt-header   with Apache License 2.0 5 votes vote down vote up
package services

import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future


@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {

  // This code is called when the application starts.
  private val start: Instant = clock.instant
  Logger.info(s"ApplicationTimer demo: Starting application at $start.")

  // When the application starts, register a stop hook with the
  // ApplicationLifecycle object. The code inside the stop hook will
  // be run when the application stops.
  appLifecycle.addStopHook { () =>
    val stop: Instant = clock.instant
    val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
    Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
    Future.successful(())
  }
} 
Example 43
Source File: package.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger

import java.time.Clock
import java.util.UUID

import com.daml.lf.data.Ref
import com.daml.ledger.api.auth.{
  AuthServiceStatic,
  Authorizer,
  Claim,
  ClaimActAsParty,
  ClaimAdmin,
  ClaimPublic,
  ClaimReadAsParty,
  Claims
}

package object rxjava {

  private[rxjava] def untestedEndpoint: Nothing =
    throw new UnsupportedOperationException("Untested endpoint, implement if needed")

  private[rxjava] val authorizer =
    new Authorizer(() => Clock.systemUTC().instant(), "testLedgerId", "testParticipantId")

  private[rxjava] val emptyToken = "empty"
  private[rxjava] val publicToken = "public"
  private[rxjava] val adminToken = "admin"

  private[rxjava] val someParty = UUID.randomUUID.toString
  private[rxjava] val somePartyReadToken = UUID.randomUUID.toString
  private[rxjava] val somePartyReadWriteToken = UUID.randomUUID.toString

  private[rxjava] val someOtherParty = UUID.randomUUID.toString
  private[rxjava] val someOtherPartyReadToken = UUID.randomUUID.toString
  private[rxjava] val someOtherPartyReadWriteToken = UUID.randomUUID.toString

  private[rxjava] val mockedAuthService =
    AuthServiceStatic {
      case `emptyToken` => Claims(Nil)
      case `publicToken` => Claims(Seq[Claim](ClaimPublic))
      case `adminToken` => Claims(Seq[Claim](ClaimAdmin))
      case `somePartyReadToken` =>
        Claims(Seq[Claim](ClaimPublic, ClaimReadAsParty(Ref.Party.assertFromString(someParty))))
      case `somePartyReadWriteToken` =>
        Claims(Seq[Claim](ClaimPublic, ClaimActAsParty(Ref.Party.assertFromString(someParty))))
      case `someOtherPartyReadToken` =>
        Claims(
          Seq[Claim](ClaimPublic, ClaimReadAsParty(Ref.Party.assertFromString(someOtherParty))))
      case `someOtherPartyReadWriteToken` =>
        Claims(Seq[Claim](ClaimPublic, ClaimActAsParty(Ref.Party.assertFromString(someOtherParty))))
    }

} 
Example 44
Source File: StorageCache.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.cache

import java.time.{Clock, Instant}
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap

import akka.actor.ActorSystem
import cats.Monad
import cats.effect.{Effect, Timer}
import cats.implicits._
import ch.epfl.bluebrain.nexus.commons.cache.{KeyValueStore, KeyValueStoreConfig}
import ch.epfl.bluebrain.nexus.kg.RevisionedValue
import ch.epfl.bluebrain.nexus.kg.cache.Cache._
import ch.epfl.bluebrain.nexus.kg.cache.StorageProjectCache._
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.kg.storage.Storage
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri

class StorageCache[F[_]: Effect: Timer] private (projectToCache: ConcurrentHashMap[UUID, StorageProjectCache[F]])(
    implicit
    as: ActorSystem,
    config: KeyValueStoreConfig,
    clock: Clock
) {

  
private class StorageProjectCache[F[_]: Monad] private (store: KeyValueStore[F, AbsoluteIri, RevisionedStorage])
    extends Cache[F, AbsoluteIri, RevisionedStorage](store) {

  implicit private val ordering: Ordering[RevisionedStorage] = Ordering.by((s: RevisionedStorage) => s.rev).reverse

  implicit private def revisioned(storage: Storage)(implicit instant: Instant): RevisionedStorage =
    RevisionedValue(instant.toEpochMilli, storage)

  def get: F[List[Storage]] =
    store.values.map(_.toList.sorted.map(_.value))

  def getDefault: F[Option[Storage]]                            =
    get.map(_.collectFirst { case storage if storage.default => storage })

  def getBy(id: AbsoluteIri): F[Option[Storage]]                =
    get(id).map(_.collectFirst { case RevisionedValue(_, storage) if storage.id == id => storage })

  def put(storage: Storage)(implicit instant: Instant): F[Unit] =
    if (storage.deprecated) store.remove(storage.id)
    else store.put(storage.id, storage)
}

private object StorageProjectCache {

  type RevisionedStorage = RevisionedValue[Storage]

  def apply[F[_]: Effect: Timer](
      project: ProjectRef
  )(implicit as: ActorSystem, config: KeyValueStoreConfig): StorageProjectCache[F] =
    new StorageProjectCache(
      KeyValueStore.distributed(s"storage-${project.id}", (_, storage) => storage.value.rev)
    )

}

object StorageCache {

  def apply[F[_]: Timer: Effect](implicit as: ActorSystem, config: KeyValueStoreConfig, clock: Clock): StorageCache[F] =
    new StorageCache(new ConcurrentHashMap[UUID, StorageProjectCache[F]]())
} 
Example 45
Source File: CompositeResolutionSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.resolve

import java.time.Clock
import java.util.UUID
import java.util.regex.Pattern.quote

import cats.instances.try_._
import cats.syntax.show._
import ch.epfl.bluebrain.nexus.commons.test.Resources
import ch.epfl.bluebrain.nexus.kg.resources.Ref
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.implicits._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.{OptionValues, TryValues}

import scala.util.Try

class CompositeResolutionSpec extends AnyWordSpecLike with Resources with Matchers with TryValues with OptionValues {

  "CompositeResolution" should {

    implicit val clock = Clock.systemUTC

    val resource1Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}"
    val resource2Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}"
    val staticResolution1         = StaticResolution[Try](
      Map(
        resource1Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )
    val staticResolution2         = StaticResolution[Try](
      Map(
        resource2Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource2Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )
    val staticResolution3         = StaticResolution[Try](
      Map(
        resource1Uri -> jsonContentOf(
          "/resolve/simple-resource.json",
          Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString)
        )
      )
    )

    val compositeResolution = CompositeResolution[Try](List(staticResolution1, staticResolution2, staticResolution3))

    "return the resource from the first resolver which returns the resource" in {

      compositeResolution.resolve(Ref(resource1Uri)).success.value.value shouldEqual staticResolution1
        .resolve(Ref(resource1Uri))
        .success
        .value
        .value
      compositeResolution.resolve(Ref(resource2Uri)).success.value.value shouldEqual staticResolution2
        .resolve(Ref(resource2Uri))
        .success
        .value
        .value
    }
  }

} 
Example 46
Source File: TarFlowSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.archives

import java.nio.file.Files
import java.time.{Clock, Instant, ZoneId}

import akka.actor.ActorSystem
import akka.stream.scaladsl.FileIO
import akka.testkit.TestKit
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.storage.digestSink
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration._

class TarFlowSpec
    extends TestKit(ActorSystem("TarFlowSpec"))
    with AnyWordSpecLike
    with Matchers
    with TestHelper
    with ScalaFutures {

  implicit private val ec    = system.dispatcher
  implicit private val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault())

  implicit override def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds)

  "A TarFlow" should {

    "tar a bunch of sources" in {
      val digest   =
        "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886"
      val elems    = 500
      val contents =
        List.tabulate(2) { i =>
          val content = (i until (i + elems)).toList.mkString(",") + "\n"
          ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content))
        }
      val path     = Files.createTempFile("test", ".tar")
      TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue
      FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest
      Files.delete(path)
    }
  }
} 
Example 47
Source File: ArchiveCacheSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.archives

import java.time.{Clock, Instant, ZoneId}

import cats.effect.{IO, Timer}
import ch.epfl.bluebrain.nexus.admin.client.types.Project
import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture
import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues
import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription}
import ch.epfl.bluebrain.nexus.kg.resources.Id
import ch.epfl.bluebrain.nexus.kg.resources.syntax._
import ch.epfl.bluebrain.nexus.service.config.Settings
import org.scalatest.concurrent.Eventually
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration._

class ArchiveCacheSpec
    extends ActorSystemFixture("ArchiveCacheSpec", true)
    with TestHelper
    with AnyWordSpecLike
    with Matchers
    with IOOptionValues
    with Eventually {

  implicit override def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds)

  private val appConfig                 = Settings(system).serviceConfig
  implicit private val config           =
    appConfig.copy(kg =
      appConfig.kg.copy(archives = appConfig.kg.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100))
    )
  implicit private val timer: Timer[IO] = IO.timer(system.dispatcher)
  implicit private val archivesCfg      = config.kg.archives

  private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue
  implicit private val clock          = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault())
  private val instant                 = clock.instant()

  def randomProject() = {
    val instant = Instant.EPOCH
    // format: off
    Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri)
    // format: on
  }

  "An archive cache" should {

    "write and read an Archive" in {
      val resId     = Id(randomProject().ref, genIri)
      val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None)
      val file1     = File(genIri, randomProject(), None, None, None)
      val archive   = Archive(resId, instant, Anonymous, Set(resource1, file1))
      val _         = cache.put(archive).value.some
      cache.get(archive.resId).value.some shouldEqual archive
    }

    "read a non existing resource" in {
      val resId = Id(randomProject().ref, genIri)
      cache.get(resId).value.ioValue shouldEqual None
    }

    "read after timeout" in {
      val resId   = Id(randomProject().ref, genIri)
      val set     = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None))
      val archive = Archive(resId, instant, Anonymous, set)
      val _       = cache.put(archive).value.some
      val time    = System.currentTimeMillis()
      cache.get(resId).value.some shouldEqual archive
      eventually {
        cache.get(resId).value.ioValue shouldEqual None
      }
      val diff    = System.currentTimeMillis() - time
      diff should be > config.kg.archives.cacheInvalidateAfter.toMillis
      diff should be < config.kg.archives.cacheInvalidateAfter.toMillis + 300
    }
  }
} 
Example 48
Source File: TestHelper.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg

import java.time.Clock
import java.util.UUID

import akka.stream.Materializer
import akka.stream.scaladsl.Source
import akka.util.ByteString
import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness}
import ch.epfl.bluebrain.nexus.iam.acls.AccessControlList
import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.iam.types.{Identity, Permission, ResourceF => IamResourceF}
import ch.epfl.bluebrain.nexus.kg.config.Schemas.unconstrainedSchemaUri
import ch.epfl.bluebrain.nexus.kg.resources.ResourceF.Value
import ch.epfl.bluebrain.nexus.kg.resources.{Ref, ResId, ResourceF}
import ch.epfl.bluebrain.nexus.kg.storage.AkkaSource
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.Json

trait TestHelper extends EitherValues with Randomness {

  private val clock     = Clock.systemUTC()
  val read: Permission  = Permission.unsafe("resources/read")
  val write: Permission = Permission.unsafe("files/write")

  def consume(source: AkkaSource)(implicit mt: Materializer): String = {
    import org.scalatest.concurrent.ScalaFutures._
    source.runFold("")(_ ++ _.utf8String).futureValue
  }

  def produce(string: String, chunkSize: Int = 100): AkkaSource =
    Source(string.grouped(chunkSize).map(ByteString(_)).toList)

  def resourceAcls(acl: AccessControlList): IamResourceF[AccessControlList] =
    IamResourceF(
      url"http://example.com/id",
      1L,
      Set.empty,
      clock.instant(),
      Anonymous,
      clock.instant(),
      Anonymous,
      acl
    )

  def simpleV(
      id: ResId,
      value: Json,
      rev: Long = 1L,
      types: Set[AbsoluteIri] = Set.empty,
      deprecated: Boolean = false,
      schema: Ref = Ref(unconstrainedSchemaUri),
      created: Identity = Anonymous,
      updated: Identity = Anonymous
  )(implicit clock: Clock): ResourceF[Value] =
    ResourceF(
      id,
      rev,
      types,
      deprecated,
      Map.empty,
      None,
      clock.instant(),
      clock.instant(),
      created,
      updated,
      schema,
      Value(value, value.contextValue, value.toGraph(id.value).rightValue)
    )

  def simpleV(res: ResourceF[Json])(implicit clock: Clock) =
    ResourceF(
      res.id,
      res.rev,
      res.types,
      res.deprecated,
      Map.empty,
      None,
      clock.instant(),
      clock.instant(),
      res.createdBy,
      res.updatedBy,
      res.schema,
      Value(res.value, res.value.contextValue, res.value.toGraph(res.id.value).rightValue)
    )

  def genUUID: UUID = UUID.randomUUID()

  def genIri: AbsoluteIri = url"http://example.com/" + genUUID.toString

  private def sourceInChunks(input: String): AkkaSource =
    Source.fromIterator(() => input.grouped(10000).map(ByteString(_)))

  def genSource: AkkaSource = sourceInChunks(genString())

} 
Example 49
Source File: SparqlLinkSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.indexing

import java.time.{Clock, Instant, ZoneId}

import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding
import ch.epfl.bluebrain.nexus.kg.config.Schemas._
import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink}
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.Vocabulary._
import ch.epfl.bluebrain.nexus.rdf.implicits._
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.OptionValues
import org.scalatest.matchers.should.Matchers
import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv

class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues {

  "A SparqlLink" should {

    val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())

    val id        = url"http://example.com/id"
    val property  = url"http://example.com/friend"
    val property2 = url"http://example.com/friend2"
    val paths     = List(property, property2)

    "build SparqlExternalLink from SPARQL response" in {
      val bindings = Map(
        "s"     -> Binding("uri", id.asString),
        "paths" -> Binding("literal", s"${property.asString} ${property2.asString}")
      )
      SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths)
    }

    "build SparqlResourceLink from SPARQL response" in {
      val self     = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id"
      val project  = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/"
      val author   = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me"
      val bindings = Map(
        "s"              -> Binding("uri", id.asString),
        "paths"          -> Binding("literal", s"${property.asString} ${property2.asString}"),
        "_rev"           -> Binding("literal", "1", datatype = Some(xsd.long.asString)),
        "_self"          -> Binding("uri", self.asString),
        "_project"       -> Binding("uri", project.asString),
        "types"          -> Binding("literal", s"${nxv.Resolver.value.asString} ${nxv.Schema.value.asString}"),
        "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString),
        "_createdBy"     -> Binding("uri", author.asString),
        "_updatedBy"     -> Binding("uri", author.asString),
        "_createdAy"     -> Binding("uri", author.asString),
        "_createdAt"     -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)),
        "_updatedAt"     -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)),
        "_deprecated"    -> Binding("literal", "false", datatype = Some(xsd.boolean.asString))
      )
      SparqlResourceLink(bindings).value shouldEqual
        SparqlResourceLink(
          id,
          project,
          self,
          1L,
          Set[AbsoluteIri](nxv.Schema.value, nxv.Resolver.value),
          false,
          clock.instant(),
          clock.instant(),
          author,
          author,
          unconstrainedRef,
          paths
        )
    }
  }

} 
Example 50
Source File: TaggingAdapterSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.persistence

import java.time.{Clock, Instant, ZoneId}

import akka.persistence.journal.Tagged
import cats.syntax.show._
import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.config.Schemas._
import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other
import ch.epfl.bluebrain.nexus.kg.resources.Event._
import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref}
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv
import io.circe.Json
import org.scalatest.Inspectors
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper {

  "A TaggingAdapter" should {
    val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())

    def genJson(): Json = Json.obj("key" -> Json.fromString(genString()))

    val adapter = new TaggingAdapter()
    val orgRef  = OrganizationRef(genUUID)
    val id      = Id(ProjectRef(genUUID), nxv.projects.value)

    val mapping = Map(
      Set(
        s"type=${nxv.Schema.value.show}",
        s"type=${nxv.Resource.value.show}",
        s"project=${id.parent.id}",
        s"org=${orgRef.show}",
        "event"
      )                                                                                                   ->
        Created(
          id,
          orgRef,
          Ref(shaclSchemaUri),
          Set(nxv.Schema.value, nxv.Resource.value),
          genJson(),
          clock.instant(),
          Anonymous
        ),
      Set(
        s"type=${nxv.Resolver.value.show}",
        s"type=${nxv.Resource.value.show}",
        s"project=${id.parent.id}",
        s"org=${orgRef.show}",
        "event"
      )                                                                                                   ->
        Updated(id, orgRef, 1L, Set(nxv.Resource.value, nxv.Resolver.value), genJson(), clock.instant(), Anonymous),
      Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") ->
        Deprecated(id, orgRef, 1L, Set(nxv.Resource.value), clock.instant(), Anonymous),
      Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event")                                     ->
        TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous)
    )

    "set the appropriate tags" in {
      forAll(mapping.toList) {
        case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags)
      }
    }

    "return an empty manifest" in {
      adapter.manifest(Other(genString())) shouldEqual ""
    }
  }
}

object TaggingAdapterSpec {
  final private[persistence] case class Other(value: String)

} 
Example 51
Source File: StorageCacheSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.cache

import java.nio.file.Paths
import java.time.Clock

import akka.testkit._
import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.kg.storage.Storage.DiskStorage
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.service.config.{ServiceConfig, Settings}
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.{Inspectors, TryValues}

import scala.concurrent.duration._

//noinspection NameBooleanParameters
class StorageCacheSpec
    extends ActorSystemFixture("StorageCacheSpec", true)
    with Matchers
    with Inspectors
    with ScalaFutures
    with TryValues
    with TestHelper {

  implicit override def patienceConfig: PatienceConfig = PatienceConfig(3.seconds.dilated, 5.milliseconds)

  implicit private val clock: Clock             = Clock.systemUTC
  implicit private val appConfig: ServiceConfig = Settings(system).serviceConfig
  implicit private val keyValueStoreCfg         = appConfig.kg.keyValueStore.keyValueStoreConfig

  val ref1 = ProjectRef(genUUID)
  val ref2 = ProjectRef(genUUID)

  val time   = clock.instant()
  val lastId = url"http://example.com/lastA"
  // initialInstant.minusSeconds(1L + genInt().toLong)

  val tempStorage = DiskStorage(ref1, genIri, 1L, false, true, "alg", Paths.get("/tmp"), read, write, 1024L)

  val lastStorageProj1 = tempStorage.copy(id = lastId)
  val lastStorageProj2 = tempStorage.copy(ref = ref2, id = lastId)

  val storagesProj1: List[DiskStorage] = List.fill(5)(tempStorage.copy(id = genIri)) :+ lastStorageProj1
  val storagesProj2: List[DiskStorage] = List.fill(5)(tempStorage.copy(ref = ref2, id = genIri)) :+ lastStorageProj2

  private val cache = StorageCache[Task]

  "StorageCache" should {

    "index storages" in {
      forAll((storagesProj1 ++ storagesProj2).zipWithIndex) {
        case (storage, index) =>
          implicit val instant = time.plusSeconds(index.toLong)
          cache.put(storage).runToFuture.futureValue
          cache.get(storage.ref, storage.id).runToFuture.futureValue shouldEqual Some(storage)
      }
    }

    "get latest default storage" in {
      cache.getDefault(ref1).runToFuture.futureValue shouldEqual Some(lastStorageProj1)
      cache.getDefault(ref2).runToFuture.futureValue shouldEqual Some(lastStorageProj2)
      cache.getDefault(ProjectRef(genUUID)).runToFuture.futureValue shouldEqual None
    }

    "list storages" in {
      cache.get(ref1).runToFuture.futureValue should contain theSameElementsAs storagesProj1
      cache.get(ref2).runToFuture.futureValue should contain theSameElementsAs storagesProj2
    }

    "deprecate storage" in {
      val storage          = storagesProj1.head
      implicit val instant = time.plusSeconds(30L)
      cache.put(storage.copy(deprecated = true, rev = 2L)).runToFuture.futureValue
      cache.get(storage.ref, storage.id).runToFuture.futureValue shouldEqual None
      cache.get(ref1).runToFuture.futureValue should contain theSameElementsAs storagesProj1.filterNot(_ == storage)
    }
  }
} 
Example 52
Source File: ResourceFSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.types

import java.time.{Clock, Instant, ZoneId}

import ch.epfl.bluebrain.nexus.util.{EitherValues, Resources}
import ch.epfl.bluebrain.nexus.iam.testsyntax._
import ch.epfl.bluebrain.nexus.iam.types.Identity.User
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig
import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv
import io.circe.Printer
import io.circe.syntax._
import org.scalatest.Inspectors
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

//noinspection TypeAnnotation
class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources {

  "A ResourceMetadata" should {
    val user          = User("mysubject", "myrealm")
    val user2         = User("mysubject2", "myrealm")
    implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com")
    val clock: Clock  = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault())
    val instant       = clock.instant()
    val id            = url"http://example.com/id"
    val printer       = Printer.spaces2.copy(dropNullValues = true)

    "be converted to Json correctly" when {
      "using multiple types" in {
        val json  = jsonContentOf("/resources/write-response.json")
        val model =
          ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value, nxv.Realm.value), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
      "using a single type" in {
        val json  = jsonContentOf("/resources/write-response-singletype.json")
        val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
      "using no types" in {
        val json  = jsonContentOf("/resources/write-response-notypes.json")
        val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2)
        model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer)
      }
    }
  }
} 
Example 53
Source File: Main.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.storage

import java.nio.file.Paths
import java.time.Clock

import akka.actor.ActorSystem
import akka.event.{Logging, LoggingAdapter}
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import akka.util.Timeout
import cats.effect.Effect
import ch.epfl.bluebrain.nexus.storage.Storages.DiskStorage
import ch.epfl.bluebrain.nexus.storage.attributes.AttributesCache
import ch.epfl.bluebrain.nexus.storage.config.{AppConfig, Settings}
import ch.epfl.bluebrain.nexus.storage.config.AppConfig._
import ch.epfl.bluebrain.nexus.storage.routes.Routes
import com.typesafe.config.{Config, ConfigFactory}
import kamon.Kamon
import monix.eval.Task
import monix.execution.Scheduler

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success}

//noinspection TypeAnnotation
// $COVERAGE-OFF$
object Main {

  def loadConfig(): Config = {
    val cfg = sys.env.get("STORAGE_CONFIG_FILE") orElse sys.props.get("storage.config.file") map { str =>
      val file = Paths.get(str).toAbsolutePath.toFile
      ConfigFactory.parseFile(file)
    } getOrElse ConfigFactory.empty()
    (cfg withFallback ConfigFactory.load()).resolve()
  }

  def setupMonitoring(config: Config): Unit = {
    if (sys.env.getOrElse("KAMON_ENABLED", "false").toBoolean) {
      Kamon.reconfigure(config)
      Kamon.loadModules()
    }
  }

  def shutdownMonitoring(): Unit = {
    if (sys.env.getOrElse("KAMON_ENABLED", "false").toBoolean) {
      Await.result(Kamon.stopModules(), 10.seconds)
    }
  }

  @SuppressWarnings(Array("UnusedMethodParameter"))
  def main(args: Array[String]): Unit = {
    val config = loadConfig()
    setupMonitoring(config)

    implicit val appConfig: AppConfig = Settings(config).appConfig

    implicit val as: ActorSystem                          = ActorSystem(appConfig.description.fullName, config)
    implicit val ec: ExecutionContext                     = as.dispatcher
    implicit val eff: Effect[Task]                        = Task.catsEffect(Scheduler.global)
    implicit val iamIdentities: IamIdentitiesClient[Task] = new IamIdentitiesClient[Task](appConfig.iam)
    implicit val timeout                                  = Timeout(1.minute)
    implicit val clock                                    = Clock.systemUTC

    val storages: Storages[Task, AkkaSource] =
      new DiskStorage(appConfig.storage, appConfig.digest, AttributesCache[Task, AkkaSource])

    val logger: LoggingAdapter = Logging(as, getClass)

    logger.info("==== Cluster is Live ====")
    val routes: Route = Routes(storages)

    val httpBinding: Future[Http.ServerBinding] = {
      Http().bindAndHandle(routes, appConfig.http.interface, appConfig.http.port)
    }
    httpBinding onComplete {
      case Success(binding) =>
        logger.info(s"Bound to ${binding.localAddress.getHostString}: ${binding.localAddress.getPort}")
      case Failure(th)      =>
        logger.error(th, "Failed to perform an http binding on {}:{}", appConfig.http.interface, appConfig.http.port)
        Await.result(as.terminate(), 10.seconds)
    }

    as.registerOnTermination {
      shutdownMonitoring()
    }
    // attempt to leave the cluster before shutting down
    val _ = sys.addShutdownHook {
      Await.result(as.terminate().map(_ => ()), 10.seconds)
    }
  }
}
// $COVERAGE-ON$ 
Example 54
Source File: AttributesCache.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.storage.attributes

import java.nio.file.Path
import java.time.Clock

import akka.actor.{ActorRef, ActorSystem}
import akka.pattern.{ask, AskTimeoutException}
import akka.util.Timeout
import cats.effect.{ContextShift, Effect, IO}
import cats.implicits._
import ch.epfl.bluebrain.nexus.storage.File.FileAttributes
import ch.epfl.bluebrain.nexus.storage.StorageError.{InternalError, OperationTimedOut}
import ch.epfl.bluebrain.nexus.storage.attributes.AttributesCacheActor.Protocol._
import ch.epfl.bluebrain.nexus.storage.config.AppConfig.DigestConfig
import com.typesafe.scalalogging.Logger

import scala.util.control.NonFatal

trait AttributesCache[F[_]] {

  
  def asyncComputePut(filePath: Path, algorithm: String): Unit
}

object AttributesCache {
  private[this] val logger = Logger[this.type]

  def apply[F[_], Source](implicit
      system: ActorSystem,
      clock: Clock,
      tm: Timeout,
      F: Effect[F],
      computation: AttributesComputation[F, Source],
      config: DigestConfig
  ): AttributesCache[F] =
    apply(system.actorOf(AttributesCacheActor.props(computation)))

  private[attributes] def apply[F[_]](
      underlying: ActorRef
  )(implicit system: ActorSystem, tm: Timeout, F: Effect[F]): AttributesCache[F] =
    new AttributesCache[F] {
      implicit private val contextShift: ContextShift[IO] = IO.contextShift(system.dispatcher)

      override def get(filePath: Path): F[FileAttributes] =
        IO.fromFuture(IO.shift(system.dispatcher) >> IO(underlying ? Get(filePath)))
          .to[F]
          .flatMap[FileAttributes] {
            case attributes: FileAttributes => F.pure(attributes)
            case other                      =>
              logger.error(s"Received unexpected reply from the file attributes cache: '$other'")
              F.raiseError(InternalError("Unexpected reply from the file attributes cache"))
          }
          .recoverWith {
            case _: AskTimeoutException =>
              F.raiseError(OperationTimedOut("reply from the file attributes cache timed out"))
            case NonFatal(th)           =>
              logger.error("Exception caught while exchanging messages with the file attributes cache", th)
              F.raiseError(InternalError("Exception caught while exchanging messages with the file attributes cache"))
          }

      override def asyncComputePut(filePath: Path, algorithm: String): Unit =
        underlying ! Compute(filePath)

    }
} 
Example 55
Source File: ApplicationTimer.scala    From Full-Stack-Scala-Starter   with Apache License 2.0 5 votes vote down vote up
package services

import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future


@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {

  // This code is called when the application starts.
  private val start: Instant = clock.instant
  Logger.info(s"ApplicationTimer demo: Starting application at $start.")

  // When the application starts, register a stop hook with the
  // ApplicationLifecycle object. The code inside the stop hook will
  // be run when the application stops.
  appLifecycle.addStopHook { () =>
    val stop: Instant = clock.instant
    val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
    Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
    Future.successful(())
  }
}