package websockets import ( "net/http" "net/url" "os" "os/signal" "sync" "syscall" "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" ) func TestBroadcastInit(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.InfoLevel)) assert.NoError(t, err) cfg := &config.Viper{ Path: "configs/.rr-websockets-init.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("TestWSInit", wsInit) stopCh <- struct{}{} wg.Wait() } 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 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) }