Add websocket test (#2053)

* Add websocket test

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Use trap for exit code

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Remove readme

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Format, go version fix

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Update image name

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Use spec dir

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Change directory before running file

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>

* Use

Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>
This commit is contained in:
Harsh Thakur
2021-06-10 12:27:20 +05:30
committed by GitHub
parent 827b64d633
commit f01de5c802
10 changed files with 211 additions and 0 deletions
+1
View File
@@ -85,6 +85,7 @@ main() {
$ROOT/test/tests/mqtrigger/nats/test_mqtrigger_error.sh \
$ROOT/test/tests/test_huge_response/test_huge_response.sh \
$ROOT/test/tests/test_kubectl/test_kubectl.sh
$ROOT/test/tests/websocket/test_ws.sh
export JOBS=3
source $ROOT/test/run_test.sh \
+1
View File
@@ -540,6 +540,7 @@ run_all_tests() {
$ROOT/test/tests/mqtrigger/nats/test_mqtrigger_error.sh \
$ROOT/test/tests/test_huge_response/test_huge_response.sh \
$ROOT/test/tests/test_kubectl/test_kubectl.sh
$ROOT/test/tests/websocket/test_ws.sh
FAILURES=$?
export JOBS=3
+15
View File
@@ -0,0 +1,15 @@
module.exports = async function(ws, clients) {
ws.on('message', function incoming(data) {
clients.forEach(function each(client) {
client.send(data);
});
});
ws.on('close', function close() {
return {
status: 200,
message: "I am done"
}
});
}
+5
View File
@@ -0,0 +1,5 @@
module ws
go 1.15
require github.com/gorilla/websocket v1.4.2
+2
View File
@@ -0,0 +1,2 @@
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+68
View File
@@ -0,0 +1,68 @@
package main
import (
"log"
"os"
"time"
"github.com/gorilla/websocket"
)
func main() {
router := os.Getenv("FISSION_ROUTER")
if len(router) == 0 {
log.Fatal("FISSION_ROUTER variable is not set")
}
funcURL := "ws://" + router + "/fission-function/bs"
conn, _, err := websocket.DefaultDialer.Dial(funcURL, nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
done := make(chan struct{})
go func() {
defer close(done)
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
}
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
stop := time.After(10 * time.Second)
for i := 0; i < 30; i++ {
select {
case <-done:
return
case t := <-ticker.C:
err := conn.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Fatal("write:", err)
}
case <-stop:
log.Println("Closing")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Fatal("write close:", err)
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
@@ -0,0 +1,29 @@
apiVersion: fission.io/v1
kind: Environment
metadata:
creationTimestamp: null
name: nodejs
namespace: default
spec:
builder: {}
imagepullsecret: ""
keeparchive: false
poolsize: 3
resources:
# limits:
# cpu: 80m
# memory: 100Mi
requests:
cpu: 10m
memory: 80Mi
runtime:
image: fission/node-env:latest
podspec:
containers:
- name: nodejs
imagepullpolicy: Always
env:
- name: TIMEOUT
value: 1000
version: 1
@@ -0,0 +1,7 @@
# This file is generated by the 'fission spec init' command.
# See the README in this directory for background and usage information.
# Do not edit the UID below: that will break 'fission spec apply'
apiVersion: fission.io/v1
kind: DeploymentConfig
name: websocket
uid: 1df9464b-5f73-4623-8187-a2f431d5c828
@@ -0,0 +1,56 @@
include:
- broadcast.js
kind: ArchiveUploadSpec
name: broadcast-js-LHtQ
---
apiVersion: fission.io/v1
kind: Package
metadata:
creationTimestamp: null
name: bs-7baf1b6f-f876-436f-a2b2-ae1a3e3af8b7
namespace: default
spec:
deployment:
checksum: {}
type: url
url: archive://broadcast-js-LHtQ
environment:
name: nodejs
namespace: default
source:
checksum: {}
status:
buildstatus: none
lastUpdateTimestamp: "2021-04-07T10:13:22Z"
---
apiVersion: fission.io/v1
kind: Function
metadata:
creationTimestamp: null
name: bs
namespace: default
spec:
InvokeStrategy:
ExecutionStrategy:
ExecutorType: poolmgr
MaxScale: 0
MinScale: 0
SpecializationTimeout: 120
TargetCPUPercent: 0
StrategyType: execution
concurrency: 5
configmaps: null
environment:
name: nodejs
namespace: default
functionTimeout: 60
idletimeout: 120
package:
packageref:
name: bs-7baf1b6f-f876-436f-a2b2-ae1a3e3af8b7
namespace: default
requestsPerPod: 5
resources: {}
secrets: null
+27
View File
@@ -0,0 +1,27 @@
#!/bin/bash
#
# Create a function and trigger it using NATS
#
set -euo pipefail
source $(dirname $0)/../../utils.sh
cleanup() {
log "Deleting websocket setup"
fission spec destroy
}
DIR=$(dirname $0)
if [ -z "${TEST_NOCLEANUP:-}" ]; then
trap cleanup EXIT
else
log "TEST_NOCLEANUP is set; not cleaning up test artifacts afterwards."
fi
log "Creating websocket setup.."
fission spec apply --specdir=$DIR/specs
log "Testing websocket connection"
cd $DIR && go run main.go