PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/casbah-core/src/test/scala/MaxTimeSpec.scala

http://github.com/mongodb/casbah
Scala | 131 lines | 87 code | 23 blank | 21 comment | 2 complexity | 9b9a138b0cb1db7edcc1c2e71f227e04 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah.test.core
  23. import com.mongodb.MongoExecutionTimeoutException
  24. import org.specs2.specification.BeforeAfterExample
  25. import scala.concurrent.duration.{ Duration, SECONDS }
  26. import com.mongodb.casbah.Imports._
  27. class MaxTimeSpec extends CasbahDBTestSpecification with BeforeAfterExample {
  28. skipAllUnless(serverIsAtLeastVersion(2, 5))
  29. val oneSecond = Duration(1, SECONDS)
  30. def before: Unit = {
  31. collection.drop()
  32. enableMaxTimeFailPoint()
  33. }
  34. def after: Unit = disableMaxTimeFailPoint()
  35. "MaxTime" should {
  36. "be supported by aggregation" in {
  37. val aggregationOptions = AggregationOptions(oneSecond)
  38. lazy val aggregation = collection.aggregate(
  39. List(
  40. MongoDBObject("$match" -> ("score" $gte 7)),
  41. MongoDBObject("$project" -> MongoDBObject("score" -> 1))
  42. ),
  43. aggregationOptions
  44. )
  45. aggregation should throwA[MongoExecutionTimeoutException]
  46. }
  47. "be supported by findAndModify" in {
  48. lazy val findAndModify = collection.findAndModify(query = MongoDBObject("_id" -> 1), fields = MongoDBObject(),
  49. sort = MongoDBObject(), remove = false, update = MongoDBObject("a" -> 1),
  50. returnNew = true, upsert = false, maxTime = oneSecond)
  51. findAndModify should throwA[MongoExecutionTimeoutException]
  52. }
  53. "be supported by cursors" in {
  54. val cursor = collection.find().maxTime(oneSecond)
  55. cursor.next() should throwA[MongoExecutionTimeoutException]
  56. cursor.toList should throwA[MongoExecutionTimeoutException]
  57. }
  58. "be supported when calling findOne" in {
  59. lazy val op = collection.findOne(MongoDBObject.empty, MongoDBObject.empty,
  60. MongoDBObject.empty, ReadPreference.Primary,
  61. oneSecond)
  62. op should throwA[MongoExecutionTimeoutException]
  63. }
  64. "be supported when calling one" in {
  65. lazy val op = collection.find().maxTime(oneSecond).one()
  66. op should throwA[MongoExecutionTimeoutException]
  67. }
  68. "be supported when calling getCount" in {
  69. lazy val op = collection.getCount(maxTime = oneSecond)
  70. op should throwA[MongoExecutionTimeoutException]
  71. }
  72. "be supported when calling count" in {
  73. lazy val op = collection.count(maxTime = oneSecond)
  74. op should throwA[MongoExecutionTimeoutException]
  75. }
  76. "be supported when calling a chained count" in {
  77. lazy val op = collection.find().maxTime(oneSecond).count()
  78. op should throwA[MongoExecutionTimeoutException]
  79. }
  80. "be supported when calling size" in {
  81. lazy val op = collection.find().maxTime(oneSecond).size
  82. op should throwA[MongoExecutionTimeoutException]
  83. }
  84. "be supported when calling commands" in {
  85. lazy val op = database.command(MongoDBObject("isMaster" -> 1, "maxTimeMS" -> 1)).throwOnError()
  86. op should throwA[MongoExecutionTimeoutException]
  87. }
  88. "be supported when calling mapReduce" in {
  89. collection += MongoDBObject("x" -> List(1, 2, 3))
  90. collection += MongoDBObject("x" -> List(1, 2, 3))
  91. val mapJS = "function(){ for ( var i=0; i<this.x.length; i++ ){ emit( this.x[i] , 1 ); } }"
  92. val reduceJS = "function(key,values){ var sum=0; for( var i=0; i<values.length; i++ ) sum += values[i]; return sum;}"
  93. lazy val op = collection.mapReduce(mapJS, reduceJS, "test", maxTime = Some(oneSecond))
  94. op should throwA[MongoExecutionTimeoutException]
  95. }
  96. "be supported when calling getMore" in {
  97. disableMaxTimeFailPoint()
  98. for (i <- 1 to 20) {
  99. collection += MongoDBObject("x" -> i)
  100. }
  101. val docs = collection.find().maxTime(oneSecond).batchSize(10)
  102. docs.next()
  103. enableMaxTimeFailPoint()
  104. lazy val getMoreOp = while (docs.hasNext) { docs.next() }
  105. getMoreOp should throwA[MongoExecutionTimeoutException]
  106. }
  107. }
  108. }