You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2017/12/09 06:23:06 UTC
[GitHub] dubeejw closed pull request #250: Update websocket test to use local websocket server
dubeejw closed pull request #250: Update websocket test to use local websocket server
URL: https://github.com/apache/incubator-openwhisk-catalog/pull/250
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/.gitignore b/.gitignore
index 879f234..b3c6415 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,10 @@ tests/.classpath
tests/.gitignore
tests/.project
tests/.settings/
+
+# NodeJS
+node_modules/
+
+# IntelliJ
+.idea
+out/
\ No newline at end of file
diff --git a/.scalafmt.conf b/.scalafmt.conf
new file mode 100644
index 0000000..0dc41ec
--- /dev/null
+++ b/.scalafmt.conf
@@ -0,0 +1,6 @@
+style = intellij
+danglingParentheses = false
+maxColumn = 120
+docstrings = JavaDoc
+rewrite.rules = [SortImports]
+project.git = true
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index a15e4c4..edb2a67 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,8 +3,6 @@
sudo: required
language: scala
-scala:
- - 2.11.6
services:
- docker
diff --git a/settings.gradle b/settings.gradle
index 81f4288..093728f 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -11,6 +11,6 @@ include 'tests'
rootProject.name = 'openwhisk-catalog'
gradle.ext.scala = [
- version: '2.11.8',
+ version: '2.11.11',
compileFlags: ['-feature', '-unchecked', '-deprecation', '-Xfatal-warnings', '-Ywarn-unused-import']
]
diff --git a/tests/src/test/scala/packages/websocket/WebSocketTests.scala b/tests/src/test/scala/packages/websocket/WebSocketTests.scala
index a9a1767..54353eb 100644
--- a/tests/src/test/scala/packages/websocket/WebSocketTests.scala
+++ b/tests/src/test/scala/packages/websocket/WebSocketTests.scala
@@ -17,81 +17,141 @@
package packages.websocket
+import java.io.File
import java.net.URI
import scala.concurrent.duration.DurationInt
import scala.language.postfixOps
-
import org.junit.runner.RunWith
import org.scalatest.BeforeAndAfterAll
import org.scalatest.junit.JUnitRunner
-
-import common.TestHelpers
-import common.Wsk
-import common.WskProps
-import common.WskTestHelpers
+import common._
import spray.json._
import spray.json.DefaultJsonProtocol._
@RunWith(classOf[JUnitRunner])
-class WebSocketTests
- extends TestHelpers
- with WskTestHelpers
- with BeforeAndAfterAll {
-
- implicit val wskprops = WskProps()
- val wsk = new Wsk()
-
- val websocketSendAction = "/whisk.system/websocket/send"
-
- behavior of "Websocket action"
-
- /**
- * This test requires a websocket server running on the given URI.
- */
- var serverURI: URI = new URI("ws://169.46.21.246:80")
-
- ignore should "Use the websocket action to send a payload" in {
- val uniquePayload = s"The cow says ${System.currentTimeMillis()}".toJson
- val run = wsk.action.invoke(websocketSendAction, Map("uri" -> serverURI.toString.toJson, "payload" -> uniquePayload))
- withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) {
- activation =>
- activation.response.success shouldBe true
- activation.response.result shouldBe Some(JsObject(
- "payload" -> uniquePayload))
- }
+class WebSocketTests extends TestHelpers with WskTestHelpers with BeforeAndAfterAll {
+
+ implicit val wskprops: WskProps = WskProps()
+ val wsk = new Wsk()
+
+ val websocketSendAction = "/whisk.system/websocket/send"
+
+ val containerName = "websocketServerOW"
+ val userDir: String = System.getProperty("user.dir")
+ val websocketDir: String = userDir + "/dat/websocket"
+ val websocketHost: String = wskprops.apihost
+ val websocketPort = "20002"
+
+ behavior of "Websocket action"
+
+ /**
+ * This test requires a websocket server running on the given URI.
+ */
+ val serverURI: URI = new URI(s"ws://$websocketHost:$websocketPort")
+
+ it should "Use the websocket action to send a payload" in {
+ val uniquePayload = s"The cow says ${System.currentTimeMillis()}".toJson
+ val run =
+ wsk.action.invoke(websocketSendAction, Map("uri" -> serverURI.toString.toJson, "payload" -> uniquePayload))
+ withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) { activation =>
+ activation.response.success shouldBe true
+ activation.response.result shouldBe Some(JsObject("payload" -> uniquePayload))
}
+ }
- it should "Return an error due to a malformed URI" in {
- val badURI = new URI("ws://localhost:80")
+ it should "Return an error due to a malformed URI" in {
+ val badURI = new URI("ws://localhost:80")
- val run = wsk.action.invoke(websocketSendAction, Map("uri" -> badURI.toString.toJson, "payload" -> "This is the message to send".toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
+ val run = wsk.action.invoke(
+ websocketSendAction,
+ Map("uri" -> badURI.toString.toJson, "payload" -> "This is the message to send".toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
- // the exact error content comes from the ws Node module
- activation.response.result.get.fields.get("error") shouldBe defined
- }
+ // the exact error content comes from the ws Node module
+ activation.response.result.get.fields.get("error") shouldBe defined
}
+ }
- it should "Require a payload parameter" in {
- val run = wsk.action.invoke(websocketSendAction, Map("uri" -> serverURI.toString.toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
- activation.response.result shouldBe Some(JsObject(
- "error" -> "You must specify a payload parameter.".toJson))
- }
+ it should "Require a payload parameter" in {
+ val run = wsk.action.invoke(websocketSendAction, Map("uri" -> serverURI.toString.toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
+ activation.response.result shouldBe Some(JsObject("error" -> "You must specify a payload parameter.".toJson))
}
+ }
- it should "Require a uri parameter" in {
- val run = wsk.action.invoke(websocketSendAction, Map("payload" -> "This is the message to send".toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
- activation.response.result shouldBe Some(JsObject(
- "error" -> "You must specify a uri parameter.".toJson))
- }
+ it should "Require a uri parameter" in {
+ val run = wsk.action.invoke(websocketSendAction, Map("payload" -> "This is the message to send".toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
+ activation.response.result shouldBe Some(JsObject("error" -> "You must specify a uri parameter.".toJson))
}
+ }
+
+ override def beforeAll() {
+
+ val websocketServer =
+ """
+ | const WebSocket = require("ws");
+ | const wss = new WebSocket.Server({ port: 8080 });
+ | wss.on("connection", function connection(ws) {
+ | ws.on("message", function incoming(message) {
+ | ws.send(message);
+ | });
+ | });
+ """.replace("\r", "").stripMargin.lines.mkString
+ TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "kill", containerName)
+ TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "rm", containerName)
+ TestUtils.runCmd(
+ 0,
+ new File("."),
+ "docker",
+ "run",
+ "-d",
+ "-p",
+ "20002:8080",
+ "--name",
+ containerName,
+ "node",
+ "sh",
+ "-c",
+ s"npm install ws; echo '$websocketServer' | node")
+ sleepUntilContainerRunning()
+
+ }
+
+ def sleepUntilContainerRunning() {
+ var counter = 12
+ var running = false
+ do {
+ counter = counter - 1
+ val isdb2Running = TestUtils
+ .runCmd(
+ TestUtils.DONTCARE_EXIT,
+ new File("."),
+ "docker",
+ "exec",
+ "-t",
+ containerName,
+ "curl",
+ "http://localhost:8080",
+ "--connect-timeout",
+ "5")
+
+ if (isdb2Running.exitCode != 0) {
+ println("sleeping 5 seconds to wait for websocket server")
+ Thread.sleep(5000)
+ } else {
+ running = true
+ }
+ } while (counter > 0 && !running)
+ running shouldBe true
+ }
+
+ override def afterAll() {
+ TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "kill", containerName)
+ TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "rm", containerName)
+ }
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services