@@ -14,6 +14,7 @@ import (
14
14
"github.com/stretchr/testify/require"
15
15
"go.opentelemetry.io/otel"
16
16
"go.opentelemetry.io/otel/propagation"
17
+ "golang.org/x/exp/slices"
17
18
"golang.org/x/xerrors"
18
19
19
20
"cdr.dev/slog"
@@ -1547,6 +1548,47 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1547
1548
}
1548
1549
})
1549
1550
1551
+ t .Run ("MultipleTimingsForSameAgentScript" , func (t * testing.T ) {
1552
+ t .Parallel ()
1553
+
1554
+ // Given: a build with multiple timings for the same script
1555
+ build := makeBuild (t )
1556
+ resource := dbgen .WorkspaceResource (t , db , database.WorkspaceResource {
1557
+ JobID : build .JobID ,
1558
+ })
1559
+ agent := dbgen .WorkspaceAgent (t , db , database.WorkspaceAgent {
1560
+ ResourceID : resource .ID ,
1561
+ })
1562
+ script := dbgen .WorkspaceAgentScript (t , db , database.WorkspaceAgentScript {
1563
+ WorkspaceAgentID : agent .ID ,
1564
+ })
1565
+ timings := make ([]database.WorkspaceAgentScriptTiming , 3 )
1566
+ scriptStartedAt := dbtime .Now ()
1567
+ for i := range timings {
1568
+ timings [i ] = dbgen .WorkspaceAgentScriptTiming (t , db , database.WorkspaceAgentScriptTiming {
1569
+ StartedAt : scriptStartedAt ,
1570
+ EndedAt : scriptStartedAt .Add (1 * time .Minute ),
1571
+ ScriptID : script .ID ,
1572
+ })
1573
+
1574
+ // Add an hour to the previous "started at" so we can
1575
+ // reliably differentiate the scripts from each other.
1576
+ scriptStartedAt = scriptStartedAt .Add (1 * time .Hour )
1577
+ }
1578
+
1579
+ // When: fetching timings for the build
1580
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1581
+ t .Cleanup (cancel )
1582
+ res , err := client .WorkspaceBuildTimings (ctx , build .ID )
1583
+ require .NoError (t , err )
1584
+
1585
+ // Then: return a response with the first agent script timing
1586
+ require .Len (t , res .AgentScriptTimings , 1 )
1587
+
1588
+ require .Equal (t , timings [0 ].StartedAt .UnixMilli (), res .AgentScriptTimings [0 ].StartedAt .UnixMilli ())
1589
+ require .Equal (t , timings [0 ].EndedAt .UnixMilli (), res .AgentScriptTimings [0 ].EndedAt .UnixMilli ())
1590
+ })
1591
+
1550
1592
t .Run ("AgentScriptTimings" , func (t * testing.T ) {
1551
1593
t .Parallel ()
1552
1594
@@ -1558,10 +1600,10 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1558
1600
agent := dbgen .WorkspaceAgent (t , db , database.WorkspaceAgent {
1559
1601
ResourceID : resource .ID ,
1560
1602
})
1561
- script := dbgen .WorkspaceAgentScript (t , db , database.WorkspaceAgentScript {
1603
+ scripts := dbgen .WorkspaceAgentScripts (t , db , 5 , database.WorkspaceAgentScript {
1562
1604
WorkspaceAgentID : agent .ID ,
1563
1605
})
1564
- agentScriptTimings := dbgen .WorkspaceAgentScriptTimings (t , db , script , 5 )
1606
+ agentScriptTimings := dbgen .WorkspaceAgentScriptTimings (t , db , scripts )
1565
1607
1566
1608
// When: fetching timings for the build
1567
1609
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -1571,6 +1613,12 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1571
1613
1572
1614
// Then: return a response with the expected timings
1573
1615
require .Len (t , res .AgentScriptTimings , 5 )
1616
+ slices .SortFunc (res .AgentScriptTimings , func (a , b codersdk.AgentScriptTiming ) int {
1617
+ return a .StartedAt .Compare (b .StartedAt )
1618
+ })
1619
+ slices .SortFunc (agentScriptTimings , func (a , b database.WorkspaceAgentScriptTiming ) int {
1620
+ return a .StartedAt .Compare (b .StartedAt )
1621
+ })
1574
1622
for i := range res .AgentScriptTimings {
1575
1623
timingRes := res .AgentScriptTimings [i ]
1576
1624
genTiming := agentScriptTimings [i ]
0 commit comments