...

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

Documentation: github.com/containerd/fifo

     1  //go:build !linux && !windows
     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  	"syscall"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestFifoCloseAfterRm(t *testing.T) {
    33  	tmpdir, err := os.MkdirTemp("", "fifos")
    34  	assert.NoError(t, err)
    35  	defer os.RemoveAll(tmpdir)
    36  
    37  	// non-linux version of this test leaks a goroutine
    38  
    39  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    40  	defer cancel()
    41  	f, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
    42  	assert.NoError(t, err)
    43  
    44  	time.Sleep(time.Second) // non-linux doesn't allow removing before syscall has been called. will cause an error.
    45  
    46  	err = os.RemoveAll(filepath.Join(tmpdir, "f0"))
    47  	assert.NoError(t, err)
    48  
    49  	cerr := make(chan error)
    50  
    51  	go func() {
    52  		b := make([]byte, 32)
    53  		_, err := f.Read(b)
    54  		cerr <- err
    55  	}()
    56  
    57  	select {
    58  	case err := <-cerr:
    59  		t.Fatalf("read should have blocked, but got %v", err)
    60  	case <-time.After(500 * time.Millisecond):
    61  	}
    62  
    63  	err = f.Close()
    64  	assert.NoError(t, err)
    65  
    66  	select {
    67  	case err := <-cerr:
    68  		assert.EqualError(t, err, "reading from a closed fifo")
    69  	case <-time.After(500 * time.Millisecond):
    70  		t.Fatal("read should have been unblocked")
    71  	}
    72  
    73  	cancel()
    74  	ctx, cancel = context.WithCancel(context.Background())
    75  	cerr = make(chan error)
    76  	go func() {
    77  		_, err = OpenFifo(ctx, filepath.Join(tmpdir, "f1"), syscall.O_WRONLY|syscall.O_CREAT, 0600)
    78  		cerr <- err
    79  	}()
    80  
    81  	select {
    82  	case err := <-cerr:
    83  		t.Fatalf("open should have blocked, but got %v", err)
    84  	case <-time.After(500 * time.Millisecond):
    85  	}
    86  
    87  	err = os.RemoveAll(filepath.Join(tmpdir, "f1"))
    88  	cancel()
    89  
    90  	select {
    91  	case err := <-cerr:
    92  		assert.Error(t, err)
    93  	case <-time.After(500 * time.Millisecond):
    94  		t.Fatal("open should have been unblocked")
    95  	}
    96  }
    97  

View as plain text