Browse Source

Fix incorrect results.

Previously the play-scala-mongodb test handled both the single query and
multiple query tests through /db (maybe this was the format of an older
round of TFB?), and so returned a list for the single query test and
couldn't handle non-integer or out-of-range requests for the multi
query test.  Now both should pass.
Joshua Maddux 10 years ago
parent
commit
6f8f24b629

+ 29 - 4
frameworks/Scala/play-scala-mongodb/app/controllers/Application.scala

@@ -32,21 +32,46 @@ object Application extends Controller {
 
   private def collection: JSONCollection = database.collection[JSONCollection]("world")
   private val projection = Json.obj("_id" -> 0)
+  /**
+   * Returns the closest number to <code>toRestrict</code> that is within the
+   * specified bounds, inclusive on both ends.
+   */
+  private def restrictWithin(toRestrict: String, lowerBound: Int, upperBound: Int): Option[Int] = {
+    try {
+      Some(math.min(upperBound, math.max(toRestrict.toInt, lowerBound)))
+    } catch {
+      case e: Exception => None
+    }
+  }
 
-  def db(queries: Int) = Action.async {
+  def dbqueries(requestedQueries: String) = Action.async {
     import scala.concurrent.ExecutionContext.Implicits.global
 
     val random = ThreadLocalRandom.current()
+    val queries = restrictWithin(requestedQueries, 1, 500).getOrElse(1)
     val futureWorlds = Future.sequence((for {
       _ <- 1 to queries
     } yield { collection
       .find(Json.obj("id" -> (random.nextInt(TestDatabaseRows) + 1)), projection)
       .one[JsValue]
     }))
-
     futureWorlds.map { worlds =>
-      Ok(Json.toJson(worlds))
+      Ok(Json.toJson(worlds.map {maybeWorld =>
+        maybeWorld.map {world =>
+          world.as[Map[String, Int]]
+        }
+      }))
     }
   }
+  def singledb() = Action.async {
+    import scala.concurrent.ExecutionContext.Implicits.global
 
-}
+    val random = ThreadLocalRandom.current()
+    val futureWorld = collection
+      .find(Json.obj("id" -> (random.nextInt(TestDatabaseRows) + 1)), projection)
+      .one[JsValue]
+    futureWorld.map { world =>
+      Ok(Json.toJson(world.head.as[Map[String, Int]]))
+    }
+  }
+}

+ 1 - 1
frameworks/Scala/play-scala-mongodb/benchmark_config

@@ -4,7 +4,7 @@
     "default": {
       "setup_file": "setup",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
       "port": 9000,
       "approach": "Realistic",
       "classification": "Fullstack",

+ 2 - 1
frameworks/Scala/play-scala-mongodb/conf/routes

@@ -3,7 +3,8 @@
 # ~~~~
 
 # Home page
-GET     /db                             controllers.Application.db(queries: Int ?= 1)
+GET     /db                             controllers.Application.singledb()
+GET     /queries                        controllers.Application.dbqueries(queries: String ?= "1")
 
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)