Gatling - doIfOrElse使用Map.size条件进行错误的分支

时间:2014-08-19 05:56:15

标签: scala conditional-statements gatling

在下面的代码片段中,userMap是一个空地图,但是当它运行时,执行了else块并且我收到错误

package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import io.gatling.jsonpath._

class Test1 extends Simulation {

    //Global Users
  val userMap = scala.collection.concurrent.TrieMap[String, String]()
  //Randomiser 
  val rnd = new scala.util.Random
  val httpConf = http
    .baseURL("http://demo1263864.mockable.io/") // Here is the root for all relative URLs
    .acceptHeader("application/json") // Here are the common headers
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

  val headers_10 = Map("Content-Type" -> "application/json") // Note the headers specific to a given request

  val scn = scenario("Simple Battle")
    .doIfOrElse(rnd.nextInt(3) > 2 || userMap.size == 0) {
      println("++++++++++++++++" + userMap.size)
      exec(
        http("If request ")
          .get("gatling1")
          .headers(headers_10)
          .check(jsonPath("$.result").is("SUCCESS")) // Because we get warning when no password is sent and a new profile is created 
          ) // executed if the session value stored in "myKey" is equal to "myValue"
        .exec(session => {
          println("++" + session)
          userMap("1") = "2"
          session
        })
        .exec(session => {
          println("++" + userMap.size)
          session
        })
    } {
      val index = rnd.nextInt(userMap.size)
      val userArray = userMap.toArray.map(x => Array(x._1, x._2))
      exec(http("else request")
        .get("gatling1")
        .headers(headers_10)
        .check(jsonPath("$.result").is("SUCCESS"))) // Because we get warning when no password is sent and a new profile is created 

    }

  setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}

输出显示即使userMap.size == 0

也执行了else块
Simulation computerdatabase.Test1 started...
++++++++++++++++0
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Random.java:300)
    at scala.util.Random.nextInt(Random.scala:65)
    at computerdatabase.Test1.<init>(Test1.scala:43)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at io.gatling.core.runner.Runner.run(Runner.scala:37)
    at io.gatling.app.Gatling.start(Gatling.scala:235)
    at io.gatling.app.Gatling$.fromMap(Gatling.scala:54)
    at io.gatling.app.Gatling$.runGatling(Gatling.scala:79)
    at io.gatling.app.Gatling$.runGatling(Gatling.scala:58)
    at io.gatling.app.Gatling$.main(Gatling.scala:50)
    at io.gatling.app.Gatling.main(Gatling.scala)

1 个答案:

答案 0 :(得分:1)

我在Gatling的小组上回答了你的问题:https://groups.google.com/forum/#!topic/gatling/J8O27q112oU