Skip to content

Commit 4c7068f

Browse files
committed
Fix scala-js/scala-js#3420: Make timeout tests less restrictive
Originally they were written to test our own implementation of `setTimeout` for Rhino. Now they are just a means to run things after returning to the event loop once, so we can reduce them significantly.
1 parent d3b0c92 commit 4c7068f

File tree

2 files changed

+18
-103
lines changed

2 files changed

+18
-103
lines changed

js-envs-test-kit/src/main/scala/org/scalajs/jsenv/test/TimeoutComTests.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private[test] class TimeoutComTests(config: JSEnvSuiteConfig) {
1919

2020
@Test
2121
def delayedInitTest: Unit = {
22+
val deadline = 100.millis.fromNow
2223
val run = kit.start(s"""
2324
setTimeout(function() {
2425
scalajsCom.init(function(msg) {
@@ -28,9 +29,6 @@ private[test] class TimeoutComTests(config: JSEnvSuiteConfig) {
2829
""", RunConfig())
2930

3031
try {
31-
// Deadline only starts now. Execution must happen asynchronously.
32-
val deadline = 100.millis.fromNow
33-
3432
run.run.send("Hello World")
3533
assertEquals("Got: Hello World", run.waitNextMessage())
3634
assertTrue("Execution took too little time", deadline.isOverdue())
@@ -49,7 +47,7 @@ private[test] class TimeoutComTests(config: JSEnvSuiteConfig) {
4947

5048
try {
5149
for (i <- 1 to 10) {
52-
val deadline = 190.millis.fromNow // give some slack
50+
val deadline = 200.millis.fromNow
5351
run.run.send(s"Hello World: $i")
5452
assertEquals(s"Got: Hello World: $i", run.waitNextMessage())
5553
assertTrue("Execution took too little time", deadline.isOverdue())
@@ -61,14 +59,19 @@ private[test] class TimeoutComTests(config: JSEnvSuiteConfig) {
6159

6260
@Test
6361
def intervalSendTest: Unit = {
62+
val deadline = 250.millis.fromNow
63+
6464
val run = kit.start(s"""
6565
scalajsCom.init(function(msg) {});
66-
var interval = setInterval(scalajsCom.send, 50, "Hello");
67-
setTimeout(clearInterval, 295, interval);
66+
var sent = 0
67+
var interval = setInterval(function () {
68+
scalajsCom.send("Hello");
69+
sent++;
70+
if (sent >= 5) clearInterval(interval);
71+
}, 50);
6872
""", RunConfig())
6973

7074
try {
71-
val deadline = 245.millis.fromNow
7275
for (i <- 1 to 5)
7376
assertEquals("Hello", run.waitNextMessage())
7477

js-envs-test-kit/src/main/scala/org/scalajs/jsenv/test/TimeoutRunTests.scala

Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -38,110 +38,22 @@ private[test] class TimeoutRunTests(config: JSEnvSuiteConfig, withCom: Boolean)
3838

3939
}
4040

41-
@Test
42-
def clearTimeoutTest: Unit = {
43-
44-
val deadline = 300.millis.fromNow
45-
46-
"""
47-
var c = setTimeout(function() { console.log("1"); }, 200);
48-
setTimeout(function() {
49-
console.log("2");
50-
clearTimeout(c);
51-
}, 100);
52-
setTimeout(function() { console.log("3"); }, 300);
53-
setTimeout(function() { console.log("4"); }, 0);
54-
""" hasOutput
55-
"""|4
56-
|2
57-
|3
58-
|""".stripMargin
59-
60-
assertTrue("Execution took too little time", deadline.isOverdue())
61-
62-
}
63-
64-
@Test // #2368
65-
def timeoutSingleArgTest: Unit = {
66-
"""
67-
setTimeout(function() { console.log("ok"); });
68-
""" hasOutput "ok\n"
69-
}
70-
71-
@Test
72-
def timeoutArgTest: Unit = {
73-
74-
val deadline = 300.millis.fromNow
75-
76-
"""
77-
setTimeout(function(a, b) { console.log("1" + a + b); }, 200, "foo", "bar");
78-
setTimeout(function() { console.log("2"); }, 100);
79-
setTimeout(function(msg) { console.log(msg); }, 300, "Hello World");
80-
setTimeout(function() { console.log("4"); }, 0);
81-
""" hasOutput
82-
"""|4
83-
|2
84-
|1foobar
85-
|Hello World
86-
|""".stripMargin
87-
88-
assertTrue("Execution took too little time", deadline.isOverdue())
89-
90-
}
91-
9241
@Test
9342
def intervalTest: Unit = {
43+
val deadline = 100.millis.fromNow
9444

95-
val deadline = 1.second.fromNow
96-
45+
// We rely on the test kit to terminate the test after 5 iterations.
9746
"""
98-
var i1 = setInterval(function() { console.log("each 2200"); }, 2200);
99-
var i2 = setInterval(function() { console.log("each 3100"); }, 3100);
100-
var i3 = setInterval(function() { console.log("each 1300"); }, 1300);
101-
102-
setTimeout(function() {
103-
clearInterval(i1);
104-
clearInterval(i2);
105-
clearInterval(i3);
106-
}, 10000);
47+
setInterval(function() { console.log("tick"); }, 20);
10748
""" hasOutput
108-
"""|each 1300
109-
|each 2200
110-
|each 1300
111-
|each 3100
112-
|each 1300
113-
|each 2200
114-
|each 1300
115-
|each 3100
116-
|each 1300
117-
|each 2200
118-
|each 1300
119-
|each 2200
120-
|each 1300
121-
|each 3100
49+
"""|tick
50+
|tick
51+
|tick
52+
|tick
53+
|tick
12254
|""".stripMargin
12355

12456
assertTrue("Execution took too little time", deadline.isOverdue())
12557

12658
}
127-
128-
@Test
129-
def intervalSelfClearTest: Unit = {
130-
131-
val deadline = 100.millis.fromNow
132-
133-
"""
134-
var c = 0;
135-
var i = setInterval(function() {
136-
c++;
137-
console.log(c.toString());
138-
if (c >= 10)
139-
clearInterval(i);
140-
}, 10);
141-
""" hasOutput (1 to 10).map(_ + "\n").mkString
142-
143-
assertTrue("Execution took too little time", deadline.isOverdue())
144-
145-
}
146-
14759
}

0 commit comments

Comments
 (0)