Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Last active February 17, 2018 04:26
Show Gist options
  • Save JohnMurray/2feb029cb89297a517df8cf867bd7a1c to your computer and use it in GitHub Desktop.
Save JohnMurray/2feb029cb89297a517df8cf867bd7a1c to your computer and use it in GitHub Desktop.
Some simple test code we can use to play around with stuff in a REPL or simple project
import scala.concurrent._
import ExecutionContext.Implicits.global
object MockAPI {
private val rand = new scala.util.Random()
class RateLimitException(msg: String) extends Throwable(msg, null)
def simpleService: Future[String] = Future { "1, 2, 3, 4, 5, 6" }
def throwingRateLimitedService: Future[String] = Future {
if (rand.nextInt(100) > 50) {
throw new RateLimitException("Rate limit exceeded")
}
"1, 2, 3, 4, 5, 6"
}
def nonThrowingRateLimitedService: Future[String] = Future {
if (rand.nextInt(100) > 50) {
"RATELIMIT_EXCEEDED"
}
else {
"1, 2, 3, 4, 5, 6"
}
}
def maybeThrowingRateLimitedService: Future[String] = Future {
rand.nextInt(100) match {
case x if x > 75 => throw new RateLimitException("Rate limit exceeded")
case x if x > 50 => "RATELIMIT_EXCEEDED"
case _ => "1, 2, 3, 4, 5, 6"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment