Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Make TestAgent and TestWorkspaceAgentPTY less flaky #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 18, 2022

Conversation

dwahler
Copy link
Contributor

@dwahler dwahler commented May 18, 2022

TestAgent/ReconnectingPTY was making some fragile assumptions about the data returned from a net.Conn. This PR makes it a bit more robust.

The previous version was sending echo test via a PTY, and then expecting to see the string test in the output twice: once from the echoed command line, and once from the command output itself. But it was expecting them to be returned by two different calls to Read(), and there's no guarantee of that.

The new version uses buffered I/O to read one line at a time, and expects to see at least one line that contains echo test, followed by at least one line that contains test but not echo. It also sleeps for 100ms to reduce the likelihood that the shell prompt test will be interleaved with the local echo of the command.

There's still some room for improvement here, because ideally the test would be controlling both ends of the PTY, instead of just assuming the user's shell behaves predictably.

Fixes #1348, fixes #1368

@dwahler dwahler requested a review from kylecarbs May 18, 2022 16:38
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely top-tier fix! 🥳

Comment on lines +210 to +212
// Brief pause to reduce the likelihood that we send keystrokes while
// the shell is simultaneously sending a prompt.
time.Sleep(100 * time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should properly be buffered on the server-side, were you seeing it differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a problem with the server, just an inherent race. The actual issue I was seeing was that the test was sending the command before it received the prompt, which means the command text actually got echoed twice. This is because after bash prints the prompt, it helpfully re-echoes anything that was pending in the input buffer, in order to make line editing work properly.

For instance, if you type sleep 10<ENTER>echo foo<ENTER> quickly, you get output on your terminal that looks like:

$ sleep 10
echo foo
$ echo foo
foo

It would certainly be cleaner to wait for the prompt and then send the input, but I'm not sure that we can easily/portably do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see. Makes sense!

@dwahler dwahler changed the title fix: Make TestAgent/ReconnectingPTY less flaky fix: Make TestAgent and TestWorkspaceAgentPTY less flaky May 18, 2022
@dwahler dwahler enabled auto-merge (squash) May 18, 2022 17:00
@dwahler dwahler merged commit a50a6e8 into main May 18, 2022
@dwahler dwahler deleted the david/1348-testagent-reconnectingpty branch May 18, 2022 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: test flake - Test/go/PostgreSQL TestWorkspaceAgentPTY Bug: TestAgent failing randomly
2 participants