github.com/labulakalia/water@v0.0.5-0.20231118024244-f351ca6784b6/ipv4_go1.11_test.go (about)

     1  // +build go1.11
     2  
     3  package water
     4  
     5  import (
     6  	"context"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestCloseUnblockPendingRead(t *testing.T) {
    12  	ifce, err := New(Config{DeviceType: TUN})
    13  	if err != nil {
    14  		t.Fatalf("creating TUN error: %v\n", err)
    15  	}
    16  
    17  	c := make(chan struct{})
    18  	go func() {
    19  		ifce.Read(make([]byte, 1<<16))
    20  		close(c)
    21  	}()
    22  
    23  	// make sure ifce.Close() happens after ifce.Read() blocks
    24  	time.Sleep(1 * time.Second)
    25  
    26  	ifce.Close()
    27  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    28  	defer cancel()
    29  
    30  	select {
    31  	case <-c:
    32  		t.Log("Pending Read unblocked")
    33  	case <-ctx.Done():
    34  		t.Fatal("Timeouted, pending read blocked")
    35  	}
    36  }