github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/osutil/udev/netlink/conn_test.go (about) 1 package netlink 2 3 import ( 4 "syscall" 5 "testing" 6 "time" 7 8 . "gopkg.in/check.v1" 9 ) 10 11 func Test(t *testing.T) { TestingT(t) } 12 13 func TestConnect(t *testing.T) { 14 conn := new(UEventConn) 15 if err := conn.Connect(UdevEvent); err != nil { 16 t.Fatal("unable to subscribe to netlink uevent, err:", err) 17 } 18 defer conn.Close() 19 20 conn2 := new(UEventConn) 21 if err := conn2.Connect(UdevEvent); err == nil { 22 // see issue: https://github.com/pilebones/go-udev/issues/3 by @stolowski 23 t.Fatal("can't subscribing a second time to netlink socket with PID", conn2.Addr.Pid) 24 } 25 defer conn2.Close() 26 } 27 28 func TestMonitorStop(t *testing.T) { 29 conn := new(UEventConn) 30 if err := conn.Connect(UdevEvent); err != nil { 31 t.Fatal("unable to subscribe to netlink uevent, err:", err) 32 } 33 defer conn.Close() 34 35 stop := conn.Monitor(nil, nil, nil) 36 ok := stop(200 * time.Millisecond) 37 if !ok { 38 t.Fatal("stop timed out instead of working") 39 } 40 } 41 42 func TestMonitorSelectTimeoutIsHarmless(t *testing.T) { 43 conn := new(UEventConn) 44 if err := conn.Connect(UdevEvent); err != nil { 45 t.Fatal("unable to subscribe to netlink uevent, err:", err) 46 } 47 defer conn.Close() 48 49 selectCalled := 0 50 oldStopperSelectTimeout := stopperSelectTimeout 51 stopperSelectTimeout = func() *syscall.Timeval { 52 selectCalled += 1 53 return &syscall.Timeval{ 54 Usec: 10 * 1000, // 10ms 55 } 56 } 57 defer func() { 58 stopperSelectTimeout = oldStopperSelectTimeout 59 }() 60 61 stop := conn.Monitor(nil, nil, nil) 62 time.Sleep(100 * time.Millisecond) 63 ok := stop(200 * time.Millisecond) 64 if !ok { 65 t.Fatal("stop timed out instead of working") 66 } 67 if selectCalled <= 1 { 68 t.Fatal("select->read->select should have been exercised at least once") 69 } 70 }