summaryrefslogtreecommitdiff
path: root/tests/plugins/websockets/websocket_plugin_test.go
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/websocket_plugin_test.go
parent5527b31b2da2b60ed8877b8c43badb73f98ec7bb (diff)
- Tests for the ws-redis, ws-memory
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests/plugins/websockets/websocket_plugin_test.go')
-rw-r--r--tests/plugins/websockets/websocket_plugin_test.go294
1 files changed, 274 insertions, 20 deletions
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,
+ }
+}