Make test suite more deterministic

expectNeighUpdate might fail on unexpected updates
This commit is contained in:
Julian Kornberger 2018-11-20 21:22:26 +01:00 committed by Alessandro Boch
parent 25298936a6
commit 78a3099b70
2 changed files with 18 additions and 27 deletions

View File

@ -3,7 +3,6 @@
package netlink package netlink
import ( import (
"fmt"
"net" "net"
"testing" "testing"
"time" "time"
@ -257,26 +256,17 @@ func TestNeighAddDelProxy(t *testing.T) {
} }
} }
func expectNeighUpdate(ch <-chan NeighUpdate, t uint16, state int, ip net.IP) error { // expectNeighUpdate returns whether the expected updated is received within one second.
func expectNeighUpdate(ch <-chan NeighUpdate, t uint16, state int, ip net.IP) bool {
for { for {
timeout := time.After(time.Second) timeout := time.After(time.Second)
select { select {
case update := <-ch: case update := <-ch:
if update.Type != t { if update.Type == t && update.Neigh.State == state && update.Neigh.IP != nil && update.Neigh.IP.Equal(ip) {
return fmt.Errorf("want %d got %d", t, update.Type) return true
} }
if update.Neigh.State != state {
return fmt.Errorf("want %d got %d", state, update.State)
}
if update.Neigh.IP == nil {
return fmt.Errorf("Neigh.IP is nil")
}
if !update.Neigh.IP.Equal(ip) {
return fmt.Errorf("Neigh.IP: want %s got %s", ip, update.Neigh.IP)
}
return nil
case <-timeout: case <-timeout:
return fmt.Errorf("timeout") return false
} }
} }
} }
@ -313,14 +303,14 @@ func TestNeighSubscribe(t *testing.T) {
if err := NeighAdd(entry); err != nil { if err := NeighAdd(entry); err != nil {
t.Errorf("Failed to NeighAdd: %v", err) t.Errorf("Failed to NeighAdd: %v", err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP) {
t.Fatalf("Add update not received as expected: %s", err) t.Fatalf("Add update not received as expected")
} }
if err := NeighDel(entry); err != nil { if err := NeighDel(entry); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_FAILED, entry.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_FAILED, entry.IP) {
t.Fatalf("Del update not received as expected: %s", err) t.Fatalf("Del update not received as expected")
} }
} }
@ -367,8 +357,8 @@ func TestNeighSubscribeWithOptions(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Failed to NeighAdd: %v", err) t.Errorf("Failed to NeighAdd: %v", err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP) {
t.Fatalf("Add update not received as expected: %s", err) t.Fatalf("Add update not received as expected")
} }
} }
@ -418,8 +408,8 @@ func TestNeighSubscribeAt(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Failed to NeighAdd: %v", err) t.Errorf("Failed to NeighAdd: %v", err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry.IP) {
t.Fatalf("Add update not received as expected: %s", err) t.Fatalf("Add update not received as expected")
} }
} }
@ -473,8 +463,8 @@ func TestNeighSubscribeListExisting(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry1.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_REACHABLE, entry1.IP) {
t.Fatalf("Existing add update not received as expected: %s", err) t.Fatalf("Existing add update not received as expected")
} }
entry2 := &Neigh{ entry2 := &Neigh{
@ -489,7 +479,7 @@ func TestNeighSubscribeListExisting(t *testing.T) {
t.Errorf("Failed to NeighAdd: %v", err) t.Errorf("Failed to NeighAdd: %v", err)
} }
if err := expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_PERMANENT, entry2.IP); err != nil { if !expectNeighUpdate(ch, unix.RTM_NEWNEIGH, NUD_PERMANENT, entry2.IP) {
t.Fatalf("Existing add update not received as expected: %s", err) t.Fatalf("Existing add update not received as expected")
} }
} }

View File

@ -227,6 +227,7 @@ func TestRouteAddIncomplete(t *testing.T) {
} }
} }
// expectNeighUpdate returns whether the expected updated is received within one minute.
func expectRouteUpdate(ch <-chan RouteUpdate, t uint16, dst net.IP) bool { func expectRouteUpdate(ch <-chan RouteUpdate, t uint16, dst net.IP) bool {
for { for {
timeout := time.After(time.Minute) timeout := time.After(time.Minute)