github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/websocket_norace_test.go (about)

     1  // +build !race
     2  
     3  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     4  // See LICENSE.txt for license information.
     5  
     6  package api4
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/masterhung0112/hk_server/v5/model"
    15  )
    16  
    17  // TestWebSocket is intentionally made to skip -race mode
    18  // because the websocket client is known to be racy and needs a big overhaul
    19  // to fix everything.
    20  func TestWebSocket(t *testing.T) {
    21  	th := Setup(t).InitBasic()
    22  	defer th.TearDown()
    23  	WebSocketClient, err := th.CreateWebSocketClient()
    24  	require.Nil(t, err)
    25  	defer WebSocketClient.Close()
    26  
    27  	time.Sleep(300 * time.Millisecond)
    28  
    29  	// Test closing and reconnecting
    30  	WebSocketClient.Close()
    31  	err = WebSocketClient.Connect()
    32  	require.Nil(t, err)
    33  
    34  	WebSocketClient.Listen()
    35  
    36  	resp := <-WebSocketClient.ResponseChannel
    37  	require.Equal(t, resp.Status, model.STATUS_OK, "should have responded OK to authentication challenge")
    38  
    39  	WebSocketClient.SendMessage("ping", nil)
    40  	resp = <-WebSocketClient.ResponseChannel
    41  	require.Equal(t, resp.Data["text"].(string), "pong", "wrong response")
    42  
    43  	WebSocketClient.SendMessage("", nil)
    44  	resp = <-WebSocketClient.ResponseChannel
    45  	require.Equal(t, resp.Error.Id, "api.web_socket_router.no_action.app_error", "should have been no action response")
    46  
    47  	WebSocketClient.SendMessage("junk", nil)
    48  	resp = <-WebSocketClient.ResponseChannel
    49  	require.Equal(t, resp.Error.Id, "api.web_socket_router.bad_action.app_error", "should have been bad action response")
    50  
    51  	WebSocketClient.UserTyping("", "")
    52  	resp = <-WebSocketClient.ResponseChannel
    53  	require.Equal(t, resp.Error.Id, "api.websocket_handler.invalid_param.app_error", "should have been invalid param response")
    54  	require.Equal(t, resp.Error.DetailedError, "", "detailed error not cleared")
    55  
    56  	WebSocketClient.UserTyping(th.BasicChannel.Id, "")
    57  	resp = <-WebSocketClient.ResponseChannel
    58  	require.Nil(t, resp.Error)
    59  
    60  	WebSocketClient.UserTyping(th.BasicPrivateChannel2.Id, "")
    61  	resp = <-WebSocketClient.ResponseChannel
    62  	require.Equal(t, resp.Error.Id, "api.websocket_handler.invalid_param.app_error", "should have been invalid param response")
    63  	require.Equal(t, resp.Error.DetailedError, "", "detailed error not cleared")
    64  }