github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/web_hub_test.go (about) 1 package app 2 3 import ( 4 "net" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/gorilla/websocket" 10 goi18n "github.com/nicksnyder/go-i18n/i18n" 11 "github.com/stretchr/testify/require" 12 13 "github.com/mattermost/mattermost-server/model" 14 ) 15 16 func dummyWebsocketHandler(t *testing.T) http.HandlerFunc { 17 return func(w http.ResponseWriter, req *http.Request) { 18 upgrader := &websocket.Upgrader{ 19 ReadBufferSize: 1024, 20 WriteBufferSize: 1024, 21 } 22 conn, err := upgrader.Upgrade(w, req, nil) 23 for err == nil { 24 _, _, err = conn.ReadMessage() 25 } 26 if _, ok := err.(*websocket.CloseError); !ok { 27 require.NoError(t, err) 28 } 29 } 30 } 31 32 func registerDummyWebConn(t *testing.T, a *App, addr net.Addr, userId string) *WebConn { 33 session, appErr := a.CreateSession(&model.Session{ 34 UserId: userId, 35 }) 36 require.Nil(t, appErr) 37 38 d := websocket.Dialer{} 39 c, _, err := d.Dial("ws://"+addr.String()+"/ws", nil) 40 require.NoError(t, err) 41 42 wc := a.NewWebConn(c, *session, goi18n.IdentityTfunc(), "en") 43 a.HubRegister(wc) 44 go wc.Pump() 45 return wc 46 } 47 48 func TestHubStopWithMultipleConnections(t *testing.T) { 49 th := Setup().InitBasic() 50 defer th.TearDown() 51 52 s := httptest.NewServer(http.HandlerFunc(dummyWebsocketHandler(t))) 53 defer s.Close() 54 55 th.App.HubStart() 56 wc1 := registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id) 57 wc2 := registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id) 58 wc3 := registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id) 59 defer wc1.Close() 60 defer wc2.Close() 61 defer wc3.Close() 62 }