Skip to content

Commit

Permalink
chore: update based on pr
Browse files Browse the repository at this point in the history
  • Loading branch information
denopink committed Apr 3, 2024
1 parent 77872d0 commit 239e04c
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions internal/websocket/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/xmidt-org/retry"
"github.com/xmidt-org/wrp-go/v3"
ws "github.com/xmidt-org/xmidt-agent/internal/websocket"
"github.com/xmidt-org/xmidt-agent/internal/websocket/event"
"nhooyr.io/websocket"
)

func TestEndToEnd(t *testing.T) {
var finished bool
type EndToEndTestSuite struct {
suite.Suite
finished bool
websocket *ws.Websocket
}

assert := assert.New(t)
require := require.New(t)
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (ts *EndToEndTestSuite) AfterTest(suiteName, testName string) {
ts.finished = true
ts.websocket.Stop()
}

func (ts *EndToEndTestSuite) TestEndToEnd() {
assert := assert.New(ts.T())
require := require.New(ts.T())

s := httptest.NewServer(
http.HandlerFunc(
Expand All @@ -47,7 +59,7 @@ func TestEndToEnd(t *testing.T) {

mt, got, err := c.Read(ctx)
// server will halt until the websocket closes resulting in a EOF
if finished {
if ts.finished {
return
}

Expand All @@ -66,7 +78,7 @@ func TestEndToEnd(t *testing.T) {

var msgCnt, connectCnt, disconnectCnt atomic.Int64

got, err := ws.New(
websocket, err := ws.New(
ws.URL(s.URL),
ws.DeviceID("mac:112233445566"),
ws.AddMessageListener(
Expand Down Expand Up @@ -102,12 +114,13 @@ func TestEndToEnd(t *testing.T) {
}),
)
require.NoError(err)
require.NotNil(got)
require.NotNil(websocket)

got.Start()
ts.websocket = websocket
ts.websocket.Start()

// Allow multiple calls to start.
got.Start()
ts.websocket.Start()

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
Expand All @@ -124,7 +137,7 @@ func TestEndToEnd(t *testing.T) {
}
}

got.Send(context.Background(),
ts.websocket.Send(context.Background(),
wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "client",
Expand All @@ -142,9 +155,10 @@ func TestEndToEnd(t *testing.T) {
}
time.Sleep(10 * time.Millisecond)
}
finished = true
got.Stop()
time.Sleep(10 * time.Millisecond)
}

func TestEndToEnd(t *testing.T) {
suite.Run(t, new(EndToEndTestSuite))
}

func TestEndToEndBadData(t *testing.T) {
Expand Down

0 comments on commit 239e04c

Please sign in to comment.