@@ -15,6 +15,7 @@ import (
15
15
"runtime"
16
16
"strconv"
17
17
"strings"
18
+ "sync"
18
19
"testing"
19
20
"time"
20
21
@@ -1406,4 +1407,51 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
1406
1407
require .Equal (t , []string {"Origin" , "X-Foobar" }, deduped )
1407
1408
require .Equal (t , []string {"baz" }, resp .Header .Values ("X-Foobar" ))
1408
1409
})
1410
+
1411
+ t .Run ("ReportStats" , func (t * testing.T ) {
1412
+ t .Parallel ()
1413
+
1414
+ reporter := & fakeStatsReporter {}
1415
+ collector := workspaceapps .NewStatsCollector (workspaceapps.StatsCollectorOptions {
1416
+ Reporter : reporter ,
1417
+ ReportInterval : time .Second ,
1418
+ RollupWindow : time .Minute ,
1419
+ })
1420
+ appDetails := setupProxyTest (t , & DeploymentOptions {
1421
+ StatsCollector : collector ,
1422
+ })
1423
+
1424
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1425
+ defer cancel ()
1426
+
1427
+ u := appDetails .PathAppURL (appDetails .Apps .Owner )
1428
+ resp , err := requestWithRetries (ctx , t , appDetails .AppClient (t ), http .MethodGet , u .String (), nil )
1429
+ require .NoError (t , err )
1430
+ defer resp .Body .Close ()
1431
+ _ , err = io .Copy (io .Discard , resp .Body )
1432
+ require .NoError (t , err )
1433
+ require .Equal (t , http .StatusOK , resp .StatusCode )
1434
+
1435
+ require .Eventually (t , func () bool {
1436
+ return len (reporter .stats ()) > 0
1437
+ }, testutil .WaitLong , testutil .IntervalMedium , "stats not reported" )
1438
+ })
1439
+ }
1440
+
1441
+ type fakeStatsReporter struct {
1442
+ mu sync.Mutex
1443
+ s []workspaceapps.StatsReport
1444
+ }
1445
+
1446
+ func (r * fakeStatsReporter ) stats () []workspaceapps.StatsReport {
1447
+ r .mu .Lock ()
1448
+ defer r .mu .Unlock ()
1449
+ return r .s
1450
+ }
1451
+
1452
+ func (r * fakeStatsReporter ) Report (_ context.Context , stats []workspaceapps.StatsReport ) error {
1453
+ r .mu .Lock ()
1454
+ r .s = append (r .s , stats ... )
1455
+ r .mu .Unlock ()
1456
+ return nil
1409
1457
}
0 commit comments