gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/state/tests/load_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tests
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestLoadHooks(t *testing.T) {
    22  	runTestCases(t, false, "load-hooks", []any{
    23  		// Root object being a struct.
    24  		afterLoadStruct{v: 1},
    25  		valueLoadStruct{v: 1},
    26  		genericContainer{v: &afterLoadStruct{v: 1}},
    27  		genericContainer{v: &valueLoadStruct{v: 1}},
    28  		sliceContainer{v: []any{&afterLoadStruct{v: 1}}},
    29  		sliceContainer{v: []any{&valueLoadStruct{v: 1}}},
    30  		// Root object being a pointer.
    31  		&afterLoadStruct{v: 1},
    32  		&valueLoadStruct{v: 1},
    33  		&genericContainer{v: &afterLoadStruct{v: 1}},
    34  		&genericContainer{v: &valueLoadStruct{v: 1}},
    35  		&sliceContainer{v: []any{&afterLoadStruct{v: 1}}},
    36  		&sliceContainer{v: []any{&valueLoadStruct{v: 1}}},
    37  		&mapContainer{v: map[int]any{0: &afterLoadStruct{v: 1}}},
    38  		&mapContainer{v: map[int]any{0: &valueLoadStruct{v: 1}}},
    39  	})
    40  }
    41  
    42  func TestCycles(t *testing.T) {
    43  	// cs is a single object cycle.
    44  	cs := cycleStruct{nil}
    45  	cs.c = &cs
    46  
    47  	// cs1 and cs2 are in a two object cycle.
    48  	cs1 := cycleStruct{nil}
    49  	cs2 := cycleStruct{nil}
    50  	cs1.c = &cs2
    51  	cs2.c = &cs1
    52  
    53  	runTestCases(t, false, "cycles", []any{
    54  		cs,
    55  		cs1,
    56  	})
    57  }
    58  
    59  func TestDeadlock(t *testing.T) {
    60  	// bs is a single object cycle. This does not cause deadlock because an
    61  	// object cannot wait for itself.
    62  	bs := badCycleStruct{nil}
    63  	bs.b = &bs
    64  
    65  	runTestCases(t, false, "self", []any{
    66  		&bs,
    67  	})
    68  
    69  	// bs2 and bs2 are in a deadlocking cycle.
    70  	bs1 := badCycleStruct{nil}
    71  	bs2 := badCycleStruct{nil}
    72  	bs1.b = &bs2
    73  	bs2.b = &bs1
    74  
    75  	runTestCases(t, true, "deadlock", []any{
    76  		&bs1,
    77  	})
    78  }