Skip to content

Instantly share code, notes, and snippets.

Krzysiek Ciesielski kciesielski

  • SoftwareMill
  • Lublin, Poland
View GitHub Profile
View TimeSpec.scala
it should "should succeed when it finishes shortly before the timeout" in {
// given
val slowService = new SlowService(responseTimes = List(2800.millis))
val service = new TimeoutService(slowService) // 3s
// when
val result = service.run().unsafeToFuture()
ec.tick(TimeoutService.DefaultTimeout) // 3s forward in time!
// then
@kciesielski
kciesielski / CassandraJornalContextStream.scala
Last active Sep 23, 2019
How to stream from Cassandra and pass the context around using FlowWithContext
View CassandraJornalContextStream.scala
case class MsgMetadata(offset: query.Offset, persistenceId: String, seqNum: Long)
trait EventsJournalOffsetDao {
def offsetFor(projection: ProjectionId): Future[Option[query.Offset]]
def saveOffset(projection: ProjectionId, currentOffset: query.Offset): Future[Unit]
}
class EventsStreamFactory(
config: EventsStreamConfig,
val projectionId: ProjectionId,
View Semiauto.scala
import io.circe.semiauto._
import io.circe.syntax._
final case class Address(street: String, zipCode: String, city: String)
object Address {
implicit val addressEcncoder: Encoder[Address] = deriveEncoder[Address]
implicit val addressDecoder: Decoder[Address] = deriveDecoder[Address]
}
View AroundDirectives.scala
object AroundDirectives {
val timeoutResponse = HttpResponse(StatusCodes.NetworkReadTimeout, entity = "Unable to serve response within time limit.")
def aroundRequest(onRequest: HttpRequest => Try[RouteResult] => Unit)(implicit ec: ExecutionContext): Directive0 = {
extractRequestContext.flatMap { ctx =>
{
val onDone = onRequest(ctx.request)
mapInnerRoute { inner =>
withRequestTimeoutResponse(
View KafkaPlayground.scala
package akka.kafka.scaladsl
import java.util.concurrent.TimeUnit
import java.util.{Properties, UUID}
import akka.actor.ActorSystem
import akka.kafka.{ConsumerSettings, ProducerSettings}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.testkit.TestKit
View KafkaPauses.java
package com.softwaremill.react.kafka;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Arrays;
import java.util.Properties;
import java.util.UUID;
@kciesielski
kciesielski / freemonads.scala
Last active Apr 12, 2019
Free Monads example
View freemonads.scala
package com.softwaremill.freemonads
import cats.free.Free
import cats.~>
import cats._, cats.std.all._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
sealed trait External[A]
case class Tickets(count: Int) extends AnyVal
@kciesielski
kciesielski / aggregate2.scala
Created Sep 25, 2015
Functional aggregate example with Kleislis replaced by simple monads + flatMaps
View aggregate2.scala
object aggregate {
type ValidationStatus[S] = \/[String, S]
type ProcessingStatus[S] = \/[String, S]
type ReaderTStatus[A, S] = ReaderT[ValidationStatus, A, S]
object ReaderTStatus extends KleisliInstances with KleisliFunctions {
def apply[A, S](f: A => ValidationStatus[S]): ReaderTStatus[A, S] = kleisli(f)
}
View gist:5347375
package com.mtgox.api;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Mac;
View FunctionalDecorator.scala
object Rebates {
type RebatePolicy = (Product, Int, Money) => Money
}
object standardRebate extends ((Double, Int) => RebatePolicy) {
def apply(rebate: Double, minimalQuantity: Int) = {
(product, quantity, regularCost) => {
val rebateRatio = BigDecimal(rebate / 100)
if (quantity >= minimalQuantity)
You can’t perform that action at this time.