-
Notifications
You must be signed in to change notification settings - Fork 5
/
CBJsonSpec.scala
107 lines (89 loc) · 3.2 KB
/
CBJsonSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/** @author giabao
* created: 2013-10-22 10:23
* (c) 2011-2013 sandinh.com */
package akka.contrib.couchbase
import play.api.libs.json.Json
import org.specs2.mutable.Specification
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration._
import CbFutureAsScala._
import net.spy.memcached.ops.StatusCode.ERR_NOT_FOUND
class CBJsonSpec extends Specification with CBHelper {
sequential
import TrophyType._
trait WithMyCB extends WithCB {
protected def cb = CBJsonSpec.this.cb
}
sealed trait TrophyType {
def id: Int
def name: String
def bonus: Long
final def luuDanh = this != TuTai
}
object TrophyType {
case object TuTai extends TrophyType {
val id = 0
val name = "Tú Tài"
val bonus = 10000000L
}
case object CuNhan extends TrophyType {
val id = 1
val name = "Cử Nhân"
val bonus = 150000000L
}
}
case class Trophy(awardCount: Int)
object Trophy extends CBJson[Trophy] with Key2[Trophy, Int, TrophyType] with WithMyCB {
protected def key(uid: Int, t: TrophyType) = "u" + t.id + uid
protected implicit val fmt = Json.format[Trophy]
}
//as of version 2.0.9, the following code will not compilable, with the following compile error:
//overriding method set in trait WritesKey1 of type (a: String, value: Int)scala.concurrent.Future[Boolean];
//[error] method set in trait CBWrites of type (key: String, value: Int)scala.concurrent.Future[Boolean] cannot override final member
//[error] object WritesIntWithStringKey extends WritesKey1[Int, String] with CBWrites[Int] with WithMyCB{
object WritesIntWithStringKey extends WritesKey1[Int, String] with CBWrites[Int] with WithMyCB {
override protected def Expiry = 60
protected def key(a: String) = s"test$a"
protected def writes(v: Int) = v.toString
}
"CBJson" should {
"accessible with Key2" in {
Trophy.set(1, TuTai, Trophy(2)).map(_.booleanValue) must beTrue.await
val uids = Seq(1, 2, 3)
val trophies = Seq(Trophy(5), Trophy(6), Trophy(7))
val atMost = Duration(1, SECONDS)
Await.result(Trophy.setBulk(uids, CuNhan, trophies), atMost) must have size uids.size
Trophy.get(1, CuNhan).map(_.awardCount) must beEqualTo(5).await
Trophy.delete(1, CuNhan).map(_.booleanValue) must beTrue.await
Await.result(Trophy.get(1, CuNhan), atMost) must throwA[CBException].like {
case CBException(ERR_NOT_FOUND) => ok
}
}
/* The following code will throw StackOverflowError at line C.set("x")
trait A{
def set(k: String){ println("set: " + k) }
}
trait B[X] extends A{
def key(x: X): String
def set(x: X): Unit = set(key(x))
}
object C extends B[String] with A{
def key(x: String) = x
}
C.set("x")
But if we define B.set as:
def set(x: X) = super.set(key(x))
Then every thing is OK.
And, if we declare
trait B[X] { this: A =>
As we declare in version 2.0.9:
ReadsKey1[T, A] { this: CBReads[T] =>
Then object C will not compilable. @see WritesIntWithStringKey
*/
"not throws StackOverflowError" in {
WritesIntWithStringKey.set("a", 1) must not(throwA[Exception])
}
assertTerminate()
}
}