summaryrefslogtreecommitdiffstats
path: root/app/web_hub_test.go
blob: 62c2552de90dd8a8f71146e24d46754f97faf0c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package app

import (
	"net"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/gorilla/websocket"
	goi18n "github.com/nicksnyder/go-i18n/i18n"
	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/model"
)

func dummyWebsocketHandler(t *testing.T) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		upgrader := &websocket.Upgrader{
			ReadBufferSize:  1024,
			WriteBufferSize: 1024,
		}
		conn, err := upgrader.Upgrade(w, req, nil)
		for err == nil {
			_, _, err = conn.ReadMessage()
		}
		if _, ok := err.(*websocket.CloseError); !ok {
			require.NoError(t, err)
		}
	}
}

func registerDummyWebConn(t *testing.T, a *App, addr net.Addr, userId string) *WebConn {
	session, appErr := a.CreateSession(&model.Session{
		UserId: userId,
	})
	require.Nil(t, appErr)

	d := websocket.Dialer{}
	c, _, err := d.Dial("ws://"+addr.String()+"/ws", nil)
	require.NoError(t, err)

	wc := a.NewWebConn(c, *session, goi18n.IdentityTfunc(), "en")
	a.HubRegister(wc)
	go wc.Pump()
	return wc
}

func TestHubStopWithMultipleConnections(t *testing.T) {
	th := Setup().InitBasic()
	defer th.TearDown()

	s := httptest.NewServer(http.HandlerFunc(dummyWebsocketHandler(t)))
	defer s.Close()

	th.App.HubStart()
	registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id)
	registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id)
	registerDummyWebConn(t, th.App, s.Listener.Addr(), th.BasicUser.Id)
	th.App.HubStop()
}