Home > Article > Backend Development > Problem when interactively testing cli prompt, scanner does not wait for user input
php Xiaobian Yuzai found that when conducting interactive testing, sometimes problems will be encountered. One of the common problems is that when interacting using the command line interface (CLI), the scanner may not wait for user input. This means that when the user is prompted for input, the program continues execution immediately without waiting for the user's response. This may cause the program to error or not execute correctly. The solution to this problem is to use appropriate techniques or methods to ensure that the scanner waits for user input to ensure smooth interaction testing.
I'm trying to write a test to validate a cli prompt, simulating user input in response to some program output.
How to make scanner.scan
wait for the rest of the writes?
What I have so far:
b := &bytes.buffer{} fmt.fprint(b, "0") go func() { time.sleep(1 * time.second) for i := 1; i < 4; i++ { fmt.fprint(b, i) time.sleep(1 * time.second) } }() scanner := bufio.newscanner(b) for scanner.scan() { log.print(scanner.text()) } if err := scanner.err(); err != nil { log.println("problem while scanning:", err) }
The expected result is: 0123
The actual result is: 0
I tried the version of io.pipe
r, w := io.Pipe() fmt.Fprint(w, "0") go func() { time.Sleep(1 * time.Second) for i := 1; i < 4; i++ { fmt.Fprint(w, i) time.Sleep(1 * time.Second) } }() scanner := bufio.NewScanner(r) for scanner.Scan() { log.Print(scanner.Text()) } if err := scanner.Err(); err != nil { log.Println("problem while scanning:", err) }
Result: Fatal error: all goroutines are sleeping - deadlock!
When using a pipe, writing and reading are synchronized. Without a matching read, the write cannot complete. Move the first write to a goroutine. And close the write end of the pipe so the scanner stops scanning.
r, w := io.Pipe() go func() { defer w.Close() fmt.Fprint(w, "0") time.Sleep(1 * time.Second) for i := 1; i < 4; i++ { fmt.Fprint(w, i) time.Sleep(1 * time.Second) } }() scanner := bufio.NewScanner(r) for scanner.Scan() { log.Print(scanner.Text()) } if err := scanner.Err(); err != nil { log.Println("problem while scanning:", err) }
The above is the detailed content of Problem when interactively testing cli prompt, scanner does not wait for user input. For more information, please follow other related articles on the PHP Chinese website!