Linux set interface underlying fd to non-blocking

This commit is contained in:
LE Manh Cuong 2018-11-09 09:30:00 +07:00 committed by Song Gao
parent 8c63d7a3c7
commit 4d58427608
2 changed files with 47 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package water
import (
"context"
"net"
"testing"
"time"
@ -74,3 +75,36 @@ readFrame:
}
}
}
func TestCloseUnblockPendingRead(t *testing.T) {
var (
self = net.IPv4(192, 168, 150, 1)
mask = net.IPv4Mask(255, 255, 255, 0)
)
ifce, err := New(Config{DeviceType: TUN})
if err != nil {
t.Fatalf("creating TUN error: %v\n", err)
}
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
c := make(chan struct{})
go func() {
ifce.Read(make([]byte, 1<<16))
close(c)
}()
// make sure ifce.Close() happens after ifce.Read()
time.Sleep(1 * time.Second)
ifce.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
select {
case <-c:
t.Log("Pending Read unblocked")
case <-ctx.Done():
t.Fatal("Timeouted, pending read blocked")
}
}