summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-05-27 19:59:04 +0300
committerValery Piashchynski <[email protected]>2021-05-27 19:59:04 +0300
commite701d4d97fbbc7551e5d931731890933687ca8cd (patch)
tree8161a022a5b74ce6660ed154344134ec493c3f74 /tests
parent1ee9f178f95c777d60afae4ac3a74e6134001e27 (diff)
- Update Makefile
- Update arch diagram Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/plugins/websockets/configs/.rr-websockets-init.yaml16
-rw-r--r--tests/plugins/websockets/websocket_plugin_test.go63
2 files changed, 61 insertions, 18 deletions
diff --git a/tests/plugins/websockets/configs/.rr-websockets-init.yaml b/tests/plugins/websockets/configs/.rr-websockets-init.yaml
index 9973b2dc..1b60d3e7 100644
--- a/tests/plugins/websockets/configs/.rr-websockets-init.yaml
+++ b/tests/plugins/websockets/configs/.rr-websockets-init.yaml
@@ -1,7 +1,6 @@
rpc:
listen: tcp://127.0.0.1:6001
-
server:
command: "php ../../psr-worker-bench.php"
user: ""
@@ -10,11 +9,9 @@ server:
relay_timeout: "20s"
http:
- address: 127.0.0.1:15395
+ address: 127.0.0.1:11111
max_request_size: 1024
- middleware: ["websockets"]
- uploads:
- forbid: [ ".php", ".exe", ".bat" ]
+ middleware: [ "websockets" ]
trusted_subnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
pool:
num_workers: 2
@@ -30,16 +27,9 @@ websockets:
# pubsubs should implement PubSub interface to be collected via endure.Collects
# also, they should implement RPC methods to publish data into them
# pubsubs might use general config section or its own
-
pubsubs: [ "redis" ]
-
- # sample of the own config section for the redis pubsub driver
-
- redis:
- addrs:
- - localhost:1111
- # path used as websockets path
path: "/ws"
+
logs:
mode: development
level: debug
diff --git a/tests/plugins/websockets/websocket_plugin_test.go b/tests/plugins/websockets/websocket_plugin_test.go
index a9b90fd0..97577be8 100644
--- a/tests/plugins/websockets/websocket_plugin_test.go
+++ b/tests/plugins/websockets/websocket_plugin_test.go
@@ -1,6 +1,8 @@
package websockets
import (
+ "net/http"
+ "net/url"
"os"
"os/signal"
"sync"
@@ -8,14 +10,18 @@ import (
"testing"
"time"
+ "github.com/fasthttp/websocket"
+ json "github.com/json-iterator/go"
endure "github.com/spiral/endure/pkg/container"
"github.com/spiral/roadrunner/v2/plugins/config"
httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
+ "github.com/spiral/roadrunner/v2/plugins/kv/drivers/memory"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/plugins/redis"
rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc"
"github.com/spiral/roadrunner/v2/plugins/server"
"github.com/spiral/roadrunner/v2/plugins/websockets"
+ "github.com/spiral/roadrunner/v2/utils"
"github.com/stretchr/testify/assert"
)
@@ -36,6 +42,7 @@ func TestBroadcastInit(t *testing.T) {
&redis.Plugin{},
&websockets.Plugin{},
&httpPlugin.Plugin{},
+ &memory.Plugin{},
)
assert.NoError(t, err)
@@ -84,19 +91,65 @@ func TestBroadcastInit(t *testing.T) {
}
}()
- time.Sleep(time.Second * 1000)
- t.Run("test1", test1)
- t.Run("test2", test2)
+ time.Sleep(time.Second * 1)
+ t.Run("TestWSInit", wsInit)
stopCh <- struct{}{}
wg.Wait()
}
-func test1(t *testing.T) {
+type Msg struct {
+ // Topic message been pushed into.
+ T []string `json:"topic"`
+ // Command (join, leave, headers)
+ C string `json:"command"`
+
+ // Broker (redis, memory)
+ B string `json:"broker"`
+
+ // Payload to be broadcasted
+ P []byte `json:"payload"`
}
-func test2(t *testing.T) {
+func wsInit(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
+ }
+
+ connURL := url.URL{Scheme: "ws", Host: "localhost:11111", Path: "/ws"}
+
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
+
+ defer func() {
+ _ = resp.Body.Close()
+ }()
+
+ m := &Msg{
+ T: []string{"foo", "foo2"},
+ C: "join",
+ B: "memory",
+ P: []byte("hello websockets"),
+ }
+ d, err := json.Marshal(m)
+ if err != nil {
+ panic(err)
+ }
+
+ err = c.WriteMessage(websocket.BinaryMessage, d)
+ assert.NoError(t, err)
+
+ _, msg, err := c.ReadMessage()
+ retMsg := utils.AsString(msg)
+ assert.NoError(t, err)
+
+ // subscription done
+ assert.Equal(t, `{"topic":"@join","payload":["foo","foo2"]}`, retMsg)
+
+ err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
+ assert.NoError(t, err)
}