Skip to content

Commit

Permalink
Merge pull request #1084 from xmidt-org/denopink/cherry-pick/patch/fi…
Browse files Browse the repository at this point in the history
…xed-duplicated–device-removal

patch: fixed duplicated device removing itself (#439 cherry picked)
  • Loading branch information
denopink authored Dec 5, 2024
2 parents 05dec6d + 82b21e1 commit e6d4ac5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ report.json
.idea
server/cpuprofile
server/memprofile

# VSCode
*.code-workspace
.vscode/*
.dev/*

15 changes: 13 additions & 2 deletions device/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,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 @@ -635,3 +638,11 @@ func (m *manager) Route(request *Request) (*Response, error) {
func (m *manager) MaxDevices() int {
return m.devices.limit
}

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 @@ -533,3 +533,50 @@ func newTestCounter() *testCounter {
labelPairs: make(map[string]string),
}
}

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 e6d4ac5

Please sign in to comment.