Exemplo n.º 1
0
func TestClientMessage(t *testing.T) {
	for i, message_string := range tests {
		message := []byte(message_string)
		go func() {
			conn, err := net.Dial("tcp", ":3000")
			if err != nil {
				t.Fatal(err)
			}
			defer conn.Close()
			client := kamaji.NewClient(conn)
			n, err := client.SendMessage(message)
			t.Logf("Bytes Written: %d\n", n)
		}()

		l, err := net.Listen("tcp", ":3000")
		if err != nil {
			t.Fatal(err)
		}

		for {
			conn, err := l.Accept()
			if err != nil {
				return
			}
			defer conn.Close()
			client := kamaji.NewClient(conn)
			recv_message, err := client.ReadMessage()
			recv_string := string(recv_message[:])
			if err != nil {
				t.Error(err)
			}
			if recv_string != message_string {
				t.Errorf("recv_message = %s, want %s", recv_string[:10], message_string[:10])
				for i, ch := range message {
					if ch != recv_message[i] {
						t.Logf("%d, %d==%d\n", i-1, message[i-1], recv_message[i-1])
						t.Logf("Diff occured at index: %d, %d!=%d\n", i, message[i], recv_message[i])
						t.Logf("%d, %d!=%d\n", i+1, message[i+1], recv_message[i+1])
						break
					}
				}
			} else {
				t.Logf("Test %d passed.", i+1)
			}
			break
		}
		l.Close()
	}
}
Exemplo n.º 2
0
func cli(cn int, wg *sync.WaitGroup) {
	defer wg.Done()
	//time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
	conn, err := net.Dial("tcp", "127.0.0.1:1314")
	if err != nil {
		fmt.Println("Error!")
		return
	}
	defer conn.Close()
	clientConn := new(ClientConn)
	clientConn.Client = kamaji.NewClient(conn)
	clientConn.ID = uuid.NewRandom()
	clientConn.Name = clientConn.ID.String()
	clientConn.Name = fmt.Sprintf("node%03d.test.now", cn)
	clientConn.sender = make(chan *proto_msg.KamajiMessage)
	go clientConn.messageSender()
	//go reportStats(clientConn)
	for {
		tmp, err := clientConn.ReadMessage()
		if err != nil {
			break
		}
		message := &proto_msg.KamajiMessage{}
		err = proto.Unmarshal(tmp, message)
		if err != nil {
			fmt.Println(err)
			break
		}
		handleMessage(clientConn, message)
	}
	fmt.Println("Exiting Client Loop.")
}