Skip to content

Instantly share code, notes, and snippets.

View stanislav-chetvertkov's full-sized avatar

Stanislav Chetvertkov stanislav-chetvertkov

View GitHub Profile
// Test Dependencies
// val akkaVersion = "2.4.0"
// val akkaStreamsVersion = "1.0"
// "org.scalatest" %% "scalatest" % "2.2.5" % "test",
// "com.typesafe.akka" %% "akka-http-testkit-experimental" % akkaStreamsVersion % "test",
// "com.typesafe.akka" %% "akka-stream-testkit-experimental" % akkaStreamsVersion % "test",
// "com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test",
class FileScannerActorSpec extends TestKit(ActorSystem("spec-name"))
with DefaultTimeout with ImplicitSender
// Problem: type erasure
def complete[T :Marshaller](status: StatusCode, obj: T): Unit
def complete(future: Future[HttpResponse]): Int
def complete(future: Future[StatusCode]): Int
// Usage of CompletionMagnet
def complete(magnet: CompletionMagnet): magnet.Result = magnet()
sealed trait CompletionMagnet {
type Result
<scalastyle>
<name>Scalastyle standard configuration</name>
<check level="warning" class="org.scalastyle.file.FileTabChecker" enabled="true"/>
<check level="warning" class="org.scalastyle.file.FileLengthChecker" enabled="true">
<parameters>
<parameter name="maxFileLength"><![CDATA[800]]></parameter>
</parameters>
</check>
<check level="warning" class="org.scalastyle.file.HeaderMatchesChecker" enabled="false">
<parameters>
@stanislav-chetvertkov
stanislav-chetvertkov / gist:9f6d44c25edeb7b4b46b
Created February 17, 2016 16:47
Try with resources in scala
import scala.util.{Failure, Success, Try}
class Loan[A <: AutoCloseable](resource: A) {
def to[B](block: A => B): B = {
Try(block(resource)) match {
case Success(result) =>
resource.close()
result
case Failure(e) =>
resource.close()
//Requires scalaz core and shapeless
import akka.actor.Actor.Receive
import akka.actor._
import shapeless.{Coproduct, :+:, CNil}
import scalaz.{Tag, @@}
object Test extends App {
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{Uri, HttpRequest, HttpResponse}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Sink, Source}
import scala.concurrent.Future
/**
* Mock Akka HTTP Server, will handle requests which is provided with handle request
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Sink, Source}
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
def splitErrorsFlow[L, R, M, M1, M2](
routesSource: Source[Either[L, R], M],
leftSink: Sink[L, M1],
rightSink: Sink[R, M2]
)(implicit ec: ExecutionContext): RunnableGraph[(M, M1, M2)] =
RunnableGraph.fromGraph(GraphDSL.create(routesSource, leftSink, rightSink)((_, _, _)) { implicit b => (s, l, r) =>
import GraphDSL.Implicits._
val broadcast = b.add(Broadcast[Either[L, R]](2))
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Partition, RunnableGraph, Sink, Source}
import akka.stream.{ActorMaterializer, ClosedShape, OverflowStrategy}
object GraphApp extends App {
implicit val actorSystem: ActorSystem = ActorSystem(name = "prog-voice")
implicit val mt: ActorMaterializer = ActorMaterializer()
val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder ⇒