github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/host/wait_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 host 16 17 import ( 18 "testing" 19 "time" 20 21 "golang.org/x/sys/unix" 22 "github.com/SagerNet/gvisor/pkg/sentry/contexttest" 23 "github.com/SagerNet/gvisor/pkg/waiter" 24 ) 25 26 func TestWait(t *testing.T) { 27 var fds [2]int 28 err := unix.Pipe(fds[:]) 29 if err != nil { 30 t.Fatalf("Unable to create pipe: %v", err) 31 } 32 33 defer unix.Close(fds[1]) 34 35 ctx := contexttest.Context(t) 36 file, err := NewFile(ctx, fds[0]) 37 if err != nil { 38 unix.Close(fds[0]) 39 t.Fatalf("NewFile failed: %v", err) 40 } 41 42 defer file.DecRef(ctx) 43 44 r := file.Readiness(waiter.ReadableEvents) 45 if r != 0 { 46 t.Fatalf("File is ready for read when it shouldn't be.") 47 } 48 49 e, ch := waiter.NewChannelEntry(nil) 50 file.EventRegister(&e, waiter.ReadableEvents) 51 defer file.EventUnregister(&e) 52 53 // Check that there are no notifications yet. 54 if len(ch) != 0 { 55 t.Fatalf("Channel is non-empty") 56 } 57 58 // Write to the pipe, so it should be writable now. 59 unix.Write(fds[1], []byte{1}) 60 61 // Check that we get a notification. We need to yield the current thread 62 // so that the fdnotifier can deliver notifications, so we use a 63 // 1-second timeout instead of just checking the length of the channel. 64 select { 65 case <-ch: 66 case <-time.After(1 * time.Second): 67 t.Fatalf("Channel not notified") 68 } 69 }