Skip to content

Commit 43e11f3

Browse files
committed
test: make createHash a test helper, don't call in examples
This makes a bit more of the test suite execute without panicking when run without permissions. Running the examples will still panic. createHash() is not expanded when reading the examples in GoDoc, making them less useful for new readers. Replace it with a call to NewMap() with all MapSpec parameters visible. Signed-off-by: Timo Beckers <[email protected]>
1 parent 776ff07 commit 43e11f3

4 files changed

+53
-18
lines changed

collection_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ func TestIncompleteLoadAndAssign(t *testing.T) {
586586
t.Fatal("expected error loading invalid ProgramSpec")
587587
}
588588

589+
if s.Valid == nil {
590+
t.Fatal("expected valid prog to be non-nil")
591+
}
592+
589593
if fd := s.Valid.FD(); fd != -1 {
590594
t.Fatal("expected valid prog to have closed fd -1, got:", fd)
591595
}

map_test.go

+39-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ var (
3131
}
3232
)
3333

34+
// newHash returns a new Map of type Hash. Cleanup is handled automatically.
35+
func newHash(t *testing.T) *Map {
36+
hash, err := NewMap(&MapSpec{
37+
Type: Hash,
38+
KeySize: 5,
39+
ValueSize: 4,
40+
MaxEntries: 10,
41+
})
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
t.Cleanup(func() { hash.Close() })
46+
return hash
47+
}
48+
3449
func TestMap(t *testing.T) {
3550
m := createArray(t)
3651
defer m.Close()
@@ -1214,8 +1229,7 @@ func TestMapGuessNonExistentKey(t *testing.T) {
12141229
}
12151230

12161231
func TestNotExist(t *testing.T) {
1217-
hash := createHash()
1218-
defer hash.Close()
1232+
hash := newHash(t)
12191233

12201234
var tmp uint32
12211235
err := hash.Lookup("hello", &tmp)
@@ -1246,8 +1260,7 @@ func TestNotExist(t *testing.T) {
12461260
}
12471261

12481262
func TestExist(t *testing.T) {
1249-
hash := createHash()
1250-
defer hash.Close()
1263+
hash := newHash(t)
12511264

12521265
if err := hash.Put("hello", uint32(21)); err != nil {
12531266
t.Errorf("Failed to put key/value pair into hash: %v", err)
@@ -1611,8 +1624,8 @@ func TestMapGetNextID(t *testing.T) {
16111624
var next MapID
16121625
var err error
16131626

1614-
hash := createHash()
1615-
defer hash.Close()
1627+
// Ensure there is at least one map on the system.
1628+
_ = newHash(t)
16161629

16171630
if next, err = MapGetNextID(MapID(0)); err != nil {
16181631
t.Fatal("Can't get next ID:", err)
@@ -1638,8 +1651,7 @@ func TestMapGetNextID(t *testing.T) {
16381651
}
16391652

16401653
func TestNewMapFromID(t *testing.T) {
1641-
hash := createHash()
1642-
defer hash.Close()
1654+
hash := newHash(t)
16431655

16441656
info, err := hash.Info()
16451657
testutils.SkipIfNotSupported(t, err)
@@ -1985,7 +1997,15 @@ func ExampleMap_perCPU() {
19851997
// Note that using unsafe.Pointer is only marginally faster than
19861998
// implementing Marshaler on the type.
19871999
func ExampleMap_zeroCopy() {
1988-
hash := createHash()
2000+
hash, err := NewMap(&MapSpec{
2001+
Type: Hash,
2002+
KeySize: 5,
2003+
ValueSize: 4,
2004+
MaxEntries: 10,
2005+
})
2006+
if err != nil {
2007+
panic(err)
2008+
}
19892009
defer hash.Close()
19902010

19912011
key := [5]byte{'h', 'e', 'l', 'l', 'o'}
@@ -2004,7 +2024,7 @@ func ExampleMap_zeroCopy() {
20042024
// Output: The value is: 23
20052025
}
20062026

2007-
func createHash() *Map {
2027+
func ExampleMap_NextKey() {
20082028
hash, err := NewMap(&MapSpec{
20092029
Type: Hash,
20102030
KeySize: 5,
@@ -2014,11 +2034,6 @@ func createHash() *Map {
20142034
if err != nil {
20152035
panic(err)
20162036
}
2017-
return hash
2018-
}
2019-
2020-
func ExampleMap_NextKey() {
2021-
hash := createHash()
20222037
defer hash.Close()
20232038

20242039
if err := hash.Put("hello", uint32(21)); err != nil {
@@ -2045,7 +2060,15 @@ func ExampleMap_NextKey() {
20452060
// ExampleMap_Iterate demonstrates how to iterate over all entries
20462061
// in a map.
20472062
func ExampleMap_Iterate() {
2048-
hash := createHash()
2063+
hash, err := NewMap(&MapSpec{
2064+
Type: Hash,
2065+
KeySize: 5,
2066+
ValueSize: 4,
2067+
MaxEntries: 10,
2068+
})
2069+
if err != nil {
2070+
panic(err)
2071+
}
20492072
defer hash.Close()
20502073

20512074
if err := hash.Put("hello", uint32(21)); err != nil {

marshaler_example_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ func (ce *customEncoding) UnmarshalBinary(buf []byte) error {
2727

2828
// ExampleMarshaler shows how to use custom encoding with map methods.
2929
func Example_customMarshaler() {
30-
hash := createHash()
30+
hash, err := NewMap(&MapSpec{
31+
Type: Hash,
32+
KeySize: 5,
33+
ValueSize: 4,
34+
MaxEntries: 10,
35+
})
36+
if err != nil {
37+
panic(err)
38+
}
3139
defer hash.Close()
3240

3341
if err := hash.Put(&customEncoding{"hello"}, uint32(111)); err != nil {

prog_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func TestProgramVerifierLogTruncated(t *testing.T) {
441441
}
442442
var ve *internal.VerifierError
443443
if !errors.As(err, &ve) {
444-
t.Error("Error is not a VerifierError")
444+
t.Fatal("Error is not a VerifierError")
445445
}
446446
if !ve.Truncated {
447447
t.Errorf("VerifierError is not truncated: %+v", ve)

0 commit comments

Comments
 (0)