github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/kernel/epoll/epoll_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 epoll
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs/filetest"
    22  	"github.com/SagerNet/gvisor/pkg/waiter"
    23  )
    24  
    25  func TestFileDestroyed(t *testing.T) {
    26  	f := filetest.NewTestFile(t)
    27  	id := FileIdentifier{f, 12}
    28  
    29  	ctx := contexttest.Context(t)
    30  	efile := NewEventPoll(ctx)
    31  	e := efile.FileOperations.(*EventPoll)
    32  	if err := e.AddEntry(id, 0, waiter.ReadableEvents, [2]int32{}); err != nil {
    33  		t.Fatalf("addEntry failed: %v", err)
    34  	}
    35  
    36  	// Check that we get an event reported twice in a row.
    37  	evt := e.ReadEvents(1)
    38  	if len(evt) != 1 {
    39  		t.Fatalf("Unexpected number of ready events: want %v, got %v", 1, len(evt))
    40  	}
    41  
    42  	evt = e.ReadEvents(1)
    43  	if len(evt) != 1 {
    44  		t.Fatalf("Unexpected number of ready events: want %v, got %v", 1, len(evt))
    45  	}
    46  
    47  	// Destroy the file. Check that we get no more events.
    48  	f.DecRef(ctx)
    49  
    50  	evt = e.ReadEvents(1)
    51  	if len(evt) != 0 {
    52  		t.Fatalf("Unexpected number of ready events: want %v, got %v", 0, len(evt))
    53  	}
    54  
    55  }