...

Source file src/github.com/Microsoft/hcsshim/internal/gcs/iochannel_test.go

Documentation: github.com/Microsoft/hcsshim/internal/gcs

     1  //go:build windows
     2  
     3  package gcs
     4  
     5  import (
     6  	"io"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/Microsoft/go-winio"
    12  )
    13  
    14  const pipeName = `\\.\pipe\iochannel-test`
    15  
    16  func TestIoChannelClose(t *testing.T) {
    17  	l, err := winio.ListenPipe(pipeName, nil)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	ioc := newIoChannel(l)
    22  	defer ioc.Close()
    23  	ch := make(chan error, 1)
    24  	go func() {
    25  		_, err := io.ReadAll(ioc)
    26  		ch <- err
    27  	}()
    28  	time.Sleep(100 * time.Millisecond)
    29  	ioc.Close()
    30  	if err := <-ch; err == nil || !strings.Contains(err.Error(), "use of closed network connection") {
    31  		t.Error("unexpected: ", err)
    32  	}
    33  }
    34  
    35  func TestIoChannelRead(t *testing.T) {
    36  	l, err := winio.ListenPipe(pipeName, nil)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	ioc := newIoChannel(l)
    41  	defer ioc.Close()
    42  	var b []byte
    43  	ch := make(chan error, 1)
    44  	go func() {
    45  		var err error
    46  		b, err = io.ReadAll(ioc)
    47  		ch <- err
    48  	}()
    49  	time.Sleep(100 * time.Millisecond)
    50  	c, err := winio.DialPipe(pipeName, nil)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	defer c.Close()
    55  	_, err = c.Write(([]byte)("hello world"))
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	c.Close()
    60  	if err := <-ch; err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if string(b) != "hello world" {
    64  		t.Errorf("unexpected: %q", string(b))
    65  	}
    66  }
    67  
    68  func TestIoChannelWrite(t *testing.T) {
    69  	l, err := winio.ListenPipe(pipeName, nil)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	ioc := newIoChannel(l)
    74  	defer ioc.Close()
    75  	ch := make(chan error, 1)
    76  	go func() {
    77  		_, err := ioc.Write(([]byte)("hello world"))
    78  		ioc.Close()
    79  		ch <- err
    80  	}()
    81  	time.Sleep(100 * time.Millisecond)
    82  	c, err := winio.DialPipe(pipeName, nil)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	defer c.Close()
    87  	b, err := io.ReadAll(c)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	if err := <-ch; err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if string(b) != "hello world" {
    95  		t.Errorf("unexpected: %q", string(b))
    96  	}
    97  }
    98  

View as plain text