github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/dialOnly.go (about) 1 package netTester 2 3 import ( 4 "fmt" 5 "io" 6 "net" 7 "time" 8 9 "github.com/bronze1man/kmg/kmgNet" 10 "github.com/bronze1man/kmg/kmgTime" 11 ) 12 13 //只dial,没有传输任何数据就挂掉了. 14 func dialOnly(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) { 15 kmgTime.MustNotTimeout(func() { 16 listener := listenAccept(listenerNewer, func(c net.Conn) { 17 time.Sleep(time.Microsecond) //防止出现reset 18 c.Close() 19 }) 20 defer listener.Close() 21 //client 双方主动关闭 22 for i := 0; i < 2; i++ { 23 conn1, err := Dialer() 24 mustNotError(err) 25 if debug { 26 conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 1 %d", i)) 27 } 28 conn1.Close() 29 } 30 31 //client 被动关闭 32 buf := make([]byte, 1024) 33 for i := 0; i < 2; i++ { 34 conn1, err := Dialer() 35 mustNotError(err) 36 if debug { 37 conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 2 %d", i)) 38 } 39 defer conn1.Close() 40 _, err = conn1.Read(buf) 41 if err != io.EOF { 42 panic("remote close, local not get io.EOF") 43 } 44 conn1.Close() 45 } 46 47 listener.Close() 48 }, time.Second) 49 50 time.Sleep(time.Microsecond) 51 kmgTime.MustNotTimeout(func() { 52 listener := listenAccept(listenerNewer, func(c net.Conn) { 53 buf := make([]byte, 1024) 54 defer c.Close() 55 _, err := c.Read(buf) 56 if err != io.EOF { 57 panic("local close, remote not get io.EOF") 58 } 59 }) 60 defer listener.Close() 61 //client 主动关闭 62 for i := 0; i < 2; i++ { 63 conn1, err := Dialer() 64 mustNotError(err) 65 if debug { 66 conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 1 %d", i)) 67 } 68 time.Sleep(time.Microsecond) //防止出现reset 69 conn1.Close() 70 } 71 72 listener.Close() 73 }, time.Second) 74 75 }