Skip to content

Commit dc9766c

Browse files
committed
Fix failing tests. Prevent Windows line seperator settings from failing the tests in multiline string matches
1 parent d8e2ff3 commit dc9766c

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

Diff for: compiler/src/dotty/tools/dotc/profile/FileUtils.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean
2727
import scala.concurrent.duration.Duration
2828
import scala.concurrent.{Await, Promise}
2929
import scala.util.{Failure, Success}
30+
import scala.annotation.internal.sharable
3031

3132
object FileUtils {
3233
def newAsyncBufferedWriter(path: Path, charset: Charset = StandardCharsets.UTF_8.nn, options: Array[OpenOption] = NO_OPTIONS, threadsafe: Boolean = false): LineWriter = {
@@ -72,8 +73,8 @@ object FileUtils {
7273
}
7374

7475
private object AsyncBufferedWriter {
75-
private val Close = CharBuffer.allocate(0)
76-
private val Flush = CharBuffer.allocate(0)
76+
@sharable private val Close = CharBuffer.allocate(0)
77+
@sharable private val Flush = CharBuffer.allocate(0)
7778
}
7879
private class AsyncBufferedWriter(val underlying: Writer, bufferSize : Int = 4096) extends LineWriter {
7980
private var current: CharBuffer = allocate

Diff for: compiler/test/dotty/tools/dotc/profile/ChromeTraceTest.scala

+46-46
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,41 @@ import java.util.concurrent.locks.LockSupport
1010
import scala.concurrent.duration.*
1111

1212
class ChromeTraceTest:
13-
private def testTraceOutputs(generator: ChromeTrace => Unit)(checkContent: String => Unit): Unit = {
13+
private def testTraceOutputs(generator: ChromeTrace => Unit)(checkContent: PartialFunction[List[String], Unit]): Unit = {
1414
val outfile = Files.createTempFile("trace-", ".json").nn
1515
val tracer = new ChromeTrace(outfile)
1616
try generator(tracer)
1717
finally tracer.close()
18-
val content = scala.io.Source.fromFile(outfile.toFile().nn).mkString
19-
checkContent(content)
18+
val contentLines = scala.io.Source.fromFile(outfile.toFile().nn).getLines().toList
19+
checkContent.applyOrElse(contentLines, content => fail(s"Invalid output lines: ${content.mkString(System.lineSeparator())}"))
2020
}
2121

2222
@Test def traceCounterEvent(): Unit = testTraceOutputs{ tracer =>
2323
tracer.traceCounterEvent("foo", "counter1", 42, processWide = true)
2424
tracer.traceCounterEvent("bar", "counter2", 21, processWide = false)
2525
}{
26-
case s"""{"traceEvents":[
27-
{"cat":"scalac","name":"foo","ph":"C","tid":"${tid1}","pid":"${pid1}","ts":${ts1},"args":{"counter1":42}}
28-
,{"cat":"scalac","name":"bar","ph":"C","tid":"${tid2}","pid":"${pid2}","ts":${ts2},"args":{"counter2":21}}
29-
]}""" =>
30-
assertEquals(tid1, tid2)
31-
assertTrue(tid1.toIntOption.isDefined)
32-
assertNotEquals(pid1, pid2)
33-
assertTrue(pid1.toIntOption.isDefined)
34-
assertEquals(s"$pid1-$tid1", pid2)
35-
assertTrue(ts1.toLong < ts2.toLong)
26+
case """{"traceEvents":[""" ::
27+
s"""{"cat":"scalac","name":"foo","ph":"C","tid":"${tid1}","pid":"${pid1}","ts":${ts1},"args":{"counter1":42}}""" ::
28+
s""",{"cat":"scalac","name":"bar","ph":"C","tid":"${tid2}","pid":"${pid2}","ts":${ts2},"args":{"counter2":21}}""" ::
29+
"]}" :: Nil =>
30+
assertEquals(tid1, tid2)
31+
assertTrue(tid1.toIntOption.isDefined)
32+
assertNotEquals(pid1, pid2)
33+
assertTrue(pid1.toIntOption.isDefined)
34+
assertEquals(s"$pid1-$tid1", pid2)
35+
assertTrue(ts1.toLong < ts2.toLong)
3636
}
3737

3838
@Test def traceDurationEvent(): Unit = testTraceOutputs{ tracer =>
3939
tracer.traceDurationEvent(name = "name1", startNanos = 1000L, durationNanos = 2500L, tid = "this-thread")
4040
tracer.traceDurationEvent(name = "name2", startNanos = 1000L, durationNanos = 5000L, tid = "this-thread", pidSuffix = "pidSuffix")
4141
}{
42-
case s"""{"traceEvents":[
43-
{"cat":"scalac","name":"name1","ph":"X","tid":"this-thread","pid":"${pid1}","ts":1,"dur":2}
44-
,{"cat":"scalac","name":"name2","ph":"X","tid":"this-thread","pid":"${pid2}","ts":1,"dur":5}
45-
]}""" =>
46-
assertTrue(pid1.toIntOption.isDefined)
47-
assertEquals(s"$pid1-pidSuffix", pid2)
42+
case """{"traceEvents":[""" ::
43+
s"""{"cat":"scalac","name":"name1","ph":"X","tid":"this-thread","pid":"${pid1}","ts":1,"dur":2}""" ::
44+
s""",{"cat":"scalac","name":"name2","ph":"X","tid":"this-thread","pid":"${pid2}","ts":1,"dur":5}""" ::
45+
"]}" :: Nil =>
46+
assertTrue(pid1.toIntOption.isDefined)
47+
assertEquals(s"$pid1-pidSuffix", pid2)
4848
}
4949

5050
@Test def traceDurationEvents(): Unit = {
@@ -58,33 +58,33 @@ class ChromeTraceTest:
5858
LockSupport.parkNanos(8.millis.toNanos)
5959
tracer.traceDurationEventEnd(cat = "test1", name = "event1", colour = "RED", pidSuffix = "pid-suffix")
6060
}{
61-
case s"""{"traceEvents":[
62-
{"cat":"test1","name":"event1","ph":"B","pid":"${pid1}","tid":"${tid1}","ts":${ts1}}
63-
,{"cat":"test2","name":"event2","ph":"B","pid":"${pid2}","tid":"${tid2}","ts":${ts2},"cname":"RED"}
64-
,{"cat":"test2","name":"event2","ph":"E","pid":"${pid3}","tid":"${tid3}","ts":${ts3}}
65-
,{"cat":"test1","name":"event1","ph":"E","pid":"${pid4}","tid":"${tid4}","ts":${ts4},"cname":"RED"}
66-
]}""" =>
67-
val traceEnd = System.nanoTime()
68-
assertTrue(tid1.toIntOption.isDefined)
69-
assertEquals(pid1, pid3)
70-
assertTrue(pid1.endsWith(s"-$tid1"))
71-
assertEquals(pid2, pid4)
72-
assertTrue(pid2.endsWith("-pid-suffix"))
73-
List(tid1, tid2, tid3).foreach: tid =>
74-
assertEquals(tid4, tid)
75-
List(pid1, pid2, pid3, pid4).foreach: pid =>
76-
assertTrue(pid.takeWhile(_ != '-').toIntOption.isDefined)
61+
case """{"traceEvents":[""" ::
62+
s"""{"cat":"test1","name":"event1","ph":"B","pid":"${pid1}","tid":"${tid1}","ts":${ts1}}""" ::
63+
s""",{"cat":"test2","name":"event2","ph":"B","pid":"${pid2}","tid":"${tid2}","ts":${ts2},"cname":"RED"}""" ::
64+
s""",{"cat":"test2","name":"event2","ph":"E","pid":"${pid3}","tid":"${tid3}","ts":${ts3}}""" ::
65+
s""",{"cat":"test1","name":"event1","ph":"E","pid":"${pid4}","tid":"${tid4}","ts":${ts4},"cname":"RED"}""" ::
66+
"]}" :: Nil =>
67+
val traceEnd = System.nanoTime()
68+
assertTrue(tid1.toIntOption.isDefined)
69+
assertEquals(pid1, pid3)
70+
assertTrue(pid1.endsWith(s"-$tid1"))
71+
assertEquals(pid2, pid4)
72+
assertTrue(pid2.endsWith("-pid-suffix"))
73+
List(tid1, tid2, tid3).foreach: tid =>
74+
assertEquals(tid4, tid)
75+
List(pid1, pid2, pid3, pid4).foreach: pid =>
76+
assertTrue(pid.takeWhile(_ != '-').toIntOption.isDefined)
7777

78-
List(ts1, ts2, ts3, ts4).map(_.toLong) match {
79-
case all @ List(ts1, ts2, ts3, ts4) =>
80-
all.foreach: ts =>
81-
// Timestamps are presented using Epoch microsecondos
82-
assertTrue(ts >= testStart / 1000)
83-
assertTrue(ts <= traceEnd / 1000)
84-
assertTrue(ts2 >= ts1 + 2.millis.toMicros)
85-
assertTrue(ts3 >= ts2 + 4.millis.toMicros)
86-
assertTrue(ts4 >= ts3 + 8.millis.toMicros)
87-
case _ => fail("unreachable")
88-
}
78+
List(ts1, ts2, ts3, ts4).map(_.toLong) match {
79+
case all @ List(ts1, ts2, ts3, ts4) =>
80+
all.foreach: ts =>
81+
// Timestamps are presented using Epoch microsecondos
82+
assertTrue(ts >= testStart / 1000)
83+
assertTrue(ts <= traceEnd / 1000)
84+
assertTrue(ts2 >= ts1 + 2.millis.toMicros)
85+
assertTrue(ts3 >= ts2 + 4.millis.toMicros)
86+
assertTrue(ts4 >= ts3 + 8.millis.toMicros)
87+
case _ => fail("unreachable")
88+
}
8989
}
9090
}

0 commit comments

Comments
 (0)