github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/readWrite.go (about)

     1  package netTester
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/bronze1man/kmg/kmgNet"
     7  	"github.com/bronze1man/kmg/kmgTime"
     8  )
     9  
    10  //client 先读后写
    11  func readWrite(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
    12  	kmgTime.MustNotTimeout(func() {
    13  
    14  		listener := runEchoServer(listenerNewer)
    15  		defer listener.Close()
    16  
    17  		toWrite := []byte("hello world")
    18  
    19  		conn1, err := Dialer()
    20  		mustNotError(err)
    21  		if debug {
    22  			conn1 = kmgNet.NewDebugConn(conn1, "readWrite")
    23  		}
    24  		defer conn1.Close()
    25  		for i := 0; i < 2; i++ {
    26  			go func() {
    27  				time.Sleep(time.Microsecond)
    28  				_, err := conn1.Write(toWrite)
    29  				mustNotError(err)
    30  			}()
    31  			mustReadSame(conn1, toWrite)
    32  			time.Sleep(time.Microsecond)
    33  		}
    34  		conn1.Close()
    35  
    36  		listener.Close()
    37  	}, time.Second)
    38  
    39  }