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:
pancho horrillo
2019-10-08 17:53:15 +02:00
parent e0dd6f5dd5
commit 037347dbe3
2 changed files with 27 additions and 1 deletions
+4 -1
View File
@@ -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.rs = append(srl.rs, r)
l := len(srl.rs)
srl.m.Unlock()
return model.Route{Index: l - 1}
}
func (srl *safeRouteList) Snapshot() []model.Route {
+23
View File
@@ -142,3 +142,26 @@ func TestSnapshotNonBlockingReadWithOtherReaders(t *testing.T) {
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)
}
}