gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/cleanup/cleanup_test.go (about) 1 // Copyright 2020 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 cleanup 16 17 import "testing" 18 19 func testCleanupHelper(clean, cleanAdd *bool, release bool) func() { 20 cu := Make(func() { 21 *clean = true 22 }) 23 cu.Add(func() { 24 *cleanAdd = true 25 }) 26 defer cu.Clean() 27 if release { 28 return cu.Release() 29 } 30 return nil 31 } 32 33 func TestCleanup(t *testing.T) { 34 clean := false 35 cleanAdd := false 36 testCleanupHelper(&clean, &cleanAdd, false) 37 if !clean { 38 t.Fatalf("cleanup function was not called.") 39 } 40 if !cleanAdd { 41 t.Fatalf("added cleanup function was not called.") 42 } 43 } 44 45 func TestRelease(t *testing.T) { 46 clean := false 47 cleanAdd := false 48 cleaner := testCleanupHelper(&clean, &cleanAdd, true) 49 50 // Check that clean was not called after release. 51 if clean { 52 t.Fatalf("cleanup function was called.") 53 } 54 if cleanAdd { 55 t.Fatalf("added cleanup function was called.") 56 } 57 58 // Call the cleaner function and check that both cleanup functions are called. 59 cleaner() 60 if !clean { 61 t.Fatalf("cleanup function was not called.") 62 } 63 if !cleanAdd { 64 t.Fatalf("added cleanup function was not called.") 65 } 66 }