Skip to content

Commit

Permalink
Add mqtt.Retryer interface to Device (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Oct 19, 2021
1 parent 0dc9c0b commit 8df39ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
8 changes: 4 additions & 4 deletions awsiotdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (

// Device is an AWS IoT device.
type Device interface {
mqtt.Client
mqtt.ReconnectClient
ThingName() string
}

type device struct {
mqtt.Client
mqtt.ReconnectClient
thingName string
}

Expand All @@ -39,8 +39,8 @@ func New(thingName string, dialer mqtt.Dialer, opts ...mqtt.ReconnectOption) (De
return nil, ioterr.New(err, "creating MQTT connection")
}
return &device{
Client: cli,
thingName: thingName,
ReconnectClient: cli,
thingName: thingName,
}, nil
}

Expand Down
20 changes: 13 additions & 7 deletions jobs/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ import (

var errPublish = errors.New("publish failure")

type mockClient interface {
mqtt.Client
mqtt.Handler
}

type mockDevice struct {
*mockmqtt.Client
mockClient
mqtt.Retryer
}

func (d *mockDevice) ThingName() string {
Expand All @@ -45,7 +51,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{
cli := &mockDevice{mockClient: &mockmqtt.Client{
SubscribeFn: func(ctx context.Context, subs ...mqtt.Subscription) ([]mqtt.Subscription, error) {
return nil, errDummy
},
Expand All @@ -65,7 +71,7 @@ func TestNotify(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
j, err := New(ctx, cli)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -152,7 +158,7 @@ func TestGetPendingJobs(t *testing.T) {

var j Jobs
cli := &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
if testCase.publishFailure {
return errPublish
Expand Down Expand Up @@ -267,7 +273,7 @@ func TestDescribeJob(t *testing.T) {

var j Jobs
cli := &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
if testCase.publishFailure {
return errPublish
Expand Down Expand Up @@ -458,7 +464,7 @@ func TestUpdateJob(t *testing.T) {

var j Jobs
cli := &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
if testCase.publishFailure {
return errPublish
Expand Down Expand Up @@ -530,7 +536,7 @@ func TestHandlers_InvalidResponse(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{Client: &mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}

j, err := New(ctx, cli)
if err != nil {
Expand Down
24 changes: 15 additions & 9 deletions shadow/shadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ import (
"github.com/seqsense/aws-iot-device-sdk-go/v6/internal/ioterr"
)

type mockClient interface {
mqtt.Client
mqtt.Handler
}

type mockDevice struct {
*mockmqtt.Client
mockClient
mqtt.Retryer
}

func (d *mockDevice) ThingName() string {
Expand All @@ -44,7 +50,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{
cli := &mockDevice{mockClient: &mockmqtt.Client{
SubscribeFn: func(ctx context.Context, subs ...mqtt.Subscription) ([]mqtt.Subscription, error) {
return nil, errDummy
},
Expand All @@ -64,7 +70,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
operation := "foo/bar"

testCases := map[string]struct {
Expand Down Expand Up @@ -400,7 +406,7 @@ func TestHandlers_InvalidResponse(t *testing.T) {
defer cancel()

var cli *mockDevice
cli = &mockDevice{Client: &mockmqtt.Client{}}
cli = &mockDevice{mockClient: &mockmqtt.Client{}}

s, err := New(ctx, cli)
if err != nil {
Expand Down Expand Up @@ -432,7 +438,7 @@ func TestOnDelta(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
s, err := New(ctx, cli)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -658,7 +664,7 @@ func TestGet(t *testing.T) {
var s Shadow
var cli *mockDevice
cli = &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
req := &simpleRequest{}
if err := json.Unmarshal(msg.Payload, req); err != nil {
Expand Down Expand Up @@ -766,7 +772,7 @@ func TestDesire(t *testing.T) {
var s Shadow
var cli *mockDevice
cli = &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
req := &simpleRequest{}
if err := json.Unmarshal(msg.Payload, req); err != nil {
Expand Down Expand Up @@ -873,7 +879,7 @@ func TestReport(t *testing.T) {
var s Shadow
var cli *mockDevice
cli = &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
req := &simpleRequest{}
if err := json.Unmarshal(msg.Payload, req); err != nil {
Expand Down Expand Up @@ -952,7 +958,7 @@ func TestDelete(t *testing.T) {
var s Shadow
var cli *mockDevice
cli = &mockDevice{
Client: &mockmqtt.Client{
mockClient: &mockmqtt.Client{
PublishFn: func(ctx context.Context, msg *mqtt.Message) error {
req := &simpleRequest{}
if err := json.Unmarshal(msg.Payload, req); err != nil {
Expand Down
18 changes: 12 additions & 6 deletions tunnel/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ import (
"github.com/seqsense/aws-iot-device-sdk-go/v6/internal/ioterr"
)

type mockClient interface {
mqtt.Client
mqtt.Handler
}

type mockDevice struct {
*mockmqtt.Client
mockClient
mqtt.Retryer
}

func (d *mockDevice) ThingName() string {
Expand All @@ -42,7 +48,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
_, err := New(ctx, cli, map[string]Dialer{}, func(opts *Options) error {
return errDummy
})
Expand All @@ -58,7 +64,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{
cli := &mockDevice{mockClient: &mockmqtt.Client{
SubscribeFn: func(ctx context.Context, subs ...mqtt.Subscription) ([]mqtt.Subscription, error) {
return nil, errDummy
},
Expand All @@ -76,7 +82,7 @@ func TestNew(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
_, err := New(ctx, cli, map[string]Dialer{}, func(opts *Options) error {
opts.TopicFunc = func(operation string) string {
return "##"
Expand All @@ -98,7 +104,7 @@ func TestHandlers(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cli := &mockDevice{&mockmqtt.Client{}}
cli := &mockDevice{mockClient: &mockmqtt.Client{}}
tu, err := New(ctx, cli, map[string]Dialer{})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -156,7 +162,7 @@ func TestHandlers_InvalidResponse(t *testing.T) {
defer cancel()

var cli *mockDevice
cli = &mockDevice{Client: &mockmqtt.Client{}}
cli = &mockDevice{mockClient: &mockmqtt.Client{}}

s, err := New(ctx, cli, map[string]Dialer{})
if err != nil {
Expand Down

0 comments on commit 8df39ad

Please sign in to comment.