com.twitter.util.Await Scala Examples

The following examples show how to use com.twitter.util.Await. 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: CirceSpec.scala    From featherbed   with Apache License 2.0 6 votes vote down vote up
package featherbed.circe

import cats.implicits._
import com.twitter.util.Future
import io.circe._
import io.circe.generic.auto._
import io.circe.parser.parse
import io.circe.syntax._
import org.scalatest.FlatSpec
import shapeless.{Coproduct, Witness}
import shapeless.union.Union

case class Foo(someText: String, someInt: Int)

class CirceSpec extends FlatSpec {

  "post request of a case class" should "derive JSON encoder" in {

    import com.twitter.util.{Future, Await}
    import com.twitter.finagle.{Service, Http}
    import com.twitter.finagle.http.{Request, Response}
    import java.net.InetSocketAddress

    val server = Http.serve(new InetSocketAddress(8766), new Service[Request, Response] {
      def apply(request: Request): Future[Response] = Future {
        val rep = Response()
        rep.contentString = s"${request.contentString}"
        rep.setContentTypeJson()
        rep
      }
    })

    import java.net.URL
    val client = new featherbed.Client(new URL("http://localhost:8766/api/"))

    import io.circe.generic.auto._

    val req = client.post("foo/bar")
      .withContent(Foo("Hello world!", 42), "application/json")
        .accept[Coproduct.`"application/json"`.T]

    val result = Await.result {
       req.send[Foo]()
    }

    Foo("test", 42).asJson.toString

    parse("""{"someText": "test", "someInt": 42}""").toValidated.map(_.as[Foo])

    Await.result(server.close())

  }

  "API example" should "compile" in {
    import shapeless.Coproduct
    import java.net.URL
    import com.twitter.util.Await
    case class Post(userId: Int, id: Int, title: String, body: String)

    case class Comment(postId: Int, id: Int, name: String, email: String, body: String)
    class JSONPlaceholderAPI(baseUrl: URL) {

      private val client = new featherbed.Client(baseUrl)
      type JSON = Coproduct.`"application/json"`.T

      object posts {

        private val listRequest = client.get("posts").accept[JSON]
        private val getRequest = (id: Int) => client.get(s"posts/$id").accept[JSON]

        def list(): Future[Seq[Post]] = listRequest.send[Seq[Post]]()
        def get(id: Int): Future[Post] = getRequest(id).send[Post]()
      }

      object comments {
        private val listRequest = client.get("comments").accept[JSON]
        private val getRequest = (id: Int) => client.get(s"comments/$id").accept[JSON]

        def list(): Future[Seq[Comment]] = listRequest.send[Seq[Comment]]()
        def get(id: Int): Future[Comment] = getRequest(id).send[Comment]()
      }
    }

    val apiClient = new JSONPlaceholderAPI(new URL("http://jsonplaceholder.typicode.com/"))

    Await.result(apiClient.posts.list())
  }

} 
Example 2
Source File: Greetings.scala    From finch-101   with Apache License 2.0 5 votes vote down vote up
package i.f.workshop.finch

import com.twitter.finagle.Http
import com.twitter.util.Await

import io.finch._
import io.finch.circe._
import io.circe.generic.auto._

object Greetings extends App {

  // GET /hi/:name
  val hi: Endpoint[String] =
    get("hi" / string) { name: String =>
      Ok(s"Hi, $name!")
    }

  // GET /hello/:name?title=Mr.
  val title: RequestReader[String] = paramOption("title").withDefault("")
  val hello: Endpoint[String] =
    get("hello" / string ? title) { (name: String, title: String) =>
      Ok(s"Hello, $title$name!")
    }

  // GET /salute?name=Bob&title=Mr.
  case class Who(name: String, title: String)
  val who: RequestReader[Who] = (param("name") :: title).as[Who]
  val salute: Endpoint[String] =
    get("salute" ? who) { w: Who =>
      Ok(s"Salute, ${w.title}${w.name}!")
    }

  Await.ready(Http.serve(":8081", (hi :+: hello :+: salute).toService))
} 
Example 3
Source File: HeapDumpTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.util

import java.time.Instant

import com.twitter.finagle.http.{Request, Status}
import com.twitter.util.Await
import org.scalatest.{FunSpec, Matchers}

class HeapDumpTest extends FunSpec with Matchers {

  describe("HeapDump") {
    it("creates the correct heapdump file") {
      val clock = TestClocks.fixed(Instant.ofEpochMilli(0))
      val response = Await.result(new HeapDump("bob", clock).apply(Request()))
      response.status shouldBe Status.Ok
      response.headerMap("Content-disposition").startsWith("inline; filename=\"heapdump-bob-1970-01-01") shouldBe true
      response.contentType shouldBe Some("application/x-heap-dump;charset=utf-8")
    }
  }
} 
Example 4
Source File: HelloWorld.scala    From finch-101   with Apache License 2.0 5 votes vote down vote up
package i.f.workshop.finagle

import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{ListeningServer, Http, Service}
import com.twitter.io.Buf
import com.twitter.util.{Await, Future}

object HelloWorld extends App {

  val service: Service[Request, Response] = new Service[Request, Response] {
    def apply(req: Request): Future[Response] = {
      val rep: Response = Response()
      rep.content = Buf.Utf8("Hello, World!")

      Future.value(rep)
    }
  }

  val server: ListeningServer = Http.server.serve(":8081", service)
  Await.ready(server)
} 
Example 5
Source File: ProductFinagleMysqlSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.mysql

import io.getquill.context.sql.ProductSpec
import com.twitter.util.Await
import com.twitter.util.Future
import io.getquill.context.sql.Id

class ProductFinagleMysqlSpec extends ProductSpec {

  val context = testContext
  import testContext._

  def await[T](r: Future[T]) = Await.result(r)

  override def beforeAll = {
    await(testContext.run(quote(query[Product].delete)))
    ()
  }

  "Product" - {
    "Insert multiple products" in {
      val inserted = await(Future.collect(productEntries.map(product => testContext.run(productInsert(lift(product))))))
      val product = await(testContext.run(productById(lift(inserted(2))))).head
      product.description mustEqual productEntries(2).description
      product.id mustEqual inserted(2)
    }
    "Single insert product" in {
      val inserted = await(testContext.run(productSingleInsert))
      val product = await(testContext.run(productById(lift(inserted)))).head
      product.description mustEqual "Window"
      product.id mustEqual inserted
    }

    "Single insert with inlined free variable" in {
      val prd = Product(0L, "test1", 1L)
      val inserted = await {
        testContext.run {
          product.insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returningGenerated(_.id)
        }
      }
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test1"
      returnedProduct.sku mustEqual 1L
      returnedProduct.id mustEqual inserted
    }

    "Single insert with free variable and explicit quotation" in {
      val prd = Product(0L, "test2", 2L)
      val q1 = quote {
        product.insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returningGenerated(_.id)
      }
      val inserted = await(testContext.run(q1))
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test2"
      returnedProduct.sku mustEqual 2L
      returnedProduct.id mustEqual inserted
    }

    "Single product insert with a method quotation" in {
      val prd = Product(0L, "test3", 3L)
      val inserted = await(testContext.run(productInsert(lift(prd))))
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test3"
      returnedProduct.sku mustEqual 3L
      returnedProduct.id mustEqual inserted
    }

    "Single insert with value class" in {
      case class Product(id: Id, description: String, sku: Long)
      val prd = Product(Id(0L), "test2", 2L)
      val q1 = quote {
        query[Product].insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returningGenerated(_.id)
      }
      await(testContext.run(q1)) mustBe a[Id]
    }

    "supports casts from string to number" - {
      "toInt" in {
        case class Product(id: Long, description: String, sku: Int)
        val queried = await {
          testContext.run {
            query[Product].filter(_.sku == lift("1004").toInt)
          }
        }.head
        queried.sku mustEqual 1004L
      }
      "toLong" in {
        val queried = await {
          testContext.run {
            query[Product].filter(_.sku == lift("1004").toLong)
          }
        }.head
        queried.sku mustEqual 1004L
      }
    }
  }
} 
Example 6
Source File: DepartmentsFinagleMysqlSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.mysql

import com.twitter.util.Await
import com.twitter.util.Future

import io.getquill.context.sql.DepartmentsSpec

class DepartmentsFinagleMysqlSpec extends DepartmentsSpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Department].delete)
          _ <- testContext.run(query[Employee].delete)
          _ <- testContext.run(query[Task].delete)

          _ <- testContext.run(liftQuery(departmentEntries).foreach(e => departmentInsert(e)))
          _ <- testContext.run(liftQuery(employeeEntries).foreach(e => employeeInsert(e)))
          _ <- testContext.run(liftQuery(taskEntries).foreach(e => taskInsert(e)))
        } yield {}
      }
    }

  "Example 8 - nested naive" in {
    await(testContext.run(`Example 8 expertise naive`(lift(`Example 8 param`)))) mustEqual `Example 8 expected result`
  }

  "Example 9 - nested db" in {
    await(testContext.run(`Example 9 expertise`(lift(`Example 9 param`)))) mustEqual `Example 9 expected result`
  }

  "Example 9 - streamed result" in {
    await(testContext.stream(`Example 9 expertise`(lift(`Example 9 param`))).flatMap(_.toSeq())) mustEqual `Example 9 expected result`
  }
} 
Example 7
Source File: OnConflictFinagleSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.mysql

import com.twitter.util.{ Await, Future }
import io.getquill.context.sql.OnConflictSpec

class OnConflictFinagleSpec extends OnConflictSpec {
  val ctx = testContext
  import ctx._

  def await[T](future: Future[T]) = Await.result(future)

  override protected def beforeAll(): Unit = {
    await(ctx.run(qr1.delete))
    ()
  }

  "INSERT IGNORE" in {
    import `onConflictIgnore`._
    await(ctx.run(testQuery1)) mustEqual res1
    await(ctx.run(testQuery2)) mustEqual res2
    await(ctx.run(testQuery3)) mustEqual res3
  }

  "ON DUPLICATE KEY UPDATE i=i " in {
    import `onConflictIgnore(_.i)`._
    await(ctx.run(testQuery1)) mustEqual res1
    await(ctx.run(testQuery2)) mustEqual res2 + 1
    await(ctx.run(testQuery3)) mustEqual res3
  }

  "ON DUPLICATE KEY UPDATE ..." in {
    import `onConflictUpdate((t, e) => ...)`._
    await(ctx.run(testQuery(e1))) mustEqual res1
    await(ctx.run(testQuery(e2))) mustEqual res2 + 1
    await(ctx.run(testQuery(e3))) mustEqual res3 + 1
    await(ctx.run(testQuery4)) mustEqual res4
  }
} 
Example 8
Source File: CaseClassQueryFinagleSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.mysql

import com.twitter.util.{ Await, Future }
import io.getquill.context.sql.CaseClassQuerySpec
import org.scalatest.matchers.should.Matchers._

class CaseClassQueryFinagleSpec extends CaseClassQuerySpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Contact].delete)
          _ <- testContext.run(query[Address].delete)
          _ <- testContext.run(liftQuery(peopleEntries).foreach(e => peopleInsert(e)))
          _ <- testContext.run(liftQuery(addressEntries).foreach(e => addressInsert(e)))
        } yield {}
      }
    }

  "Example 1 - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1 CaseClass Record Output`)) should contain theSameElementsAs `Ex 1 CaseClass Record Output expected result`
  }
  "Example 1A - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1A CaseClass Record Output`)) should contain theSameElementsAs `Ex 1 CaseClass Record Output expected result`
  }
  "Example 1B - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1B CaseClass Record Output`)) should contain theSameElementsAs `Ex 1 CaseClass Record Output expected result`
  }

  "Example 2 - Single Record Mapped Join" in {
    await(testContext.run(`Ex 2 Single-Record Join`)) should contain theSameElementsAs `Ex 2 Single-Record Join expected result`
  }

  "Example 3 - Inline Record as Filter" in {
    await(testContext.run(`Ex 3 Inline Record Usage`)) should contain theSameElementsAs `Ex 3 Inline Record Usage exepected result`
  }
} 
Example 9
Source File: PeopleFinagleMysqlSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.mysql

import com.twitter.util.Await
import com.twitter.util.Future

import io.getquill.context.sql.PeopleSpec

class PeopleFinagleMysqlSpec extends PeopleSpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Couple].delete)
          _ <- testContext.run(query[Person].filter(_.age > 0).delete)
          _ <- testContext.run(liftQuery(peopleEntries).foreach(e => peopleInsert(e)))
          _ <- testContext.run(liftQuery(couplesEntries).foreach(e => couplesInsert(e)))
        } yield {}
      }
    }

  "Example 1 - differences" in {
    await(testContext.run(`Ex 1 differences`)) mustEqual `Ex 1 expected result`
  }

  "Example 2 - range simple" in {
    await(testContext.run(`Ex 2 rangeSimple`(lift(`Ex 2 param 1`), lift(`Ex 2 param 2`)))) mustEqual `Ex 2 expected result`
  }

  "Examples 3 - satisfies" in {
    await(testContext.run(`Ex 3 satisfies`)) mustEqual `Ex 3 expected result`
  }

  "Examples 4 - satisfies" in {
    await(testContext.run(`Ex 4 satisfies`)) mustEqual `Ex 4 expected result`
  }

  "Example 5 - compose" in {
    await(testContext.run(`Ex 5 compose`(lift(`Ex 5 param 1`), lift(`Ex 5 param 2`)))) mustEqual `Ex 5 expected result`
  }

  "Example 6 - predicate 0" in {
    await(testContext.run(satisfies(eval(`Ex 6 predicate`)))) mustEqual `Ex 6 expected result`
  }

  "Example 7 - predicate 1" in {
    await(testContext.run(satisfies(eval(`Ex 7 predicate`)))) mustEqual `Ex 7 expected result`
  }

  "Example 8 - contains empty" in {
    await(testContext.run(`Ex 8 and 9 contains`(liftQuery(`Ex 8 param`)))) mustEqual `Ex 8 expected result`
  }

  "Example 9 - contains non empty" in {
    await(testContext.run(`Ex 8 and 9 contains`(liftQuery(`Ex 9 param`)))) mustEqual `Ex 9 expected result`
  }
} 
Example 10
Source File: TransactionSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import com.twitter.util.{ Await, Future, Throw }

import io.getquill.context.sql.ProductSpec

class TransactionSpec extends ProductSpec {
  val context = testContext
  import context._

  def await[T](future: Future[T]) = Await.result(future)

  val p = Product(0L, "Scala Compiler", 1L)

  "If outer transaction fails, inner transactions shouldn't commit" in {
    val id: Long = await {
      context.transaction {
        for {
          id <- context.transaction {
            context.run(productInsert(lift(p)))
          }
          Throw(_) <- context.transaction {
            context.run(quote {
              query[Product].insert(lift(p.copy(id = id)))
            }).liftToTry
          }
        } yield id
      }
    }
    // Since a query inside a transaction failed, the outermost transaction had
    // to rollback.
    val res: List[Product] = await { context.run(productById(lift(id))) }
    res mustEqual List()
  }

  "Transaction inside transaction should not open a new client" in {
    val res: Product = await {
      context.transaction {
        for {
          id: Long <- context.run(productInsert(lift(p)))
          // A subtransaction should have access to the previous queries of an
          // outer transaction.
          res: List[Product] <- context.transaction {
            context.run(productById(lift(id)))
          }
        } yield res.head
      }
    }
    res mustEqual p.copy(id = res.id)
  }

  override def beforeAll = {
    await(context.run(quote { query[Product].delete }))
    ()
  }
} 
Example 11
Source File: PeopleFinaglePostgresSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import com.twitter.util.Await
import com.twitter.util.Future

import io.getquill.context.sql.PeopleSpec

class PeopleFinaglePostgresSpec extends PeopleSpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Couple].delete)
          _ <- testContext.run(query[Person].filter(_.age > 0).delete)
          _ <- testContext.run(liftQuery(peopleEntries).foreach(e => peopleInsert(e)))
          _ <- testContext.run(liftQuery(couplesEntries).foreach(e => couplesInsert(e)))
        } yield {}
      }
    }

  "Example 1 - differences" in {
    await(testContext.run(`Ex 1 differences`)) mustEqual `Ex 1 expected result`
  }

  "Example 2 - range simple" in {
    await(testContext.run(`Ex 2 rangeSimple`(lift(`Ex 2 param 1`), lift(`Ex 2 param 2`)))) mustEqual `Ex 2 expected result`
  }

  "Examples 3 - satisfies" in {
    await(testContext.run(`Ex 3 satisfies`)) mustEqual `Ex 3 expected result`
  }

  "Examples 4 - satisfies" in {
    await(testContext.run(`Ex 4 satisfies`)) mustEqual `Ex 4 expected result`
  }

  "Example 5 - compose" in {
    await(testContext.run(`Ex 5 compose`(lift(`Ex 5 param 1`), lift(`Ex 5 param 2`)))) mustEqual `Ex 5 expected result`
  }

  "Example 6 - predicate 0" in {
    await(testContext.run(satisfies(eval(`Ex 6 predicate`)))) mustEqual `Ex 6 expected result`
  }

  "Example 7 - predicate 1" in {
    await(testContext.run(satisfies(eval(`Ex 7 predicate`)))) mustEqual `Ex 7 expected result`
  }

  "Example 8 - contains empty" in {
    await(testContext.run(`Ex 8 and 9 contains`(liftQuery(`Ex 8 param`)))) mustEqual `Ex 8 expected result`
  }

  "Example 9 - contains non empty" in {
    await(testContext.run(`Ex 8 and 9 contains`(liftQuery(`Ex 9 param`)))) mustEqual `Ex 9 expected result`
  }
} 
Example 12
Source File: OnConflictFinagleSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import com.twitter.util.{ Await, Future }
import io.getquill.context.sql.OnConflictSpec

class OnConflictFinagleSpec extends OnConflictSpec {
  val ctx = testContext
  import ctx._

  def await[T](future: Future[T]) = Await.result(future)

  override protected def beforeAll(): Unit = {
    await(ctx.run(qr1.delete))
    ()
  }

  "ON CONFLICT DO NOTHING" in {
    import `onConflictIgnore`._
    await(ctx.run(testQuery1)) mustEqual res1
    await(ctx.run(testQuery2)) mustEqual res2
    await(ctx.run(testQuery3)) mustEqual res3
  }

  "ON CONFLICT (i) DO NOTHING" in {
    import `onConflictIgnore(_.i)`._
    await(ctx.run(testQuery1)) mustEqual res1
    await(ctx.run(testQuery2)) mustEqual res2
    await(ctx.run(testQuery3)) mustEqual res3
  }

  "ON CONFLICT (i) DO UPDATE ..." in {
    import `onConflictUpdate(_.i)((t, e) => ...)`._
    await(ctx.run(testQuery(e1))) mustEqual res1
    await(ctx.run(testQuery(e2))) mustEqual res2
    await(ctx.run(testQuery(e3))) mustEqual res3
    await(ctx.run(testQuery4)) mustEqual res4
  }
} 
Example 13
Source File: DepartmentsFinaglePostgresSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import com.twitter.util.Await
import com.twitter.util.Future

import io.getquill.context.sql.DepartmentsSpec

class DepartmentsPostgresMysqlSpec extends DepartmentsSpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Department].delete)
          _ <- testContext.run(query[Employee].delete)
          _ <- testContext.run(query[Task].delete)

          _ <- testContext.run(liftQuery(departmentEntries).foreach(e => departmentInsert(e)))
          _ <- testContext.run(liftQuery(employeeEntries).foreach(e => employeeInsert(e)))
          _ <- testContext.run(liftQuery(taskEntries).foreach(e => taskInsert(e)))
        } yield {}
      }
    }

  "Example 8 - nested naive" in {
    await(testContext.run(`Example 8 expertise naive`(lift(`Example 8 param`)))) mustEqual `Example 8 expected result`
  }

  "Example 9 - nested db" in {
    await(testContext.run(`Example 9 expertise`(lift(`Example 9 param`)))) mustEqual `Example 9 expected result`
  }
} 
Example 14
Source File: CaseClassQueryFinagleSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import com.twitter.util.{ Await, Future }
import io.getquill.context.sql.CaseClassQuerySpec
import org.scalatest.matchers.should.Matchers._

class CaseClassQueryFinagleSpec extends CaseClassQuerySpec {

  val context = testContext
  import testContext._

  def await[T](future: Future[T]) = Await.result(future)

  override def beforeAll =
    await {
      testContext.transaction {
        for {
          _ <- testContext.run(query[Contact].delete)
          _ <- testContext.run(query[Address].delete)
          _ <- testContext.run(liftQuery(peopleEntries).foreach(e => peopleInsert(e)))
          _ <- testContext.run(liftQuery(addressEntries).foreach(e => addressInsert(e)))
        } yield {}
      }
    }

  "Example 1 - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1 CaseClass Record Output`)) should contain theSameElementsAs (`Ex 1 CaseClass Record Output expected result`)
  }
  "Example 1A - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1A CaseClass Record Output`)) should contain theSameElementsAs `Ex 1 CaseClass Record Output expected result`
  }
  "Example 1B - Single Case Class Mapping" in {
    await(testContext.run(`Ex 1B CaseClass Record Output`)) should contain theSameElementsAs `Ex 1 CaseClass Record Output expected result`
  }

  "Example 2 - Single Record Mapped Join" in {
    await(testContext.run(`Ex 2 Single-Record Join`)) should contain theSameElementsAs `Ex 2 Single-Record Join expected result`
  }

  "Example 3 - Inline Record as Filter" in {
    await(testContext.run(`Ex 3 Inline Record Usage`)) should contain theSameElementsAs `Ex 3 Inline Record Usage exepected result`
  }
} 
Example 15
Source File: ProductFinaglePostgresSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.context.finagle.postgres

import io.getquill.context.sql.ProductSpec
import com.twitter.util.Await
import com.twitter.util.Future
import io.getquill.context.sql.Id

class ProductFinaglePostgresSpec extends ProductSpec {

  val context = testContext
  import testContext._

  def await[T](r: Future[T]) = Await.result(r)

  override def beforeAll = {
    await(testContext.run(quote(query[Product].delete)))
    ()
  }

  "Product" - {
    "Insert multiple products" in {
      val inserted = await(Future.collect(productEntries.map(product => testContext.run(productInsert(lift(product))))))
      val product = await(testContext.run(productById(lift(inserted(2))))).head
      product.description mustEqual productEntries(2).description
      product.id mustEqual inserted(2)
    }
    "Single insert product" in {
      val inserted = await(testContext.run(productSingleInsert))
      val product = await(testContext.run(productById(lift(inserted)))).head
      product.description mustEqual "Window"
      product.id mustEqual inserted
    }

    "Single insert with inlined free variable" in {
      val prd = Product(0L, "test1", 1L)
      val inserted = await {
        testContext.run {
          product.insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returning(_.id)
        }
      }
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test1"
      returnedProduct.sku mustEqual 1L
      returnedProduct.id mustEqual inserted
    }

    "Single insert with free variable and explicit quotation" in {
      val prd = Product(0L, "test2", 2L)
      val q1 = quote {
        product.insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returning(_.id)
      }
      val inserted = await(testContext.run(q1))
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test2"
      returnedProduct.sku mustEqual 2L
      returnedProduct.id mustEqual inserted
    }

    "Single product insert with a method quotation" in {
      val prd = Product(0L, "test3", 3L)
      val inserted = await(testContext.run(productInsert(lift(prd))))
      val returnedProduct = await(testContext.run(productById(lift(inserted)))).head
      returnedProduct.description mustEqual "test3"
      returnedProduct.sku mustEqual 3L
      returnedProduct.id mustEqual inserted
    }

    "Single insert with value class" in {
      case class Product(id: Id, description: String, sku: Long)
      val prd = Product(Id(0L), "test2", 2L)
      val q1 = quote {
        query[Product].insert(_.sku -> lift(prd.sku), _.description -> lift(prd.description)).returning(_.id)
      }
      await(testContext.run(q1)) mustBe a[Id]
    }

    "supports casts from string to number" - {
      "toInt" in {
        case class Product(id: Long, description: String, sku: Int)
        val queried = await {
          testContext.run {
            query[Product].filter(_.sku == lift("1004").toInt)
          }
        }.head
        queried.sku mustEqual 1004L
      }
      "toLong" in {
        val queried = await {
          testContext.run {
            query[Product].filter(_.sku == lift("1004").toLong)
          }
        }.head
        queried.sku mustEqual 1004L
      }
    }
  }
} 
Example 16
Source File: App.scala    From finagle-metrics   with MIT License 5 votes vote down vote up
import com.codahale.metrics.ConsoleReporter
import com.twitter.finagle.{ Http, Service }
import com.twitter.finagle.metrics.MetricsStatsReceiver
import com.twitter.finagle.http.{ Request, Response, Status }
import com.twitter.io.Charsets
import com.twitter.server.TwitterServer
import com.twitter.util.{ Await, Future }
import java.util.concurrent.TimeUnit

object App extends TwitterServer {

  val service = new Service[Request, Response] {
    def apply(request: Request) = {
      val response = Response(request.version, Status.Ok)
      response.contentString = "hello"
      Future.value(response)
    }
  }

  val reporter = ConsoleReporter
    .forRegistry(MetricsStatsReceiver.metrics)
    .convertRatesTo(TimeUnit.SECONDS)
    .convertDurationsTo(TimeUnit.MILLISECONDS)
    .build

  def main() = {
    val server = Http.serve(":8080", service)
    reporter.start(5, TimeUnit.SECONDS)

    onExit { server.close() }

    Await.ready(server)
  }

} 
Example 17
Source File: TestHttpServerTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.testing

import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.Status.{Accepted, Conflict}
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.finagle.{Http, Service}
import com.twitter.util.{Await, Future}
import io.fintrospect.RouteSpec
import org.scalatest.{BeforeAndAfterEach, FunSpec, Matchers}


class TestHttpServerTest extends FunSpec with Matchers with BeforeAndAfterEach {
  val originalStatus = Conflict

  private val server = new TestHttpServer(9888, RouteSpec().at(Get) bindTo Service.mk { r: Request => Future(Response(originalStatus)) })

  override def beforeEach() = {
    Await.result(server.start())
  }

  override def afterEach() = {
    Await.result(server.stop())
  }

  it("will serve routes that are passed to it") {
    statusShouldBe(originalStatus)
  }

  it("can override status") {
    server.respondWith(Accepted)
    statusShouldBe(Accepted)
  }

  private def statusShouldBe(expected: Status): Unit = {
    Await.result(Http.newService("localhost:9888", "")(Request())).status shouldBe expected
  }
} 
Example 18
Source File: OverridableHttpServiceTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.testing

import com.twitter.finagle.Service
import com.twitter.finagle.http.Status.{Accepted, Conflict}
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.util.{Await, Future}
import org.scalatest.{FunSpec, Matchers}

class OverridableHttpServiceTest extends FunSpec with Matchers {

  val originalStatus = Conflict

  val overridableHttpService = new OverridableHttpService[Request](Service.mk { r: Request => Future(Response(originalStatus)) })
  it("will serve routes that are passed to it") {
    statusShouldBe(originalStatus)
  }

  it("can override status") {
    overridableHttpService.respondWith(Accepted)
    statusShouldBe(Accepted)
  }

  private def statusShouldBe(expected: Status): Unit = {
    Await.result(overridableHttpService.service(Request())).status shouldBe expected
  }
} 
Example 19
Source File: FileServiceSpec.scala    From peregrine   with Apache License 2.0 5 votes vote down vote up
package io.peregrine

import com.twitter.finagle.http.service.NullService
import com.twitter.finagle.http.{Request => FinagleRequest}
import com.twitter.util.Await
import org.jboss.netty.handler.codec.http.{HttpHeaders, HttpResponseStatus}


class FileServiceSpec extends ShouldSpec {
  val fileService = new FileService

  // We assert the content, rather than just 200, since FileService always defers to AppService to render the 404.
  "looking up static files" should "return gif content" in {
    val r = FinagleRequest("/dealwithit.gif")
    val response = fileService(r, NullService)
    Await.result(response).getContent().array().length should not equal 0
  }

  "looking up static files with params" should "return gif content" in {
    val r = FinagleRequest("/dealwithit.gif", "foo" -> "bar")
    val response = fileService(r, NullService)
    Await.result(response).getContent().array().length should not equal 0
  }

  "looking up static files" should "set Content-Length" in {
    val r = FinagleRequest("/dealwithit.gif", "foo" -> "bar")
    val response = fileService(r, NullService)
    Await.result(response).contentLength should equal (Some(422488L))
  }

  "looking up static files" should "set Last-Modified" in {
    val r = FinagleRequest("/dealwithit.gif")
    val response = fileService(r, NullService)
    Await.result(response).lastModified should not equal None
  }

  "looking up static files" should "return NOT_MODIFIED for unmodified file" in {
    val req1 = FinagleRequest("/dealwithit.gif")
    val res1 = fileService(req1, NullService)
    val lastModified = Await.result(res1).lastModified.get

    val req2 = FinagleRequest("/dealwithit.gif")
    req2.headers().set(HttpHeaders.Names.IF_MODIFIED_SINCE, lastModified)
    val res2 = fileService(req2, NullService)
    Await.result(res2).status should equal(HttpResponseStatus.NOT_MODIFIED)
  }

  "looking up static files in production" should "set Last-Modified" in {
    System.setProperty("io.peregrine.config.env", "production")
    try {
      val r = FinagleRequest("/dealwithit.gif")
      val response = fileService(r, NullService)
      Await.result(response).lastModified should not equal None
    } finally {
      System.setProperty("io.peregrine.config.env", "development")
    }
  }

  "looking up static files in production" should "return NOT_MODIFIED for unmodified file" in {
    System.setProperty("io.peregrine.config.env", "production")
    try {
      val req1 = FinagleRequest("/dealwithit.gif")
      val res1 = fileService(req1, NullService)
      val lastModified = Await.result(res1).lastModified.get

      val req2 = FinagleRequest("/dealwithit.gif")
      req2.headers().set(HttpHeaders.Names.IF_MODIFIED_SINCE, lastModified)
      val res2 = fileService(req2, NullService)
      Await.result(res2).status should equal(HttpResponseStatus.NOT_MODIFIED)
    } finally {
      System.setProperty("io.peregrine.config.env", "development")
    }
  }


  // TODO mustache is required in here and it's being removed so this wont pass
//  "looking up a directory" should "return a non-empty, html response" in {
//    System.setProperty("io.peregrine.config.showDirectories", "true")
//    val r  = FinagleRequest("/components")
//    val response = fileService(r, NullService)
//    Await.result(response).contentType should equal(Some("text/html"))
//    Await.result(response).contentLength should not equal Some(0)
//  }

  "looking up / " should "not serve a file" in {
    val r  = FinagleRequest("/")
    val response = fileService(r, NullService)
    response should not be None
  }
} 
Example 20
Source File: ExampleApp.scala    From caliban   with Apache License 2.0 5 votes vote down vote up
package caliban.finch

import caliban.ExampleData.sampleCharacters
import caliban.ExampleService.ExampleService
import caliban.{ ExampleApi, ExampleService, FinchAdapter }
import com.twitter.io.{ Buf, BufReader, Reader }
import com.twitter.util.Await
import io.finch.Endpoint
import zio.clock.Clock
import zio.console.Console
import zio.internal.Platform
import zio.interop.catz._
import zio.{ Runtime, Task }

object ExampleApp extends App with Endpoint.Module[Task] {

  implicit val runtime: Runtime[ExampleService with Console with Clock] =
    Runtime.unsafeFromLayer(ExampleService.make(sampleCharacters) ++ Console.live ++ Clock.live, Platform.default)

  val interpreter = runtime.unsafeRun(ExampleApi.api.interpreter)

  
  import com.twitter.finagle.Http
  import io.finch._
  import io.finch.circe._

  val endpoint = "api" :: "graphql" :: FinchAdapter.makeHttpService(interpreter)

  val graphiqlBuf = {
    val stream = getClass.getResourceAsStream("/graphiql.html")
    BufReader.readAll(Reader.fromStream(stream))
  }

  val grapihql: Endpoint[Task, Buf] = get("graphiql") {
    graphiqlBuf.map(Ok)
  }

  val services = Bootstrap.serve[Application.Json](endpoint).serve[Text.Html](grapihql).toService

  val server = Http.server.serve(":8088", services)

  println(s"Server online at http://localhost:8088/\nPress RETURN to stop...")
  Await.ready(server)

} 
Example 21
Source File: QuerySpec.scala    From roc   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package roc
package integrations

import com.twitter.util.Await
import org.specs2.Specification
import roc.postgresql.Request

final class QuerySpec extends Specification 
  with SqlReader
  with Client { def is = sequential ^ s2""" 

  Query
    must execute a CREATE command   $testCreate
    must execute an INSERT command  $testInsert
    must execute an UPDATE command  $testUpdate
    must execute a SELECT command   $testSelect
    must execute a DELETE command   $testDelete
    must execute a DROP command     $testDrop

                                                                        """

  def testCreate() = {
    val sql     = readSql("query/create.sql")
    val request = new Request(sql)
    val result  = Await.result(Postgres.query(request))
    result.completedCommand must_== "CREATE TABLE"
  }

  def testInsert() = {
    val sql     = readSql("query/insert.sql")
    val request = new Request(sql)
    val result  = Await.result(Postgres.query(request))
    result.completedCommand must_== "INSERT 0 1"
  }

  def testUpdate() = {
    val sql     = readSql("query/update.sql")
    val request = new Request(sql)
    val result  = Await.result(Postgres.query(request))
    result.completedCommand must_== "UPDATE 1"
  }

  def testSelect() = {
    val request = new Request("SELECT name FROM roc_tests WHERE id = 1")
    val result  = Await.result(Postgres.query(request))
    result.toList.length must_== 1
  }

  def testDelete() = {
    val request = new Request("DELETE FROM roc_tests WHERE id = 1;")
    val result  = Await.result(Postgres.query(request))
    result.completedCommand must_== "DELETE 1"
  }

  def testDrop() = {
    val request = new Request("DROP TABLE roc_tests;")
    val result = Await.result(Postgres.query(request))
    result.completedCommand must_== "DROP TABLE"
  }
} 
Example 22
Source File: CookieBasedSessionSpec.scala    From peregrine   with Apache License 2.0 5 votes vote down vote up
package io.peregrine

import com.twitter.util.Await

class CookieBasedSessionSpec extends ShouldSpec {

  "#get" should "return Some(String) if there's a value with the corresponding key" in {
    val session = new CookieBasedSession("TEST_ID")
    Await.result(session.put("foo", "bar"))
    Await.result(session.get("foo")) should be (Some("bar"))
  }

  "#get" should "return None if there's a value with the corresponding key" in {
    val session = new CookieBasedSession("TEST_ID")
    Await.result(session.put("foo", "bar"))
    Await.result(session.get("zaa")) should be (None)
  }

  "#set" should "set a valid value in session object" in {
    val session = new CookieBasedSession("TEST_ID")
    Await.result(session.put("foo", "bar"))
    Await.result(session.get("foo")) should be (Some("bar"))
  }

  "#del" should "delete a value with the exsiting key" in {
    val session = new CookieBasedSession("TEST_ID")
    Await.result(session.put("foo", "bar"))
    Await.result(session.get("foo")) should be (Some("bar"))
    Await.result(session.del("foo"))
    Await.result(session.get("foo")) should be (None)
  }


} 
Example 23
Source File: SpecHelper.scala    From peregrine   with Apache License 2.0 5 votes vote down vote up
package io.peregrine

import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import com.twitter.util.{Await, Future}
import org.jboss.netty.handler.codec.http.HttpMethod
import org.jboss.netty.util.CharsetUtil.UTF_8

import scala.collection.Map

class MockResponse(val originalResponse: FinagleResponse) {

  def status                  = originalResponse.getStatus()
  def code                    = originalResponse.getStatus().getCode
  def body                    = originalResponse.getContent().toString(UTF_8)
  def getHeader(name: String) = originalResponse.headers().get(name)
  def getHeaders              = originalResponse.headerMap

}

trait SpecHelper {

  def response  = new MockResponse(Await.result(lastResponse))
  var lastResponse: Future[FinagleResponse] = null

  def server: PeregrineServer

  def get(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map()) {
    executeRequest(HttpMethod.GET,path,params,headers)
  }

  def post(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map(), body:AnyRef=null) {
    executeRequest(HttpMethod.POST,path,params,headers,body)
  }

  def put(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map(), body:AnyRef=null) {
    executeRequest(HttpMethod.PUT,path,params,headers,body)
  }

  def delete(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map()) {
    executeRequest(HttpMethod.DELETE,path,params,headers)
  }

  def head(path:String,params:Map[String,String]=Map(), headers:Map[String,String]=Map()) {
    executeRequest(HttpMethod.HEAD,path,params,headers)
  }

  def patch(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map()) {
    executeRequest(HttpMethod.PATCH,path,params,headers)
  }

  def options(path:String, params:Map[String,String]=Map(), headers:Map[String,String]=Map(), body:AnyRef=null) {
    executeRequest(HttpMethod.OPTIONS,path,params,headers,body)
  }

  def send(request: FinagleRequest) {
    executeRequest(request)
  }

  private def executeRequest(
                              method: HttpMethod,
                              path: String,
                              params: Map[String, String] = Map(),
                              headers: Map[String,String] = Map(),
                              body: AnyRef = null
                              ) {
    val app = MockApp(server)
    val result: MockResult = app.execute(method = method, path = path, params = params, headers = headers, body = body)
    lastResponse = result.response
  }

  private def executeRequest(request: FinagleRequest) {
    val app = MockApp(server)
    val result: MockResult = app.execute(request)
    lastResponse = result.response
  }

} 
Example 24
Source File: CsrfFilter.scala    From peregrine   with Apache License 2.0 5 votes vote down vote up
package io.peregrine

import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.{Future,Await}
import org.jboss.netty.handler.codec.http.HttpMethod

class CsrfFilter extends SimpleFilter[FinagleRequest, FinagleResponse] with Sessions {

  def apply(req: FinagleRequest, service: Service[FinagleRequest, FinagleResponse]): Future[FinagleResponse] = {
    // we bypass on assets requests
    if (req.path.startsWith(config.assetsPathPrefix())) {
      service(req)
    } else {
      _applyCsrfProtection(req, service)
    }
  }

  private def _applyCsrfProtection(req: FinagleRequest, service: Service[FinagleRequest, FinagleResponse]) = {
    if (req.method == HttpMethod.GET) {
      for {
        _           <- _addCsrfToken(req)
        res         <- service(req)
      } yield res
    } else {
      for {
        csrfToken   <- _addCsrfToken(req)
        authToken   <- Future(req.cookies.getOrElse("_authenticity_token", buildVoidCookie).value)
        paramToken  <- Future(req.params.getOrElse("_csrf_token", ""))
        res         <- if (csrfToken == paramToken && csrfToken == authToken) service(req)
                       else Future(new ResponseBuilder().status(403).body("CSRF failed").build)
      } yield res
    }
  }

  private def _addCsrfToken(req: FinagleRequest) = {
    for {
      session     <- session(new Request(req))
      csrfToken   <- session.getOrElseUpdate[String]("_csrf_token", generateToken)
      _           <- Future(req.response.addCookie(buildCsrfCookie(csrfToken)))
    } yield csrfToken
  }

  protected def generateToken = IdGenerator.hexString(32)

  private def buildVoidCookie = new CookieBuilder().name("_authenticity_token").value("").build()
  private def buildCsrfCookie(value: String) = {
    new CookieBuilder().name("_authenticity_token")
      .value(value)
      .httpOnly(httpOnly = true)
      // enables cookies for secure session if cert and key are provided
      .secure(!config.certificatePath().isEmpty && !config.keyPath().isEmpty)
      .build()
  }
} 
Example 25
Source File: AppService.scala    From peregrine   with Apache License 2.0 5 votes vote down vote up
package io.peregrine

import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import com.twitter.util.{Await, Future}

class AppService(controllers: ControllerCollection)
  extends Service[FinagleRequest, FinagleResponse] {

  def render: ResponseBuilder = new ResponseBuilder

  def apply(rawRequest: FinagleRequest): Future[FinagleResponse] = {
    val adaptedRequest = RequestAdapter(rawRequest)

    try {
      attemptRequest(rawRequest).handle {
        case t: Throwable =>
          Await.result(
            ErrorHandler(adaptedRequest, t, controllers)
          )
      }
    } catch {
      case e: Exception =>
        ErrorHandler(adaptedRequest, e, controllers)
    }
  }

  def attemptRequest(rawRequest: FinagleRequest): Future[FinagleResponse] = {
    val adaptedRequest = RequestAdapter(rawRequest)

    controllers.dispatch(rawRequest) match {
      case Some(response) =>
        response.asInstanceOf[Future[FinagleResponse]]
      case None           =>
        ResponseAdapter(adaptedRequest, controllers.notFoundHandler(adaptedRequest))
    }
  }
} 
Example 26
Source File: Server.scala    From typed-schema   with Apache License 2.0 5 votes vote down vote up
package ru.tinkoff.tschema.example

import cats.effect.ExitCode
import cats.effect.concurrent.Ref
import cats.instances.list._
import cats.syntax.foldable._
import cats.syntax.semigroupk._
import com.twitter.finagle
import com.twitter.finagle.http.Response
import com.twitter.util.{Await, Duration}
import monix.eval.{Task, TaskApp}
import ru.tinkoff.tschema.example.sample.SampleModule
import ru.tinkoff.tschema.finagle.RunHttp
import tofu.Void

object Server extends TaskApp {
  def modules[H[_]]: List[ExampleModule[Http]] =
    List(
      new Greeting[Http, Example](),
      new SampleModule[Http, Example](),
      new FiltersModule(),
      new FormFieldsModule(),
      new MultiParameters(),
      new ProxyModule(),
      new VersionModule(),
      new Authorize
    )

  val svc: Http[Response] = modules.foldMapK(_.route) <+> ExampleSwagger.route

  val server = for {
    srv <- RunHttp.run[Example](svc)
    list <- Example.delay(finagle.Http.serve("0.0.0.0:9191", srv))
    _ <- Example.delay(println(s"started at ${list.boundAddress}"))
    _ <- Example.delay(Await.ready(list, Duration.Top)).fork
    res <- Example.fromTask(Task.never[Void])
  } yield res

  def run(args: List[String]): Task[ExitCode] =
    for {
      ref <- Ref[Task].of(0)
      _ <- server
        .onErrorHandle(ex => println(ex.getMessage))
        .run(ExampleEnv("lol", ref))
    } yield ExitCode.Success
} 
Example 27
Source File: Server.scala    From typed-schema   with Apache License 2.0 5 votes vote down vote up
package ru.tinkoff.tschema.example

import Swagger._
import cats.effect.ExitCode
import cats.effect.concurrent.Ref
import cats.instances.list._
import cats.syntax.foldable._
import cats.syntax.semigroupk._
import com.twitter.finagle
import finagle.{Http, http}
import com.twitter.finagle.http.Response
import com.twitter.util.{Await, Duration}
import monix.eval.{Task, TaskApp}
import ru.tinkoff.tschema.finagle.Runnable
import tofu.Void

object Server extends TaskApp {
  implicit val greeting: Greeting[Example] = Greeting[Example]

  val modules: List[ExampleModule] =
    List(GreetingModule[Example, Http](),
         TestModule,
         FiltersModule,
         FormFieldsModule,
         MultiParameters,
         ProxyModule,
         VersionModule,
         Authorize)

  val svc: Http[Response] = modules.foldMapK(_.route) <+> Swagger.route

  val server = for {
    srv  <- Runnable.run[Example](svc)
    list <- Example.delay(finagle.Http.serve("0.0.0.0:9191", srv))
    _    <- Example.delay(println(s"started at ${list.boundAddress}"))
    _    <- Example.delay(Await.ready(list, Duration.Top)).fork
    res  <- Example.fromTask(Task.never[Void])
  } yield res

  def run(args: List[String]): Task[ExitCode] =
    for {
      ref <- Ref[Task].of(0)
      _ <- server
            .onErrorHandle(ex => println(ex.getMessage))
            .run(ExampleEnv("lol", ref))
    } yield ExitCode.Success
} 
Example 28
Source File: Server.scala    From typed-schema   with Apache License 2.0 5 votes vote down vote up
package ru.tinkoff.tschema.example

import ExampleSwagger._
import cats.instances.list._
import cats.syntax.foldable._
import cats.syntax.semigroupk._
import com.twitter.finagle.{Http => FHttp}
import com.twitter.finagle.http.Response
import com.twitter.util.{Await, Duration}
import ru.tinkoff.tschema.finagle.RunHttp
import zio.blocking.Blocking
import zio.console._
import zio.{blocking => _, _}

object Server extends App {
  val modules: List[ExampleModule] =
    List(
      Greeting,
      TestModule,
      FiltersModule,
      FormFieldsModule,
      MultiParameters,
      ProxyModule,
      VersionModule,
      Authorize,
      ReceiveModule
    )

  val svc: Http[Response] = modules.foldMapK(_.route) <+> ExampleSwagger.route

  val server = for {
    srv <- RunHttp.run[Example](svc)
    list <- ZIO.effect(FHttp.serve("0.0.0.0:9191", srv))
    _ <- putStr(s"started at ${list.boundAddress}")
    _ <- ZIO.effect(Await.ready(list, Duration.Top)).fork
    res <- ZIO.never
  } yield res

  val layer: URLayer[Blocking with Console, FullEnv] =
    ZLayer.identity[Blocking with Console] ++ ExampleEnv.live

  def run(args: List[String]): URIO[Blocking with Console, Int] =
    layer.build.use(r => server.catchAll(ex => putStr(ex.getMessage)).provide(r)) as 0
} 
Example 29
Source File: HttpClientSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.cosmos

import com.mesosphere.cosmos.error.CosmosException
import com.mesosphere.cosmos.error.EndpointUriSyntax
import com.mesosphere.universe
import io.lemonlabs.uri.Uri
import io.lemonlabs.uri.dsl._
import com.twitter.finagle.stats.NullStatsReceiver
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.util.Await
import io.circe.jawn.parse
import org.scalatest.FreeSpec
import scala.io.Source

final class HttpClientSpec extends FreeSpec {

  import HttpClientSpec._

  "HttpClient.fetch() retrieves the given URL" in {
    val future = HttpClient.fetch(JsonContentUrl) { responseData =>
      assertResult(universe.MediaTypes.UniverseV4Repository)(responseData.contentType)

      val contentString = Source.fromInputStream(responseData.contentStream).mkString
      assert(parse(contentString).isRight)
    }

    Await.result(future)
  }

  "HttpClient.fetch() reports URL syntax problems" - {

    "relative URI" in {
      val cosmosException = intercept[CosmosException](
        Await.result(HttpClient.fetch("foo/bar")(_ => ()))
      )
      assert(cosmosException.error.isInstanceOf[EndpointUriSyntax])
    }

    "unknown protocol" in {
      val cosmosException = intercept[CosmosException](
        Await.result(HttpClient.fetch("foo://bar.com")(_ => ()))
      )
      assert(cosmosException.error.isInstanceOf[EndpointUriSyntax])
    }

    "URISyntaxException" in {
      val cosmosException = intercept[CosmosException](
        Await.result(HttpClient.fetch("/\\")(_ => ()))
      )
      assert(cosmosException.error.isInstanceOf[EndpointUriSyntax])
    }

  }

}

object HttpClientSpec {
  implicit val stats: StatsReceiver = NullStatsReceiver
  val JsonContentUrl: Uri = "https://downloads.mesosphere.com/universe/" +
    "ae6a07ac0b53924154add2cd61403c5233272d93/repo/repo-up-to-1.10.json"
} 
Example 30
Source File: query-redis.scala    From zipkin-mesos-framework   with Apache License 2.0 5 votes vote down vote up
import com.twitter.conversions.time._
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.redis.util.StringToChannelBuffer
import com.twitter.finagle.redis.{Client, Redis}
import com.twitter.util.Await
import com.twitter.zipkin.builder.QueryServiceBuilder
import com.twitter.zipkin.storage.redis.RedisSpanStore

val serverPort = sys.env.get("QUERY_PORT").getOrElse("9411").toInt
val adminPort = sys.env.get("QUERY_ADMIN_PORT").getOrElse("9901").toInt
val logLevel = sys.env.get("QUERY_LOG_LEVEL").getOrElse("INFO")

val host = sys.env.get("REDIS_HOST").getOrElse("0.0.0.0")
val port = sys.env.get("REDIS_PORT").map(_.toInt).getOrElse(6379)

val client = Client(ClientBuilder().hosts(host + ":" + port)
                                   .hostConnectionLimit(4)
                                   .hostConnectionCoresize(4)
                                   .codec(Redis())
                                   .build())

val authPassword = sys.env.get("REDIS_PASSWORD")
if (authPassword.isDefined) {
  Await.result(client.auth(StringToChannelBuffer(authPassword.get)))
}

val spanStore = new RedisSpanStore(client, Some(7.days))

QueryServiceBuilder(
  "0.0.0.0:" + serverPort,
  adminPort,
  logLevel,
  spanStore
) 
Example 31
Source File: HDFSStoreSpec.scala    From speedo   with Apache License 2.0 5 votes vote down vote up
package com.htc.speedo.caffe

import com.twitter.util.Await

import org.specs2.mutable.SpecificationWithJUnit


class HDFSStoreSpec extends SpecificationWithJUnit {
  sequential

  val key = "key"
  val value1 = 1
  val value2 = 2

  "HDFSStore" should {
    val store = HDFSStore[Int]("target/hdfsstore-test")

    "get None for non-existance keys" in {
      Await.result(store.get(key)) must beNone
    }
    "put and get correctly" in {
      Await.result(store.put(key, Some(value1)))
      Await.result(store.get(key)).get must_== value1
    }
    "override existing keys with put correctly" in {
      Await.result(store.put(key, Some(value2)))
      Await.result(store.get(key)).get must_== value2
    }
    "delete existing keys correctly" in {
      Await.result(store.put(key, None))
      Await.result(store.get(key)) must_== None
    }
    "delete non-existing keys as no-op" in {
      Await.result(store.put(key, None))
      Await.result(store.get(key)) must_== None
    }
    step { store.close() }
  }
} 
Example 32
Source File: EmailSenderSpec.scala    From diffy   with GNU Affero General Public License v3.0 5 votes vote down vote up
package ai.diffy.util

import java.util.Date

import ai.diffy.ParentSpec
import com.twitter.logging.Logger
import com.twitter.util.Await
import org.junit.runner.RunWith
import org.mockito.Mockito._
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class EmailSenderSpec extends ParentSpec {

  val log = mock[Logger]
  val sender = new EmailSender(log, _ => ())
  describe("EmailSender") {
    it("should not encouter any errors while trying to compose emails") {
      Await.result(
        sender(
          SimpleMessage(
            from = "Diffy <[email protected]>",
            to = "[email protected]",
            bcc = "[email protected]",
            subject = "Diffy Report at " + new Date,
            body = "just testing emails from mesos!"
          )
        )
      )
      verifyZeroInteractions(log)
    }
  }
} 
Example 33
Source File: HttpFeatureTest.scala    From diffy   with GNU Affero General Public License v3.0 5 votes vote down vote up
package ai.diffy

import java.net.ServerSocket

import ai.diffy.examples.http.ExampleServers
import ai.diffy.proxy.DifferenceProxy
import com.google.common.collect.ImmutableMap
import com.google.inject.Stage
import com.twitter.finagle.Http
import com.twitter.finagle.http.Status
import com.twitter.finagle.util.DefaultTimer
import com.twitter.finatra.http.EmbeddedHttpServer
import com.twitter.inject.Test
import com.twitter.util.{Await, Duration, Future, FuturePool}

class HttpFeatureTest extends Test {
  def getPort(): Int = {
    val s  = new ServerSocket(0)
    val port = s.getLocalPort
    s.close()
    port
  }

  val env@Seq(p,s,c,d) = Seq.fill(4)(getPort())
  val environment = FuturePool.unboundedPool(ExampleServers.main(env.take(3).map(_.toString).toArray))

  val diffy = new MainService
  lazy val differenceProxy = diffy.injector.instance[DifferenceProxy]

  val server = new EmbeddedHttpServer(
    twitterServer = diffy,
    flags = Map(
      "proxy.port" -> s":$d",
      "candidate" -> s"localhost:$c",
      "master.primary" -> s"localhost:$p",
      "master.secondary" -> s"localhost:$s",
      "serviceName" -> "myHttpService",
      "service.protocol" -> "http",
      "summary.email" ->"test"
    ),
    stage = Stage.PRODUCTION
  )

  test("verify startup") {
    server.assertHealthy()
  }

  test("verify DifferenceCollector") {
    assert(differenceProxy.collector.fields.isEmpty)
    Await.result(Http.fetchUrl(s"http://localhost:$d/json?Twitter").liftToTry)
    var tries = 0
    while(differenceProxy.outstandingRequests.get() > 0 && tries < 10) {
      Await.result(Future.sleep(Duration.fromSeconds(1))(DefaultTimer.twitter))
      tries = tries + 1
    }
    assert(!differenceProxy.collector.fields.isEmpty)
  }

  test("verify present differences via API") {
    val response =
      Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/undefined_endpoint/stats"))
    assertResult(Status.Ok)(response.status)
    assert(response.getContentString().contains(""""differences":1"""))
  }

  test("verify absent endpoint in API") {
    val response =
      Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/json/stats"))
    assertResult(Status.Ok)(response.status)
    assertResult("""{"error":"key not found: json"}""")(response.getContentString())
  }
  Seq(
    "/api/1/overview",
    "/api/1/report",
    "/api/1/endpoints",
    "/api/1/endpoints/json/stats",
    "/api/1/endpoints/json/fields/result.200.values.value.name.PrimitiveDifference/results",
    "/api/1/endpoints/json/fields/result.200.values.value.name.PrimitiveDifference/results/0"
  ) foreach { endpoint =>
    test(s"ping ${endpoint}") {
      val response =
        Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}${endpoint}"))
      assertResult(Status.Ok)(response.status)
    }
  }
} 
Example 34
Source File: ThriftFeatureTest.scala    From diffy   with GNU Affero General Public License v3.0 5 votes vote down vote up
package ai.diffy

import java.io.{File, FileOutputStream}
import java.net.ServerSocket
import java.nio.file.Files
import java.util.zip.{ZipEntry, ZipOutputStream}

import ai.diffy.examples.thrift.ExampleServers
import ai.diffy.proxy.DifferenceProxy
import ai.diffy.thriftscala.Adder
import com.google.inject.Stage
import com.twitter.finagle.http.Status
import com.twitter.finagle.util.DefaultTimer
import com.twitter.finagle.{Http, ThriftMux}
import com.twitter.finatra.http.EmbeddedHttpServer
import com.twitter.inject.Test
import com.twitter.util.{Await, Duration, Future, FuturePool}

import scala.io.Source

class ThriftFeatureTest extends Test {

  def getPort(): Int = {
    val s  = new ServerSocket(0)
    val port = s.getLocalPort
    s.close()
    port
  }

  val env@Seq(p,s,c,d) = Seq.fill(4)(getPort())
  val environment = FuturePool.unboundedPool(ExampleServers.main(env.take(3).map(_.toString).toArray))

  val diffy = new MainService
  lazy val differenceProxy = diffy.injector.instance[DifferenceProxy]


  val thriftFile = new File("src/test/thrift/example.thrift")
  val data = Source.fromInputStream(Files.newInputStream(thriftFile.toPath), "UTF-8").mkString
  val thriftJar = Files.createTempFile("thrift", "jar")
  thriftJar.toFile.deleteOnExit()
  val out = new ZipOutputStream(new FileOutputStream(thriftJar.toFile))
  out.putNextEntry(new ZipEntry(thriftFile.getAbsolutePath))
  out.write(data.getBytes)
  out.closeEntry()
  out.close()

  val server = new EmbeddedHttpServer(
    twitterServer = diffy,
    flags = Map(
      "proxy.port" -> s":$d",
      "candidate" -> s"localhost:$c",
      "master.primary" -> s"localhost:$p",
      "master.secondary" -> s"localhost:$s",
      "serviceName" -> "myThriftService",
      "service.protocol" -> "thrift",
      "thrift.jar" -> thriftJar.toAbsolutePath.toString,
      "thrift.serviceClass" -> "Adder",
      "summary.email" -> "test"
    ),
    stage = Stage.PRODUCTION
  )

  val client = ThriftMux.client.build[Adder.MethodPerEndpoint](s"localhost:$d")

  test("verify startup") {
    server.assertHealthy()
  }

  test("verify DifferenceCollector") {
    assert(differenceProxy.collector.fields.isEmpty)
    Await.result(client.add(1, 1).liftToTry)
    var tries = 0
    while(differenceProxy.outstandingRequests.get() > 0 && tries < 10) {
      Await.result(Future.sleep(Duration.fromSeconds(1))(DefaultTimer))
      tries = tries + 1
    }
    assert(!differenceProxy.collector.fields.isEmpty)
  }

  test("verify present differences via API") {
    val response =
      Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/add/stats"))
    assertResult(Status.Ok)(response.status)
    assert(response.getContentString().contains(""""differences":1"""))
  }

  test("verify absent endpoint in API") {
    val response =
      Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/subtract/stats"))
    assertResult(Status.Ok)(response.status)
    assertResult("""{"error":"key not found: subtract"}""")(response.getContentString())
  }

} 
Example 35
Source File: ServicesIntegrationSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.cosmos

import io.lemonlabs.uri.dsl._
import com.twitter.conversions.storage._
import com.twitter.finagle.http.RequestBuilder
import com.twitter.finagle.http.Status
import com.twitter.util.Await
import com.twitter.util.Return
import org.scalatest.FreeSpec

final class ServicesIntegrationSpec extends FreeSpec {

  "Services" - {
    "adminRouterClient should" - {
      "be able to connect to an https site" in {
        val url = "https://www.google.com"
        val Return(client) = Services.adminRouterClient(url, 5.megabytes)

        val request = RequestBuilder().url(url).buildGet()
        val response = Await.result(client(request))
        assertResult(response.status)(Status.Ok)
      }
    }
  }
} 
Example 36
Source File: ItUtil.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.cosmos

import com.mesosphere.cosmos.test.CosmosIntegrationTestClient
import com.twitter.util.Await
import scala.concurrent.duration._

object ItUtil {

  def waitForDeployment(adminRouter: AdminRouter)(attempts: Int): Boolean = {
    Stream.tabulate(attempts) { _ =>
      Thread.sleep(1.second.toMillis)
      val deployments = Await.result {
        adminRouter.listDeployments()(CosmosIntegrationTestClient.Session)
      }

      deployments.isEmpty
    }.dropWhile(done => !done).nonEmpty
  }

} 
Example 37
Source File: ServicesSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.cosmos

import com.mesosphere.cosmos.error.CosmosException
import com.mesosphere.cosmos.error.ServiceUnavailable
import io.lemonlabs.uri.dsl._
import com.twitter.conversions.storage._
import com.twitter.finagle.http.RequestBuilder
import com.twitter.util.Await
import com.twitter.util.Return
import io.netty.handler.codec.http.HttpResponseStatus
import org.scalatest.FreeSpec

final class ServicesSpec extends FreeSpec {

  "Services" - {

    "adminRouterClient should" - {
      "be able to be created with a well formed URI with a domain that doesn't resolve" in {
        val url = "http://some.domain.that-im-pretty-sure-doesnt-exist-anywhere"
        val Return(client) = Services.adminRouterClient(url, 5.megabytes)

        try {
          val request = RequestBuilder().url(url).buildGet()
          Await.result(client(request))
        } catch {
          case e: CosmosException if e.error.isInstanceOf[ServiceUnavailable] =>
            assertResult(HttpResponseStatus.SERVICE_UNAVAILABLE)(e.error.status)
        } finally {
          val _ = Await.ready(client.close())
        }
      }
    }

  }
} 
Example 38
Source File: WeatherApi.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.context

import com.twitter.finagle._
import com.twitter.finagle.context.Contexts
import com.twitter.finagle.http.service.HttpResponseClassifier
import com.twitter.server.TwitterServer
import com.twitter.util.Await
import io.circe.generic.auto._
import io.finch._
import io.finch.circe._
import net.gutefrage.temperature.thrift._
import net.gutefrage.{Dtabs, Env}
import org.slf4j.LoggerFactory
import org.slf4j.bridge.SLF4JBridgeHandler


    case class Mean(mean: Double)
    case class MeanForUser(mean: Double, userId: Long)

    // mean temperature endpoint
    val mean: Endpoint[Mean] = get("weather" / "mean") {
      client.mean().map(mean => Ok(Mean(mean)))
    }

    // user endpoint which sets a UserContext
    val userMean: Endpoint[MeanForUser] =
      get("weather" / "mean" / "user" :: long) { userId: Long =>
        val userContext = UserContext(userId)
        Contexts.broadcast.let(UserContext, userContext) {
          client.mean().map(mean => Ok(MeanForUser(mean, userId)))
        }
      }

    // compose endpoints
    // https://github.com/finagle/finch/blob/master/docs/endpoint.md#composing-endpoints
    val api = mean :+: userMean

    // start and announce the server
    val server = Http.server
      .withLabel("weather-api")
      .withResponseClassifier(HttpResponseClassifier.ServerErrorsAsFailures)
      .serve(
        addr = s":${port()}",
        service = api.toService
      )

    closeOnExit(server)
    Await.ready(server)

  }

} 
Example 39
Source File: SerialIntegrationTest.scala    From finagle-serial   with Apache License 2.0 5 votes vote down vote up
package io.github.finagle.serial.tests

import com.twitter.finagle.{Client, ListeningServer, Server, Service}
import com.twitter.util.{Await, Future, Try}
import io.github.finagle.serial.Serial
import java.net.{InetAddress, InetSocketAddress}
import org.scalatest.Matchers
import org.scalatest.prop.Checkers
import org.scalacheck.{Arbitrary, Gen, Prop}


  def testFunctionService[I, O](
    f: I => O
  )(implicit
    inCodec: C[I],
    outCodec: C[O],
    arb: Arbitrary[I]
  ): Unit = {
    val (fServer, fClient) = createServerAndClient(f)(inCodec, outCodec)

    check(serviceFunctionProp(fClient)(f)(arb.arbitrary))

    Await.result(fServer.close())
  }
} 
Example 40
Source File: RoundTripBenchmark.scala    From finagle-serial   with Apache License 2.0 5 votes vote down vote up
package io.github.finagle.serial.benchmark

import java.net.InetSocketAddress
import java.util.concurrent.TimeUnit

import com.twitter.finagle.{Server, Client, Service}
import com.twitter.util.{Closable, Await, Future}
import org.openjdk.jmh.annotations._

@State(Scope.Thread)
abstract class RoundTripBenchmark[A](val workload: A) {

  val echo = new Service[A, A] {
    override def apply(a: A) = Future.value(a)
  }

  var s: Closable = _
  var c: Service[A, A] = _

  def server: Server[A, A]
  def client: Client[A, A]

  @Setup
  def setUp(): Unit = {
    s = server.serve(new InetSocketAddress(8123), echo)
    c = client.newService("localhost:8123")
  }

  @TearDown
  def tearDown(): Unit = {
    Await.ready(c.close())
    Await.ready(s.close())
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def test: A = Await.result(c(workload))
} 
Example 41
Source File: RoundTripThriftSmallBenchmark.scala    From finagle-serial   with Apache License 2.0 5 votes vote down vote up
package io.github.finagle.serial.benchmark

import java.net.InetSocketAddress
import java.util.concurrent.TimeUnit

import com.twitter.finagle.{Client, Server, Service, ThriftMux}
import com.twitter.util.{Closable, Await, Future}
import org.openjdk.jmh.annotations._


@State(Scope.Thread)
class RoundTripThriftSmallBenchmark {
  private val smallSize = 20

  val small: thriftscala.Small =
    thriftscala.Small((for (i <- 1 to smallSize) yield i % 2 == 0).toList, "foo bar baz")

  val echo = new thriftscala.EchoService.FutureIface {
    def echo(small: thriftscala.Small) = Future.value(small)
  }

  var s: Closable = _
  var c: thriftscala.EchoService.FutureIface = _

  @Setup
  def setUp(): Unit = {
    s = ThriftMux.serveIface(new InetSocketAddress(8124), echo)
    c = ThriftMux.newIface[thriftscala.EchoService.FutureIface]("localhost:8124")
  }

  @TearDown
  def tearDown(): Unit = {
    Await.ready(s.close())
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def test: thriftscala.Small = Await.result(c.echo(small))
} 
Example 42
Source File: TwitterSpec.scala    From interop-twitter   with Apache License 2.0 5 votes vote down vote up
package zio.interop

import java.util.concurrent.atomic.AtomicInteger

import com.twitter.util.{ Await, Future, Promise }
import zio.{ Task, ZIO }
import zio.interop.twitter._
import zio.test._
import zio.test.Assertion._

import scala.util.{ Failure, Success, Try }

object TwitterSpec extends DefaultRunnableSpec {
  val runtime = runner.runtime

  override def spec =
    suite("TwitterSpec")(
      suite("Task.fromTwitterFuture")(
        testM("return failing `Task` if future failed.") {
          val error  = new Exception
          val future = Task(Future.exception[Int](error))
          val task   = Task.fromTwitterFuture(future).unit

          assertM(task.either)(isLeft(equalTo(error)))
        },
        testM("return successful `Task` if future succeeded.") {
          val value  = 10
          val future = Task(Future.value(value))
          val task   = Task.fromTwitterFuture(future).option

          assertM(task)(isSome(equalTo(value)))
        },
        testM("ensure future is interrupted together with task.") {
          val value = new AtomicInteger(0)

          val promise = new Promise[Unit] with Promise.InterruptHandler {
            override protected def onInterrupt(t: Throwable): Unit = setException(t)
          }

          val future = Task(promise.flatMap(_ => Future(value.incrementAndGet())))

          val task =
            (for {
              fiber <- Task.fromTwitterFuture(future).fork
              _     <- fiber.interrupt
              _     <- Task.effect(promise.setDone())
              a     <- fiber.await
            } yield a).fold(_ => false, exit => exit.toEither.isLeft)

          task.map(b => assert(b)(isTrue) && assert(value.get())(equalTo(0)))
        }
      ),
      suite("Runtime.unsafeRunToTwitterFuture")(
        test("return successful `Future` if Task evaluation succeeded.") {
          assert(Await.result(runtime.unsafeRunToTwitterFuture(Task.succeed(2))))(equalTo(2))
        },
        test("return failed `Future` if Task evaluation failed.") {
          val e      = new Throwable
          val task   = Task.fail(e).unit
          val result =
            Try(Await.result(runtime.unsafeRunToTwitterFuture(task))) match {
              case Failure(exception) => Some(exception)
              case Success(_)         => None
            }

          assert(result)(isSome(equalTo(e)))
        },
        testM("ensure Task evaluation is interrupted together with Future.") {
          val value                                  = new AtomicInteger(0)
          val ex                                     = new Exception
          val task: ZIO[Any, Throwable, Future[Int]] = for {
            promise <- zio.Promise.make[Throwable, Int]
            t        = promise.await.flatMap(_ => Task.effectTotal(value.incrementAndGet()))
            future   = runtime.unsafeRunToTwitterFuture(t)
            _        = future.raise(ex)
            _       <- promise.succeed(1)
          } yield future

          assertM(task.map(Await.result(_)).run)(isInterrupted).map(_ && assert(value.get)(equalTo(0)))
        }
      )
    )
} 
Example 43
Source File: WeatherApi.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.basic

import com.twitter.finagle._
import com.twitter.finagle.http.service.HttpResponseClassifier
import com.twitter.server.TwitterServer
import com.twitter.util.Await
import io.circe.generic.auto._
import io.finch._
import io.finch.circe._
import net.gutefrage.temperature.thrift._
import net.gutefrage.{Dtabs, Env}
import org.slf4j.LoggerFactory
import org.slf4j.bridge.SLF4JBridgeHandler


    case class Mean(mean: Double)

    // mean temperature endpoint
    val api: Endpoint[Mean] = get("weather" / "mean") {
      client.mean().map(mean => Ok(Mean(mean)))
    }

    // start and announce the server
    val server = Http.server
      .withLabel("weather-api")
      .withResponseClassifier(HttpResponseClassifier.ServerErrorsAsFailures)
      .serve(
        addr = s":${port()}",
        service = api.toService
      )

    closeOnExit(server)
    Await.ready(server)

  }

} 
Example 44
Source File: TemperatureSensorStatic.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.basic

import com.twitter.conversions.time._
import com.twitter.finagle.ThriftMux
import com.twitter.finagle.util.DefaultTimer
import com.twitter.server.TwitterServer
import com.twitter.util.logging.Logging
import com.twitter.util.{Await, Future}
import net.gutefrage.temperature.thrift._
import net.gutefrage.{Dtabs, Env}
import org.slf4j.bridge.SLF4JBridgeHandler


object TemperatureSensorStatic extends TwitterServer with Logging{
  val env = flag[Env]("env", Env.Local, "environment this server runs")

  premain {
    SLF4JBridgeHandler.removeHandlersForRootLogger()
    SLF4JBridgeHandler.install()

    // initialise custom dtabs
    Dtabs.init()
  }

  onExit {
    info("Shutting down sensor")
  }

  def main(): Unit = {
    // create a thrift client and resolve address via dtabs
    val client = ThriftMux.client.newIface[TemperatureService.FutureIface](
      // schema ! args
      dest = s"zk2!127.0.0.1:2181!/service/${env().name}/temperature",
      label = "temperature-sensor"
    )

    implicit val timer = DefaultTimer.twitter
    val randomTemp = new java.util.Random()

    def sendLoop: Future[Unit] = {
      val datum = TemperatureDatum(randomTemp.nextInt(40) - 10,
                                   System.currentTimeMillis / 1000)
      info(s"Sending data: $datum")
      for {
        _ <- Future.sleep(1.second)
        _ <- client.add(datum)
        _ <- sendLoop
      } yield ()
    }

    Await.ready(sendLoop)
  }

} 
Example 45
Source File: TemperatureServer.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.basic

import java.util.concurrent.atomic.AtomicLong

import com.twitter.finagle._
import com.twitter.finagle.thrift.Protocols
import com.twitter.logging.Logger
import com.twitter.server.TwitterServer
import com.twitter.util.logging.Logging
import com.twitter.util.{Await, Future}
import net.gutefrage.Env
import net.gutefrage.temperature.thrift._
import org.slf4j.bridge.SLF4JBridgeHandler



object TemperatureServer extends TwitterServer with Logging{

  val port = flag[Int]("port", 8080, "port this server should use")
  val env = flag[Env]("env", Env.Local, "environment this server runs")

  override def failfastOnFlagsNotParsed: Boolean = true

  premain {
    SLF4JBridgeHandler.removeHandlersForRootLogger()
    SLF4JBridgeHandler.install()
  }

  onExit {
    info("Shutting down temperature server")
  }

  val appLog = Logger("application")

  def main() {
    appLog.info(s"Starting temperature server in environment ${env().name} on port ${port()}")

    // the actual server implementation
    val service = new TemperatureService.FutureIface {

      val numElements = new AtomicLong()
      val sumTemperature = new AtomicLong()

      override def add(datum: TemperatureDatum): Future[Unit] = {
        numElements.incrementAndGet()
        sumTemperature.addAndGet(datum.celsius)
        Future.Unit
      }

      override def mean(): Future[Double] = {
        val n = numElements.get()
        val mean = if(n == 0L) 0.0 else sumTemperature.get() / n
        Future.value(mean)
      }
    }

    // Wrap the service implementation into a real finagle service
    val finagledService: Service[Array[Byte], Array[Byte]] = new TemperatureService.FinagledService(
      service, Protocols.binaryFactory()
    )

    // run and announce the service
    val server = ThriftMux.server
      .withLabel("temperature-service")
      .serveAndAnnounce(
        // schema ! host ! path ! shardId
        name = s"zk!127.0.0.1:2181!/service/${env().name}/temperature!0",
        // bind to local address
        addr = s":${port()}",
        service = finagledService
      )

    // Keep waiting for the server and prevent the java process to exit
    closeOnExit(server)
    Await.ready(server)
  }

} 
Example 46
Source File: TemperatureSensorDtabs.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.basic

import com.twitter.app.Flaggable
import com.twitter.conversions.time._
import com.twitter.finagle.{Dtab, ThriftMux}
import com.twitter.finagle.util.DefaultTimer
import com.twitter.logging.Logging
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
import net.gutefrage.temperature.thrift._
import net.gutefrage.{Dtabs, Env}
import org.slf4j.bridge.SLF4JBridgeHandler


  override def failfastOnFlagsNotParsed: Boolean = true

  premain {
    SLF4JBridgeHandler.removeHandlersForRootLogger()
    SLF4JBridgeHandler.install()

    // initialise custom dtabs
    Dtabs.init(baseDtab())
    info(
      s"""|Use base dtab:
          |${Dtab.base.show}
       """.stripMargin)
  }

  onExit {
    info("Shutting down sensor")
  }

  def main(): Unit = {
    // create a thrift client and resolve address via dtabs
    val client = ThriftMux.client.newIface[TemperatureService.FutureIface](
      dest = "/s/temperature",
      label = "temperature-sensor"
    )

    implicit val timer = DefaultTimer.twitter
    val randomTemp = new java.util.Random()

    def sendLoop: Future[Unit] = {
      val datum = TemperatureDatum(randomTemp.nextInt(40) - 10,
                                   System.currentTimeMillis / 1000)
      info(s"Sending data: $datum")
      for {
        _ <- Future.sleep(1.second)
        _ <- client.add(datum)
        _ <- sendLoop
      } yield ()
    }

    Await.ready(sendLoop)
  }

} 
Example 47
Source File: ScodecIntegrationTest.scala    From finagle-serial   with Apache License 2.0 5 votes vote down vote up
package io.github.finagle.serial.scodec

import _root_.scodec._
import _root_.scodec.codecs._
import com.twitter.util.Await
import io.github.finagle.serial.{ApplicationError, CodecError}
import io.github.finagle.serial.tests.SerialIntegrationTest
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.FunSuite

class ScodecIntegrationTest extends FunSuite with ScodecSerial with SerialIntegrationTest {
  implicit val intCodec: Codec[Int] = int32
  implicit val stringCodec: Codec[String] = variableSizeBits(uint24, utf8)

  case class Foo(i: Int, s: String)

  implicit val fooCodec: Codec[Foo] = (uint8 :: stringCodec).as[Foo]

  implicit val fooArbitrary: Arbitrary[Foo] = Arbitrary(
    for {
      i <- Gen.choose(0, 255)
      s <- Gen.alphaStr
    } yield Foo(i, s)
  )

  test("A service that doubles an integer should work on all integers") {
    testFunctionService[Int, Int](_ * 2)
  }

  test("A service that returns the length of a string should work on all strings") {
    testFunctionService[String, Int](s => s.length)
  }

  test("A service that changes a case class should work on all instances") {
    testFunctionService[Foo, Foo] {
      case Foo(i, s) => Foo(i % 128, s * 2)
    }
  }

  test("A service should correctly throw encoding errors on the client side") {
    val (server, client) = createServerAndClient[Foo, Int](_.i)

    an[CodecError] should be thrownBy Await.result(client(Foo(Int.MaxValue, "foo")))

    server.close()
  }

  test("A service should correctly throw encoding errors on the server side") {
    val (server, client) = createServerAndClient[Foo, Foo] {
      case Foo(i, s) => Foo(Int.MaxValue, s)
    }

    an[CodecError] should be thrownBy Await.result(client(Foo(1, "foo")))

    server.close()
  }

  test("A service should correctly throw handled application errors") {
    val (server, client) = createServerAndClient[String, Int](_.toInt)

    an[NumberFormatException] should be thrownBy Await.result(client("not an integer"))

    server.close()
  }

  case class UnknownError(message: String) extends Throwable

  test("A service should correctly wrap unhandled application errors") {
    val (server, client) = createServerAndClient[String, Int] { s =>
      throw UnknownError("something happened")
    }

    an[ApplicationError] should be thrownBy Await.result(client("not an integer"))

    server.close()
  }
} 
Example 48
Source File: TemperatureServer.scala    From the-finagle-docs   with MIT License 5 votes vote down vote up
package net.gutefrage.context

import java.util.concurrent.atomic.AtomicLong

import com.twitter.finagle._
import com.twitter.finagle.thrift.Protocols
import com.twitter.logging.{Logger, Logging}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
import net.gutefrage.Env
import net.gutefrage.temperature.thrift._
import org.slf4j.bridge.SLF4JBridgeHandler



object TemperatureServer extends TwitterServer with Logging{

  val port = flag[Int]("port", 8080, "port this server should use")
  val env = flag[Env]("env", Env.Local, "environment this server runs")

  override def failfastOnFlagsNotParsed: Boolean = true

  premain {
    SLF4JBridgeHandler.removeHandlersForRootLogger()
    SLF4JBridgeHandler.install()
  }

  onExit {
    info("Shutting down temperature server")
  }

  val appLog = Logger("application")

  def main() {
    appLog.info(s"Starting temperature server in environment ${env().name} on port ${port()}")

    // the actual server implementation
    val service = new TemperatureService.FutureIface {

      val numElements = new AtomicLong()
      val sumTemperature = new AtomicLong()

      override def add(datum: TemperatureDatum): Future[Unit] = {
        numElements.incrementAndGet()
        sumTemperature.addAndGet(datum.celsius)
        Future.Unit
      }

      override def mean(): Future[Double] = {
        // Accessing a given userContext if provided
        UserContext.current.foreach { userContext =>
          appLog.info(s"Call received with context: ${userContext}")
        }

        val n = numElements.get()
        val mean = if(n == 0L) 0.0 else sumTemperature.get() / n
        Future.value(mean)
      }
    }

    // Wrap the service implementation into a real finagle service
    val finagledService: Service[Array[Byte], Array[Byte]] = new TemperatureService.FinagledService(
      service, Protocols.binaryFactory()
    )

    // run and announce the service
    val server = ThriftMux.server
      .withLabel("temperature-service")
      .serveAndAnnounce(
        // schema ! host ! path ! shardId
        name = s"zk!127.0.0.1:2181!/service/${env.name}/temperature!0",
        // bind to local address
        addr = s":${port()}",
        service = finagledService
      )

    // Keep waiting for the server and prevent the java process to exit
    closeOnExit(server)
    Await.ready(server)
  }

} 
Example 49
Source File: MyServer.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package myapp.server

import wvlet.airframe.http.Router
import wvlet.airframe.http.finagle._
import wvlet.log.LogSupport
import myapp.spi.MyService
import myapp.spi.MyRPC._
import com.twitter.util.Await

class MyServiceImpl extends myapp.spi.MyService {
  override def hello(id: Int): String    = s"hello ${id}"
  override def books(limit: Int): String = s"${limit} books"
}

class MyRPCImpl extends myapp.spi.MyRPC {
  override def world()                                                                 = myapp.spi.MyRPC.World("world")
  override def addEntry(id: Int, name: String)                                         = s"${id}:${name}"
  override def createPage(createPageRequest: CreatePageRequest): String                = s"${0}:${createPageRequest}"
  override def createPageWithId(id: Int, createPageRequest: CreatePageRequest): String = s"${id}:${createPageRequest}"
}

object MyServer extends LogSupport {

  def main(args: Array[String]): Unit = {
    val router = Router.add[MyServiceImpl].add[MyRPCImpl]
    info(router)

    val d =
      newFinagleServerDesign(router = router)
        .add(finagleClientDesign)

    d.build[FinagleClient] { finagleClient =>
      val client = new myapp.spi.ServiceClient(finagleClient)
      val future = client.myService.hello(100).map { v =>
        logger.info(v)
        assert(v == "hello 100")
      }
      Await.result(future)

      val syncClient = new myapp.spi.ServiceSyncClient(finagleClient.syncClient)
      val ret        = syncClient.myService.hello(101)
      info(ret)
      assert(ret == "hello 101")

      val future2 = client.myService.books(10).map { v =>
        logger.info(v)
        assert(v == s"10 books")
      }
      Await.result(future2)

      val ret2 = syncClient.myRPC.world()
      info(ret2)
      assert(ret2 == myapp.spi.MyRPC.World("world"))

      val ret3 = syncClient.myRPC.addEntry(1234, "rpc")
      info(ret3)
      assert(ret3 == "1234:rpc")

      val ret4 = syncClient.myRPC.createPage(CreatePageRequest("hello"))
      info(ret4)
      assert(ret4 == """0:CreatePageRequest(hello)""")

      val ret5 = syncClient.myRPC.createPageWithId(10, CreatePageRequest("hello"))
      info(ret5)
      assert(ret5 == """10:CreatePageRequest(hello)""")
    }
  }
} 
Example 50
Source File: Main.scala    From finch-oauth2   with Apache License 2.0 5 votes vote down vote up
package io.finch.oauth2

import cats.effect.IO
import com.twitter.finagle._
import com.twitter.finagle.oauth2.{AuthInfo, GrantResult}
import com.twitter.util.Await
import io.circe.generic.auto._
import io.finch._
import io.finch.catsEffect._
import io.finch.circe._


object Main extends App {

  case class UnprotectedUser(name: String)

  def authInfo: Endpoint[IO, AuthInfo[OAuthUser]] = authorize[IO, OAuthUser](InMemoryDataHandler)
  def accessToken: Endpoint[IO, GrantResult] = issueAccessToken[IO, OAuthUser](InMemoryDataHandler)

  def users: Endpoint[IO, OAuthUser] = get("users" :: "current" :: authInfo) {
    ai: AuthInfo[OAuthUser] => Ok(ai.user)
  }

  def tokens: Endpoint[IO, GrantResult] = post("users" :: "auth" :: accessToken)

  def unprotected: Endpoint[IO, UnprotectedUser] = get("users" :: "unprotected") {
    Ok(UnprotectedUser("unprotected"))
  }

  Await.ready(
    Http.server
      .serve(":8081", (tokens :+: users :+: unprotected).toServiceAs[Application.Json])
  )
} 
Example 51
Source File: TwitterFuture.scala    From arrows   with Apache License 2.0 5 votes vote down vote up
package benchmarks

import com.twitter.util.Promise
import com.twitter.util.Future
import org.openjdk.jmh.annotations.Benchmark
import scala.util.Try

trait TwitterFuture {
  this: Benchmarks =>

  private[this] final val gen = TwitterFutureGen(dist)

  @Benchmark
  def twitterFuture = {
    import com.twitter.util.Await
    Try(Await.result(gen(1)))
  }
}

object TwitterFutureGen extends Gen[Int => Future[Int]] {

  def sync = Future.value _

  def async(schedule: Runnable => Unit) = {
    v =>
      val p = Promise.apply[Int]()
      schedule(() => p.setValue(v))
      p
  }

  def failure(ex: Throwable) = _ => Future.exception(ex)

  def map(t: Int => Future[Int], f: Int => Int) =
    t.andThen(_.map(f(_)))

  def flatMap(t: Int => Future[Int], f: Int => Future[Int]) =
    t.andThen(_.flatMap(f))

  def handle(t: Int => Future[Int], i: Int) =
    t.andThen(_.handle { case _ => i })
} 
Example 52
Source File: ArrowsTwitter.scala    From arrows   with Apache License 2.0 5 votes vote down vote up
package benchmarks

import com.twitter.util.Promise

import arrows.twitter.Arrow
import arrows.twitter.Task
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Benchmark
import com.twitter.util.Await
import scala.util.Try

trait ArrowsTwitter {
  this: Benchmarks =>

  private[this] final val arrowGen = ArrowsTwitterArrowGen(dist)
  private[this] final val taskGen = ArrowsTwitterTaskGen(dist)

  @Benchmark
  def arrowsTwitterArrow = {
    Try(Await.result(arrowGen.run(1)))
  }

  @Benchmark
  def arrowsTwitterTask = {
    Try(Await.result(taskGen(1).run))
  }
}

object ArrowsTwitterTaskGen extends Gen[Int => Task[Int]] {

  def sync = Task.value _

  def async(schedule: Runnable => Unit) = {
    v =>
      val p = new Promise[Int] with Runnable {
        override def run() = setValue(v)
      }
      schedule(p)
      Task.async(p)
  }

  def failure(ex: Throwable) = v => Task.exception(ex)

  def map(t: Int => Task[Int], f: Int => Int) =
    t.andThen(_.map(f))

  def flatMap(t: Int => Task[Int], f: Int => Task[Int]) =
    t.andThen(_.flatMap(f))

  def handle(t: Int => Task[Int], i: Int) =
    t.andThen(_.handle { case _ => i })
}

object ArrowsTwitterArrowGen extends Gen[Arrow[Int, Int]] {

  def sync = Arrow[Int]

  def async(schedule: Runnable => Unit) =
    Arrow[Int].flatMap { v =>
      val p = new Promise[Int] with Runnable {
        override def run() = setValue(v)
      }
      schedule(p)
      Task.async(p)
    }

  def failure(ex: Throwable) = Arrow.exception(ex)

  def map(t: Arrow[Int, Int], f: Int => Int) =
    t.map(f)

  def flatMap(t: Arrow[Int, Int], f: Arrow[Int, Int]) =
    t.flatMap(f)

  def handle(t: Arrow[Int, Int], i: Int) =
    t.handle { case _ => i }
} 
Example 53
Source File: ConnectionQuerySpec.scala    From finagle-postgres   with Apache License 2.0 5 votes vote down vote up
package com.twitter.finagle.postgres.connection

import com.twitter.concurrent.AsyncStream
import com.twitter.finagle.postgres.{Row, Spec}
import com.twitter.finagle.postgres.messages._
import com.twitter.finagle.postgres.values.Charsets
import com.twitter.util.Await
import io.netty.buffer.Unpooled

class ConnectionQuerySpec extends Spec {
  "A postgres connection" should {
    def toList[T](rows: AsyncStream[T]): List[T] = Await.result(rows.toSeq).toList
    "handle an empty query response" in {
      val connection = new Connection(Connected)

      connection.send(Query(""))
      connection.receive(EmptyQueryResponse)
      val r@SelectResult(fields, rows) = connection.receive(ReadyForQuery('I')).get

      fields.length mustEqual 0
      r.complete.isDone mustBe true
      toList(rows) mustEqual List()
    }

    "handle a create table query" in {
      val connection = new Connection(Connected)

      connection.send(Query("create table ids"))
      connection.receive(CommandComplete(CreateTable))
      val response = connection.receive(ReadyForQuery('I'))

      response must equal(Some(CommandCompleteResponse(1)))
    }

    "handle a delete query" in {
      val connection = new Connection(Connected)

      connection.send(Query("delete from ids"))
      connection.receive(CommandComplete(Delete(2)))
      val response = connection.receive(ReadyForQuery('I'))

      response must equal(Some(CommandCompleteResponse(2)))

    }

    "handle an insert query" in {
      val connection = new Connection(Connected)

      connection.send(Query("insert into ids values (1)"))
      connection.receive(CommandComplete(Insert(1)))
      val response = connection.receive(ReadyForQuery('I'))

      response must equal(Some(CommandCompleteResponse(1)))

    }

    "handle an update query" in {
      val connection = new Connection(Connected)

      connection.send(Query("update ids set id = 2 where id = 1"))
      connection.receive(CommandComplete(Update(1)))
      val response = connection.receive(ReadyForQuery('I'))

      response must equal(Some(CommandCompleteResponse(1)))
    }

    "handle an empty select query" in {
      val connection = new Connection(Connected)

      connection.send(Query("select * from emails"))
      val r@SelectResult(fields, rows) = connection.receive(RowDescription(Array(FieldDescription("email",16728,2,1043,-1,-1,0)))).get
      connection.receive(CommandComplete(Select(0)))
      connection.receive(ReadyForQuery('I'))

      assert(fields sameElements Array(Field("email", 0, 1043)))
      r.complete.isDone mustBe true
      toList(rows) must equal (List())
    }

    "handle a select query" in {
      val connection = new Connection(Connected)

      val row1 = DataRow(Array(Some(Unpooled.copiedBuffer("[email protected]".getBytes(Charsets.Utf8)))))
      val row2 = DataRow(Array(Some(Unpooled.copiedBuffer("[email protected]".getBytes(Charsets.Utf8)))))

      connection.send(Query("select * from emails"))
      val r@SelectResult(fields, rows) = connection.receive(RowDescription(Array(FieldDescription("email",16728,2,1043,-1,-1,0)))).get

      connection.receive(row1)
      connection.receive(row2)
      connection.receive(CommandComplete(Select(2)))
      connection.receive(ReadyForQuery('I'))

      fields must contain theSameElementsAs Array(Field("email", 0, 1043))
      r.complete.isDone mustBe true
      toList(rows) must equal (List(row1, row2))
    }
  }
} 
Example 54
Source File: TransactionSpec.scala    From finagle-postgres   with Apache License 2.0 5 votes vote down vote up
package com.twitter.finagle.postgres.integration

import com.twitter.finagle.Postgres
import com.twitter.finagle.postgres.Spec
import com.twitter.util.{Await, Future}

class TransactionSpec extends Spec {
  for {
    hostPort <- sys.env.get("PG_HOST_PORT")
    user <- sys.env.get("PG_USER")
    password = sys.env.get("PG_PASSWORD")
    dbname <- sys.env.get("PG_DBNAME")
    useSsl = sys.env.getOrElse("USE_PG_SSL", "0") == "1"
  } yield {

    val client = Postgres.Client()
      .withCredentials(user, password)
      .database(dbname)
      .conditionally(useSsl, _.withTransport.tlsWithoutValidation)
      .newRichClient(hostPort)

    Await.result(client.query(
      """
        |DROP TABLE IF EXISTS transaction_test;
        |CREATE TABLE transaction_test(id integer primary key);
      """.stripMargin))

    "A postgres transaction" should {

      "commit if the transaction future is successful" in {
        Await.result {
          client.inTransaction {
            c => for {
              _ <- c.prepareAndExecute("DELETE FROM transaction_test")
              _ <- c.prepareAndExecute("INSERT INTO transaction_test VALUES(1)")
              _ <- c.prepareAndExecute("INSERT INTO transaction_test VALUES(2)")
            } yield ()
          }
        }
        val count = Await.result(client.prepareAndQuery("SELECT COUNT(*)::int4 AS count FROM transaction_test WHERE id IN (1,2)") {
          row => row.get[Int]("count")
        }.map(_.head))
        assert(count == 2)
      }

      "rollback the transaction if the transaction future fails" in {
        val failed = client.inTransaction {
          c => for {
            _ <- c.prepareAndExecute("DELETE FROM transaction_test")
            _ <- c.prepareAndExecute("INSERT INTO transaction_test VALUES(3)")
            _ <- c.prepareAndExecute("INSERT INTO transaction_test VALUES(4)")
            _ <- Future.exception(new Exception("Roll it back!"))
            _ <- c.prepareAndExecute("INSERT INTO transaction_test VALUES(5)")
          } yield ()
        }.liftToTry

        val failedResult = Await.result(failed)

        val inTable = Await.result(client.prepareAndQuery("SELECT * FROM transaction_test") {
          row => row.get[Int]("id")
        }).toList.sorted
        assert(inTable == List(1, 2))
      }

    }
  }
} 
Example 55
Source File: NumericSpec.scala    From finagle-postgres   with Apache License 2.0 5 votes vote down vote up
package com.twitter.finagle.postgres.integration

import com.twitter.finagle.Postgres
import com.twitter.finagle.postgres._
import com.twitter.util.{Await, Future}
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

class NumericSpec extends Spec with ScalaCheckDrivenPropertyChecks {


  for {
    hostPort <- sys.env.get("PG_HOST_PORT")
    user <- sys.env.get("PG_USER")
    password = sys.env.get("PG_PASSWORD")
    dbname <- sys.env.get("PG_DBNAME")
    useSsl = sys.env.getOrElse("USE_PG_SSL", "0") == "1"
  } yield {

    val binaryClient = Postgres.Client()
      .database(dbname)
      .withCredentials(user, password)
      .withBinaryParams(true)
      .withBinaryResults(true)
      .newRichClient(hostPort)

    val textClient = Postgres.Client()
      .database(dbname)
      .withCredentials(user, password)
      .withBinaryParams(false)
      .withBinaryResults(false)
      .newRichClient(hostPort)

    Await.result((textClient.query(
      """
        |DROP TABLE IF EXISTS numeric_test;
        |CREATE TABLE numeric_test(d DECIMAL NOT NULL);
      """.stripMargin)))

    def rowsOf(qr: QueryResponse): Future[Seq[Row]] = qr match {
      case OK(_) => Future.value(Seq.empty)
      case ResultSet(rs) => rs.toSeq
    }

    def testBinaryEncode(in: BigDecimal) = Await.result {
      for {
        _ <- binaryClient.execute("DELETE FROM numeric_test")
        _ <- binaryClient.prepareAndExecute("INSERT INTO numeric_test VALUES($1)", Param(in))
        r <- textClient.query("SELECT * FROM numeric_test")
        rows <- rowsOf(r)
      } yield rows.map(_.get[BigDecimal](0)) must equal(Seq(in))
    }

    def testBinaryDecode(in: BigDecimal) = Await.result {
      for {
        _ <- textClient.execute("DELETE FROM numeric_test")
        _ <- textClient.prepareAndExecute("INSERT INTO numeric_test VALUES($1)", Param(in))
        r <- binaryClient.query("SELECT * FROM numeric_test")
        rows <- rowsOf(r)
      } yield rows.map(_.get[BigDecimal](0)) must equal(Seq(in))
    }

    "Binary client" should {
      "encode decimal agree with text client" in forAll { in: BigDecimal =>
        testBinaryEncode(in)
      }
      "decode decimal agree with text client" in forAll { in: BigDecimal =>
        testBinaryDecode(in)
      }
    }

  }
} 
Example 56
Source File: HttpMonitoringFilterSpec.scala    From finagle-prometheus   with MIT License 5 votes vote down vote up
package com.samstarling.prometheusfinagle.filter

import com.samstarling.prometheusfinagle.UnitTest
import com.samstarling.prometheusfinagle.helper.CollectorHelper
import com.samstarling.prometheusfinagle.metrics.Telemetry
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Method, Request, Response, Status}
import com.twitter.util.{Await, Future}
import io.prometheus.client.CollectorRegistry
import org.specs2.specification.Scope

import scala.collection.JavaConverters._

class HttpMonitoringFilterSpec extends UnitTest {

  trait Context extends Scope {
    val registry = new CollectorRegistry(true)
    val telemetry = new Telemetry(registry, "test")
    val labeller = new TestLabeller
    val filter = new HttpMonitoringFilter(telemetry, labeller)
    val service = mock[Service[Request, Response]]
    val request = Request(Method.Get, "/foo/bar")
    val serviceResponse = Response(Status.Created)
    val counter = telemetry.counter(name = "incoming_http_requests_total")
    service.apply(request) returns Future.value(serviceResponse)
  }

  "HttpMonitoringFilter" >> {
    "passes requests on to the next service" in new Context {
      Await.result(filter.apply(request, service))
      there was one(service).apply(request)
    }

    "returns the Response from the next service" in new Context {
      val actualResponse = Await.result(filter.apply(request, service))
      actualResponse ==== serviceResponse
    }

    "increments the incoming_http_requests_total counter" in new Context {
      Await.result(filter.apply(request, service))
      Await.result(filter.apply(request, service))
      CollectorHelper.firstSampleFor(counter).map { sample =>
        sample.value ==== 2.0
      }
    }

    "adds the correct help label" in new Context {
      Await.result(filter.apply(request, service))
      CollectorHelper.firstMetricFor(counter).map { metric =>
        metric.help ==== "The number of incoming HTTP requests"
      }
    }

    "increments the counter with the labels from the labeller" in new Context {
      Await.result(filter.apply(request, service))
      CollectorHelper.firstSampleFor(counter).map { sample =>
        sample.labelNames.asScala(0) ==== "foo"
        sample.labelValues.asScala(0) ==== "bar"
      }
    }
  }
} 
Example 57
Source File: ContractProxyModuleTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect

import com.twitter.finagle.Service
import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.Request
import com.twitter.util.Await
import io.fintrospect.formats.PlainText.ResponseBuilder._
import io.fintrospect.parameters.Query
import org.scalatest.{FunSpec, Matchers}

object TestContract extends Contract {

  object Endpoint extends ContractEndpoint {
    val query = Query.required.string("query")
    override val route = RouteSpec().taking(query).at(Get) / "hello"
  }

}

class ContractProxyModuleTest extends FunSpec with Matchers {

  describe("ContractProxyModule") {
    it("cretes service which proxies requests to the underlying service") {
      val svc = Service.mk { req: Request => Ok(TestContract.Endpoint.query <-- req) }
      Await.result(ContractProxyModule("remote", svc, TestContract).toService(Request("/hello?query=value"))).contentString shouldBe "value"
    }
  }
} 
Example 58
Source File: InboxApp.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.circe

import com.twitter.finagle.Http
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.path.Root
import com.twitter.util.Await
import examples.circe.Emails.InMemoryEmails
import io.fintrospect.RouteModule
import io.fintrospect.renderers.swagger2dot0.{ApiInfo, Swagger2dot0Json}


object InboxApp extends App {

  private val emails = new InMemoryEmails()

  private val inboxModule = RouteModule(Root / "inbox", Swagger2dot0Json(ApiInfo("Inbox Example", "1.0")))
    .withRoute(new AddMessage(emails).route)
    .withRoute(new EmailList(emails).route)
    .withRoute(new FindUserWithEmail(emails).route)
    .withRoute(new UserList(emails).route)

  println("See the service description at: http://localhost:8181/inbox")

  Await.ready(
    Http.serve(":8181", new HttpFilter(Cors.UnsafePermissivePolicy).andThen(inboxModule.toService))
  )
} 
Example 59
Source File: LibraryApp.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.extended

import com.twitter.finagle.Http
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.path.Root
import com.twitter.util.Await
import io.fintrospect.RouteModule
import io.fintrospect.renderers.simplejson.SimpleJson
import io.fintrospect.renderers.swagger2dot0.{ApiInfo, Swagger2dot0Json}


object LibraryApp extends App {

  val apiInfo = ApiInfo("Library Example", "1.0", "A simple example of how to construct a Fintrospect module")
  val renderer = Swagger2dot0Json(apiInfo) //  choose your renderer implementation

  val books = new Books()

  // use CORs settings that suit your particular use-case. This one allows any cross-domain traffic at all and is applied
  // to all routes in the module
  val globalCorsFilter = new HttpFilter(Cors.UnsafePermissivePolicy)

  val libraryModule = RouteModule(Root / "library", renderer)
    .withRoute(new BookAdd(books).route)
    .withRoute(new BookCollection(books).route)
    .withRoute(new BookLookup(books).route)
    .withRoute(new BookLengthSearch(books).route)
    .withRoute(new BookTermSearch(books).route)

  val statusModule = RouteModule(Root / "internal", SimpleJson())
    .withRoute(new Ping().route)

  println("See the service description at: http://localhost:8080/library")

  Await.ready(
    Http.serve(":8080", globalCorsFilter.andThen(libraryModule.andThen(statusModule).toService))
  )
} 
Example 60
Source File: ClientSideAndSharedRouteSpecExample.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.clients

import java.time.LocalDate

import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.fintrospect.RouteSpec
import io.fintrospect.formats.PlainText.ResponseBuilder._
import io.fintrospect.parameters._
import io.fintrospect.testing.TestHttpServer
import io.fintrospect.util.HttpRequestResponseUtil.{headersFrom, statusAndContentFrom}


object ClientSideAndSharedRouteSpecExample extends App {

  val theDate = Path.localDate("date")
  val theWeather = Query.optional.string("weather")
  val theUser = Header.required.string("user")
  val gender = FormField.optional.string("gender")
  val body = Body.form(gender)

  val sharedRouteSpec = RouteSpec()
    .taking(theUser)
    .taking(theWeather)
    .body(body)
    .at(Get) / "firstSection" / theDate

  val fakeServerRoute = sharedRouteSpec bindTo (dateFromPath => Service.mk[Request, Response] {
    request: Request => {
      println("URL was " + request.uri)
      println("Headers were " + headersFrom(request))
      println("Form sent was " + (body <-- request))
      println("Date send was " + dateFromPath.toString)
      Ok(dateFromPath.toString)
    }
  })

  Await.result(new TestHttpServer(10000, fakeServerRoute).start())

  val client = sharedRouteSpec bindToClient Http.newService("localhost:10000")

  val theCall = client(theWeather --> Option("sunny"), body --> Form(gender --> "male"), theDate --> LocalDate.of(2015, 1, 1), theUser --> System.getenv("USER"))

  val response = Await.result(theCall)

  println("Response headers: " + headersFrom(response))
  println("Response: " + statusAndContentFrom(response))
} 
Example 61
Source File: ContractProxyExample.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.clients

import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.fintrospect.configuration.{Credentials, Host, Port}
import io.fintrospect.filters.RequestFilters.{AddHost, BasicAuthorization}
import io.fintrospect.parameters.Query
import io.fintrospect.{Contract, ContractEndpoint, ContractProxyModule, RouteSpec}


object ContractProxyExample extends App {

  val proxyModule = ContractProxyModule("brewdog", BrewdogApiHttp(), BrewdogApiContract)

  Await.ready(
    Http.serve(":9000", new HttpFilter(Cors.UnsafePermissivePolicy).andThen(proxyModule.toService))
  )
}

object BrewdogApiHttp {
  private val apiAuthority = Host("punkapi.com").toAuthority(Port(443))

  def apply(): Service[Request, Response] = {
    AddHost(apiAuthority)
      .andThen(BasicAuthorization(Credentials("22244d6b88574064bbbfe284f1631eaf", "")))
      .andThen(Http.client.withTlsWithoutValidation.newService(apiAuthority.toString))
  }
}

object BrewdogApiContract extends Contract {

  object LookupBeers extends ContractEndpoint {
    val brewedBefore = Query.optional.string("brewed_before", "e.g. 01-2010 (format is mm-yyyy)")
    val alcoholContent = Query.optional.int("abv_gt", "Minimum alcohol %")

    override val route =
      RouteSpec("lookup beers")
        .taking(brewedBefore)
        .taking(alcoholContent)
        .at(Get) / "api" / "v1" / "beers"
  }

  object RandomBeer extends ContractEndpoint {
    override val route = RouteSpec("get a random beer recipe")
      .at(Get) / "api" / "v1" / "beers" / "random"
  }

} 
Example 62
Source File: MultiBodyTypeRoute.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.strictcontenttypes

import com.twitter.finagle.http.Method.Post
import com.twitter.finagle.http.Request
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.path.Root
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.fintrospect.parameters.Body
import io.fintrospect.renderers.simplejson.SimpleJson
import io.fintrospect.util.MultiBodyType
import io.fintrospect.{RouteModule, RouteSpec}


object MultiBodyTypeRoute extends App {

  private val json = Body.json("json body")

  private val echoJson = Service.mk { (rq: Request) =>
    import io.fintrospect.formats.Argo.ResponseBuilder._
    Ok(json <-- rq)
  }

  private val xml = Body.xml("xml body")

  private val echoXml = Service.mk { (rq: Request) =>
    import io.fintrospect.formats.Xml.ResponseBuilder._
    Ok(xml <-- rq)
  }

  val route = RouteSpec("echo posted content in either JSON or XML").at(Post) / "echo" bindTo MultiBodyType(json -> echoJson, xml -> echoXml)

  println("See the service description at: http://localhost:8080")

  Await.ready(
    Http.serve(":8080", new HttpFilter(Cors.UnsafePermissivePolicy)
      .andThen(RouteModule(Root, SimpleJson()).withRoute(route).toService))
  )
} 
Example 63
Source File: StrictMultiContentTypeRoute.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.strictcontenttypes

import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.Request
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.path.Root
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.fintrospect.ContentTypes.{APPLICATION_JSON, APPLICATION_XML}
import io.fintrospect.filters.RequestFilters
import io.fintrospect.parameters.Path
import io.fintrospect.renderers.simplejson.SimpleJson
import io.fintrospect.util.StrictContentTypeNegotiation
import io.fintrospect.{RouteModule, RouteSpec}



object StrictMultiContentTypeRoute extends App {

  private def serveJson(name: String) = Service.mk { req: Request =>
    import io.fintrospect.formats.Argo.JsonFormat._
    import io.fintrospect.formats.Argo.ResponseBuilder._
    Ok(obj("field" -> string(name)))
  }

  private def serveXml(name: String) = Service.mk {
    import io.fintrospect.formats.Xml.ResponseBuilder._
    req: Request =>
      Ok(<root>
        <field>
          {name}
        </field>
      </root>)
  }

  val route = RouteSpec()
    .producing(APPLICATION_XML, APPLICATION_JSON)
    .at(Get) / "multi" / Path.string("name") bindTo StrictContentTypeNegotiation(APPLICATION_XML -> serveXml, APPLICATION_JSON -> serveJson)

  val jsonOnlyRoute = RouteSpec()
    .producing(APPLICATION_JSON)
    .at(Get) / "json" / Path.string("name") bindTo ((s) => RequestFilters.StrictAccept(APPLICATION_JSON).andThen(serveJson(s)))

  println("See the service description at: http://localhost:8080 . The route at /multi should match wildcard Accept headers set in a browser.")

  Await.ready(
    Http.serve(":8080", new HttpFilter(Cors.UnsafePermissivePolicy)
      .andThen(RouteModule(Root, SimpleJson()).withRoute(route).toService))
  )
} 
Example 64
Source File: TemplatingApp.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package examples.templating

import java.net.URL

import com.twitter.finagle.http.Method.Get
import com.twitter.finagle.http.Request
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.filter.Cors.HttpFilter
import com.twitter.finagle.http.path.Root
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.fintrospect.formats.PlainText
import io.fintrospect.renderers.SiteMapModuleRenderer
import io.fintrospect.templating.{MustacheTemplates, RenderView}
import io.fintrospect.{RouteModule, RouteSpec}

object TemplatingApp extends App {

  val devMode = true
  val renderer = if (devMode) MustacheTemplates.HotReload("src/main/resources") else MustacheTemplates.CachingClasspath(".")

  val renderView = new RenderView(PlainText.ResponseBuilder, renderer)

  val module = RouteModule(Root, new SiteMapModuleRenderer(new URL("http://my.cool.app")), renderView)
    .withRoute(RouteSpec().at(Get) / "echo" bindTo Service.mk { rq: Request => MustacheView(rq.uri) })

  println("See the Sitemap description at: http://localhost:8181")

  Await.ready(
    Http.serve(":8181", new HttpFilter(Cors.UnsafePermissivePolicy).andThen(module.toService))
  )
} 
Example 65
Source File: ArgoJsonModuleRendererTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.renderers

import com.twitter.finagle.Service
import com.twitter.finagle.http.Method.{Get, Post}
import com.twitter.finagle.http.path.Root
import com.twitter.finagle.http.{Request, Status}
import com.twitter.util.{Await, Future}
import io.fintrospect.ContentTypes.{APPLICATION_ATOM_XML, APPLICATION_JSON, APPLICATION_SVG_XML}
import io.fintrospect._
import io.fintrospect.formats.Argo
import io.fintrospect.formats.Argo.JsonFormat.{number, obj, parse}
import io.fintrospect.parameters._
import io.fintrospect.util.HttpRequestResponseUtil.statusAndContentFrom
import io.fintrospect.util.{Echo, ExtractionError}
import org.scalatest.{FunSpec, Matchers}

import scala.io.Source

abstract class ArgoJsonModuleRendererTest() extends FunSpec with Matchers {
  def name: String = this.getClass.getSimpleName

  def renderer: ModuleRenderer

  describe(name) {
    it("renders as expected") {

      val customBody = Body.json("the body of the message", obj("anObject" -> obj("notAStringField" -> number(123))))

      val module = RouteModule(Root / "basepath", renderer)
        .securedBy(ApiKey(Header.required.string("the_api_key"), Service.const(Future(true))))
        .withRoute(
          RouteSpec("summary of this route", "some rambling description of what this thing actually does")
            .producing(APPLICATION_JSON)
            .taking(Header.optional.string("header", "description of the header"))
            .returning(ResponseSpec.json(Status.Ok -> "peachy", obj("anAnotherObject" -> obj("aNumberField" -> number(123)))))
            .returning(ResponseSpec.json(Status.Accepted -> "peachy", obj("anAnotherObject" -> obj("aNumberField" -> number(123)))))
            .returning(Status.Forbidden -> "no way jose")
            .taggedWith("tag3")
            .taggedWith("tag1")
            .at(Get) / "echo" / Path.string("message") bindTo ((s: String) => Echo(s)))
        .withRoute(
          RouteSpec("a post endpoint")
            .consuming(APPLICATION_ATOM_XML, APPLICATION_SVG_XML)
            .producing(APPLICATION_JSON)
            .returning(ResponseSpec.json(Status.Forbidden -> "no way jose", obj("aString" -> Argo.JsonFormat.string("a message of some kind"))))
            .taking(Query.required.int("query"))
            .body(customBody)
            .taggedWith("tag1")
            .taggedWith(TagInfo("tag2", "description of tag"), TagInfo("tag2", "description of tag"))
            .at(Post) / "echo" / Path.string("message") bindTo ((s: String) => Echo(s)))
        .withRoute(
          RouteSpec("a friendly endpoint")
            .taking(Query.required.boolean("query", "description of the query"))
            .body(Body.form(FormField.required.int("form", "description of the form")))
            .at(Get) / "welcome" / Path.string("firstName") / "bertrand" / Path.string("secondName") bindTo ((x: String, y: String, z: String) => Echo(x, y, z)))

      val expected = parse(Source.fromInputStream(this.getClass.getResourceAsStream(s"$name.json")).mkString)

      val actual = Await.result(module.toService(Request("/basepath"))).contentString
//      println(Argo.JsonFormat.pretty(parse(actual)))
      parse(actual) shouldBe expected
    }

    it("can build 400") {
      val response = statusAndContentFrom(renderer.badRequest(Seq(ExtractionError(Query.required.string("bob"), "missing"))))
      response._1 shouldBe Status.BadRequest
      parse(response._2).getStringValue("message") shouldBe "Missing/invalid parameters"
    }

    it("can build 404") {
      val response = statusAndContentFrom(renderer.notFound(Request()))
      response._1 shouldBe Status.NotFound
      parse(response._2).getStringValue("message") shouldBe "No route found on this path. Have you used the correct HTTP verb?"
    }

  }
} 
Example 66
Source File: HttpLatencyMonitoringFilterSpec.scala    From finagle-prometheus   with MIT License 5 votes vote down vote up
package com.samstarling.prometheusfinagle.filter

import com.samstarling.prometheusfinagle.UnitTest
import com.samstarling.prometheusfinagle.helper.{CollectorHelper, CollectorRegistryHelper}
import com.samstarling.prometheusfinagle.metrics.Telemetry
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Method, Request, Response, Status}
import com.twitter.finagle.util.DefaultTimer
import com.twitter.util.{Await, Duration, Future, Timer}
import io.prometheus.client.CollectorRegistry
import org.specs2.specification.Scope

class HttpLatencyMonitoringFilterSpec extends UnitTest {

  class SlowService extends Service[Request, Response] {
    implicit val timer = DefaultTimer.twitter

    override def apply(request: Request): Future[Response] = {
      Future
        .value(Response(request.version, Status.Ok))
        .delayed(Duration.fromMilliseconds(1500))
    }
  }

  trait Context extends Scope {
    val registry = new CollectorRegistry(true)
    val registryHelper = CollectorRegistryHelper(registry)
    val telemetry = new Telemetry(registry, "test")
    val buckets = Seq(1.0, 2.0)
    val labeller = new TestLabeller
    val filter = new HttpLatencyMonitoringFilter(telemetry, buckets, labeller)
    val service = mock[Service[Request, Response]]
    val slowService = new SlowService
    val request = Request(Method.Get, "/foo/bar")
    val serviceResponse = Response(Status.Created)
    val histogram =
      telemetry.histogram(name = "incoming_http_request_latency_seconds")
    service.apply(request) returns Future.value(serviceResponse)
  }

  "HttpLatencyMonitoringFilter" >> {
    "passes requests on to the next service" in new Context {
      Await.result(filter.apply(request, service))
      there was one(service).apply(request)
    }

    "returns the Response from the next service" in new Context {
      val actualResponse = Await.result(filter.apply(request, service))
      actualResponse ==== serviceResponse
    }

    "counts the request" in new Context {
      Await.result(filter.apply(request, slowService))

      registryHelper.samples
        .get("test_incoming_http_request_latency_seconds_count")
        .map(_.map(_.value).sum) must beSome(1.0).eventually
    }

    "increments the counter with the labels from the labeller" in new Context {
      Await.result(filter.apply(request, service))

      registryHelper.samples
        .get("test_incoming_http_request_latency_seconds_count")
        .map(_.head.dimensions.get("foo").get) must beSome("bar").eventually
    }

    "categorises requests into the correct bucket" in new Context {
      Await.result(filter.apply(request, slowService))

      // Our request takes ~1500ms, so it should NOT fall into the "less than or equal to 1 second" bucket (le=0.5)
      registryHelper.samples
        .get("test_incoming_http_request_latency_seconds_bucket")
        .flatMap(_.find(_.dimensions.get("le").contains("1.0")))
        .map(_.value) must beSome(0.0).eventually

      // However, it should fall into the "less than or equal to 2 seconds" bucket (le=0.5)
      registryHelper.samples
        .get("test_incoming_http_request_latency_seconds_bucket")
        .flatMap(_.find(_.dimensions.get("le").contains("2.0")))
        .map(_.value) must beSome(1.0).eventually

      // It should also fall into the "+Inf" bucket
      registryHelper.samples
        .get("test_incoming_http_request_latency_seconds_bucket")
        .flatMap(_.find(_.dimensions.get("le").contains("+Inf")))
        .map(_.value) must beSome(1.0).eventually
    }
  }
} 
Example 67
Source File: MetricsServiceSpec.scala    From finagle-prometheus   with MIT License 5 votes vote down vote up
package com.samstarling.prometheusfinagle.metrics

import com.samstarling.prometheusfinagle.UnitTest
import com.twitter.finagle.http.{Method, Request}
import com.twitter.util.Await
import io.prometheus.client.CollectorRegistry
import org.specs2.specification.Scope

class MetricsServiceSpec extends UnitTest {

  trait Context extends Scope {
    val registry = new CollectorRegistry(true)
    val telemetry = new Telemetry(registry, "unit_test")
    val service = new MetricsService(registry)
  }

  "it renders metrics correctly" in new Context {
    telemetry.counter("foo").inc()
    val request = Request(Method.Get, "/")
    val response = Await.result(service.apply(request))

    response.getContentString.trim ====
      "# HELP unit_test_foo No help provided\n" +
        "# TYPE unit_test_foo counter\n" +
        "unit_test_foo 1.0"
  }
} 
Example 68
Source File: TodoListApp.scala    From freestyle   with Apache License 2.0 5 votes vote down vote up
package examples.todolist

import cats._
import cats.effect.IO
import cats.implicits._
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, ListeningServer, Service}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
import doobie.util.transactor.Transactor
import examples.todolist.http.Api
import examples.todolist.persistence.Persistence
import examples.todolist.services.Services
import freestyle.tagless.config.ConfigM
import freestyle.tagless.config.implicits._
import freestyle.tagless.effects.error.ErrorM
import freestyle.tagless.effects.error.implicits._
import freestyle.tagless.logging.LoggingM
import freestyle.tagless.loggingJVM.log4s.implicits._
import freestyle.tagless.module
import io.circe.generic.auto._
import io.finch.circe._

@module
trait App[F[_]] {
  val persistence: Persistence[F]
  val services: Services[F]
}

object TodoListApp extends TwitterServer {

  import examples.todolist.runtime.implicits._

  def bootstrap[F[_]: Monad](
      implicit app: App[F],
      handler: F ~> Future,
      T: Transactor[F],
      api: Api[F]): F[ListeningServer] = {

    val service: Service[Request, Response] = api.endpoints.toService
    val log: LoggingM[F]                    = app.services.log
    val cfg: ConfigM[F]                     = app.services.config

    for {
      _      <- log.info("Trying to load application.conf")
      config <- cfg.load
      host = config.string("http.host").getOrElse("localhost")
      port = config.int("http.port").getOrElse("8080")
      _ <- log.debug(s"Host: $host")
      _ <- log.debug(s"Port $port")
    } yield
      Await.ready(
        Http.server.withAdmissionControl
          .concurrencyLimit(maxConcurrentRequests = 10, maxWaiters = 10)
          .serve(s"$host:$port", service)
      )
  }

  def main() =
    bootstrap[IO].unsafeRunAsync {
      case Left(error)   => println(s"Error executing server. ${error.getMessage}")
      case Right(server) => server.close()
    }

} 
Example 69
Source File: RerunnableTimerSuite.scala    From catbird   with Apache License 2.0 5 votes vote down vote up
package io.catbird.util.effect

import cats.effect.Timer
import org.scalatest.Outcome
import org.scalatest.funsuite.FixtureAnyFunSuite
import com.twitter.util
import com.twitter.util.{ Await, Future }
import io.catbird.util.Rerunnable

import scala.concurrent.duration._

class RerunnableTimerSuite extends FixtureAnyFunSuite {

  protected final class FixtureParam {
    val twitterTimer: util.Timer = new util.JavaTimer()
  }

  test("A timer can be used to delay execution") { f =>
    implicit val timer: Timer[Rerunnable] = RerunnableTimer(f.twitterTimer)

    val result = Await.result(
      Future.selectIndex(
        Vector(
          for {
            _ <- Timer[Rerunnable].sleep(100.milliseconds).run
            r <- Future.value("slow")
          } yield r,
          Future.value("fast").delayed(util.Duration.fromMilliseconds(50))(f.twitterTimer)
        )
      )
    )

    assert(result == 1) // The first future is delayed for longer, so we expect the second one to win
  }

  override protected def withFixture(test: OneArgTest): Outcome = withFixture(test.toNoArgTest(new FixtureParam))
} 
Example 70
Source File: RerunnableContextShiftSuite.scala    From catbird   with Apache License 2.0 5 votes vote down vote up
package io.catbird.util.effect

import cats.effect.{ ContextShift, IO, Sync }
import com.twitter.util.{ Await, Future, FuturePool }
import io.catbird.util.Rerunnable
import org.scalatest.Outcome
import org.scalatest.funsuite.FixtureAnyFunSuite

class RerunnableContextShiftSuite extends FixtureAnyFunSuite with ThreadPoolNamingSupport {

  protected final class FixtureParam {
    val futurePoolName = "future-pool"
    val otherPoolName = "other-pool"
    val ioPoolName = "io-pool"

    val futurePool = FuturePool.interruptible(newNamedThreadPool(futurePoolName))
    val otherPool = newNamedThreadPool(otherPoolName)
    val ioPool = newNamedThreadPool(ioPoolName)

    def newIO: IO[String] = IO(currentThreadName())

    def newFuture: Future[String] = futurePool(currentThreadName())

    def newRerunnable: Rerunnable[String] = Rerunnable(currentThreadName())
  }

  test("ContextShift[Rerunnable].shift shifts to the pool of the instance") { f =>
    implicit val cs: ContextShift[Rerunnable] =
      RerunnableContextShift.fromExecutionContext(f.ioPool)

    val (poolName1, poolName2, poolName3) =
      (for {
        poolName1 <- Rerunnable.fromFuture(f.newFuture)

        _ <- ContextShift[Rerunnable](cs).shift

        poolName2 <- Sync[Rerunnable].delay(currentThreadName())

        poolName3 <- Rerunnable.fromFuture(f.newFuture)
      } yield (poolName1, poolName2, poolName3)).run.await

    assert(poolName1 == f.futurePoolName)
    assert(poolName2 == f.ioPoolName)
    assert(poolName2 == f.ioPoolName)
  }

  test("ContextShift[Rerunnable].evalOn executes on correct pool and shifts back to previous pool") { f =>
    implicit val cs: ContextShift[Rerunnable] =
      RerunnableContextShift.fromExecutionContext(f.ioPool)

    val (poolName1, poolName2, poolName3) =
      (for {
        poolName1 <- f.newRerunnable

        poolName2 <- ContextShift[Rerunnable].evalOn(f.otherPool)(f.newRerunnable)

        poolName3 <- f.newRerunnable
      } yield (poolName1, poolName2, poolName3)).run.await

    assert(poolName1 == currentThreadName()) // The first rerunnable is not explicitly evaluated on a dedicated pool
    assert(poolName2 == f.otherPoolName)
    assert(poolName3 == f.ioPoolName)
  }

  test("ContextShift[Rerunnable].evalOn executes on correct pool and shifts back to future pool") { f =>
    implicit val cs: ContextShift[Rerunnable] =
      RerunnableContextShift.fromExecutionContext(f.ioPool)

    val (poolName1, poolName2, poolName3) =
      (for {
        poolName1 <- Rerunnable.fromFuture(f.newFuture) // The future was started on a dedicated pool (e.g. netty)

        poolName2 <- ContextShift[Rerunnable].evalOn(f.otherPool)(f.newRerunnable)

        poolName3 <- Rerunnable.fromFuture(f.newFuture)
      } yield (poolName1, poolName2, poolName3)).run.await

    assert(poolName1 == f.futurePoolName)
    assert(poolName2 == f.otherPoolName)
    assert(poolName3 == f.futurePoolName)
  }

  implicit private class FutureAwaitOps[A](future: Future[A]) {
    def await: A = Await.result(future)
  }

  override protected def withFixture(test: OneArgTest): Outcome = withFixture(test.toNoArgTest(new FixtureParam))
} 
Example 71
Source File: RerunnableClockSuite.scala    From catbird   with Apache License 2.0 5 votes vote down vote up
package io.catbird.util.effect

import java.time.Instant
import java.util.concurrent.TimeUnit

import cats.effect.Clock
import com.twitter.util.Await
import io.catbird.util.Rerunnable
import org.scalatest.Outcome
import org.scalatest.concurrent.Eventually
import org.scalatest.funsuite.FixtureAnyFunSuite


class RerunnableClockSuite extends FixtureAnyFunSuite with Eventually {

  protected final class FixtureParam {
    def now: Instant = Instant.now()

    val clock: Clock[Rerunnable] = RerunnableClock()
  }

  test("Retrieval of real time") { f =>
    eventually {
      val result = Await.result(
        f.clock.realTime(TimeUnit.MILLISECONDS).map(Instant.ofEpochMilli).run
      )

      assert(java.time.Duration.between(result, f.now).abs().toMillis < 50)
    }
  }

  test("Retrieval of monotonic time") { f =>
    eventually {
      val result = Await.result(
        f.clock.monotonic(TimeUnit.NANOSECONDS).run
      )

      val durationBetween = Math.abs(System.nanoTime() - result)
      assert(TimeUnit.MILLISECONDS.convert(durationBetween, TimeUnit.NANOSECONDS) < 5)
    }
  }

  override protected def withFixture(test: OneArgTest): Outcome = withFixture(test.toNoArgTest(new FixtureParam))
} 
Example 72
Source File: RerunnableSuite.scala    From catbird   with Apache License 2.0 5 votes vote down vote up
package io.catbird.util.effect

import cats.effect.laws.discipline.EffectTests
import cats.effect.laws.discipline.arbitrary.catsEffectLawsArbitraryForIO
import cats.effect.laws.util.{ TestContext, TestInstances }
import cats.effect.{ Bracket, IO }
import cats.instances.either._
import cats.instances.int._
import cats.instances.tuple._
import cats.instances.unit._
import cats.kernel.Eq
import cats.laws.discipline.arbitrary._
import com.twitter.util.{ Await, Monitor, Throw }
import io.catbird.util.{ ArbitraryInstances, Rerunnable }
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.prop.Configuration
import org.typelevel.discipline.scalatest.FunSuiteDiscipline

class RerunnableSuite
    extends AnyFunSuite
    with FunSuiteDiscipline
    with Configuration
    with ArbitraryInstances
    with TestInstances {
  implicit val context: TestContext = TestContext()
  implicit def rerunnableEq[A](implicit A: Eq[A]): Eq[Rerunnable[A]] =
    Eq.by[Rerunnable[A], IO[A]](rerunnableToIO)

  checkAll("Rerunnable[Int]", EffectTests[Rerunnable].effect[Int, Int, Int])

  test("Exceptions thrown by release are handled by Monitor") {
    val useException = new Exception("thrown by use")
    val releaseException = new Exception("thrown by release")

    var monitoredException: Throwable = null
    val monitor = Monitor.mk { case e => monitoredException = e; true; }

    val rerunnable = Bracket[Rerunnable, Throwable]
      .bracket(Rerunnable.Unit)(_ => Rerunnable.raiseError(useException))(_ => Rerunnable.raiseError(releaseException))
      .liftToTry

    val result = Await.result(Monitor.using(monitor)(rerunnable.run))

    assert(result == Throw(useException))
    assert(monitoredException == releaseException)
  }
} 
Example 73
Source File: RerunnableBenchmark.scala    From catbird   with Apache License 2.0 5 votes vote down vote up
package io.catbird.benchmark

import com.twitter.util.{ Await, Future, FuturePool }
import io.catbird.util.Rerunnable
import java.util.concurrent.{ ExecutorService, Executors, TimeUnit }
import org.openjdk.jmh.annotations._


@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class RerunnableBenchmark {
  val count: Int = 100000
  val numbers: IndexedSeq[Int] = 0 to count
  var es: ExecutorService = _
  var pool: FuturePool = _

  @Setup
  def initPool(): Unit = {
    es = Executors.newFixedThreadPool(4)
    pool = FuturePool(es)
  }

  @TearDown
  def shutdownPool(): Unit = es.shutdown()

  @Benchmark
  def sumIntsF: Int = Await.result(
    numbers.foldLeft(Future(0)) {
      case (acc, i) => acc.flatMap(prev => Future(prev + i))
    }
  )

  @Benchmark
  def sumIntsR: Int = Await.result(
    numbers
      .foldLeft(Rerunnable(0)) {
        case (acc, i) => acc.flatMap(prev => Rerunnable(prev + i))
      }
      .run
  )

  @Benchmark
  def sumIntsPF: Int = Await.result(
    numbers.foldLeft(pool(0)) {
      case (acc, i) => acc.flatMap(prev => pool(prev + i))
    }
  )

  @Benchmark
  def sumIntsPR: Int = Await.result(
    numbers
      .foldLeft(Rerunnable.withFuturePool(pool)(0)) {
        case (acc, i) => acc.flatMap(prev => Rerunnable.withFuturePool(pool)(prev + i))
      }
      .run
  )
}