Stuck in dual command pipeline - BR6

[tester::#BR6] Running tests for Stage #BR6 (Pipelines - Dual-command pipeline)
[tester::#BR6] [setup] export PATH=/tmp/apple/banana/strawberry:$PATH
[tester::#BR6] Running ./your_program.sh
[tester::#BR6] [setup] echo -e "orange strawberry\nmango pear\napple grape\nbanana raspberry\npineapple blueberry" > "/tmp/owl/file-24"
[your-program] $ cat /tmp/owl/file-24 | wc
[your-program]        5      10      78
[tester::#BR6] ✓ Received expected response
[tester::#BR6] [setup] echo -e "1. blueberry pineapple\n2. apple raspberry\n3. pear orange" > "/tmp/fox/file-27"
[your-program] $ tail -f /tmp/fox/file-27 | head -n 5
[your-program] 1. blueberry pineapple
[your-program] 2. apple raspberry
[your-program] 3. pear orange
[tester::#BR6] ✓ Received redirected file content
[tester::#BR6] Didn't find expected line.
[tester::#BR6] Expected: "4. orange strawberry"
[tester::#BR6] Received: "" (no line received)
[tester::#BR6] Assertion failed.
[tester::#BR6] Test failed

This is the testcase failing, the command does not continue after the initial file read

This is my github url: https://git.codecrafters.io/8b07156b96060021

Function handling this:-


private void handleDefault(String[] _args) {

        try {

ArrayList<ProcessBuilder> processeBuilders = new ArrayList<>();

boolean wc = false;




for (ArrayList<String> currArgs: nextCommands) {

String command = currArgs.get(0);

if (command.trim().equals("wc"))    wc = true;

if (!executables.containsKey(command)) {

handleOutput(command + ": not found\n\r");

return; 

                }




currArgs.set(0, executables.get(command));

ProcessBuilder pb = new ProcessBuilder(currArgs);

processeBuilders.add(pb);

            }




boolean finalWc = wc;

ArrayList<Thread> outputThreads = new ArrayList<>();




List<Process> processes = ProcessBuilder.startPipeline(processeBuilders);




for (int i = 1; i < processes.size(); i++) {

Process currProcess = processes.get(i);

Process prevProcess = processes.get(i - 1);

Thread pipe = new Thread(() -> {

try {

OutputStream currIn = currProcess.getOutputStream();

InputStream prevOut = prevProcess.getInputStream();

int byteRead;




while ((byteRead = prevOut.read()) != -1) {

try {

currIn.write(byteRead);

currIn.flush();

                            } catch (IOException e) {

break;

                            }

                        }

                    } catch (IOException e) {

prevProcess.destroy();

                    }

                });




Thread errorPipe = new Thread(() -> {

try {

InputStream errorIn = prevProcess.getErrorStream();

OutputStream errorOut = System.out;




int byteRead;

while ((byteRead = errorIn.read()) != -1) {

errorOut.write(byteRead);

errorOut.flush();

                        }

                    } catch (IOException e) {

System.out.println("\n\rError in process: " + e.getMessage());

                    }

                });




pipe.start();

errorPipe.start();

outputThreads.add(pipe);

outputThreads.add(errorPipe);

            }




if (processes.size() > 0) {

Thread outputStream = new Thread(() -> {

try {

Process last = processes.getLast();

InputStream in = last.getInputStream();

OutputStream out = System.out;




int byteRead;

if (finalWc) out.write(32);

while ((byteRead = in.read()) != -1) {

out.write(byteRead);

if (byteRead == '\n')   out.write('\r');

out.flush();

                        }

System.out.print("\r");

                    } catch (IOException e) {

System.out.println("\rError while executing process: " + e.getMessage());

                    } 

                });




Thread errorOutputStream = new Thread(() -> {

try {

Process last = processes.getLast();

int byteRead;

InputStream errorIn = last.getErrorStream();

OutputStream errorOut = System.out;




while ((byteRead = errorIn.read()) != -1) {

errorOut.write(byteRead);

errorOut.flush();

                        }

                    } catch (IOException e) {

System.out.println("\rError while executing process: " + e.getMessage());

                    } 

                });




outputStream.start();

errorOutputStream.start();

outputThreads.add(outputStream);

outputThreads.add(errorOutputStream);

            }




for (Process process: processes) {

process.waitFor();

            }




for (Thread currThread: outputThreads) {

currThread.join();

            }

        } catch (IOException | InterruptedException e) {

System.out.println("\n\r" + e.getMessage());

        }

    }

Hey @Shubh-81, there might be several issues at play here. Here are the first two I noticed:

  1. After calling ProcessBuilder.startPipeline (which handles piping connections for you), there’s no need to manually wire up the processes again.

  2. The code isn’t passing a previous stage #IP1 (Run a program):

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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