@@ -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"
@@ -1421,6 +1422,47 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1421
1422
}
1422
1423
})
1423
1424
1425
+ t .Run ("MultipleTimingsForSameAgentScript" , func (t * testing.T ) {
1426
+ t .Parallel ()
1427
+
1428
+ // Given: a build with multiple timings for the same script
1429
+ build := makeBuild (t )
1430
+ resource := dbgen .WorkspaceResource (t , db , database.WorkspaceResource {
1431
+ JobID : build .JobID ,
1432
+ })
1433
+ agent := dbgen .WorkspaceAgent (t , db , database.WorkspaceAgent {
1434
+ ResourceID : resource .ID ,
1435
+ })
1436
+ script := dbgen .WorkspaceAgentScript (t , db , database.WorkspaceAgentScript {
1437
+ WorkspaceAgentID : agent .ID ,
1438
+ })
1439
+ timings := make ([]database.WorkspaceAgentScriptTiming , 3 )
1440
+ scriptStartedAt := dbtime .Now ()
1441
+ for i := range timings {
1442
+ timings [i ] = dbgen .WorkspaceAgentScriptTiming (t , db , database.WorkspaceAgentScriptTiming {
1443
+ StartedAt : scriptStartedAt ,
1444
+ EndedAt : scriptStartedAt .Add (1 * time .Minute ),
1445
+ ScriptID : script .ID ,
1446
+ })
1447
+
1448
+ // Add an hour to the previous "started at" so we can
1449
+ // reliably differentiate the scripts from each other.
1450
+ scriptStartedAt = scriptStartedAt .Add (1 * time .Hour )
1451
+ }
1452
+
1453
+ // When: fetching timings for the build
1454
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1455
+ t .Cleanup (cancel )
1456
+ res , err := client .WorkspaceBuildTimings (ctx , build .ID )
1457
+ require .NoError (t , err )
1458
+
1459
+ // Then: return a response with the first agent script timing
1460
+ require .Len (t , res .AgentScriptTimings , 1 )
1461
+
1462
+ require .Equal (t , timings [0 ].StartedAt .UnixMilli (), res .AgentScriptTimings [0 ].StartedAt .UnixMilli ())
1463
+ require .Equal (t , timings [0 ].EndedAt .UnixMilli (), res .AgentScriptTimings [0 ].EndedAt .UnixMilli ())
1464
+ })
1465
+
1424
1466
t .Run ("AgentScriptTimings" , func (t * testing.T ) {
1425
1467
t .Parallel ()
1426
1468
@@ -1432,10 +1474,10 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1432
1474
agent := dbgen .WorkspaceAgent (t , db , database.WorkspaceAgent {
1433
1475
ResourceID : resource .ID ,
1434
1476
})
1435
- script := dbgen .WorkspaceAgentScript (t , db , database.WorkspaceAgentScript {
1477
+ scripts := dbgen .WorkspaceAgentScripts (t , db , 5 , database.WorkspaceAgentScript {
1436
1478
WorkspaceAgentID : agent .ID ,
1437
1479
})
1438
- agentScriptTimings := dbgen .WorkspaceAgentScriptTimings (t , db , script , 5 )
1480
+ agentScriptTimings := dbgen .WorkspaceAgentScriptTimings (t , db , scripts )
1439
1481
1440
1482
// When: fetching timings for the build
1441
1483
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -1445,6 +1487,12 @@ func TestWorkspaceBuildTimings(t *testing.T) {
1445
1487
1446
1488
// Then: return a response with the expected timings
1447
1489
require .Len (t , res .AgentScriptTimings , 5 )
1490
+ slices .SortFunc (res .AgentScriptTimings , func (a , b codersdk.AgentScriptTiming ) int {
1491
+ return a .StartedAt .Compare (b .StartedAt )
1492
+ })
1493
+ slices .SortFunc (agentScriptTimings , func (a , b database.WorkspaceAgentScriptTiming ) int {
1494
+ return a .StartedAt .Compare (b .StartedAt )
1495
+ })
1448
1496
for i := range res .AgentScriptTimings {
1449
1497
timingRes := res .AgentScriptTimings [i ]
1450
1498
genTiming := agentScriptTimings [i ]
0 commit comments