func TestClientLoop(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) defer cancel() sIn := make(chan *transport.Message) sOut := make(chan *transport.Message) serv := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { upgrader := websocket.Upgrader{} ws, err := upgrader.Upgrade(resp, req, http.Header{}) if err != nil { http.Error(resp, "Application error", http.StatusInternalServerError) return } defer ws.Close() handleConnection(ctx, ws, sIn, sOut) })) addr := fmt.Sprintf("ws://%s", serv.URL[len("http://"):]) s := &client{ addr: addr, dialer: &websocket.Dialer{}, } in := make(chan *transport.Message) out := make(chan *transport.Message) ch := s.Loop(ctx, in, out) // s.Loop(ctx, in, out) time.Sleep(1 * time.Millisecond) msg, err := transport.NewMessage(transport.CmdRequest, "data") require.NoError(t, err) sOut <- msg require.NoError(t, err) recvdMsg := <-in require.Equal(t, msg, recvdMsg) out <- msg recvdMsg = <-sIn require.NoError(t, err) require.Equal(t, msg, recvdMsg) cancel() err = <-ch require.NoError(t, err) }
func TestHandleConnection(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) defer cancel() sock, err := newSock() require.NoError(t, err) err = sock.Listen(testAddr) require.NoError(t, err) defer sock.Close() in := make(chan *transport.Message) out := make(chan *transport.Message) ch := make(chan error) go func() { defer close(ch) ch <- handleConnection(ctx, sock, in, out) }() client, err := newSock() require.NoError(t, err) err = client.Dial(testAddr) require.NoError(t, err) msg, err := transport.NewMessage(transport.CmdRequest, "data") require.NoError(t, err) data, err := json.Marshal(msg) require.NoError(t, err) client.Send(data) recvdMsg := <-in require.Equal(t, msg, recvdMsg) out <- msg data, err = client.Recv() require.NoError(t, err) recvdMsg = &transport.Message{} err = json.Unmarshal(data, recvdMsg) require.NoError(t, err) require.Equal(t, msg, recvdMsg) cancel() err = <-ch require.NoError(t, err) }
func TestMangoClientLoop(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) defer cancel() sock, err := newSock() require.NoError(t, err) serv, err := newSock() require.NoError(t, err) err = serv.Listen(testAddr) require.NoError(t, err) defer serv.Close() s := &client{ sock: sock, addr: testAddr, } in := make(chan *transport.Message) out := make(chan *transport.Message) ch := s.Loop(ctx, in, out) msg, err := transport.NewMessage(transport.CmdRequest, "data") require.NoError(t, err) data, err := json.Marshal(msg) require.NoError(t, err) serv.Send(data) recvdMsg := <-in require.Equal(t, msg, recvdMsg) out <- msg data, err = serv.Recv() require.NoError(t, err) recvdMsg = &transport.Message{} err = json.Unmarshal(data, recvdMsg) require.NoError(t, err) require.Equal(t, msg, recvdMsg) cancel() err = <-ch require.NoError(t, err) }
func TestServerLoop(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) defer cancel() addr := "127.0.0.1:62322" serv := &http.Server{Addr: addr} s := &server{ server: serv, } in := make(chan *transport.Message) out := make(chan *transport.Message) // ch := s.Loop(ctx, in, out) s.Loop(ctx, in, out) time.Sleep(1 * time.Millisecond) dialer := websocket.Dialer{} conn, _, err := dialer.Dial(fmt.Sprintf("ws://%s", addr), nil) require.NoError(t, err) defer conn.Close() msg, err := transport.NewMessage(transport.CmdRequest, "data") require.NoError(t, err) err = conn.WriteJSON(msg) require.NoError(t, err) recvdMsg := <-in require.Equal(t, msg, recvdMsg) out <- msg recvdMsg = &transport.Message{} err = conn.ReadJSON(recvdMsg) require.NoError(t, err) require.Equal(t, msg, recvdMsg) // cancel() // err = <-ch // require.NoError(t, err) }