Skip to content

Commit

Permalink
Merge pull request #439 from diogoViana95/master
Browse files Browse the repository at this point in the history
fixed bug where duplicated device would remove itself and the newly added device
  • Loading branch information
johnabass authored Oct 7, 2024
2 parents bad5647 + e42635b commit 4c000fe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
15 changes: 13 additions & 2 deletions device/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,11 @@ func (m *manager) dispatch(e *Event) {
// dispatches message failed events for any messages that were waiting to be delivered
// at the time of pump closure.
func (m *manager) pumpClose(d *device, c io.Closer, reason CloseReason) {
// remove will invoke requestClose()
m.devices.remove(d.id, reason)

if !m.isDeviceDuplicated(d) {
// remove will invoke requestClose()
m.devices.remove(d.id, reason)
}

closeError := c.Close()

Expand Down Expand Up @@ -493,3 +496,11 @@ func (m *manager) Route(request *Request) (*Response, error) {
return nil, ErrorDeviceNotFound
}
}

func (m *manager) isDeviceDuplicated(d *device) bool {
existing, ok := m.devices.get(d.id)
if !ok {
return false
}
return existing.state != d.state
}
47 changes: 47 additions & 0 deletions device/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,50 @@ func TestGaugeCardinality(t *testing.T) {
m.(*manager).measures.Models.With("neat", "bad").Add(-1)
})
}

func TestManagerIsDeviceDuplicated(t *testing.T) {
var(
assert = assert.New(t)
tests = []struct {
expected bool
existing *device
new *device
m *manager
} {
{
expected: false,
existing: nil,
new: &device{id:"test"},
m: NewManager(&Options{
MaxDevices: 0,
}).(*manager),
},
{
expected: false,
existing: &device{id:"test", state:stateOpen},
new: &device{id:"test", state:stateOpen},
m: NewManager(&Options{
MaxDevices: 0,
}).(*manager),
},
{
expected: true,
existing: &device{id:"test", state:stateOpen},
new: &device{id:"test", state:stateClosed},
m: NewManager(&Options{
MaxDevices: 0,
}).(*manager),
},
}
)

for _, test := range tests {
if test.existing != nil {
err := test.m.devices.add(test.existing)
if err != nil {
assert.Error(err)
}
}
assert.Equal(test.expected, test.m.isDeviceDuplicated(test.new))
}
}

0 comments on commit 4c000fe

Please sign in to comment.