-
Notifications
You must be signed in to change notification settings - Fork 23
/
KVTest.scala
207 lines (169 loc) · 5.23 KB
/
KVTest.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package hedgehog.examples.state
import hedgehog._
import hedgehog.runner._
import hedgehog.state._
import scala.concurrent.ExecutionContext.Implicits._
import java.io.{File, FileWriter}
trait KV {
def put(k: String, v: String): Unit
def get(k: String): Option[String]
}
/**
* This example has a companion tutorial.
*
* https://github.com/hedgehogqa/scala-hedgehog/blob/master/doc/state-tutorial.md
*/
object KVTest extends Properties {
override def tests: List[Prop] =
List(
property("test file KV (sequential) - broken", testSequential(KV.fileBadNamesSequential))
, property("test file KV (sequential)", testSequential(KV.fileSequential))
, property("test file KV (parallel) - broken", testParallel(KV.fileSequential))
, property("test file KV (parallel)", testParallel(KV.fileParallel))
)
def commands(kv: KV): List[CommandIO[State]] =
List(
commandPut(kv)
, commandGet(kv)
)
def testSequential(newKV: File => KV): Property = {
val root = new File("tmpdir")
val kv = newKV(root)
sequential(
Range.linear(1, 100)
, State.default
, commands(kv)
, () => TestUtil.deleteRecursively(root)
)
}
def testParallel(newKV: File => KV): Property = {
val root = new File("tmpdir")
val kv = newKV(root)
parallel(
Range.linear(0, 10)
, Range.linear(1, 10)
, State.default
, commands(kv)
, () => TestUtil.deleteRecursively(root)
)
}
case class Put(key: String, value: String)
case class Get(key: String)
case class State(map: Map[String, String])
object State {
def default: State =
State(Map())
}
def genKey : Gen[String] =
for {
c <- Gen.lower
s <- Gen.string(Gen.choice1(Gen.lower, Gen.constant('/')), Range.linear(0, 10))
} yield c +: s
def commandPut(kv: KV): CommandIO[State] =
new Command[State, Put, Unit] {
override def gen(s: State): Option[Gen[Put]] =
Some(for {
k <- s.map.keys.toList match {
case Nil =>
genKey
case h :: t =>
// Choice between known and unknown keys
Gen.frequency1(
50 -> genKey
, 50 -> Gen.element(h, t)
)
}
// Always generate a unique value
v <- Gen.string(Gen.upper, Range.linear(1, 10))
} yield Put(k, v))
override def execute(env: Environment, i: Put): Either[String, Unit] =
Right(kv.put(i.key, i.value))
override def update(s: State, i: Put, o: Var[Unit]): State =
s.copy(map = s.map + (i.key -> i.value))
override def ensure(env: Environment, s0: State, s: State, i: Put, o: Unit): Result =
Result.success
}
def commandGet(kv: KV): CommandIO[State] =
new Command[State, Get, Option[String]] {
override def gen(s: State): Option[Gen[Get]] =
Some((s.map.keys.toList match {
case Nil =>
genKey
case h :: t =>
// Mostly get values we know to exist, but also try values that don't
Gen.frequency1(
80 -> Gen.element(h, t)
, 20 -> genKey
)
}).map(Get(_)))
override def execute(env: Environment, i: Get): Either[String, Option[String]] =
Right(kv.get(i.key))
override def update(s: State, i: Get, o: Var[Option[String]]): State =
s
override def ensure(env: Environment, s0: State, s1: State, i: Get, o: Option[String]): Result =
s1.map.get(i.key) ==== o
}
}
object KV {
def fileBadNamesSequential(root: File): KV =
new KV {
override def put(k: String, v: String): Unit = {
val f = new File(root, k)
f.getParentFile.mkdirs()
val w = new FileWriter(f)
w.write(v)
w.close()
}
override def get(k: String): Option[String] = {
val f = new File(root, k)
if (!f.exists()) {
None
} else {
Some(scala.io.Source.fromFile(f).mkString)
}
}
}
def fileSequential(root: File): KV =
new KV {
def getPath(k: String): File =
new File(root, java.net.URLEncoder.encode(k, "UTF-8"))
override def put(k: String, v: String): Unit = {
val f = getPath(k)
f.getParentFile.mkdirs()
val w = new FileWriter(f)
w.write(v)
w.close()
}
override def get(k: String): Option[String] = {
val f = getPath(k)
if (!f.exists()) {
None
} else {
Some(scala.io.Source.fromFile(f).mkString)
}
}
}
def fileParallel(root: File): KV =
new KV {
def getPath(k: String): File =
new File(root, java.net.URLEncoder.encode(k, "UTF-8"))
override def put(k: String, v: String): Unit = {
val f = getPath(k)
f.getParentFile.mkdirs()
val g = new File(f.getParentFile, "." + f.getName + "_" + System.nanoTime.toString)
val w = new FileWriter(g)
w.write(v)
w.close()
g.renameTo(f)
()
}
override def get(k: String): Option[String] = {
val f = getPath(k)
if (!f.exists()) {
None
} else {
Some(scala.io.Source.fromFile(f).mkString)
}
}
}
}