Skip to content

Commit 2f333e3

Browse files
committed
Add additional tests for handler mappings
1 parent 107669a commit 2f333e3

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

ext/handler_mapping.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ func (m *handlerMappings) remove(name string, group int) bool {
4040
return false
4141
}
4242

43-
for i, handler := range currHandlers {
43+
for idx, handler := range currHandlers {
4444
if handler.Name() != name {
4545
continue
4646
}
4747

4848
// Only one item left, so just delete the group entirely.
4949
if len(currHandlers) == 1 {
50-
m.handlerGroups = append(m.handlerGroups[:group], m.handlerGroups[group+1:]...)
50+
// get index of the current group to remove it from the list of handlergroups
51+
gIdx := getIndex(group, m.handlerGroups)
52+
if gIdx != -1 {
53+
m.handlerGroups = append(m.handlerGroups[:gIdx], m.handlerGroups[gIdx+1:]...)
54+
}
5155
delete(m.handlers, group)
5256
return true
5357
}
@@ -57,13 +61,22 @@ func (m *handlerMappings) remove(name string, group int) bool {
5761
newHandlers := make([]Handler, len(m.handlers[group]))
5862
copy(newHandlers, m.handlers[group])
5963

60-
m.handlers[group] = append(newHandlers[:i], newHandlers[i+1:]...)
64+
m.handlers[group] = append(newHandlers[:idx], newHandlers[idx+1:]...)
6165
return true
6266
}
6367
// handler not found - removal failed.
6468
return false
6569
}
6670

71+
func getIndex(find int, is []int) int {
72+
for i, v := range is {
73+
if v == find {
74+
return i
75+
}
76+
}
77+
return -1
78+
}
79+
6780
func (m *handlerMappings) removeGroup(group int) bool {
6881
m.mutex.Lock()
6982
defer m.mutex.Unlock()
@@ -73,12 +86,12 @@ func (m *handlerMappings) removeGroup(group int) bool {
7386
return false
7487
}
7588

76-
for j, handlerGroup := range m.handlerGroups {
89+
for idx, handlerGroup := range m.handlerGroups {
7790
if handlerGroup != group {
7891
continue
7992
}
8093

81-
m.handlerGroups = append(m.handlerGroups[:j], m.handlerGroups[j+1:]...)
94+
m.handlerGroups = append(m.handlerGroups[:idx], m.handlerGroups[idx+1:]...)
8295
delete(m.handlers, group)
8396
// Group found, and deleted. Success!
8497
return true

ext/handler_mapping_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,47 @@ func checkList(t *testing.T, name string, got []Handler, expected ...Handler) {
9090
}
9191
}
9292
}
93+
94+
func Test_handlerMappings_remove(t *testing.T) {
95+
m := &handlerMappings{}
96+
handler := dummy{name: "test"}
97+
98+
t.Run("nonExistent", func(t *testing.T) {
99+
// removing an item that doesnt exist returns "false"
100+
if got := m.remove(handler.Name(), 0); got {
101+
t.Errorf("remove() = %v, want false", got)
102+
}
103+
})
104+
105+
t.Run("removalSuccess", func(t *testing.T) {
106+
m.add(handler, 0)
107+
// removing an item that DOES exist, returns true
108+
if got := m.remove(handler.Name(), 0); !got {
109+
t.Errorf("remove() = %v, want true", got)
110+
}
111+
// And so the second time, it returns false
112+
if got := m.remove(handler.Name(), 0); got {
113+
t.Errorf("remove() = %v, want false", got)
114+
}
115+
})
116+
117+
t.Run("removalSuccess", func(t *testing.T) {
118+
m.add(handler, 0)
119+
// removing an item that DOES exist, returns true
120+
if got := m.remove(handler.Name(), 0); !got {
121+
t.Errorf("remove() = %v, want true", got)
122+
}
123+
// And so the second time, it returns false
124+
if got := m.remove(handler.Name(), 0); got {
125+
t.Errorf("remove() = %v, want false", got)
126+
}
127+
})
128+
129+
t.Run("removalDifferentIndexes", func(t *testing.T) {
130+
m.add(handler, 1)
131+
m.add(handler, 2)
132+
if got := m.remove(handler.Name(), 2); !got {
133+
t.Errorf("remove() = %v, want true", got)
134+
}
135+
})
136+
}

0 commit comments

Comments
 (0)