@@ -1573,26 +1573,21 @@ func TestAgent_ReconnectingPTY(t *testing.T) {
1573
1573
//nolint:dogsled
1574
1574
conn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
1575
1575
id := uuid .New ()
1576
- netConn1 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash" )
1576
+ // --norc disables executing .bashrc, which is often used to customize the bash prompt
1577
+ netConn1 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash --norc" )
1577
1578
require .NoError (t , err )
1578
1579
defer netConn1 .Close ()
1580
+ tr1 := testutil .NewTerminalReader (t , netConn1 )
1579
1581
1580
1582
// A second simultaneous connection.
1581
- netConn2 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash" )
1583
+ netConn2 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash --norc " )
1582
1584
require .NoError (t , err )
1583
1585
defer netConn2 .Close ()
1586
+ tr2 := testutil .NewTerminalReader (t , netConn2 )
1584
1587
1585
- // Brief pause to reduce the likelihood that we send keystrokes while
1586
- // the shell is simultaneously sending a prompt.
1587
- time .Sleep (100 * time .Millisecond )
1588
-
1589
- data , err := json .Marshal (codersdk.ReconnectingPTYRequest {
1590
- Data : "echo test\r \n " ,
1591
- })
1592
- require .NoError (t , err )
1593
- _ , err = netConn1 .Write (data )
1594
- require .NoError (t , err )
1595
-
1588
+ matchPrompt := func (line string ) bool {
1589
+ return strings .Contains (line , "$ " ) || strings .Contains (line , "# " )
1590
+ }
1596
1591
matchEchoCommand := func (line string ) bool {
1597
1592
return strings .Contains (line , "echo test" )
1598
1593
}
@@ -1606,31 +1601,41 @@ func TestAgent_ReconnectingPTY(t *testing.T) {
1606
1601
return strings .Contains (line , "exit" ) || strings .Contains (line , "logout" )
1607
1602
}
1608
1603
1604
+ // Wait for the prompt before writing commands. If the command arrives before the prompt is written, screen
1605
+ // will sometimes put the command output on the same line as the command and the test will flake
1606
+ require .NoError (t , tr1 .ReadUntil (ctx , matchPrompt ), "find prompt" )
1607
+ require .NoError (t , tr2 .ReadUntil (ctx , matchPrompt ), "find prompt" )
1608
+
1609
+ data , err := json .Marshal (codersdk.ReconnectingPTYRequest {
1610
+ Data : "echo test\r " ,
1611
+ })
1612
+ require .NoError (t , err )
1613
+ _ , err = netConn1 .Write (data )
1614
+ require .NoError (t , err )
1615
+
1609
1616
// Once for typing the command...
1610
- tr1 := testutil .NewTerminalReader (t , netConn1 )
1611
1617
require .NoError (t , tr1 .ReadUntil (ctx , matchEchoCommand ), "find echo command" )
1612
1618
// And another time for the actual output.
1613
1619
require .NoError (t , tr1 .ReadUntil (ctx , matchEchoOutput ), "find echo output" )
1614
1620
1615
1621
// Same for the other connection.
1616
- tr2 := testutil .NewTerminalReader (t , netConn2 )
1617
1622
require .NoError (t , tr2 .ReadUntil (ctx , matchEchoCommand ), "find echo command" )
1618
1623
require .NoError (t , tr2 .ReadUntil (ctx , matchEchoOutput ), "find echo output" )
1619
1624
1620
1625
_ = netConn1 .Close ()
1621
1626
_ = netConn2 .Close ()
1622
- netConn3 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash" )
1627
+ netConn3 , err := conn .ReconnectingPTY (ctx , id , 80 , 80 , "bash --norc " )
1623
1628
require .NoError (t , err )
1624
1629
defer netConn3 .Close ()
1630
+ tr3 := testutil .NewTerminalReader (t , netConn3 )
1625
1631
1626
1632
// Same output again!
1627
- tr3 := testutil .NewTerminalReader (t , netConn3 )
1628
1633
require .NoError (t , tr3 .ReadUntil (ctx , matchEchoCommand ), "find echo command" )
1629
1634
require .NoError (t , tr3 .ReadUntil (ctx , matchEchoOutput ), "find echo output" )
1630
1635
1631
1636
// Exit should cause the connection to close.
1632
1637
data , err = json .Marshal (codersdk.ReconnectingPTYRequest {
1633
- Data : "exit\r \n " ,
1638
+ Data : "exit\r " ,
1634
1639
})
1635
1640
require .NoError (t , err )
1636
1641
_ , err = netConn3 .Write (data )
0 commit comments