@@ -10,6 +10,7 @@ import org.apache.pekko.http.scaladsl.server.{ExceptionHandler, Route}
1010import org .slf4j .{Logger , LoggerFactory }
1111
1212import java .io .File
13+ import java .nio .file .Paths
1314import scala .concurrent .duration .*
1415import scala .util .{Failure , Success }
1516
@@ -38,6 +39,7 @@ object FileServer extends App {
3839
3940 def server (address : String , port : Int ): Unit = {
4041 val resourceFileName = " payload.zip"
42+ val payloadFile = new File (Paths .get(" src/main/resources" , resourceFileName).toAbsolutePath.toString)
4143
4244 val cache : Cache [String , String ] =
4345 Scaffeine ()
@@ -61,8 +63,7 @@ object FileServer extends App {
6163 path(" download" / Segment ) { id =>
6264 logger.info(s " TRACE_ID: $id Server received download request " )
6365 get {
64- // for testing: use the same file, independent of the TRACE_ID
65- getFromFile(new File (getClass.getResource(s " / $resourceFileName" ).toURI), MediaTypes .`application/zip`)
66+ getFromFile(payloadFile, MediaTypes .`application/zip`)
6667 }
6768 } ~ path(" downloadflaky" / Segment ) { id =>
6869 logger.info(s " TRACE_ID: $id Server received flaky download request " )
@@ -72,9 +73,9 @@ object FileServer extends App {
7273 } else if (id.toInt % 5 == 0 ) { // 5, 15, 25
7374 // Causes TimeoutException on client if sleep time > 5 sec
7475 randomSleeper()
75- getFromFile(new File (getClass.getResource( s " / $resourceFileName " ).toURI) , MediaTypes .`application/zip`)
76+ getFromFile(payloadFile , MediaTypes .`application/zip`)
7677 } else {
77- getFromFile(new File (getClass.getResource( s " / $resourceFileName " ).toURI) , MediaTypes .`application/zip`)
78+ getFromFile(payloadFile , MediaTypes .`application/zip`)
7879 }
7980 }
8081 } ~ path(" downloadni" / Segment ) { id =>
@@ -88,7 +89,7 @@ object FileServer extends App {
8889 cache.put(id, " downloading" ) // to simulate blocking on concurrent requests
8990 get {
9091 randomSleeper()
91- val response = getFromFile(new File (getClass.getResource( s " / $resourceFileName " ).toURI) , MediaTypes .`application/zip`)
92+ val response = getFromFile(payloadFile , MediaTypes .`application/zip`)
9293 cache.put(id, " downloaded" )
9394 response
9495 }
@@ -125,4 +126,4 @@ object FileServer extends App {
125126 logger.info(s " -> Complete with HTTP status code: $statusCode" )
126127 statusCodes(finalRnd)
127128 }
128- }
129+ }
0 commit comments