...

Source file src/github.com/containerd/fifo/fifo_linux_test.go

Documentation: github.com/containerd/fifo

     1  //go:build linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package fifo
    20  
    21  import (
    22  	"context"
    23  	"os"
    24  	"path/filepath"
    25  	"sync"
    26  	"syscall"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestFifoCloseAfterRm(t *testing.T) {
    34  	tmpdir, err := os.MkdirTemp("", "fifos")
    35  	assert.NoError(t, err)
    36  	defer os.RemoveAll(tmpdir)
    37  
    38  	leakCheckWg = &sync.WaitGroup{}
    39  	defer func() {
    40  		leakCheckWg = nil
    41  	}()
    42  
    43  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    44  	defer cancel()
    45  
    46  	f, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
    47  	assert.NoError(t, err)
    48  
    49  	err = os.RemoveAll(filepath.Join(tmpdir, "f0"))
    50  	assert.NoError(t, err)
    51  
    52  	cerr := make(chan error)
    53  
    54  	go func() {
    55  		b := make([]byte, 32)
    56  		_, err := f.Read(b)
    57  		cerr <- err
    58  	}()
    59  
    60  	select {
    61  	case err := <-cerr:
    62  		t.Fatalf("read should have blocked, but got %v", err)
    63  	case <-time.After(500 * time.Millisecond):
    64  	}
    65  
    66  	err = f.Close()
    67  	assert.NoError(t, err)
    68  
    69  	select {
    70  	case err := <-cerr:
    71  		assert.EqualError(t, err, "reading from a closed fifo")
    72  	case <-time.After(500 * time.Millisecond):
    73  		t.Fatal("read should have been unblocked")
    74  	}
    75  
    76  	assert.NoError(t, checkWgDone(leakCheckWg))
    77  
    78  	cancel()
    79  	ctx, cancel = context.WithCancel(context.Background())
    80  
    81  	cerr = make(chan error)
    82  	go func() {
    83  		_, err = OpenFifo(ctx, filepath.Join(tmpdir, "f1"), syscall.O_WRONLY|syscall.O_CREAT, 0600)
    84  		cerr <- err
    85  	}()
    86  
    87  	select {
    88  	case err := <-cerr:
    89  		t.Fatalf("open should have blocked, but got %v", err)
    90  	case <-time.After(500 * time.Millisecond):
    91  	}
    92  
    93  	err = os.RemoveAll(filepath.Join(tmpdir, "f1"))
    94  	cancel()
    95  
    96  	select {
    97  	case err := <-cerr:
    98  		assert.EqualError(t, err, "context canceled")
    99  	case <-time.After(500 * time.Millisecond):
   100  		t.Fatal("open should have been unblocked")
   101  	}
   102  
   103  	assert.NoError(t, checkWgDone(leakCheckWg))
   104  }
   105  

View as plain text