Browse Source

Fix play2-scala-reactivemongo updates test implementation (#3280)

It was firing off the updates asynchronously and not waiting for them to
be completed (in the database) before sending a response to the client.
Michael Hixson 7 years ago
parent
commit
4f103c7fb6

+ 9 - 12
frameworks/Scala/play2-scala/play2-scala-reactivemongo/app/controllers/Application.scala

@@ -45,18 +45,15 @@ class Application (val controllerComponents: ControllerComponents, reactiveMongo
     futureFortunes
     futureFortunes
   }
   }
 
 
-  def updateWorlds(queries: Int): Future[Seq[Option[JsObject]]] = {
-    val futureWorlds: Future[Seq[Option[JsObject]]] = getRandomWorlds(queries)
-    val futureNewWorlds: Future[Seq[Option[JsObject]]] = futureWorlds.map( worlds => {
-      worlds.map(worldOption => {
-        worldOption.map(world => {
-          val newWorld = world ++ Json.obj("randomNumber" -> getNextRandom)
-          worldCollection.update(world, newWorld)
-          newWorld
-        })
-      })
-    })
-    futureNewWorlds
+  def updateWorlds(queries: Int): Future[Seq[JsObject]] = {
+    getRandomWorlds(queries)
+      .map(_.flatten)
+      .map(_.map(oldWorld => {
+        val newWorld = oldWorld ++ Json.obj("randomNumber" -> getNextRandom)
+        worldCollection.update(oldWorld, newWorld).map(result => newWorld)
+      }))
+      .map(Future.sequence(_))
+      .flatten
   }
   }
 
 
   def getNextRandom: Int = {
   def getNextRandom: Int = {