Multiple PING failed on tester 2

I’m stuck on Stage #wy1 - responding to multiple ping

This is my solution:

func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")
	
	l, lisErr := net.Listen("tcp", "0.0.0.0:6379")
	if lisErr != nil {
		fmt.Println("Failed to bind to port 6379")
		os.Exit(1)
	}

	//Close listener on exit
	// defer l.Close()
	fmt.Println("Server is listening on port 6379")

	
	conn, acceptedErr := l.Accept()
	

	if acceptedErr != nil {
		fmt.Println("Error accepting connection: ", acceptedErr.Error())
		os.Exit(1)
	}
		
	buf := make([]byte,1024)

	for {
		dataLength, err := conn.Read(buf)
		if err != nil {
			if err.Error() == "EOF" {
				fmt.Println("Connection closed")
				break
			}
			fmt.Println("Error reading:", err.Error())
			break
		}
		if dataLength == 0 {
			fmt.Println("No data read")
			break
		}
		messages := strings.Split(string(buf), "\r\n")
		for _, message := range messages {
			switch message {
			case "PING":
				conn.Write([]byte("+PONG\r\n"))
			default:
			}
		}
	}
}

And here’s a solution I analysed and compared to:

func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")
	l, err := net.Listen("tcp", "0.0.0.0:6379")
	if err != nil {
		fmt.Println("Failed to bind to port 6379")
		os.Exit(1)
	}
	connection, err := l.Accept()
	if err != nil {
		fmt.Println("Error accepting connection: ", err.Error())
		os.Exit(1)
	}
	buf := make([]byte, 1024)
	for {
		dataLength, err := connection.Read(buf)
		if err != nil {
			if err.Error() == "EOF" {
				fmt.Println("Connection closed")
				break
			}
			fmt.Println("Error reading:", err.Error())
			break
		}
		if dataLength == 0 {
			fmt.Println("No data read")
			break
		}
		messages := strings.Split(string(buf), "\r\n")
		for _, message := range messages {
			switch message {
			case "PING":
				connection.Write([]byte("+PONG\r\n"))
			default:
			}
		}
	}
}

Both looks identical to me, yet my code doesn’t work on the second tester, with it receiving empty string. Am I just utterly crazy and blind or is there some hidden flaw to my logic that I don’t fully understand?

Here are my logs:
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh

2

[compile] Compilation successful.

3

4

Debug = true

5

6

[tester::#ZU2] Running tests for Stage #ZU2 (Handle concurrent clients)

7

[tester::#ZU2] $ ./your_program.sh

8

[your_program] Logs from your program will appear here!

9

[your_program] Server is listening on port 6379

10

[tester::#ZU2] client-1: $ redis-cli PING

11

[tester::#ZU2] client-1: Sent bytes: “*1\r\n$4\r\nPING\r\n”

12

[tester::#ZU2] client-1: Received bytes: “+PONG\r\n”

13

[tester::#ZU2] client-1: Received RESP simple string: “PONG”

14

[tester::#ZU2] Received “PONG”

15

[tester::#ZU2] client-2: $ redis-cli PING

16

[tester::#ZU2] client-2: Sent bytes: “*1\r\n$4\r\nPING\r\n”

17

[tester::#ZU2] Received: “” (no content received)

18

[tester::#ZU2] ^ error

19

[tester::#ZU2] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)

20

[tester::#ZU2] Test failed

21

[tester::#ZU2] Terminating program

22

[tester::#ZU2] Program terminated successfully

include relevant logs here (please make sure to keep the backticks around this!)

And here’s a snippet of my code:

include relevant code here (please make sure to keep the backticks around this!)

+1 facing the same issue

update: got fixed in the test case we are creating another redis client at 16 to test another PING so you need to accept multiple connections using go routines.

1 Like

Hi @wangshu24, looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Yes, the code was as I presented above. And when I tried running the test, the first test manage to submit a PING and my server received the message and replied with PONG. The second PING ended in error and the tester panic with the message that said Receive message: “” . So just an empty message received. Not sure if this help but a common issue I ran into when testing locally is everytime I pinged my server with a message, it shutdown the server immediately after instead of kept it listening.

Sorry if that wasn’t anymore helpful but I was just as lost when I ran into that issue.

Thank you so much for sharing the details! This is incredibly helpful. :+1:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.