-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DataCollectorQueriesAcceptanceTest.scala
717 lines (614 loc) · 23.1 KB
/
DataCollectorQueriesAcceptanceTest.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
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
268
269
270
271
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
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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.internal.collector
import java.nio.file.Files
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.graphdb.{Node, Path, Relationship}
import org.scalatest.matchers.{MatchResult, Matcher}
import scala.collection.mutable.ArrayBuffer
class DataCollectorQueriesAcceptanceTest extends DataCollectorTestSupport {
import DataCollectorMatchers._
test("should collect and retrieve queries") {
// given
execute("MATCH (n) RETURN count(n)")
execute("MATCH (n)-->(m) RETURN n,m")
execute("WITH 'hi' AS x RETURN x+'-ho'")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n) RETURN count(n)"
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n)-->(m) RETURN n,m"
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "WITH 'hi' AS x RETURN x+'-ho'"
)
)
)
}
test("should allow explicit control over query collection") {
// given
execute("CALL db.stats.stop('QUERIES')").single
execute("RETURN 'not collected!'")
execute("CALL db.stats.collect('QUERIES')").single
execute("MATCH (n) RETURN count(n)")
execute("CALL db.stats.stop('QUERIES')").single
execute("RETURN 'not collected!'")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n) RETURN count(n)"
)
)
)
}
test("should clear queries") {
// given
assertCollecting("QUERIES")
execute("MATCH (n) RETURN count(n)")
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.retrieve('QUERIES')").toList should have size 1
// when
execute("CALL db.stats.clear('QUERIES')").single
// then
execute("CALL db.stats.retrieve('QUERIES')").toList should be(empty)
}
test("should append queries if restarted collection") {
// given
assertCollecting("QUERIES")
execute("MATCH (n) RETURN count(n)")
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.retrieve('QUERIES')").toList should have size 1
// when
execute("CALL db.stats.collect('QUERIES')").single
execute("RETURN 'another query'")
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.retrieve('QUERIES')").toList should have size 2
}
test("should stop collection after specified time") {
// given
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.collect('QUERIES', {durationSeconds: 3})").single
execute("MATCH (n) RETURN count(n)")
Thread.sleep(4000)
// then
assertIdle("QUERIES")
// and when
execute("RETURN 'late query'")
// then
execute("CALL db.stats.retrieve('QUERIES')").toList should have size 1
}
test("should not stop later collection event after initial timeout") {
// given
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.collect('QUERIES', {durationSeconds: 3})").single
execute("MATCH (n) RETURN count(n)")
execute("CALL db.stats.stop('QUERIES')").single
execute("CALL db.stats.collect('QUERIES')").single
Thread.sleep(4000)
// then
assertCollecting("QUERIES")
}
test("should retrieve query execution plan and estimated rows") {
// given
execute("CALL db.stats.stop('QUERIES')").single
execute("CREATE (a), (b), (c)")
execute("CALL db.stats.collect('QUERIES')").single
execute("MATCH (n) RETURN sum(id(n))")
execute("MATCH (n) RETURN count(n)")
execute("CALL db.stats.stop('QUERIES')").single
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n) RETURN sum(id(n))",
"queryExecutionPlan" -> Map(
"id" -> 0,
"operator" -> "ProduceResults",
"lhs" -> Map(
"id" -> 1,
"operator" -> "EagerAggregation",
"lhs" -> Map(
"id" -> 2,
"operator" -> "AllNodesScan"
)
)
),
"estimatedRows" -> List(1.0, 1.0, 3.0)
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n) RETURN count(n)",
"queryExecutionPlan" -> Map(
"id" -> 0,
"operator" -> "ProduceResults",
"lhs" -> Map(
"id" -> 1,
"operator" -> "NodeCountFromCountStore"
)
),
"estimatedRows" -> List(1.0, 1.0)
)
)
)
}
test("should retrieve invocations of query") {
// given
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> "BrassLeg"))
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 2))
execute("WITH 42 AS x RETURN x")
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 3.1))
execute("WITH 42 AS x RETURN x")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n {p: $param}) RETURN count(n)",
"invocations" -> beInvocationsInOrder(
Map("param" -> "BrassLeg"),
Map("param" -> Long.box(2)),
Map("param" -> Double.box(3.1))
)
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "WITH 42 AS x RETURN x",
"invocations" -> beInvocationCount(2)
)
)
)
}
test("should retrieve invocation summary of query") {
// given
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> "BrassLeg"))
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 2))
execute("WITH 42 AS x RETURN x")
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 3.1))
execute("WITH 42 AS x RETURN x")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> QueryWithInvocationSummary("MATCH (n {p: $param}) RETURN count(n)")
),
beMapContaining(
"section" -> "QUERIES",
"data" -> QueryWithInvocationSummary("WITH 42 AS x RETURN x")
)
)
}
test("should limit number of retrieved invocations of query") {
// given
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> "BrassLeg"))
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 2))
execute("WITH 42 AS x RETURN x")
execute("MATCH (n {p: $param}) RETURN count(n)", Map("param" -> 3.1))
execute("WITH 42 AS x RETURN x")
execute("WITH 42 AS x RETURN x")
execute("WITH 42 AS x RETURN x")
// when
val res = execute("CALL db.stats.retrieve('QUERIES', {maxInvocations: 2})").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "MATCH (n {p: $param}) RETURN count(n)",
"invocations" -> beInvocationsInOrder(
Map("param" -> "BrassLeg"),
Map("param" -> 2)
)
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "WITH 42 AS x RETURN x",
"invocations" -> beInvocationCount(2)
)
)
)
}
test("should fail on incorrect maxInvocations") {
execute("CALL db.stats.retrieve('QUERIES', {})").toList // missing maxInvocations argument is fine
execute("CALL db.stats.retrieve('QUERIES', {maxIndications: -1})").toList // non-related arguments is fine
assertInvalidArgument("CALL db.stats.retrieve('QUERIES', {maxInvocations: 'non-integer'})") // non-integer is not fine
assertInvalidArgument("CALL db.stats.retrieve('QUERIES', {maxInvocations: -1})") // negative integer is not fine
}
test("should limit the collected query text size") {
// given
val largeQuery = (0 until 1000).map(i => s"CREATE (n$i) ").mkString("\n")
execute(largeQuery)
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> largeQuery.take(10000)
)
)
)
}
test("should limit the collected query text size by configured max_query_text_size") {
// given
restartWithConfig(Map(GraphDatabaseSettings.data_collector_max_query_text_size -> "33"))
val largeQuery = (0 until 10).map(i => s"CREATE (n$i) ").mkString("\n")
execute(largeQuery)
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> largeQuery.take(33)
)
)
)
}
test("should drop query text on max_query_text_size=0") {
// given
restartWithConfig(Map(GraphDatabaseSettings.data_collector_max_query_text_size -> "0"))
execute("RETURN 1")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining("query" -> "")
)
)
}
test("should distinguish queries even though dropped query text is not unique") {
// given
restartWithConfig(Map(GraphDatabaseSettings.data_collector_max_query_text_size -> "6"))
execute("RETURN 1+1 AS a", params = Map("p" -> 1))
execute("RETURN 1+2 AS a", params = Map("p" -> 2))
execute("RETURN 1+3 AS a", params = Map("p" -> 3))
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res.size shouldBe 3
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "RETURN",
"invocations" -> beInvocationsInOrder(Map("p" -> 1))
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "RETURN",
"invocations" -> beInvocationsInOrder(Map("p" -> 2))
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "RETURN",
"invocations" -> beInvocationsInOrder(Map("p" -> 3))
)
)
)
}
test("should limit the collected query parameter sizes") {
// given
val entities = execute(
"""CREATE (node:A {p: 'startNode'})-[relationship:R {p: 'rel'}]->(_:B {p: 'endNode'})
|WITH node, relationship
|MATCH path=()-->()
|RETURN node, relationship, path
""".stripMargin).single
val node = entities("node").asInstanceOf[Node]
val relationship = entities("relationship").asInstanceOf[Relationship]
val path = entities("path").asInstanceOf[Path]
val longString: String = "".padTo(200, 'x')
val query = "RETURN $param"
execute(query, params = Map("param" -> longString))
execute(query, params = Map("param" -> List(1,2,3)))
execute(query, params = Map("param" -> Map("x" -> 1)))
execute(query, params = Map("param" -> node))
execute(query, params = Map("param" -> relationship))
execute(query, params = Map("param" -> path))
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
println(res)
// then
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> query,
"invocations" -> beInvocationsInOrder(
Map("param" -> longString.take(100)),
Map("param" -> "§LIST[3]"),
Map("param" -> "§MAP[1]"),
Map("param" -> node),
Map("param" -> relationship),
Map("param" -> "§PATH[1]")
)
)
)
)
}
test("should respect the configured max_recent_query_count") {
// given
restartWithConfig(Map(GraphDatabaseSettings.data_collector_max_recent_query_count -> "4"))
execute("RETURN 1")
execute("RETURN 1, 2")
execute("RETURN 1, 2, 3")
execute("RETURN 1, 2, 3, 4")
execute("RETURN 1, 2, 3, 4, 5")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res.size shouldBe 4
res should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining("query" -> "RETURN 1, 2")
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining("query" -> "RETURN 1, 2, 3")
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining("query" -> "RETURN 1, 2, 3, 4")
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining("query" -> "RETURN 1, 2, 3, 4, 5")
)
)
}
test("should not collect queries for max_recent_query_count=0") {
// given
restartWithConfig(Map(GraphDatabaseSettings.data_collector_max_recent_query_count -> "0"))
execute("RETURN 1")
// when
val res = execute("CALL db.stats.retrieve('QUERIES')").toList
// then
res.size shouldBe 0
}
test("[retrieveAllAnonymized] should anonymize tokens inside queries") {
// given
execute("CREATE (:User {age: 99})-[:KNOWS]->(:Buddy {p: 42})-[:WANTS]->(:Raccoon)") // create tokens
execute("MATCH (:User)-[:KNOWS]->(:Buddy)-[:WANTS]->(:Raccoon) RETURN 1")
execute("MATCH ({p: 42}), ({age: 43}) RETURN 1")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
querySection("MATCH (:L0)-[:R0]->(:L1)-[:R1]->(:L2) RETURN 1"),
querySection("MATCH ({p1: 42}), ({p0: 43}) RETURN 1")
)
}
test("[retrieveAllAnonymized] should handle pre-parser options") {
// given
execute("CREATE (:User {age: 99})-[:KNOWS]->(:Buddy {p: 42})-[:WANTS]->(:Raccoon)") // create tokens
execute("EXPLAIN MATCH (:User)-[:KNOWS]->(:Buddy)-[:WANTS]->(:Raccoon) RETURN 1")
execute("CYPHER 3.4 runtime=interpreted PROFILE CREATE ()")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
querySection("EXPLAIN MATCH (:L0)-[:R0]->(:L1)-[:R1]->(:L2) RETURN 1"),
querySection("CYPHER 3.4 runtime=interpreted PROFILE CREATE ()")
)
}
test("[retrieveAllAnonymized] should handle load csv") {
// given
val path = Files.createTempFile("data", ".csv")
val url = path.toUri.toURL.toString
execute(s"LOAD CSV FROM '$url' AS row CREATE ({key: row[0]})")
execute(s"USING PERIODIC COMMIT 30 LOAD CSV FROM '$url' AS row CREATE ({key: row[0]})")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
val urlLength = url.length
res.toList should beListWithoutOrder(
querySection(s"LOAD CSV FROM 'string[$urlLength]' AS var0 CREATE ({UNKNOWN0: var0[0]})"),
querySection(s"USING PERIODIC COMMIT 30 LOAD CSV FROM 'string[$urlLength]' AS var0 CREATE ({UNKNOWN0: var0[0]})")
)
}
test("[retrieveAllAnonymized] should anonymize unknown tokens inside queries") {
// given
execute("MATCH (:User)-[:KNOWS]->(:Buddy)-[:WANTS]->(:Raccoon) RETURN 1")
execute("MATCH ({p: 42}), ({age: 43}) RETURN 1")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
querySection("MATCH (:UNKNOWN0)-[:UNKNOWN1]->(:UNKNOWN2)-[:UNKNOWN3]->(:UNKNOWN4) RETURN 1"),
querySection("MATCH ({UNKNOWN0: 42}), ({UNKNOWN1: 43}) RETURN 1")
)
}
test("[retrieveAllAnonymized] should anonymize string literals inside queries") {
// given
execute("RETURN 'Scrooge' AS uncle, 'Donald' AS name")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
querySection("RETURN 'string[7]' AS var0, 'string[6]' AS var1")
)
}
test("[retrieveAllAnonymized] should anonymize variables and return names") {
// given
execute("RETURN 42 AS x")
execute("WITH 42 AS x RETURN x")
execute("WITH 1 AS k RETURN k + k")
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
querySection("RETURN 42 AS var0"),
querySection("WITH 42 AS var0 RETURN var0"),
querySection("WITH 1 AS var0 RETURN var0 + var0")
)
}
test("[retrieveAllAnonymized] should anonymize parameters") {
// given
execute("RETURN 42 = $user, $name", Map("user" -> "BrassLeg", "name" -> "George"))
execute("RETURN 42 = $user, $name", Map("user" -> 2, "name" -> "Glinda"))
execute("RETURN 42 = $user, $name", Map("user" -> List(3.1, 3.2), "name" -> "Kim"))
execute("RETURN $user, $name, $user + $name", Map("user" -> List(3.1, 3.2), "name" -> "Kim"))
// when
val res = execute("CALL db.stats.retrieveAllAnonymized('myToken')")
// then
res.toList should beListWithoutOrder(
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "RETURN 42 = $param0, $param1",
"invocations" -> beInvocationsInOrder(
"4ac156c0", // Map("user" -> "BrassLeg", "name" -> "George")
"b439d06c", // Map("user" -> 2, "name" -> "Glinda")
"8e2eb26e" // Map("user" -> List(3.1, 3.2), "name" -> "Kim")
)
)
),
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> "RETURN $param0, $param1, $param0 + $param1",
"invocations" -> beInvocationsInOrder(
"8e2eb26e" // Map("user" -> List(3.1, 3.2), "name" -> "Kim")
)
)
)
)
}
private def querySection(queryText: String) =
beMapContaining(
"section" -> "QUERIES",
"data" -> beMapContaining(
"query" -> beCypher(queryText)
)
)
case class QueryWithInvocationSummary(expectedQuery: String) extends Matcher[AnyRef] {
override def apply(left: AnyRef): MatchResult = {
left match {
case m: Map[String, AnyRef] =>
val query = m("query")
if (query != expectedQuery)
return MatchResult(matches = false,
s"""Expected query
| $expectedQuery
|got
| $query""".stripMargin, "")
val invocations = m("invocations").asInstanceOf[Seq[Map[String, AnyRef]]]
val compileTimes = invocations.map(inv => inv("elapsedCompileTimeInUs").asInstanceOf[Long])
val executionTimes = invocations.map(inv => inv("elapsedExecutionTimeInUs").asInstanceOf[Long])
beMapContaining(
"compileTimeInUs" -> beMapContaining(
"min" -> compileTimes.min,
"max" -> compileTimes.max,
"avg" -> (compileTimes.sum / compileTimes.size)
),
"executionTimeInUs" -> beMapContaining(
"min" -> executionTimes.min,
"max" -> executionTimes.max,
"avg" -> (executionTimes.sum / executionTimes.size)
),
"invocationCount" -> invocations.size
).apply(m("invocationSummary"))
case _ =>
MatchResult(matches = false, s"Expected map, got $left", "")
}
}
}
def beInvocationCount(count: Int) = InvocationsMatcher((0 until count).map(_ => null))
def beInvocationsInOrder(expectedParams: AnyRef*) = InvocationsMatcher(expectedParams)
case class InvocationsMatcher(expectedParams: Seq[AnyRef]) extends Matcher[AnyRef] {
override def apply(left: AnyRef): MatchResult = {
val errors = new ArrayBuffer[String]
left match {
case values: Seq[AnyRef] =>
var previousInvocationTime = 0L
for (i <- expectedParams.indices) {
val expectedParam = expectedParams(i)
if (i < values.size) {
values(i) match {
case map: Map[String, AnyRef] =>
if (expectedParam != null) {
val params = map.getOrElse("params", null)
if (params != expectedParam)
errors += s"Expected invocation with params '$expectedParam' at position $i, but has params '$params'"
}
map("elapsedExecutionTimeInUs").asInstanceOf[Long] should be > 0L
map("elapsedCompileTimeInUs").asInstanceOf[Long] should be > 0L
map("startTimestampMillis").asInstanceOf[Long] should be > 0L
val invocationTime = map("startTimestampMillis").asInstanceOf[Long]
if (invocationTime < previousInvocationTime)
errors += s"Expected invocations to be ordered by start timestamp, but got invocation with timestamp $invocationTime ordered after invocation with timestamp $previousInvocationTime"
previousInvocationTime = invocationTime
case x => errors += s"Expected all invocations to be maps, but got $x"
}
} else
errors += s"Expected invocation with param '$expectedParam' at position $i, but list was too small"
}
if (values.size > expectedParams.size)
errors += s"Expected ${expectedParams.size} invocations, but got additional invocations ${values.slice(expectedParams.size, values.size)}"
case x =>
errors += s"Expected invocation list but got '$x'"
}
MatchResult(
matches = errors.isEmpty,
rawFailureMessage = "Encountered a bunch of errors: " + errors.map(" "+_).mkString("\n", "\n", "\n"),
rawNegatedFailureMessage = "BAH"
)
}
override def toString(): String = s"invocations with params (${expectedParams.mkString(", ")})"
}
}