Skip to content

Commit

Permalink
[fix] unit test worked but broke the client await for reply
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsucho committed Jan 3, 2024
1 parent e926b52 commit 03892cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
1 change: 0 additions & 1 deletion tcp-client-server/tcp-client/tcp-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func SendUserInput(conn net.Conn, r io.Reader) {
if err := input.Err(); err != nil {
log.Fatal(err)
}
defer conn.Close()
}

func main() {
Expand Down
16 changes: 10 additions & 6 deletions tcp-client-server/tcp-client/tcp-client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package main

import (
"io"
"log"
"net"
"strings"
"testing"
Expand All @@ -18,12 +18,16 @@ func TestSendUserInput(t *testing.T) {
// call SendUserInput function with the connection
go SendUserInput(conn1, strings.NewReader(testString))

// read the data written to the connection until EOF
data, _ := io.ReadAll(conn2)
// read the 1024 bytes written to the connection
data := make([]byte, 1024)
n, err := conn2.Read(data)
if err != nil {
log.Fatal("Error reading connection: ", err)
}
r_msg := string(data[:n])

// check if the data written matches the expected output
expectedOutput := "Hello, World!\n"
if string(data) != expectedOutput {
t.Errorf("Expected output %s, got %s", expectedOutput, string(data))
if r_msg != testString {
t.Errorf("Expected output %s, got %s", testString, r_msg)
}
}

0 comments on commit 03892cb

Please sign in to comment.