gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/eventfd/eventfd_test.go (about) 1 // Copyright 2021 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 eventfd 16 17 import ( 18 "testing" 19 "time" 20 ) 21 22 func TestReadWrite(t *testing.T) { 23 efd, err := Create() 24 if err != nil { 25 t.Fatalf("failed to Create(): %v", err) 26 } 27 defer efd.Close() 28 29 // Make sure we can read actual values 30 const want = 343 31 if err := efd.Write(want); err != nil { 32 t.Fatalf("failed to write value: %d", want) 33 } 34 35 got, err := efd.Read() 36 if err != nil { 37 t.Fatalf("failed to read value: %v", err) 38 } 39 if got != want { 40 t.Fatalf("Read(): got %d, but wanted %d", got, want) 41 } 42 } 43 44 func TestWait(t *testing.T) { 45 efd, err := Create() 46 if err != nil { 47 t.Fatalf("failed to Create(): %v", err) 48 } 49 defer efd.Close() 50 51 // There's no way to test with certainty that Wait() blocks indefinitely, but 52 // as a best-effort we can wait a bit on it. 53 errCh := make(chan error) 54 go func() { 55 errCh <- efd.Wait() 56 }() 57 select { 58 case err := <-errCh: 59 t.Fatalf("Wait() returned without a call to Notify(): %v", err) 60 case <-time.After(500 * time.Millisecond): 61 } 62 63 // Notify and check that Wait() returned. 64 if err := efd.Notify(); err != nil { 65 t.Fatalf("Notify() failed: %v", err) 66 } 67 select { 68 case err := <-errCh: 69 if err != nil { 70 t.Fatalf("Read() failed: %v", err) 71 } 72 case <-time.After(5 * time.Second): 73 t.Fatalf("Read() did not return after Notify()") 74 } 75 }