Skip to content

Commit

Permalink
test: correctly close all Maps and Programs in the test suite
Browse files Browse the repository at this point in the history
Since omitting Map and Program.Close() is generally considered an error,
clean up all cases in the test suite.

These were found by adding runtime.GC() at the end of a TestMain()
in all packages in the repository.

The plan is to follow up with some instrumentation that allows the caller
to get notified when the sys.FD finalizer successfully closes a file
descriptor during a GC round.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Jul 1, 2022
1 parent d46ef79 commit eb74eec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ func TestCollectionSpecLoadCopy(t *testing.T) {
if err != nil {
t.Fatal("Loading original spec:", err)
}
defer objs.Prog.Close()

if err := spec2.LoadAndAssign(&objs, nil); err != nil {
t.Fatal("Loading copied spec:", err)
}
defer objs.Prog.Close()
}

func TestCollectionSpecRewriteMaps(t *testing.T) {
Expand Down Expand Up @@ -309,6 +311,9 @@ func TestCollectionSpecMapReplacements_SpecMismatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Map fd is duplicated by MapReplacements, this one can be safely closed.
defer newMap.Close()

coll, err := NewCollectionWithOptions(cs, CollectionOptions{
MapReplacements: map[string]*Map{
"test-map": newMap,
Expand Down Expand Up @@ -365,6 +370,8 @@ func TestCollectionSpec_LoadAndAssign_LazyLoading(t *testing.T) {
if err := spec.LoadAndAssign(&objs, nil); err != nil {
t.Fatal("Assign loads a map or program that isn't requested in the struct:", err)
}
defer objs.Prog.Close()
defer objs.Map.Close()

if objs.Prog == nil {
t.Error("Program is nil")
Expand Down Expand Up @@ -572,6 +579,8 @@ func ExampleCollectionSpec_LoadAndAssign() {
if err := spec.LoadAndAssign(&objs, nil); err != nil {
panic(err)
}
defer objs.Program.Close()
defer objs.Map.Close()

fmt.Println(objs.Program.Type())
fmt.Println(objs.Map.Type())
Expand Down
2 changes: 2 additions & 0 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ func TestTailCall(t *testing.T) {
t.Fatal(err)
}
defer obj.TailMain.Close()
defer obj.ProgArray.Close()

ret, _, err := obj.TailMain.Test(make([]byte, 14))
testutils.SkipIfNotSupported(t, err)
Expand Down Expand Up @@ -518,6 +519,7 @@ func TestSubprogRelocation(t *testing.T) {
t.Fatal(err)
}
defer obj.Main.Close()
defer obj.HashMap.Close()

ret, _, err := obj.Main.Test(make([]byte, 14))
testutils.SkipIfNotSupported(t, err)
Expand Down
2 changes: 2 additions & 0 deletions linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestFindReferences(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
if err != nil {
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestForwardFunctionDeclaration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func TestBatchMapWithLock(t *testing.T) {
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
defer coll.Close()

type spinLockValue struct {
Cnt uint32
Expand Down Expand Up @@ -353,6 +354,7 @@ func TestMapWithLock(t *testing.T) {
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
defer coll.Close()

type spinLockValue struct {
Cnt uint32
Expand Down Expand Up @@ -1240,11 +1242,11 @@ func TestIterateMapInMap(t *testing.T) {
m *Map
entries = parent.Iterate()
)
defer m.Close()

if !entries.Next(&key, &m) {
t.Fatal("Iterator encountered error:", entries.Err())
}
m.Close()

if key != 1 {
t.Error("Iterator didn't skip first entry")
Expand Down
1 change: 1 addition & 0 deletions prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func TestProgramMarshaling(t *testing.T) {
if err := arr.Lookup(idx, &prog2); err != nil {
t.Fatal("Can't unmarshal program:", err)
}
defer prog2.Close()

if prog2 == nil {
t.Fatal("Unmarshalling set program to nil")
Expand Down

0 comments on commit eb74eec

Please sign in to comment.