Example #1
0
func TestServer(t *testing.T) {
	log.Debugf("Enter TestServer..")
	numTimeout := 0
	numUnmatch := 0
	numSuccess := 0
	for i := 0; i < 10000; i++ {
		val := RandStringBytes()
		log.Infof("loop: %d. msg len: %d", i, len(val))
		msg := NewReqMsg(ENCODE_TYPE_JSON, 3, []byte(val))
		log.Debugf("request msg: %s", msg.String())
		c.SendReq(addr, msg)
		respMsg, _ := c.GetRespBlock(msg.sessionId)
		if respMsg.pCode == MSG_TIMEOUT {
			numTimeout++
			continue
		}
		if string(respMsg.data) != val {
			numUnmatch++
			t.Error("not match ", val, string(respMsg.data))
		} else {
			numSuccess++
		}
	}
	log.Infof("\nnumUnmatch: %d\nnumTimeout: %d\nnumSuccess: %d", numUnmatch, numTimeout, numSuccess)
}
Example #2
0
func init() {
	rand.Seed(time.Now().UnixNano())
	// log.SetLevel(log.LEVEL_DEBUG)
	log.SetLevel(log.LEVEL_INFO)
	log.Debugf("set log level to debug..")
	addr = "127.0.0.1:8888"
	s = NewTcpServer(addr)
	c = NewTcpClient()
	go s.Start()

	go func() {
		for reqMsg := range s.reqChan {
			respMsg := NewRespMsg(ENCODE_TYPE_JSON, reqMsg.sessionId, 3, reqMsg.data)
			conn := reqMsg.connection
			go conn.SendMsg(respMsg)
		}
	}()
}
Example #3
0
// this function don't need lock, we will lock in the caller
func (client *TcpClient) createConnection(addr string) *Connection {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		log.WarnErrorf(err, "Can't dail to %s", addr)
		return nil
	}
	connection := NewConnection(conn, false, -1)

	//move all the response from this connection to session channel
	go func(c *Connection) {
		for {
			msg := c.ReceiveMsg()
			if msg == nil {
				c.Disconnect()
				return
			}

			// move this message to session channel.
			sessionId := msg.sessionId
			client.sessionMtx.RLock()
			session, ok := client.sessionMap[sessionId]
			if ok {
				select {
				case session.sessionChan <- msg:
					log.Debugf("Write reponse for session(%d) to channel", sessionId)
				default:
					log.Infof("session(%d) is timeout..", sessionId)
				}
			} else {
				log.Warnf("A bug or a wrong message, because we can't find session(%d)", sessionId)
			}
			client.sessionMtx.RUnlock()
		}
	}(connection)

	return connection
}