Augment safeRouteList.Append() to return a model.Route with index
Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
@@ -18,10 +18,13 @@ func New() safeRouteList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srl *safeRouteList) Append(r model.Route) {
|
func (srl *safeRouteList) Append(r model.Route) model.Route {
|
||||||
srl.m.Lock()
|
srl.m.Lock()
|
||||||
srl.rs = append(srl.rs, r)
|
srl.rs = append(srl.rs, r)
|
||||||
|
l := len(srl.rs)
|
||||||
srl.m.Unlock()
|
srl.m.Unlock()
|
||||||
|
|
||||||
|
return model.Route{Index: l - 1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srl *safeRouteList) Snapshot() []model.Route {
|
func (srl *safeRouteList) Snapshot() []model.Route {
|
||||||
|
|||||||
@@ -142,3 +142,26 @@ func TestSnapshotNonBlockingReadWithOtherReaders(t *testing.T) {
|
|||||||
t.Error("Route list couldn't be readed while mutex was acquired for read")
|
t.Error("Route list couldn't be readed while mutex was acquired for read")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAppendReturnsTheInsertedRoutedWithTheActualIndexWhenEmpty(t *testing.T) {
|
||||||
|
srl := New()
|
||||||
|
|
||||||
|
r := srl.Append(model.Route{})
|
||||||
|
|
||||||
|
if r.Index != 0 {
|
||||||
|
t.Errorf("Index of the returned route is not 0, but %d", r.Index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAppendReturnsTheInsertedRoutedWithTheActualIndexWhenPopulated(t *testing.T) {
|
||||||
|
srl := New()
|
||||||
|
|
||||||
|
var r model.Route
|
||||||
|
|
||||||
|
for i := 0; i < 42; i++ {
|
||||||
|
r = srl.Append(model.Route{})
|
||||||
|
}
|
||||||
|
if r.Index != 42-1 {
|
||||||
|
t.Errorf("Index of the returned route is not the last one, i.e., 41, but %d", r.Index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user