github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/writeRead.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 writeRead(listenerNewer ListenNewer,
    12  	Dialer DirectDialer,
    13  	debug bool) {
    14  	kmgTime.MustNotTimeout(func() {
    15  		listener := runEchoServer(listenerNewer)
    16  		defer listener.Close()
    17  
    18  		toWrite := []byte("hello world")
    19  
    20  		conn1, err := Dialer()
    21  		mustNotError(err)
    22  
    23  		if debug {
    24  			conn1 = kmgNet.NewDebugConn(conn1, "writeRead")
    25  		}
    26  		defer conn1.Close()
    27  		for i := 0; i < 2; i++ {
    28  			_, err = conn1.Write(toWrite)
    29  			mustNotError(err)
    30  			time.Sleep(time.Microsecond)
    31  			mustReadSame(conn1, toWrite)
    32  			time.Sleep(time.Microsecond)
    33  		}
    34  		conn1.Close()
    35  		listener.Close()
    36  	}, time.Second)
    37  }