I’m stuck on Stage #qq0
Implement the ECHO command
I’ve tried running the changes locally using this command -
echo -ne ‘*2\r\n$4\r\nECHO\r\n$6\r\nbanana\r\n’ | nc localhost 6379
It’s working, giving the expected output: ‘$6\r\nbanana\r\n’
When, I’m debug mode, I’m watching my output response variable, it’s coming correct and hence giving correct response on the terminal too. But, it’s failing when pushing to master
Here are my logs:
[tester::#QQ0] Running tests for Stage #QQ0 (Implement the ECHO command)
[tester::#QQ0] $ ./your_program.sh
[your_program] Server is running on port 6379
[tester::#QQ0] $ redis-cli ECHO raspberry
[tester::#QQ0] Sent bytes: "*2\r\n$4\r\nECHO\r\n$9\r\nraspberry\r\n"
[tester::#QQ0] Received: "" (no content received)
[tester::#QQ0] ^ error
[tester::#QQ0] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[tester::#QQ0] Test failed
[tester::#QQ0] Terminating program
[tester::#QQ0] Program terminated successfully
And here’s a snippet of my code:
// pasting the main logic, handling threading, reader and writer above this
try {
val command = reader.readText()
val commandList = command.split("\r\n")
val commandType = commandList[2]?.trim()?.uppercase()
var resp = ""
when (commandType) {
"PING" -> {
commandList.forEach {
if (it.trim().uppercase() == "PING") {
resp += "+PONG\r\n"
}
}
}
"ECHO" -> {
val idxOfEcho = commandList.indexOf("ECHO")
for (i in idxOfEcho+1..<commandList.size) {
if (commandList[i] in listOf("", "\n", "\r\n")) continue
resp += "${commandList[i]}\r\n"
}
}
else -> {
resp = "-ERR unknown command '$commandType'\r\n"
}
}
writer.write(resp)
writer.flush()
}catch (e: Exception) {
println("Error: $e")
} finally {
clientSocket.close()
}