summaryrefslogtreecommitdiff
path: root/tests/plugins/websockets
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-05-28 13:19:02 +0300
committerValery Piashchynski <[email protected]>2021-05-28 13:19:02 +0300
commit0a64bb2a71ddb6b0ee5861e255a20df1327aa099 (patch)
tree98a607f517706174215b92be8f337d21f918b955 /tests/plugins/websockets
parent5527b31b2da2b60ed8877b8c43badb73f98ec7bb (diff)
- Tests for the ws-redis, ws-memory
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests/plugins/websockets')
-rw-r--r--tests/plugins/websockets/configs/.rr-websockets-init.yaml3
-rw-r--r--tests/plugins/websockets/configs/.rr-websockets-redis-memory.yaml39
-rw-r--r--tests/plugins/websockets/websocket_plugin_test.go294
3 files changed, 314 insertions, 22 deletions
diff --git a/tests/plugins/websockets/configs/.rr-websockets-init.yaml b/tests/plugins/websockets/configs/.rr-websockets-init.yaml
index 1b60d3e7..dc073be3 100644
--- a/tests/plugins/websockets/configs/.rr-websockets-init.yaml
+++ b/tests/plugins/websockets/configs/.rr-websockets-init.yaml
@@ -25,14 +25,13 @@ redis:
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" ]
path: "/ws"
logs:
mode: development
- level: debug
+ level: error
endure:
grace_period: 120s
diff --git a/tests/plugins/websockets/configs/.rr-websockets-redis-memory.yaml b/tests/plugins/websockets/configs/.rr-websockets-redis-memory.yaml
new file mode 100644
index 00000000..eedf5377
--- /dev/null
+++ b/tests/plugins/websockets/configs/.rr-websockets-redis-memory.yaml
@@ -0,0 +1,39 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../psr-worker-bench.php"
+ user: ""
+ group: ""
+ relay: "pipes"
+ relay_timeout: "20s"
+
+http:
+ address: 127.0.0.1:13235
+ max_request_size: 1024
+ 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
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+redis:
+ addrs:
+ - "localhost:6379"
+
+websockets:
+ # pubsubs should implement PubSub interface to be collected via endure.Collects
+ # pubsubs might use general config section
+ pubsubs: [ "redis", "memory" ]
+ path: "/ws"
+
+logs:
+ mode: development
+ level: error
+
+endure:
+ grace_period: 120s
+ print_graph: false
+ log_level: error
diff --git a/tests/plugins/websockets/websocket_plugin_test.go b/tests/plugins/websockets/websocket_plugin_test.go
index 97577be8..294bbf0d 100644
--- a/tests/plugins/websockets/websocket_plugin_test.go
+++ b/tests/plugins/websockets/websocket_plugin_test.go
@@ -1,7 +1,9 @@
package websockets
import (
+ "net"
"net/http"
+ "net/rpc"
"net/url"
"os"
"os/signal"
@@ -13,10 +15,11 @@ import (
"github.com/fasthttp/websocket"
json "github.com/json-iterator/go"
endure "github.com/spiral/endure/pkg/container"
+ goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
"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/memory"
"github.com/spiral/roadrunner/v2/plugins/redis"
rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc"
"github.com/spiral/roadrunner/v2/plugins/server"
@@ -25,8 +28,22 @@ import (
"github.com/stretchr/testify/assert"
)
+type Msg struct {
+ // Topic message been pushed into.
+ Topics_ []string `json:"topic"`
+
+ // Command (join, leave, headers)
+ Command_ string `json:"command"`
+
+ // Broker (redis, memory)
+ Broker_ string `json:"broker"`
+
+ // Payload to be broadcasted
+ Payload_ []byte `json:"payload"`
+}
+
func TestBroadcastInit(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.InfoLevel))
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)
cfg := &config.Viper{
@@ -42,8 +59,8 @@ func TestBroadcastInit(t *testing.T) {
&redis.Plugin{},
&websockets.Plugin{},
&httpPlugin.Plugin{},
- &memory.Plugin{},
)
+
assert.NoError(t, err)
err = cont.Init()
@@ -99,27 +116,122 @@ func TestBroadcastInit(t *testing.T) {
wg.Wait()
}
-type Msg struct {
- // Topic message been pushed into.
- T []string `json:"topic"`
+func wsInit(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
+ }
- // Command (join, leave, headers)
- C string `json:"command"`
+ connURL := url.URL{Scheme: "ws", Host: "localhost:11111", Path: "/ws"}
- // Broker (redis, memory)
- B string `json:"broker"`
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
- // Payload to be broadcasted
- P []byte `json:"payload"`
+ defer func() {
+ _ = resp.Body.Close()
+ }()
+
+ d, err := json.Marshal(message("join", "memory", []byte("hello websockets"), "foo", "foo2"))
+ 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)
}
-func wsInit(t *testing.T) {
+func TestWSRedisAndMemory(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-websockets-redis-memory.yaml",
+ Prefix: "rr",
+ }
+
+ err = cont.RegisterAll(
+ cfg,
+ &rpcPlugin.Plugin{},
+ &logger.ZapLogger{},
+ &server.Plugin{},
+ &redis.Plugin{},
+ &websockets.Plugin{},
+ &httpPlugin.Plugin{},
+ &memory.Plugin{},
+ )
+ assert.NoError(t, err)
+
+ err = cont.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ch, err := cont.Serve()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ sig := make(chan os.Signal, 1)
+ signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case e := <-ch:
+ assert.Fail(t, "error", e.Error.Error())
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ case <-sig:
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ case <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second * 1)
+ t.Run("RPCWsMemory", RPCWsMemory)
+ t.Run("RPCWsRedis", RPCWsRedis)
+
+ stopCh <- struct{}{}
+
+ wg.Wait()
+}
+
+func RPCWsMemory(t *testing.T) {
da := websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: time.Second * 20,
}
- connURL := url.URL{Scheme: "ws", Host: "localhost:11111", Path: "/ws"}
+ connURL := url.URL{Scheme: "ws", Host: "localhost:13235", Path: "/ws"}
c, resp, err := da.Dial(connURL.String(), nil)
assert.NoError(t, err)
@@ -128,14 +240,79 @@ func wsInit(t *testing.T) {
_ = resp.Body.Close()
}()
- m := &Msg{
- T: []string{"foo", "foo2"},
- C: "join",
- B: "memory",
- P: []byte("hello websockets"),
+ d, err := json.Marshal(message("join", "memory", []byte("hello websockets"), "foo", "foo2"))
+ 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)
+
+ publish("", "memory", "foo")
+
+ // VERIFY a message
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "hello, PHP", retMsg)
+
+ // //// LEAVE foo, foo2 /////////
+ d, err = json.Marshal(message("leave", "memory", []byte("hello websockets"), "foo"))
+ 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":"@leave","payload":["foo"]}`, retMsg)
+
+ // TRY TO PUBLISH TO UNSUBSCRIBED TOPIC
+ publish("", "memory", "foo")
+
+ go func() {
+ time.Sleep(time.Second * 5)
+ publish2("", "memory", "foo2")
+ }()
+
+ // should be only message from the subscribed foo2 topic
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "hello, PHP2", retMsg)
+
+ err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
+ assert.NoError(t, err)
+}
+
+func RPCWsRedis(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
}
- d, err := json.Marshal(m)
+ connURL := url.URL{Scheme: "ws", Host: "localhost:13235", Path: "/ws"}
+
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
+
+ defer func() {
+ _ = resp.Body.Close()
+ }()
+
+ d, err := json.Marshal(message("join", "redis", []byte("hello websockets"), "foo", "foo2"))
if err != nil {
panic(err)
}
@@ -150,6 +327,83 @@ func wsInit(t *testing.T) {
// subscription done
assert.Equal(t, `{"topic":"@join","payload":["foo","foo2"]}`, retMsg)
+ publish("", "redis", "foo")
+
+ // VERIFY a message
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "hello, PHP", retMsg)
+
+ // //// LEAVE foo, foo2 /////////
+ d, err = json.Marshal(message("leave", "redis", []byte("hello websockets"), "foo"))
+ 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":"@leave","payload":["foo"]}`, retMsg)
+
+ // TRY TO PUBLISH TO UNSUBSCRIBED TOPIC
+ publish("", "redis", "foo")
+
+ go func() {
+ time.Sleep(time.Second * 5)
+ publish2("", "redis", "foo2")
+ }()
+
+ // should be only message from the subscribed foo2 topic
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "hello, PHP2", retMsg)
+
err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
assert.NoError(t, err)
}
+
+func publish(command string, broker string, topics ...string) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ if err != nil {
+ panic(err)
+ }
+
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ var ret bool
+ err = client.Call("websockets.Publish", []*Msg{message(command, broker, []byte("hello, PHP"), topics...)}, &ret)
+ if err != nil {
+ panic(err)
+ }
+}
+
+func publish2(command string, broker string, topics ...string) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ if err != nil {
+ panic(err)
+ }
+
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ var ret bool
+ err = client.Call("websockets.Publish", []*Msg{message(command, broker, []byte("hello, PHP2"), topics...)}, &ret)
+ if err != nil {
+ panic(err)
+ }
+}
+
+func message(command string, broker string, payload []byte, topics ...string) *Msg {
+ return &Msg{
+ Topics_: topics,
+ Command_: command,
+ Broker_: broker,
+ Payload_: payload,
+ }
+}