Skip to content

Commit

Permalink
chore: fix tests and add new ws validators for nil funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
denopink committed Mar 28, 2024
1 parent 0f0ecd3 commit 96c6ae9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
44 changes: 43 additions & 1 deletion internal/websocket/internal_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package websocket

import "fmt"
import (
"fmt"
)

func validateDeviceID() Option {
return optionFunc(
Expand Down Expand Up @@ -34,3 +36,43 @@ func validateIPMode() Option {
return nil
})
}

func validateFetchURL() Option {
return optionFunc(
func(ws *Websocket) error {
if ws.urlFetcher == nil {
return fmt.Errorf("%w: nil FetchURL", ErrMisconfiguredWS)
}
return nil
})
}

func validateCredentialsDecorator() Option {
return optionFunc(
func(ws *Websocket) error {
if ws.credDecorator == nil {
return fmt.Errorf("%w: negative FetchURLTimeout", ErrMisconfiguredWS)
}
return nil
})
}

func validateNowFunc() Option {
return optionFunc(
func(ws *Websocket) error {
if ws.nowFunc == nil {
return fmt.Errorf("%w: nil NowFunc", ErrMisconfiguredWS)
}
return nil
})
}

func validRetryPolicy() Option {
return optionFunc(
func(ws *Websocket) error {
if ws.retryPolicyFactory == nil {
return fmt.Errorf("%w: nil RetryPolicy", ErrMisconfiguredWS)
}
return nil
})
}
4 changes: 4 additions & 0 deletions internal/websocket/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func New(opts ...Option) (*Websocket, error) {
validateDeviceID(),
validateURL(),
validateIPMode(),
validateFetchURL(),
validateCredentialsDecorator(),
validateNowFunc(),
validRetryPolicy(),
)

for _, opt := range opts {
Expand Down
55 changes: 47 additions & 8 deletions internal/websocket/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/retry"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/xmidt-agent/internal/websocket/event"
)
Expand Down Expand Up @@ -53,6 +54,8 @@ func TestNew(t *testing.T) {
h.Add("Credentials-Decorator", "some value")
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
),
check: func(assert *assert.Assertions, c *Websocket) {
// URL Related
Expand Down Expand Up @@ -84,6 +87,11 @@ func TestNew(t *testing.T) {
wsDefaults,
DeviceID("mac:112233445566"),
FetchURL(fetcher),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
),
check: func(assert *assert.Assertions, c *Websocket) {
u, err := c.urlFetcher(context.Background())
Expand Down Expand Up @@ -141,6 +149,10 @@ func TestNew(t *testing.T) {
NowFunc(func() time.Time {
return time.Unix(1234, 0)
}),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
RetryPolicy(retry.Config{}),
),
check: func(assert *assert.Assertions, c *Websocket) {
if assert.NotNil(c.nowFunc) {
Expand Down Expand Up @@ -196,6 +208,11 @@ func TestMessageListener(t *testing.T) {
DeviceID("mac:112233445566"),
AddMessageListener(&m),
WithIPv6(),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
)

assert.NoError(err)
Expand All @@ -219,6 +236,11 @@ func TestConnectListener(t *testing.T) {
DeviceID("mac:112233445566"),
AddConnectListener(&m),
WithIPv6(),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
)

assert.NoError(err)
Expand All @@ -242,6 +264,11 @@ func TestDisconnectListener(t *testing.T) {
DeviceID("mac:112233445566"),
AddDisconnectListener(&m),
WithIPv6(),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
)

assert.NoError(err)
Expand All @@ -265,6 +292,11 @@ func TestHeartbeatListener(t *testing.T) {
DeviceID("mac:112233445566"),
AddHeartbeatListener(&m),
WithIPv6(),
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
)

assert.NoError(err)
Expand All @@ -277,6 +309,13 @@ func TestHeartbeatListener(t *testing.T) {
}

func TestNextMode(t *testing.T) {
defaults := []Option{
CredentialsDecorator(func(h http.Header) error {
return nil
}),
NowFunc(time.Now),
RetryPolicy(retry.Config{}),
}
tests := []struct {
description string
opts []Option
Expand All @@ -287,32 +326,32 @@ func TestNextMode(t *testing.T) {
description: "IPv4 to IPv6",
mode: ipv4,
expected: ipv6,
opts: []Option{
opts: append(defaults,
WithIPv6(true),
WithIPv4(true),
},
),
}, {
description: "IPv6 to IPv4",
mode: ipv6,
expected: ipv4,
opts: []Option{
opts: append(defaults,
WithIPv6(true),
WithIPv4(true),
},
),
}, {
description: "IPv4 to IPv4",
opts: []Option{
opts: append(defaults,
WithIPv4(true),
WithIPv6(false),
},
),
mode: ipv4,
expected: ipv4,
}, {
description: "IPv6 to IPv6",
opts: []Option{
opts: append(defaults,
WithIPv4(false),
WithIPv6(true),
},
),
mode: ipv6,
expected: ipv6,
},
Expand Down

0 comments on commit 96c6ae9

Please sign in to comment.