Skip to content

Instantly share code, notes, and snippets.

@salokanto
Created October 5, 2017 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salokanto/bfd706149699d3f549aa5dc3a2264fcb to your computer and use it in GitHub Desktop.
Save salokanto/bfd706149699d3f549aa5dc3a2264fcb to your computer and use it in GitHub Desktop.
ReactiveMongo bulk insert BSON maxSize bug
package test
import org.scalatest.FunSuite
import reactivemongo.api.MongoConnection
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.{BSONDocument, BSONElement, BSONString}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.Random
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Test for ReactiveMongo bulk insert BSON maxSize bug as discussed at:
* @see https://groups.google.com/forum/#!topic/reactivemongo/ywcGNMnAuxA
*
* @author Heikki Salokanto
* @since 2017-10-05
*/
class MongoBulkFailSpec extends FunSuite {
private val url = "mongodb://..."
private val parsedUri = MongoConnection.parseURI(url).get
private val driver = new reactivemongo.api.MongoDriver
private val connection = driver.connection(parsedUri, strictUri = true).get
private val database = Await.result(connection.database("testdb"), Duration.Inf)
private def makeBsonDocs(count: Int, size: Int) =
for (i <- 1 to count) yield {
val data = BSONString(Random.alphanumeric.take(size).mkString)
val elem = BSONElement(i.toString, data)
BSONDocument(Seq(elem))
}
test("Bulk insert succeeds") {
val fut = database.collection[BSONCollection]("test")
.insert(ordered = false)
.many(makeBsonDocs(count = 10, size = 500))
println(Await.result(fut, Duration.Inf))
}
test("Bulk insert fails") {
val fut = database.collection[BSONCollection]("test")
.insert(ordered = false)
.many(makeBsonDocs(count = 10, size = 1500))
println(Await.result(fut, Duration.Inf))
/* throws:
reactivemongo.core.errors.GenericDriverException: MongoError['size of document #0 exceed the maxBsonSize: 1513 > 1000']
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment