SynchronizedTest.scala 1.4 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1 2
package io.github.dreamylost

S
Scala Steward 已提交
3 4
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
梦境迷离's avatar
梦境迷离 已提交
5 6 7 8 9 10 11

/**
 *
 * @author 梦境迷离
 * @since 2021/6/24
 * @version 1.0
 */
S
Scala Steward 已提交
12
class SynchronizedTest extends AnyFlatSpec with Matchers {
梦境迷离's avatar
梦境迷离 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  "synchronized1" should "is ok at class" in {
    @synchronized
    def getStr(k: Int): String = {
      k + ""
    }
    """@synchronized
       def getStr(k: Int): String = {
          k + ""
        }
      """ should compile

    @synchronized
    def getStr2(k: Int): String = {
      k + ""
    }
    """@synchronized
       def getStr2(k: Int) = {
          k + ""
        }
      """ should compile
  }

  "synchronized2" should "is ok by custom obj" in {

    val obj = new Object

    @synchronized(lockedName = "obj")
    def getStr3(k: Int): String = {
      k + ""
    }
    """
     @synchronized(lockedName = "obj")
     def getStr3(k: Int) = {
          k + ""
        }
      """ should compile

    object TestObject {
      // def getStr(k: Int): String = this.synchronized(k.$plus(""))
      // def getStr(k: Int): String = this.synchronized(this.synchronized(k.$plus("")))
      @synchronized
      def getStr(k: Int): String = {
        this.synchronized(k + "")
      }
    }

    """
     @synchronized(lockedName = "obj")
     class A
      """ shouldNot compile

    """
     @synchronized(lockedName = "obj")
     val s = "1"
      """ shouldNot compile
  }

}