Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve ws.conn handling #57

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/xmidt-agent/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func provideCredentials(in credsIn) (*credentials.Credentials, error) {

opts := []credentials.Option{
credentials.URL(in.Creds.URL),
// enabling `Required` allows the xmidt-agent to send connect events for auth related errors
credentials.Required(),
credentials.HTTPClient(client),
credentials.MacAddress(in.ID.DeviceID),
credentials.SerialNumber(in.ID.SerialNumber),
Expand Down
50 changes: 37 additions & 13 deletions internal/websocket/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package websocket_test

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -15,16 +16,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) {
assert := assert.New(t)
require := require.New(t)
type EndToEndTestSuite struct {
suite.Suite
finished bool
websocket *ws.Websocket
}

// 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 @@ -44,22 +59,29 @@ func TestEndToEnd(t *testing.T) {
require.NoError(err)

mt, got, err := c.Read(ctx)
// server will halt until the websocket closes resulting in a EOF
var closeErr websocket.CloseError
if ts.finished && errors.As(err, &closeErr) {
assert.Equal(closeErr.Code, websocket.StatusNormalClosure)
return
}

require.NoError(err)
require.Equal(websocket.MessageBinary, mt)
require.NotEmpty(got)

err = wrp.NewDecoderBytes(got, wrp.Msgpack).Decode(&msg)
require.NoError(err)
require.Equal(wrp.SimpleEventMessageType, msg.Type)
require.Equal("server", msg.Source)
require.Equal("client", msg.Source)

c.Close(websocket.StatusNormalClosure, "")
}))
defer s.Close()

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 All @@ -85,7 +107,6 @@ func TestEndToEnd(t *testing.T) {
Jitter: 1.0 / 3.0,
MaxInterval: 341*time.Second + 333*time.Millisecond,
}),
ws.WithIPv6(),
ws.WithIPv4(),
ws.NowFunc(time.Now),
ws.ConnectTimeout(30*time.Second),
Expand All @@ -96,12 +117,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 @@ -118,7 +140,7 @@ func TestEndToEnd(t *testing.T) {
}
}

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

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

func TestEndToEndBadData(t *testing.T) {
Expand Down Expand Up @@ -256,7 +280,7 @@ func TestEndToEndConnectionIssues(t *testing.T) {
require.NoError(err)
defer c.CloseNow()

ctx, cancel := context.WithTimeout(r.Context(), 2000000*time.Millisecond)
ctx, cancel := context.WithTimeout(r.Context(), 200*time.Millisecond)
Comment on lines -259 to +283
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a silly artifact of some test debugging code in a previous pr

defer cancel()

msg := wrp.Message{
Expand Down
Loading
Loading