@@ -1529,29 +1529,34 @@ func TestWorkspaceWatcher(t *testing.T) {
1529
1529
1530
1530
// Wait events are easier to debug with timestamped logs.
1531
1531
logger := slogtest .Make (t , nil ).Named (t .Name ()).Leveled (slog .LevelDebug )
1532
- wait := func (event string ) {
1533
- select {
1534
- case <- ctx .Done ():
1535
- require .FailNow (t , "timed out waiting for event" , event )
1536
- case _ , ok := <- wc :
1537
- require .True (t , ok , "watch channel closed: %s" , event )
1538
- logger .Info (ctx , "done waiting for event" , slog .F ("event" , event ))
1532
+ wait := func (event string , ready func (w codersdk.Workspace ) bool ) {
1533
+ for {
1534
+ select {
1535
+ case <- ctx .Done ():
1536
+ require .FailNow (t , "timed out waiting for event" , event )
1537
+ case w , ok := <- wc :
1538
+ require .True (t , ok , "watch channel closed: %s" , event )
1539
+ if ready == nil || ready (w ) {
1540
+ logger .Info (ctx , "done waiting for event" , slog .F ("event" , event ))
1541
+ return
1542
+ }
1543
+ }
1539
1544
}
1540
1545
}
1541
1546
1542
1547
coderdtest .CreateWorkspaceBuild (t , client , workspace , database .WorkspaceTransitionStart )
1543
- wait ("workspace build being created" )
1544
- wait ("workspace build being acquired" )
1545
- wait ("workspace build completing" )
1548
+ wait ("workspace build being created" , nil )
1549
+ wait ("workspace build being acquired" , nil )
1550
+ wait ("workspace build completing" , nil )
1546
1551
1547
1552
// Unfortunately, this will add ~1s to the test due to the granularity
1548
1553
// of agent timeout seconds. However, if we don't do this we won't know
1549
1554
// which trigger we received when waiting for connection.
1550
1555
//
1551
1556
// Note that the first timeout is from `coderdtest.CreateWorkspace` and
1552
1557
// the latter is from `coderdtest.CreateWorkspaceBuild`.
1553
- wait ("agent timeout after create" )
1554
- wait ("agent timeout after start" )
1558
+ wait ("agent timeout after create" , nil )
1559
+ wait ("agent timeout after start" , nil )
1555
1560
1556
1561
agentClient := agentsdk .New (client .URL )
1557
1562
agentClient .SetSessionToken (authToken )
@@ -1563,32 +1568,33 @@ func TestWorkspaceWatcher(t *testing.T) {
1563
1568
_ = agentCloser .Close ()
1564
1569
}()
1565
1570
1566
- wait ("agent connected" )
1567
- // This could be racy since we don't guarantee that agent all
1568
- // lifecycles are reported (if they happen in quick succession).
1569
- wait ("agent lifecycle starting" )
1570
- wait ("agent lifecycle ready" )
1571
+ wait ("agent connected/ready" , func (w codersdk.Workspace ) bool {
1572
+ return w .LatestBuild .Resources [0 ].Agents [0 ].Status == codersdk .WorkspaceAgentConnected &&
1573
+ w .LatestBuild .Resources [0 ].Agents [0 ].LifecycleState == codersdk .WorkspaceAgentLifecycleReady
1574
+ })
1571
1575
agentCloser .Close ()
1572
- wait ("agent disconnected" )
1576
+ wait ("agent disconnected" , func (w codersdk.Workspace ) bool {
1577
+ return w .LatestBuild .Resources [0 ].Agents [0 ].Status == codersdk .WorkspaceAgentDisconnected
1578
+ })
1573
1579
1574
1580
closeFunc .Close ()
1575
1581
build := coderdtest .CreateWorkspaceBuild (t , client , workspace , database .WorkspaceTransitionStart )
1576
- wait ("first is for the workspace build itself" )
1582
+ wait ("first is for the workspace build itself" , nil )
1577
1583
err = client .CancelWorkspaceBuild (ctx , build .ID )
1578
1584
require .NoError (t , err )
1579
- wait ("second is for the build cancel" )
1585
+ wait ("second is for the build cancel" , nil )
1580
1586
1581
1587
err = client .UpdateWorkspace (ctx , workspace .ID , codersdk.UpdateWorkspaceRequest {
1582
1588
Name : "another" ,
1583
1589
})
1584
1590
require .NoError (t , err )
1585
- wait ("update workspace name" )
1591
+ wait ("update workspace name" , nil )
1586
1592
1587
1593
err = client .UpdateActiveTemplateVersion (ctx , template .ID , codersdk.UpdateActiveTemplateVersion {
1588
1594
ID : template .ActiveVersionID ,
1589
1595
})
1590
1596
require .NoError (t , err )
1591
- wait ("update active template version" )
1597
+ wait ("update active template version" , nil )
1592
1598
1593
1599
cancel ()
1594
1600
}
0 commit comments