summaryrefslogtreecommitdiff
path: root/examples/scala-js/sbt-plugin/src/test/scala/scala/scalajs/sbtplugin/test/env/ComTests.scala
blob: c16decd2ec59a459ab762adc085d91ea6f5a8956 (plain) (blame)
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
package scala.scalajs.sbtplugin.test.env

import scala.scalajs.tools.env._
import scala.scalajs.tools.io._
import scala.scalajs.tools.classpath.PartialClasspath
import scala.scalajs.tools.logging._

import org.junit.Test
import org.junit.Assert._

/** A couple of tests that test communication for mix-in into a test suite */
trait ComTests extends AsyncTests {

  protected def newJSEnv: ComJSEnv

  private def emptyCP = PartialClasspath.empty.resolve()

  private def comRunner(code: String) = {
    val codeVF = new MemVirtualJSFile("testScript.js").withContent(code)
    newJSEnv.comRunner(emptyCP, codeVF,
        new ScalaConsoleLogger(Level.Warn), ConsoleJSConsole)
  }

  private def assertThrowClosed(msg: String, body: => Unit): Unit = {
    val thrown = try {
      body
      false
    } catch {
      case _: ComJSEnv.ComClosedException =>
        true
    }

    assertTrue(msg, thrown)
  }

  @Test
  def comCloseJVMTest = {
    val com = comRunner(s"""
      scalajsCom.init(function(msg) { scalajsCom.send("received: " + msg); });
      scalajsCom.send("Hello World");
    """)

    com.start()

    assertEquals("Hello World", com.receive())

    for (i <- 0 to 10) {
      com.send(i.toString)
      assertEquals(s"received: $i", com.receive())
    }

    com.close()
    com.await()
  }

  def comCloseJSTestCommon(timeout: Long) = {
    val com = comRunner(s"""
      scalajsCom.init(function(msg) {});
      for (var i = 0; i < 10; ++i)
        scalajsCom.send("msg: " + i);
      scalajsCom.close();
    """)

    com.start()

    Thread.sleep(timeout)

    for (i <- 0 until 10)
      assertEquals(s"msg: $i", com.receive())

    assertThrowClosed("Expect receive to throw after closing of channel",
        com.receive())

    com.close()
    com.await()
  }

  @Test
  def comCloseJSTest = comCloseJSTestCommon(0)

  @Test
  def comCloseJSTestDelayed = comCloseJSTestCommon(1000)

  @Test
  def doubleCloseTest = {
    val n = 10
    val com = pingPongRunner(n)

    com.start()

    for (i <- 0 until n) {
      com.send("ping")
      assertEquals("pong", com.receive())
    }

    com.close()
    com.await()
  }

  @Test
  def multiEnvTest = {
    val n = 10
    val envs = List.fill(5)(pingPongRunner(10))

    envs.foreach(_.start())

    val ops = List[ComJSRunner => Unit](
        _.send("ping"),
        com => assertEquals("pong", com.receive())
    )

    for {
      i   <- 0 until n
      env <- envs
      op  <- ops
    } op(env)

    envs.foreach(_.close())
    envs.foreach(_.await())
  }

  private def pingPongRunner(count: Int) = {
    comRunner(s"""
      var seen = 0;
      scalajsCom.init(function(msg) {
        scalajsCom.send("pong");
        if (++seen >= $count)
          scalajsCom.close();
      });
    """)
  }

  @Test
  def largeMessageTest = {
    // 1KB data
    val baseMsg = new String(Array.tabulate(512)(_.toChar))
    val baseLen = baseMsg.length

    // Max message size: 1KB * 2^(2*iters+1) = 1MB
    val iters = 4

    val com = comRunner("""
      scalajsCom.init(function(msg) {
        scalajsCom.send(msg + msg);
      });
    """)

    com.start()

    com.send(baseMsg)

    def resultFactor(iters: Int) = Math.pow(2, 2 * iters + 1).toInt

    for (i <- 0 until iters) {
      val reply = com.receive()

      val factor = resultFactor(i)

      assertEquals(baseLen * factor, reply.length)

      for (j <- 0 until factor)
        assertEquals(baseMsg, reply.substring(j * baseLen, (j + 1) * baseLen))

      com.send(reply + reply)
    }

    val lastLen = com.receive().length
    assertEquals(baseLen * resultFactor(iters), lastLen)

    com.close()
    com.await()
  }

  @Test
  def noInitTest = {
    val com = comRunner("")

    com.start()
    com.send("Dummy")
    com.close()
    com.await()
  }

  @Test
  def stopTest = {
    val com = comRunner(s"""scalajsCom.init(function(msg) {});""")

    com.start()

    // Make sure the VM doesn't terminate.
    Thread.sleep(1000)

    assertTrue("VM should still be running", com.isRunning)

    // Stop VM instead of closing channel
    com.stop()

    try {
      com.await()
      fail("Stopped VM should be in failure state")
    } catch {
      case _: Throwable =>
    }
  }

}