summaryrefslogtreecommitdiff
path: root/tests/plugins/websockets/websocket_plugin_test.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-06-21 08:49:47 +0300
committerGitHub <[email protected]>2021-06-21 08:49:47 +0300
commit591b69be9279158cfe9e7083152d308c4ee480b7 (patch)
tree5c6b1d4ee2aea4e6a1cc828ca1fcb2306ef9741e /tests/plugins/websockets/websocket_plugin_test.go
parent25e0841c6aa5e2686da5b9f74e3d77d3814ff592 (diff)
parent2ab22ac9e935efb126b51e9c3521073e6a5155a1 (diff)
#732 feat(plugin): rework `broadcast` plugin
#732 feat(plugin): rework `broadcast` plugin
Diffstat (limited to 'tests/plugins/websockets/websocket_plugin_test.go')
-rw-r--r--tests/plugins/websockets/websocket_plugin_test.go710
1 files changed, 317 insertions, 393 deletions
diff --git a/tests/plugins/websockets/websocket_plugin_test.go b/tests/plugins/websockets/websocket_plugin_test.go
index 8321297d..29bf28be 100644
--- a/tests/plugins/websockets/websocket_plugin_test.go
+++ b/tests/plugins/websockets/websocket_plugin_test.go
@@ -16,7 +16,7 @@ import (
json "github.com/json-iterator/go"
endure "github.com/spiral/endure/pkg/container"
goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
- websocketsv1 "github.com/spiral/roadrunner/v2/pkg/proto/websockets/v1beta"
+ "github.com/spiral/roadrunner/v2/plugins/broadcast"
"github.com/spiral/roadrunner/v2/plugins/config"
httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
"github.com/spiral/roadrunner/v2/plugins/logger"
@@ -25,6 +25,7 @@ import (
rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc"
"github.com/spiral/roadrunner/v2/plugins/server"
"github.com/spiral/roadrunner/v2/plugins/websockets"
+ websocketsv1 "github.com/spiral/roadrunner/v2/proto/websockets/v1beta"
"github.com/spiral/roadrunner/v2/utils"
"github.com/stretchr/testify/assert"
)
@@ -47,6 +48,7 @@ func TestBroadcastInit(t *testing.T) {
&websockets.Plugin{},
&httpPlugin.Plugin{},
&memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -98,52 +100,20 @@ func TestBroadcastInit(t *testing.T) {
time.Sleep(time.Second * 1)
t.Run("TestWSInit", wsInit)
+ t.Run("RPCWsMemoryPubAsync", RPCWsPubAsync("11111"))
+ t.Run("RPCWsMemory", RPCWsPub("11111"))
stopCh <- struct{}{}
wg.Wait()
}
-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()
- }()
-
- d, err := json.Marshal(messageWS("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 TestWSRedisAndMemory(t *testing.T) {
+func TestWSRedis(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",
+ Path: "configs/.rr-websockets-redis.yaml",
Prefix: "rr",
}
@@ -155,7 +125,7 @@ func TestWSRedisAndMemory(t *testing.T) {
&redis.Plugin{},
&websockets.Plugin{},
&httpPlugin.Plugin{},
- &memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -205,21 +175,20 @@ func TestWSRedisAndMemory(t *testing.T) {
}()
time.Sleep(time.Second * 1)
- t.Run("RPCWsMemoryPubAsync", RPCWsMemoryPubAsync)
- t.Run("RPCWsMemory", RPCWsMemory)
- t.Run("RPCWsRedis", RPCWsRedis)
+ t.Run("RPCWsRedisPubAsync", RPCWsPubAsync("13235"))
+ t.Run("RPCWsRedisPub", RPCWsPub("13235"))
stopCh <- struct{}{}
wg.Wait()
}
-func TestWSRedisAndMemoryGlobal(t *testing.T) {
+func TestWSRedisNoSection(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",
+ Path: "configs/.rr-websockets-broker-no-section.yaml",
Prefix: "rr",
}
@@ -231,7 +200,37 @@ func TestWSRedisAndMemoryGlobal(t *testing.T) {
&redis.Plugin{},
&websockets.Plugin{},
&httpPlugin.Plugin{},
+ &broadcast.Plugin{},
+ )
+ assert.NoError(t, err)
+
+ err = cont.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = cont.Serve()
+ assert.Error(t, err)
+}
+
+func TestWSDeny(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-websockets-deny.yaml",
+ Prefix: "rr",
+ }
+
+ err = cont.RegisterAll(
+ cfg,
+ &rpcPlugin.Plugin{},
+ &logger.ZapLogger{},
+ &server.Plugin{},
+ &websockets.Plugin{},
+ &httpPlugin.Plugin{},
&memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -281,21 +280,19 @@ func TestWSRedisAndMemoryGlobal(t *testing.T) {
}()
time.Sleep(time.Second * 1)
- t.Run("RPCWsMemoryPubAsync", RPCWsMemoryPubAsync)
- t.Run("RPCWsMemory", RPCWsMemory)
- t.Run("RPCWsRedis", RPCWsRedis)
+ t.Run("RPCWsMemoryDeny", RPCWsDeny("15587"))
stopCh <- struct{}{}
wg.Wait()
}
-func TestWSRedisNoSection(t *testing.T) {
+func TestWSDeny2(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)
cfg := &config.Viper{
- Path: "configs/.rr-websockets-redis-no-section.yaml",
+ Path: "configs/.rr-websockets-deny2.yaml",
Prefix: "rr",
}
@@ -304,10 +301,10 @@ func TestWSRedisNoSection(t *testing.T) {
&rpcPlugin.Plugin{},
&logger.ZapLogger{},
&server.Plugin{},
- &redis.Plugin{},
&websockets.Plugin{},
&httpPlugin.Plugin{},
- &memory.Plugin{},
+ &redis.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -316,234 +313,60 @@ func TestWSRedisNoSection(t *testing.T) {
t.Fatal(err)
}
- _, err = cont.Serve()
- assert.Error(t, err)
-}
-
-func RPCWsMemoryPubAsync(t *testing.T) {
- da := websocket.Dialer{
- Proxy: http.ProxyFromEnvironment,
- HandshakeTimeout: time.Second * 20,
- }
-
- 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(messageWS("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)
-
- publishAsync(t, "", "memory", "foo")
-
- // VERIFY a makeMessage
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
-
- // //// LEAVE foo, foo2 /////////
- d, err = json.Marshal(messageWS("leave", "memory", []byte("hello websockets"), "foo"))
+ ch, err := cont.Serve()
if err != nil {
- panic(err)
+ t.Fatal(err)
}
- err = c.WriteMessage(websocket.BinaryMessage, d)
- assert.NoError(t, err)
-
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
+ sig := make(chan os.Signal, 1)
+ signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
- // subscription done
- assert.Equal(t, `{"topic":"@leave","payload":["foo"]}`, retMsg)
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
- // TRY TO PUBLISH TO UNSUBSCRIBED TOPIC
- publishAsync(t, "", "memory", "foo")
+ stopCh := make(chan struct{}, 1)
go func() {
- time.Sleep(time.Second * 5)
- publishAsync2(t, "", "memory", "foo2")
- }()
-
- // should be only makeMessage from the subscribed foo2 topic
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg)
-
- err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
- assert.NoError(t, err)
-}
-
-func RPCWsMemory(t *testing.T) {
- da := websocket.Dialer{
- Proxy: http.ProxyFromEnvironment,
- HandshakeTimeout: time.Second * 20,
- }
-
- connURL := url.URL{Scheme: "ws", Host: "localhost:13235", Path: "/ws"}
-
- c, resp, err := da.Dial(connURL.String(), nil)
- assert.NoError(t, err)
-
- defer func() {
- if resp != nil && resp.Body != nil {
- _ = resp.Body.Close()
+ 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
+ }
}
}()
- d, err := json.Marshal(messageWS("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 makeMessage
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
-
- // //// LEAVE foo, foo2 /////////
- d, err = json.Marshal(messageWS("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(t, "", "memory", "foo2")
- }()
-
- // should be only makeMessage from the subscribed foo2 topic
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"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,
- }
-
- 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(messageWS("join", "redis", []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("", "redis", "foo")
-
- // VERIFY a makeMessage
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
-
- // //// LEAVE foo, foo2 /////////
- d, err = json.Marshal(messageWS("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(t, "", "redis", "foo2")
- }()
+ time.Sleep(time.Second * 1)
+ t.Run("RPCWsRedisDeny", RPCWsDeny("15588"))
- // should be only makeMessage from the subscribed foo2 topic
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg)
+ stopCh <- struct{}{}
- err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
- assert.NoError(t, err)
+ wg.Wait()
}
-func TestWSMemoryDeny(t *testing.T) {
+func TestWSStop(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)
cfg := &config.Viper{
- Path: "configs/.rr-websockets-memory-deny.yaml",
+ Path: "configs/.rr-websockets-stop.yaml",
Prefix: "rr",
}
@@ -556,6 +379,7 @@ func TestWSMemoryDeny(t *testing.T) {
&websockets.Plugin{},
&httpPlugin.Plugin{},
&memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -605,73 +429,40 @@ func TestWSMemoryDeny(t *testing.T) {
}()
time.Sleep(time.Second * 1)
- t.Run("RPCWsMemoryDeny", RPCWsMemoryDeny)
+ t.Run("RPCWsStop", RPCWsMemoryStop("11114"))
stopCh <- struct{}{}
wg.Wait()
}
-func RPCWsMemoryDeny(t *testing.T) {
- da := websocket.Dialer{
- Proxy: http.ProxyFromEnvironment,
- HandshakeTimeout: time.Second * 20,
- }
-
- connURL := url.URL{Scheme: "ws", Host: "localhost:11112", Path: "/ws"}
+func RPCWsMemoryStop(port string) func(t *testing.T) {
+ return func(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
+ }
- c, resp, err := da.Dial(connURL.String(), nil)
- assert.NoError(t, err)
- assert.NotNil(t, c)
- assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode)
+ connURL := url.URL{Scheme: "ws", Host: "localhost:" + port, Path: "/ws"}
- defer func() {
- if resp != nil && resp.Body != nil {
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NotNil(t, resp)
+ assert.Error(t, err)
+ assert.Nil(t, c)
+ assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) //nolint:staticcheck
+ assert.Equal(t, resp.Header.Get("Stop"), "we-dont-like-you") //nolint:staticcheck
+ if resp != nil && resp.Body != nil { //nolint:staticcheck
_ = resp.Body.Close()
}
- }()
-
- d, err := json.Marshal(messageWS("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)
-
- // //// LEAVE foo, foo2 /////////
- d, err = json.Marshal(messageWS("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)
-
- err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
- assert.NoError(t, err)
}
-func TestWSMemoryStop(t *testing.T) {
+func TestWSAllow(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)
cfg := &config.Viper{
- Path: "configs/.rr-websockets-memory-stop.yaml",
+ Path: "configs/.rr-websockets-allow.yaml",
Prefix: "rr",
}
@@ -684,6 +475,7 @@ func TestWSMemoryStop(t *testing.T) {
&websockets.Plugin{},
&httpPlugin.Plugin{},
&memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -733,38 +525,19 @@ func TestWSMemoryStop(t *testing.T) {
}()
time.Sleep(time.Second * 1)
- t.Run("RPCWsMemoryStop", RPCWsMemoryStop)
+ t.Run("RPCWsMemoryAllow", RPCWsPub("41278"))
stopCh <- struct{}{}
wg.Wait()
}
-func RPCWsMemoryStop(t *testing.T) {
- da := websocket.Dialer{
- Proxy: http.ProxyFromEnvironment,
- HandshakeTimeout: time.Second * 20,
- }
-
- connURL := url.URL{Scheme: "ws", Host: "localhost:11114", Path: "/ws"}
-
- c, resp, err := da.Dial(connURL.String(), nil)
- assert.NotNil(t, resp)
- assert.Error(t, err)
- assert.Nil(t, c)
- assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) //nolint:staticcheck
- assert.Equal(t, resp.Header.Get("Stop"), "we-dont-like-you") //nolint:staticcheck
- if resp != nil && resp.Body != nil { //nolint:staticcheck
- _ = resp.Body.Close()
- }
-}
-
-func TestWSMemoryOk(t *testing.T) {
+func TestWSAllow2(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)
cfg := &config.Viper{
- Path: "configs/.rr-websockets-memory-allow.yaml",
+ Path: "configs/.rr-websockets-allow2.yaml",
Prefix: "rr",
}
@@ -777,6 +550,7 @@ func TestWSMemoryOk(t *testing.T) {
&websockets.Plugin{},
&httpPlugin.Plugin{},
&memory.Plugin{},
+ &broadcast.Plugin{},
)
assert.NoError(t, err)
@@ -826,33 +600,29 @@ func TestWSMemoryOk(t *testing.T) {
}()
time.Sleep(time.Second * 1)
- t.Run("RPCWsMemoryAllow", RPCWsMemoryAllow)
+ t.Run("RPCWsMemoryAllow", RPCWsPub("41270"))
stopCh <- struct{}{}
wg.Wait()
}
-func RPCWsMemoryAllow(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:11113", Path: "/ws"}
+ connURL := url.URL{Scheme: "ws", Host: "localhost:11111", Path: "/ws"}
c, resp, err := da.Dial(connURL.String(), nil)
assert.NoError(t, err)
- assert.NotNil(t, c)
- assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode)
defer func() {
- if resp != nil && resp.Body != nil {
- _ = resp.Body.Close()
- }
+ _ = resp.Body.Close()
}()
- d, err := json.Marshal(messageWS("join", "memory", []byte("hello websockets"), "foo", "foo2"))
+ d, err := json.Marshal(messageWS("join", []byte("hello websockets"), "foo", "foo2"))
if err != nil {
panic(err)
}
@@ -867,64 +637,219 @@ func RPCWsMemoryAllow(t *testing.T) {
// subscription done
assert.Equal(t, `{"topic":"@join","payload":["foo","foo2"]}`, retMsg)
- publish("", "memory", "foo")
-
- // VERIFY a makeMessage
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
+ err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
+}
- // //// LEAVE foo, foo2 /////////
- d, err = json.Marshal(messageWS("leave", "memory", []byte("hello websockets"), "foo"))
- if err != nil {
- panic(err)
- }
+func RPCWsPubAsync(port string) func(t *testing.T) {
+ return func(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 18,
+ }
- err = c.WriteMessage(websocket.BinaryMessage, d)
- assert.NoError(t, err)
+ connURL := url.URL{Scheme: "ws", Host: "localhost:" + port, Path: "/ws"}
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
- // subscription done
- assert.Equal(t, `{"topic":"@leave","payload":["foo"]}`, retMsg)
+ defer func() {
+ _ = resp.Body.Close()
+ }()
- // TRY TO PUBLISH TO UNSUBSCRIBED TOPIC
- publish("", "memory", "foo")
+ d, err := json.Marshal(messageWS("join", []byte("hello websockets"), "foo", "foo2"))
+ if err != nil {
+ panic(err)
+ }
- go func() {
- time.Sleep(time.Second * 5)
- publish2(t, "", "memory", "foo2")
- }()
+ err = c.WriteMessage(websocket.BinaryMessage, d)
+ assert.NoError(t, err)
- // should be only makeMessage from the subscribed foo2 topic
- _, msg, err = c.ReadMessage()
- retMsg = utils.AsString(msg)
- assert.NoError(t, err)
- assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg)
+ _, msg, err := c.ReadMessage()
+ retMsg := utils.AsString(msg)
+ assert.NoError(t, err)
- err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
- assert.NoError(t, err)
+ // subscription done
+ assert.Equal(t, `{"topic":"@join","payload":["foo","foo2"]}`, retMsg)
+
+ publishAsync(t, "placeholder", "foo")
+
+ // VERIFY a makeMessage
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
+
+ // //// LEAVE foo /////////
+ d, err = json.Marshal(messageWS("leave", []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
+ publishAsync(t, "placeholder", "foo")
+
+ go func() {
+ time.Sleep(time.Second * 3)
+ publishAsync(t, "placeholder", "foo2")
+ }()
+
+ // should be only makeMessage from the subscribed foo0 topic
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP\"}", 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)
+func RPCWsPub(port string) func(t *testing.T) {
+ return func(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
+ }
+
+ connURL := url.URL{Scheme: "ws", Host: "localhost:" + port, Path: "/ws"}
+
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
+
+ defer func() {
+ if resp != nil && resp.Body != nil {
+ _ = resp.Body.Close()
+ }
+ }()
+
+ d, err := json.Marshal(messageWS("join", []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("", "foo")
+
+ // VERIFY a makeMessage
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg)
+
+ // //// LEAVE foo, foo2 /////////
+ d, err = json.Marshal(messageWS("leave", []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("", "foo")
+
+ go func() {
+ time.Sleep(time.Second * 5)
+ publish2(t, "", "foo2")
+ }()
+
+ // should be only makeMessage from the subscribed foo2 topic
+ _, msg, err = c.ReadMessage()
+ retMsg = utils.AsString(msg)
+ assert.NoError(t, err)
+ assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg)
+
+ err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
+ assert.NoError(t, err)
}
+}
- client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+func RPCWsDeny(port string) func(t *testing.T) {
+ return func(t *testing.T) {
+ da := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: time.Second * 20,
+ }
- ret := &websocketsv1.Response{}
- err = client.Call("websockets.Publish", makeMessage(command, broker, []byte("hello, PHP"), topics...), ret)
- if err != nil {
- panic(err)
+ connURL := url.URL{Scheme: "ws", Host: "localhost:" + port, Path: "/ws"}
+
+ c, resp, err := da.Dial(connURL.String(), nil)
+ assert.NoError(t, err)
+ assert.NotNil(t, c)
+ assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode)
+
+ defer func() {
+ if resp != nil && resp.Body != nil {
+ _ = resp.Body.Close()
+ }
+ }()
+
+ d, err := json.Marshal(messageWS("join", []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)
+
+ // //// LEAVE foo, foo2 /////////
+ d, err = json.Marshal(messageWS("leave", []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)
+
+ err = c.WriteControl(websocket.CloseMessage, nil, time.Time{})
+ assert.NoError(t, err)
}
}
-func publishAsync(t *testing.T, command string, broker string, topics ...string) {
+// ---------------------------------------------------------------------------------------------------
+
+func publish(command string, topics ...string) {
conn, err := net.Dial("tcp", "127.0.0.1:6001")
if err != nil {
panic(err)
@@ -933,12 +858,13 @@ func publishAsync(t *testing.T, command string, broker string, topics ...string)
client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
ret := &websocketsv1.Response{}
- err = client.Call("websockets.PublishAsync", makeMessage(command, broker, []byte("hello, PHP"), topics...), ret)
- assert.NoError(t, err)
- assert.True(t, ret.Ok)
+ err = client.Call("broadcast.Publish", makeMessage(command, []byte("hello, PHP"), topics...), ret)
+ if err != nil {
+ panic(err)
+ }
}
-func publishAsync2(t *testing.T, command string, broker string, topics ...string) {
+func publishAsync(t *testing.T, command string, topics ...string) {
conn, err := net.Dial("tcp", "127.0.0.1:6001")
if err != nil {
panic(err)
@@ -947,12 +873,12 @@ func publishAsync2(t *testing.T, command string, broker string, topics ...string
client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
ret := &websocketsv1.Response{}
- err = client.Call("websockets.PublishAsync", makeMessage(command, broker, []byte("hello, PHP2"), topics...), ret)
+ err = client.Call("broadcast.PublishAsync", makeMessage(command, []byte("hello, PHP"), topics...), ret)
assert.NoError(t, err)
assert.True(t, ret.Ok)
}
-func publish2(t *testing.T, command string, broker string, topics ...string) {
+func publish2(t *testing.T, command string, topics ...string) {
conn, err := net.Dial("tcp", "127.0.0.1:6001")
if err != nil {
panic(err)
@@ -961,27 +887,25 @@ func publish2(t *testing.T, command string, broker string, topics ...string) {
client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
ret := &websocketsv1.Response{}
- err = client.Call("websockets.Publish", makeMessage(command, broker, []byte("hello, PHP2"), topics...), ret)
+ err = client.Call("broadcast.Publish", makeMessage(command, []byte("hello, PHP2"), topics...), ret)
assert.NoError(t, err)
assert.True(t, ret.Ok)
}
-func messageWS(command string, broker string, payload []byte, topics ...string) *websocketsv1.Message {
+func messageWS(command string, payload []byte, topics ...string) *websocketsv1.Message {
return &websocketsv1.Message{
Topics: topics,
Command: command,
- Broker: broker,
Payload: payload,
}
}
-func makeMessage(command string, broker string, payload []byte, topics ...string) *websocketsv1.Request {
+func makeMessage(command string, payload []byte, topics ...string) *websocketsv1.Request {
m := &websocketsv1.Request{
Messages: []*websocketsv1.Message{
{
Topics: topics,
Command: command,
- Broker: broker,
Payload: payload,
},
},