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

fixed bug where duplicated device would remove itself and the newly added device #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions device/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,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 @@ -490,3 +493,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))
}
}