Newer
Older
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
package midterm22
import org.junit.*
import org.junit.Assert.*
import instrumentation.*
import annotation.nowarn
import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.{TrieMap, Map}
class Part8Test:
@Test
def usage() =
val insta = Instagram()
assertEquals(1, insta.add())
assertEquals(2, insta.add())
insta.follow(1, 2)
assertEquals(insta.graph, Map(1 -> List(2), 2 -> List()))
insta.follow(2, 1)
insta.unfollow(1, 2)
assertEquals(insta.graph, Map(1 -> List(), 2 -> List(1)))
insta.follow(3, 1) // fails silently
assertEquals(insta.graph, Map(1 -> List(), 2 -> List(1)))
insta.remove(1)
assertEquals(insta.graph, Map(2 -> List()))
insta.unfollow(1, 2) // fails silently
@Test
def testParallelFollowABRemoveA() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
val u1 = insta.add()
val u2 = insta.add()
(
List(
() =>
// Thread 1
insta.follow(u1, u2),
() =>
// Thread 2
insta.remove(u1)
),
results =>
val size = insta.graph.size
if size != 1 then
(false, f"Wrong number of user: expected 1 but got ${size}")
else validateGraph(insta)
)
)
@Test
def testParallelFollowABRemoveB() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
val u1 = insta.add()
val u2 = insta.add()
(
List(
() =>
// Thread 1
insta.follow(u1, u2),
() =>
// Thread 2
insta.remove(u2)
),
results =>
val size = insta.graph.size
if size != 1 then
(false, f"Wrong number of user: expected 1 but got ${size}")
else validateGraph(insta)
)
)
@Test
def testParallelFollowACRemoveB() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
val u1 = insta.add()
val u2 = insta.add()
val u3 = insta.add()
insta.follow(u1, u2)
(
List(
() =>
// Thread 1
insta.follow(u1, u3),
() =>
// Thread 2
insta.remove(u2)
),
results =>
val size = insta.graph.size
if size != 2 then
(false, f"Wrong number of user: expected 2 but got ${size}")
else validateGraph(insta)
)
)
@Test
def testParallelFollow() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
val u1 = insta.add()
val u2 = insta.add()
val u3 = insta.add()
(
List(
() =>
// Thread 1
insta.follow(u1, u2),
() =>
// Thread 2
insta.follow(u1, u3)
),
results =>
val u1FollowingSize = insta.graph(u1).size
if u1FollowingSize != 2 then
(
false,
f"Wrong number of users followed by user 1: expected 2 but got ${u1FollowingSize}"
)
else validateGraph(insta)
)
)
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
@Test
def testParallelRemove() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
// Setup
val u1 = insta.add()
val u2 = insta.add()
val u3 = insta.add()
insta.follow(u1, u2)
insta.follow(u2, u1)
insta.follow(u2, u3)
insta.follow(u3, u1)
(
List(
() =>
// Thread 1
insta.remove(u2),
() =>
// Thread 2
insta.remove(u3)
),
results =>
val size = insta.graph.size
if size != 1 then
(false, f"Wrong number of user: expected 1 but got ${size}")
else validateGraph(insta)
)
)
// We test wrong code here, so we expect an assertion error. You can replace
// the next line by `@Test` if you want to see the error with the failing
// schedule.
@Test(expected = classOf[AssertionError])
def testParallelWrongAdd() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// This implementation of `add` is wrong, because two threads might
// allocate the same id.
// Consider the following schedule:
// T1: res = 1
// T2: res = 2
// T2: graph.update(2, Nil)
// T2: 2
// T1: graph.update(2, Nil)
// T1: 2
override def add(): Int =
val res = maxId.incrementAndGet
graph.update(maxId.get, Nil)
res
(
List(
() =>
// Thread 1
insta.add(),
() =>
// Thread 2
insta.add()
),
results =>
if results(0) != results(1) then
(false, f"Allocated twice id ${results(0)}")
else validateGraph(insta)
)
)
// We test wrong code here, so we expect an assertion error. You can replace
// the next line by `@Test` if you want to see the error with the failing
// schedule.
@Test(expected = classOf[AssertionError])
def testParallelWrongRemove() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
// This implementation of `remove` is wrong because we don't retry to
// call `graph.replace` when it fails. Therefore, user 1 might end up
// following user 2 that has been removed, or not following user 3
// which is concurrently followed.
override def remove(idToRemove: Int): Unit =
graph.remove(idToRemove)
for (key, value) <- graph do
graph.replace(key, value, value.filter(_ != idToRemove))
// Note: writing `graph(key) = value.filter(_ != idToRemove)` would also
// be wrong because it does not check the previous value.
// Therefore, it could erase a concurrent update.
val u1 = insta.add()
val u2 = insta.add()
val u3 = insta.add()
insta.follow(u1, u2)
(
List(
() =>
// Thread 1
insta.follow(u1, u3),
() =>
// Thread 2
insta.remove(u2)
),
results =>
val size = insta.graph.size
if insta.graph(u1).size != 1 then
(
false,
f"Wrong number of users followed by 1: expected 1 but got ${insta.graph(u1)}"
)
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
else validateGraph(insta)
)
)
// We test wrong code here, so we expect an assertion error. You can replace
// the next line by `@Test` if you want to see the error with the failing
// schedule.
@Test(expected = classOf[AssertionError])
def testParallelWrongUnfollow() =
TestHelper.testManySchedules(
2,
scheduler =>
val insta = new Instagram:
override val graph =
ScheduledTrieMap(TrieMap[Int, List[Int]](), scheduler)
override def unfollow(a: Int, b: Int): Unit =
if !graph.contains(a) then return
val prev = graph(a) // Might throw java.util.NoSuchElementException
if !graph.replace(a, prev, prev.filter(_ != b)) then unfollow(a, b)
val u1 = insta.add()
val u2 = insta.add()
insta.follow(u1, u2)
(
List(
() =>
// Thread 1
insta.unfollow(u1, u2),
() =>
// Thread 2
insta.remove(u1)
),
results =>
val size = insta.graph.size
if size != 1 then
(false, f"Wrong number of user: expected 1 but got ${size}")
else validateGraph(insta)
)
)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@nowarn
def validateGraph(insta: Instagram): (Boolean, String) =
for (a, following) <- insta.graph; b <- following do
if !insta.graph.contains(b) then
return (false, f"User $a follows non-existing user $b")
(true, "")
final class ScheduledIterator[T](
private val myIterator: Iterator[T],
private val scheduler: Scheduler
) extends Iterator[T]:
override def hasNext =
myIterator.hasNext
override def next() =
scheduler.exec(myIterator.next)("", Some(res => f"Iterator.next == $res"))
override def knownSize: Int =
myIterator.knownSize
final class ScheduledTrieMap[K, V](
private val myMap: Map[K, V],
private val scheduler: Scheduler
) extends Map[K, V]:
override def apply(key: K): V =
scheduler.exec(myMap(key))(
"",
Some(res => f"TrieMap.apply($key) == $res")
)
override def contains(key: K): Boolean =
scheduler.exec(myMap.contains(key))(
"",
Some(res => f"TrieMap.contains($key) == $res")
)
override def get(key: K): Option[V] =
scheduler.exec(myMap.get(key))(
"",
Some(res => f"TrieMap.get($key) == $res")
)
override def addOne(kv: (K, V)) =
scheduler.exec(myMap.addOne(kv))(f"TrieMap.addOne($kv)")
this
override def subtractOne(k: K) =
scheduler.exec(myMap.subtractOne(k))(f"TrieMap.subtractOne($k)")
this
override def iterator() =
scheduler.log("TrieMap.iterator")
ScheduledIterator(myMap.iterator, scheduler)
override def replace(k: K, v: V): Option[V] =
scheduler.exec(myMap.replace(k, v))(
"",
Some(res => f"TrieMap.replace($k, $v) == $res")
)
override def replace(k: K, oldvalue: V, newvalue: V): Boolean =
scheduler.exec(myMap.replace(k, oldvalue, newvalue))(
"",
Some(res => f"TrieMap.replace($k, $oldvalue, $newvalue) == $res")
)
override def putIfAbsent(k: K, v: V): Option[V] =
scheduler.exec(myMap.putIfAbsent(k, v))(
"",
Some(res => f"TrieMap.putIfAbsent($k, $v)")
)
override def remove(k: K): Option[V] =
scheduler.exec(myMap.remove(k))(
"",
Some(res => f"TrieMap.remove($k)")
)
override def remove(k: K, v: V): Boolean =
scheduler.exec(myMap.remove(k, v))(
"",
Some(res => f"TrieMap.remove($k, $v)")
)