@@ -4,7 +4,7 @@ import com.github.blemale.scaffeine.{Cache, Scaffeine}
44import org .apache .pekko .actor .ActorSystem
55import org .apache .pekko .http .scaladsl .Http
66import org .apache .pekko .http .scaladsl .model .StatusCodes .*
7- import org .apache .pekko .http .scaladsl .model .{HttpResponse , MediaTypes , StatusCodes }
7+ import org .apache .pekko .http .scaladsl .model .{HttpResponse , MediaTypes , StatusCode , StatusCodes }
88import org .apache .pekko .http .scaladsl .server .Directives .{logRequestResult , path , * }
99import org .apache .pekko .http .scaladsl .server .{ExceptionHandler , Route }
1010import org .slf4j .{Logger , LoggerFactory }
@@ -20,13 +20,14 @@ import scala.util.{Failure, Success}
2020 *
2121 * The client can request these types of response:
2222 * - HTTP 200 response: /download/[id]
23- * - Flaky response: /downloadflaky/[id]
2423 * - Non-idempotent response: /downloadni/[id]
2524 * Allows only one download file request per id, answer with HTTP 404 on subsequent requests
25+ * - Flaky response: /downloadflaky/[id]
26+ * Reply with additional random failures on requests with certain IDs
2627 *
2728 * Uses a cache to remember the "one download per id" behaviour
28- * Note that akka -http also supports server-side caching (by wrapping caffeine in caching directives):
29- * https://doc.akka.io /docs/akka -http/current/routing-dsl/directives/ caching-directives/index .html
29+ * Note that pekko -http would also support server-side caching (by wrapping caffeine in caching directives):
30+ * https://pekko.apache.org /docs/pekko -http/current/common/ caching.html
3031 */
3132object FileServer extends App {
3233 val logger : Logger = LoggerFactory .getLogger(this .getClass)
@@ -116,14 +117,10 @@ object FileServer extends App {
116117 Thread .sleep(sleepTime.toLong)
117118 }
118119
119- def randomErrorHttpStatusCode = {
120- val statusCodes = Seq (StatusCodes .InternalServerError , StatusCodes .BadRequest , StatusCodes .ServiceUnavailable )
121- val start = 0
122- val end = statusCodes.size - 1
123- val rnd = new scala.util.Random
124- val finalRnd = start + rnd.nextInt((end - start) + 1 )
125- val statusCode = statusCodes(finalRnd)
120+ def randomErrorHttpStatusCode : StatusCode = {
121+ val statusCodes = Seq (StatusCodes .NotFound , StatusCodes .InternalServerError , StatusCodes .ServiceUnavailable )
122+ val statusCode = statusCodes(scala.util.Random .nextInt(statusCodes.size))
126123 logger.info(s " -> Complete with HTTP status code: $statusCode" )
127- statusCodes(finalRnd)
124+ statusCode
128125 }
129126}
0 commit comments