Problem when interactively testing cli prompt, scanner does not wait for user input

WBOY
Release: 2024-02-13 21:18:18
forward
1136 people have browsed it

交互测试 cli 提示时出现问题,扫描仪不等待用户输入

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.

Question content

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)
    }
Copy after login

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)
    }
Copy after login

Result: Fatal error: all goroutines are sleeping - deadlock!

Solution

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)
    }
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!