github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/state/tests/load.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  // +stateify savable
    18  type genericContainer struct {
    19  	v interface{}
    20  }
    21  
    22  // +stateify savable
    23  type afterLoadStruct struct {
    24  	v int `state:"nosave"`
    25  }
    26  
    27  func (a *afterLoadStruct) afterLoad() {
    28  	a.v++
    29  }
    30  
    31  // +stateify savable
    32  type valueLoadStruct struct {
    33  	v int `state:".(int64)"`
    34  }
    35  
    36  func (v *valueLoadStruct) saveV() int64 {
    37  	return int64(v.v) // Save as int64.
    38  }
    39  
    40  func (v *valueLoadStruct) loadV(value int64) {
    41  	v.v = int(value) // Load as int.
    42  }
    43  
    44  // +stateify savable
    45  type cycleStruct struct {
    46  	c *cycleStruct
    47  }
    48  
    49  // +stateify savable
    50  type badCycleStruct struct {
    51  	b *badCycleStruct `state:"wait"`
    52  }
    53  
    54  func (b *badCycleStruct) afterLoad() {
    55  	if b.b != b {
    56  		// This is not executable, since AfterLoad requires that the
    57  		// object and all dependencies are complete. This should cause
    58  		// a deadlock error during load.
    59  		panic("badCycleStruct.afterLoad called")
    60  	}
    61  }