ElapsedTest.scala 12.3 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1
/*
2
 * Copyright (c) 2022 bitlap
梦境迷离's avatar
梦境迷离 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

梦境迷离's avatar
梦境迷离 已提交
22
package org.bitlap.tools
梦境迷离's avatar
梦境迷离 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.Future

/**
 * @author 梦境迷离
 * @since 2021/8/7
 * @version 1.0
 */
class ElapsedTest extends AnyFlatSpec with Matchers {

  "elapsed1" should "failed, not calculate anything, the return type is not specified" in {
    //Duration and TimeUnit must Full class name
    """
      |    class A {
梦境迷离's avatar
梦境迷离 已提交
40
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
41 42 43 44
      |      def i = ???
      |    }
      |
      |    class B {
梦境迷离's avatar
梦境迷离 已提交
45
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.WARN)
梦境迷离's avatar
梦境迷离 已提交
46 47 48 49
      |      def j = ???
      |    }
      |
      |    class C {
梦境迷离's avatar
梦境迷离 已提交
50
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.DEBUG)
梦境迷离's avatar
梦境迷离 已提交
51 52 53 54
      |      def j = ???
      |    }
      |
      |    class D {
梦境迷离's avatar
梦境迷离 已提交
55
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
      |      def i:String = ???
      |   }
      |    val a = new A()
      |    val b = new B()
      |    val c = new C()
      |    val d = new D()
      |""".stripMargin shouldNot compile
  }

  "elapsed2" should "ok, get the returnType of the method " in {
    //Duration and TimeUnit must Full class name
    """
      |class A {
梦境迷离's avatar
梦境迷离 已提交
69
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.NANOSECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
70 71 72 73 74
      |      def helloWorld: String = {
      |        println("hello world")
      |        "hello"
      |      }
      |
梦境迷离's avatar
梦境迷离 已提交
75
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      |      def helloScala: String = {
      |        Thread.sleep(2000)
      |        println("hello world")
      |        "hello"
      |      }
      |}
      |    val a = new A
      |    a.helloWorld
      |    a.helloScala
      |""".stripMargin should compile
  }

  "elapsed3" should "ok" in {
    //Duration and TimeUnit must Full class name
    """
      |    class A {
梦境迷离's avatar
梦境迷离 已提交
92
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.NANOSECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
93 94 95 96
      |      def helloWorld: String = {
      |        println("") ; println(""); ""
      |      }
      |
梦境迷离's avatar
梦境迷离 已提交
97
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
98 99
      |      def helloScala1: String = { println("") ; println(""); ""}
      |
梦境迷离's avatar
梦境迷离 已提交
100
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
101 102
      |      def helloScala2: String = { println("") ; println("");  "" }
      |
梦境迷离's avatar
梦境迷离 已提交
103
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117
      |      def helloScala3: String = {
      |        val s = "hello"
      |        val x = "world"
      |        return s
      |      }
      |    }
      |    val a = new A()
      |""".stripMargin should compile
  }

  "elapsed4" should "ok, return early" in {
    //Duration and TimeUnit must Full class name
    """
      |    class A {
梦境迷离's avatar
梦境迷离 已提交
118
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
      |      def helloScala1: String = {
      |        val s = "hello"
      |        if (s == "hello") {
      |          return "world"
      |        }
      |        val x = "world"
      |        return s
      |      }
      |    }
      |
      |    val a = new A
      |    a.helloScala1
      |
      |     class B {
梦境迷离's avatar
梦境迷离 已提交
133
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      |      def helloScala11: String = {
      |        val s = "hello"
      |        if (s == "hello") {
      |          return "world" + "wooo"
      |        }
      |        val x = "world"
      |        return s
      |      }
      |    }
      |
      |    val b = new B()
      |""".stripMargin should compile
  }

  "elapsed5" should "ok, return Future" in {
    class A {

      private final val log3: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(classOf[A])

梦境迷离's avatar
梦境迷离 已提交
153 154 155 156
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
157 158 159 160 161
      def helloScala1: Future[String] = {
        Thread.sleep(1000)
        Future.successful("hello world")
      }

梦境迷离's avatar
梦境迷离 已提交
162 163 164 165
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.DEBUG
      )
梦境迷离's avatar
梦境迷离 已提交
166 167 168 169 170 171 172
      def helloScala2: Future[String] = {
        Thread.sleep(2000)
        Future {
          "hello world"
        }(scala.concurrent.ExecutionContext.Implicits.global)
      }

梦境迷离's avatar
梦境迷离 已提交
173 174 175 176
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.WARN
      )
梦境迷离's avatar
梦境迷离 已提交
177 178 179 180 181 182 183 184 185 186 187 188
      def helloScala3: Future[String] = Future {
        "hello world"
      }(scala.concurrent.ExecutionContext.Implicits.global)
    }
  }

  "elapsed6" should "failed, not support when only has one expr" in {
    class B {

      import scala.concurrent.Await
      import scala.concurrent.duration.Duration

梦境迷离's avatar
梦境迷离 已提交
189 190 191 192 193
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.WARN
      )
      def helloScala(t: String): Future[String] =
梦境迷离's avatar
梦境迷离 已提交
194 195
        Future(t)(scala.concurrent.ExecutionContext.Implicits.global)

梦境迷离's avatar
梦境迷离 已提交
196 197 198 199
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.WARN
      )
梦境迷离's avatar
梦境迷离 已提交
200 201
      def helloScala11(t: String): Future[String] = Future(t)(scala.concurrent.ExecutionContext.Implicits.global)

梦境迷离's avatar
梦境迷离 已提交
202 203 204 205
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
206 207 208 209 210 211 212 213 214 215 216 217
      def helloScala2: String = {
        val s = Future("")(scala.concurrent.ExecutionContext.Implicits.global)
        Await.result(helloScala("world"), Duration.Inf)
      }
    }
  }

  "elapsed7" should "ok at object but has runTime Error" in { //Why?
    """
      |    object A {
      |      private final val log1: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(A.getClass)
      |
梦境迷离's avatar
梦境迷离 已提交
218
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.INFO)
梦境迷离's avatar
梦境迷离 已提交
219 220 221 222 223
      |      def helloScala1: Future[String] = {
      |        Thread.sleep(1000)
      |        Future.successful("hello world")
      |      }
      |
梦境迷离's avatar
梦境迷离 已提交
224
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.DEBUG)
梦境迷离's avatar
梦境迷离 已提交
225 226 227 228 229 230 231
      |      def helloScala2: Future[String] = {
      |        Thread.sleep(2000)
      |        Future {
      |          "hello world"
      |        }(scala.concurrent.ExecutionContext.Implicits.global)
      |      }
      |
梦境迷离's avatar
梦境迷离 已提交
232
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.WARN)
梦境迷离's avatar
梦境迷离 已提交
233 234 235 236 237 238 239 240
      |      def helloScala3: Future[String] = Future {
      |        "hello world"
      |      }(scala.concurrent.ExecutionContext.Implicits.global)
      |    }
      |""".stripMargin should compile
  }

  "elapsed8" should "ok at input args" in {
梦境迷离's avatar
梦境迷离 已提交
241 242 243 244
    @elapsed(
      limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
      logLevel = LogLevel.WARN
    )
梦境迷离's avatar
梦境迷离 已提交
245 246 247 248 249 250 251 252 253 254 255 256
    def helloScala1: String = {
      println("")
      println("")
      "hello"
    }
    @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = WARN)
    def helloScala2: String = {
      println("")
      println("")
      "hello"
    }

梦境迷离's avatar
梦境迷离 已提交
257 258 259 260
    @elapsed(
      limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
      logLevel = org.bitlap.tools.LogLevel.WARN
    )
梦境迷离's avatar
梦境迷离 已提交
261 262 263 264 265 266 267 268 269
    def helloScala3: String = {
      println("")
      println("")
      "hello"
    }
  }

  "elapsed9" should "failed at input args" in {
    """
梦境迷离's avatar
梦境迷离 已提交
270
      |@elapsed(logLevel = org.bitlap.tools.LogLevel.WARN, limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS))
梦境迷离's avatar
梦境迷离 已提交
271 272 273 274 275 276 277 278 279 280
      |    def helloScala1: String = {
      |      println("")
      |      println("")
      |      "hello"
      |    }
      |""".stripMargin shouldNot compile //args not in order
  }
  "elapsed10" should "multi-return" in {
    class A {

梦境迷离's avatar
梦境迷离 已提交
281 282 283 284
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      def j: Int = {
        var i = 1
        if (i == 1) {
          val h = 0
          var l = 0
          if (j == 0) {
            val h = 0
            var l = 0
            return 1
          } else {
            val j = 0
            return 0
          }
        } else {
          i
        }
        i
      }

梦境迷离's avatar
梦境迷离 已提交
304 305 306 307
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
      def k: Unit = {
        var i = 1
        if (i == 1) {
          val i = 0
          val k = 0
          if (i == 0) {
            1
          } else {
            2
          }
        } else {
          val u = 0
          0
        }

        var k = 1
        if (k == 1) {
          val i = 0
          val k = 0
          if (i == 0) {
            return ()
          } else {
            return ()
          }
        } else {
          val u = 0
          return u
        }
        1
      }

梦境迷离's avatar
梦境迷离 已提交
339 340 341 342
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
343 344
      def l: Int = {
        val i = 0
梦境迷离's avatar
梦境迷离 已提交
345
        for (i <- Seq(1))
梦境迷离's avatar
梦境迷离 已提交
346 347 348 349 350 351
          if (i == 1) {
            return 1 //not support
          }
        0
      }

梦境迷离's avatar
梦境迷离 已提交
352 353 354 355
      @elapsed(
        limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS),
        logLevel = org.bitlap.tools.LogLevel.INFO
      )
梦境迷离's avatar
梦境迷离 已提交
356 357
      def m: Int = {
        var i = 1
梦境迷离's avatar
梦境迷离 已提交
358
        if (i == 1) {} else {
梦境迷离's avatar
梦境迷离 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
          val u = 0
          return 0
        }

        if (i == 1) {
          return 1
        } else {
          val u = 0
          return 0
        }

        1
      }

    }
  }

  "elapsed11" should "failed at abstract method" in {
    """
      |abstract class A {
梦境迷离's avatar
梦境迷离 已提交
379
      |      @elapsed(limit = scala.concurrent.duration.Duration(1, java.util.concurrent.TimeUnit.SECONDS), logLevel = org.bitlap.tools.LogLevel.WARN)
梦境迷离's avatar
梦境迷离 已提交
380 381 382 383 384 385
      |      def hello:String
      |    }
      |""".stripMargin shouldNot compile
  }

}