I’m stuck on Stage #LX4
I’ve tried .. adding print statements, to check why if my code is not running properly, but i am not able to find out why it shows the Received: “” (no content received) in the logs.
Here are my logs:
[tester::#LX4] Running tests for Stage #LX4 (Lists - Append multiple elements)
[tester::#LX4] $ ./your_program.sh
[tester::#LX4] [client] $ redis-cli RPUSH blueberry mango banana
[tester::#LX4] [client] Sent bytes: "*4\r\n$5\r\nRPUSH\r\n$9\r\nblueberry\r\n$5\r\nmango\r\n$6\r\nbanana\r\n"
[tester::#LX4] Received: "" (no content received)
[tester::#LX4] ^ error
[tester::#LX4] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[tester::#LX4] Test failed
[tester::#LX4] Terminating program
[your_program] INPUT COMMAND = RPUSH
[your_program] INSIDE RPUSH
[your_program] LIST ITEM = MANGO
[your_program] LIST ITEM = BANANA
[tester::#LX4] Program terminated successfully
And here’s a snippet of my code:
public static void main(String[] args){
// You can use print statements as follows for debugging, they'll be visible when running tests.
// System.out.println("Logs from your program will appear here!");
// Uncomment this block to pass the first stage
ServerSocket serverSocket = null;
// Socket clientSocket = null;
int port = 6379;
try {
serverSocket = new ServerSocket(port);
// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
serverSocket.setReuseAddress(true);
// Wait for connection from client.
// while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(() -> handleClientRefactored(clientSocket)).start();
// }
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
static void handleClientRefactored(final Socket clientSocket) {
try {
Map<String, SetObject> chm = new ConcurrentHashMap<>();
List list = new ArrayList<>();
// BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (true) {
InputStream inputStream = clientSocket.getInputStream();
String\[\] input = parseInputStream(inputStream);
System.out.println("INPUT STREAM LENGTH = " + input.length);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
if(input.length < 2) {
break;
}
System.out.println("INPUT COMMAND = " + input\[2\].toUpperCase());
if(input\[2\].equalsIgnoreCase("PING")) {
System.out.println("INSIDE PING");
clientSocket.getOutputStream().write("+PONG\\r\\n".getBytes());
// out.write(“+PONG\r\n”);
clientSocket.getOutputStream().flush();
}
else if(input[2].equalsIgnoreCase(“ECHO”)) {
System.out.println(“INSIDE ECHO”);
out.write(input[3] + “\r\n” + input[4] + “\r\n”);
out.flush();
}
else if(input[2].equalsIgnoreCase(“SET”)) {
System.out.println(“INSIDE SET”);
String key = input[4];
String val = input[6];
SetObject so = new SetObject();
so.setVal(val);
if(input.length < 8) {
out.write(“+OK\r\n”);
out.flush();
chm.put(key, so);
}
String expiryFlag = input[8];
if(expiryFlag.equalsIgnoreCase(“PX”)) {
String nb4 = input[9];
String time = input[10];
long ms = Long.parseLong(time);
so.setPx(true);
so.setPxTimeEnd(System.currentTimeMillis()+ms);
// System.out.println(“PX FLAG: " + ms);
}
chm.put(key, so);
}
else if(input[2].equalsIgnoreCase(“GET”)) {
System.out.println(“INSIDE GET”);
String key = input[4];
if(chm.get(key) == null) {
out.write(”$-1\r\n");
}
else {
SetObject val = chm.get(key);
if(val.isPx()) {
if(System.currentTimeMillis() > val.getPxTimeEnd()) {
chm.remove(key);
out.write(“$-1\r\n”);
}
else {
out.write(“$” + val.getVal().length() + “\r\n” + chm.get(key).getVal() + “\r\n”);
}
}
else out.write(“$” + val.getVal().length() + “\r\n” + chm.get(key).getVal() + “\r\n”);
}
out.flush();
}
else if(input[2].equalsIgnoreCase(“RPUSH”)) {
System.out.println(“INSIDE RPUSH”);
for(int i=6; i<input.length; i+=2) {
System.out.println(“LIST ITEM = " + input[i].toUpperCase());
list.add(input[i]);
}
out.write(”:" + list.size() + “\r\n”);
out.flush();
}
}
clientSocket.close();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
I tried running the command
codecrafters test –previous, not it shows this error for the PING test itself