Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 2.5.3
version = 2.6.0

style = defaultWithAlign
maxColumn = 100
Expand Down
12 changes: 8 additions & 4 deletions fetch-debug/src/main/scala/document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ abstract class Document {

object Document {

/** The empty document */
/**
* The empty document */
def empty = DocNil

/** A break, which will either be turned into a space or a line break */
/**
* A break, which will either be turned into a space or a line break */
def break = DocBreak

/** A document consisting of some text literal */
/**
* A document consisting of some text literal */
def text(s: String): Document = DocText(s)

/**
Expand All @@ -123,6 +126,7 @@ object Document {
*/
def group(d: Document): Document = DocGroup(d)

/** A nested document, which will be indented as specified. */
/**
* A nested document, which will be indented as specified. */
def nest(i: Int, d: Document): Document = DocNest(i, d)
}
8 changes: 5 additions & 3 deletions fetch/src/main/scala/datasource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import cats.kernel.{Hash => H}

/**
* `Data` is a trait used to identify and optimize access to a `DataSource`.
**/
*/
trait Data[I, A] { self =>
def name: String

Expand All @@ -47,11 +47,13 @@ trait DataSource[F[_], I, A] {

implicit def CF: Concurrent[F]

/** Fetch one identity, returning a None if it wasn't found.
/**
* Fetch one identity, returning a None if it wasn't found.
*/
def fetch(id: I): F[Option[A]]

/** Fetch many identities, returning a mapping from identities to results. If an
/**
* Fetch many identities, returning a mapping from identities to results. If an
* identity wasn't found, it won't appear in the keys.
*/
def batch(ids: NonEmptyList[I]): F[Map[I, A]] =
Expand Down
6 changes: 4 additions & 2 deletions fetch/src/main/scala/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import cats.effect._

object syntax {

/** Implicit syntax to lift any value to the context of Fetch via pure */
/**
* Implicit syntax to lift any value to the context of Fetch via pure */
implicit class FetchIdSyntax[A](val a: A) extends AnyVal {

def fetch[F[_]: Concurrent]: Fetch[F, A] =
Fetch.pure[F, A](a)
}

/** Implicit syntax to lift exception to Fetch errors */
/**
* Implicit syntax to lift exception to Fetch errors */
implicit class FetchExceptionSyntax[B](val a: Throwable) extends AnyVal {

def fetch[F[_]: Concurrent]: Fetch[F, B] =
Expand Down
34 changes: 16 additions & 18 deletions fetch/src/test/scala/FetchAsyncQueryTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ class FetchAsyncQueryTests extends FetchSpec {
val io = Fetch.run[IO](fetch)

io.map(
_ shouldEqual List(
Article(1, "An article with id 1"),
Article(1, "An article with id 1"),
Article(2, "An article with id 2")
)
_ shouldEqual List(
Article(1, "An article with id 1"),
Article(1, "An article with id 1"),
Article(2, "An article with id 2")
)
.unsafeToFuture
).unsafeToFuture
}

"We can use combinators and multiple sources in a for comprehension and interpret a fetch from async sources into an IO" in {
Expand All @@ -72,20 +71,19 @@ class FetchAsyncQueryTests extends FetchSpec {
val io = Fetch.run[IO](fetch)

io.map(
_ shouldEqual (
List(
Article(1, "An article with id 1"),
Article(1, "An article with id 1"),
Article(2, "An article with id 2")
),
List(
Author(2, "@egg2"),
Author(2, "@egg2"),
Author(3, "@egg3")
)
_ shouldEqual (
List(
Article(1, "An article with id 1"),
Article(1, "An article with id 1"),
Article(2, "An article with id 2")
),
List(
Author(2, "@egg2"),
Author(2, "@egg2"),
Author(3, "@egg3")
)
)
.unsafeToFuture
).unsafeToFuture
}
}

Expand Down
85 changes: 39 additions & 46 deletions fetch/src/test/scala/FetchBatchingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5
totalBatches(log.rounds) shouldEqual 3
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5
totalBatches(log.rounds) shouldEqual 3
}).unsafeToFuture
}

"A large fetch to a datasource with a maximum batch size is split and executed in parallel" in {
Expand All @@ -126,13 +125,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5
totalBatches(log.rounds) shouldEqual 3
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5
totalBatches(log.rounds) shouldEqual 3
}).unsafeToFuture
}

"Fetches to datasources with a maximum batch size should be split and executed in parallel and sequentially when using productR" in {
Expand All @@ -143,13 +141,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5 + 5
totalBatches(log.rounds) shouldEqual 3 + 3
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5 + 5
totalBatches(log.rounds) shouldEqual 3 + 3
}).unsafeToFuture
}

"Fetches to datasources with a maximum batch size should be split and executed in parallel and sequentially when using productL" in {
Expand All @@ -160,13 +157,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5 + 5
totalBatches(log.rounds) shouldEqual 3 + 3
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3, 4, 5)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 5 + 5
totalBatches(log.rounds) shouldEqual 3 + 3
}).unsafeToFuture
}

"A large (many) fetch to a datasource with a maximum batch size is split and executed in sequence" in {
Expand All @@ -176,13 +172,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 3
totalBatches(log.rounds) shouldEqual 2
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 3
totalBatches(log.rounds) shouldEqual 2
}).unsafeToFuture
}

"A large (many) fetch to a datasource with a maximum batch size is split and executed in parallel" in {
Expand All @@ -192,13 +187,12 @@ class FetchBatchingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
result shouldEqual List(1, 2, 3)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 3
totalBatches(log.rounds) shouldEqual 2
})
.unsafeToFuture
case (log, result) =>
result shouldEqual List(1, 2, 3)
log.rounds.size shouldEqual 1
totalFetched(log.rounds) shouldEqual 3
totalBatches(log.rounds) shouldEqual 2
}).unsafeToFuture
}

"Very deep fetches don't overflow stack or heap" in {
Expand All @@ -216,9 +210,8 @@ class FetchBatchingTests extends FetchSpec {
)

io.map({
case (log, result) =>
result shouldEqual ids.map(_.toString)
})
.unsafeToFuture
case (log, result) =>
result shouldEqual ids.map(_.toString)
}).unsafeToFuture
}
}
46 changes: 19 additions & 27 deletions fetch/src/test/scala/FetchReportingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 0
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 0
}).unsafeToFuture
}

"Single fetches are executed in one round" in {
Expand All @@ -42,9 +41,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 1
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 1
}).unsafeToFuture
}

"Single fetches are executed in one round per binding in a for comprehension" in {
Expand All @@ -57,9 +55,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 2
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 2
}).unsafeToFuture
}

"Single fetches for different data sources are executed in multiple rounds if they are in a for comprehension" in {
Expand All @@ -72,9 +69,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 2
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 2
}).unsafeToFuture
}

"Single fetches combined with cartesian are run in one round" in {
Expand All @@ -84,9 +80,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 1
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 1
}).unsafeToFuture
}

"Single fetches combined with traverse are run in one round" in {
Expand All @@ -99,9 +94,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 2
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 2
}).unsafeToFuture
}

"The product of two fetches from the same data source implies batching" in {
Expand All @@ -111,9 +105,8 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) => log.rounds.size shouldEqual 1
})
.unsafeToFuture
case (log, result) => log.rounds.size shouldEqual 1
}).unsafeToFuture
}

"The product of concurrent fetches of the same type implies everything fetched in batches" in {
Expand All @@ -137,11 +130,10 @@ class FetchReportingTests extends FetchSpec {
val io = Fetch.runLog[IO](fetch)

io.map({
case (log, result) =>
log.rounds.size shouldEqual 2
totalBatches(log.rounds) shouldEqual 1
totalFetched(log.rounds) shouldEqual 3 + 1
})
.unsafeToFuture
case (log, result) =>
log.rounds.size shouldEqual 2
totalBatches(log.rounds) shouldEqual 1
totalFetched(log.rounds) shouldEqual 3 + 1
}).unsafeToFuture
}
}
Loading