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

     1  package netTester
     2  
     3  import (
     4  	//"bytes"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/bronze1man/kmg/kmgTask"
     9  	"github.com/bronze1man/kmg/kmgTime"
    10  )
    11  
    12  //多线程测试
    13  func thread(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
    14  	kmgTime.MustNotTimeout(func() {
    15  		listener := runEchoServer(listenerNewer)
    16  		defer listener.Close()
    17  
    18  		task := kmgTask.NewLimitThreadTaskManager(10)
    19  		content := []byte("Hello world")
    20  		for i := 0; i < 30; i++ {
    21  			i := i
    22  			task.AddFunc(func() {
    23  				if debug {
    24  					fmt.Println("[thread] start", i)
    25  				}
    26  				conn, err := Dialer()
    27  				mustNotError(err)
    28  				defer conn.Close()
    29  				go func() {
    30  					for i := 0; i < 10; i++ {
    31  						_, err := conn.Write(content)
    32  						mustNotError(err)
    33  						time.Sleep(time.Microsecond)
    34  					}
    35  				}()
    36  				for i := 0; i < 10; i++ {
    37  					mustReadSame(conn, content)
    38  				}
    39  			})
    40  		}
    41  		task.Close()
    42  	}, 5*time.Second) //在rt很高的环境下可能会花费较长时间
    43  }