Example #1
0
func TestConn_multiple(t *testing.T) {
	s := &mockServer{}

	var buffers []*bytes.Buffer

	for i := 0; i < 10; i++ {
		// TODO: use newMockConn()
		b := &bytes.Buffer{}
		conn := message.NewConn(0, b, b, s.Write)
		s.conns = append(s.conns, conn)
		buffers = append(buffers, b)
	}

	// One message sent over all ios
	s.Write(func(w io.Writer) error {
		return builder.SendPing(w)
	})

	for i, conn := range s.conns {
		msgType := handler.ReadType(conn)
		if msgType != message.Types["ping"] {
			t.Error("Sent ping, but didn't received it over io", i)
		}
	}

	// Multiple messages sent over multiple conns
	chatCount := 10
	chatMsg := "Hello World! What's up bro?"
	var wg sync.WaitGroup
	for i := 0; i < chatCount; i++ {
		wg.Add(1)
		go (func() {
			defer wg.Done()

			s.Write(func(w io.Writer) error {
				return builder.SendChatSend(w, chatMsg)
			})
		})()
	}
	wg.Wait()

	for _, conn := range s.conns {
		for i := 0; i < chatCount; i++ {
			msgType := handler.ReadType(conn)
			if msgType != message.Types["chat_send"] {
				t.Error("Sent chat_send, but didn't received it at iteration", i)
			}
			receivedMsg := handler.ReadChatSend(conn)
			if receivedMsg != chatMsg {
				t.Error("Bad received chat message content at iteration", i)
			}
		}
	}
}
Example #2
0
func TestConn_one(t *testing.T) {
	conn := newMockConn()

	// One message sent over one conn
	conn.Write(builder.SendPing)
	msgType := handler.ReadType(conn)
	if msgType != message.Types["ping"] {
		t.Error("Sent ping, but didn't received it")
	}

	// Multiple messages sent over one io
	chatCount := 10
	chatMsg := "Hello World! What's up bro?"
	var wg sync.WaitGroup
	for i := 0; i < chatCount; i++ {
		wg.Add(1)
		go (func() {
			defer wg.Done()
			conn.Write(func(w io.Writer) error {
				return builder.SendChatSend(w, chatMsg)
			})
		})()
	}
	wg.Wait()

	for i := 0; i < chatCount; i++ {
		msgType := handler.ReadType(conn)
		if msgType != message.Types["chat_send"] {
			t.Error("Sent chat_send, but didn't received it at iteration", i)
		}
		receivedMsg := handler.ReadChatSend(conn)
		if receivedMsg != chatMsg {
			t.Error("Bad received chat message content at iteration", i)
		}
	}
}
Example #3
0
func testMessage(t *testing.T, msgType message.Type, send message.WriteFunc, receive receiver) {
	conn := newMockConn()

	if err := conn.Write(send); err != nil {
		t.Fatal("Cannot send message:", err)
	}

	receivedType := handler.ReadType(conn)
	if msgType != receivedType {
		t.Fatal("Sent type", msgType, "but received", receivedType)
	}

	receive(conn, t)

	// Check that there is no more data waiting to be consumed
	remaining := make([]byte, 1)
	n, _ := conn.Read(remaining)
	if n != 0 {
		t.Fatal("Some data hasn't been consumed by receiver")
	}
}