@@ -10,33 +10,6 @@ trait UsageBase
1010{
1111 protected Usage $ usage ;
1212
13- /**
14- * Retry the provided assertions until they pass or timeout (seconds).
15- *
16- * @param callable $fn Assertions to run
17- * @param int $timeout Seconds to wait before failing
18- * @param float $interval Seconds between retries
19- */
20- protected function assertEventually (callable $ fn , int $ timeout = 5 , float $ interval = 0.5 ): void
21- {
22- $ start = microtime (true );
23- $ lastException = null ;
24-
25- while (microtime (true ) - $ start < $ timeout ) {
26- try {
27- $ fn ();
28- return ;
29- } catch (\Throwable $ e ) {
30- $ lastException = $ e ;
31- usleep ((int ) ($ interval * 1_000_000 ));
32- }
33- }
34-
35- if ($ lastException ) {
36- throw $ lastException ;
37- }
38- }
39-
4013 abstract protected function initializeUsage (): void ;
4114
4215 public function setUp (): void
@@ -100,16 +73,14 @@ public function testLogBatch(): void
10073
10174 public function testGetByPeriod (): void
10275 {
103- $ this ->assertEventually (function () {
104- $ results1h = $ this ->usage ->getByPeriod ('requests ' , '1h ' );
105- $ results1d = $ this ->usage ->getByPeriod ('requests ' , '1d ' );
106- $ resultsInf = $ this ->usage ->getByPeriod ('storage ' , 'inf ' );
107-
108- // SummingMergeTree / upsert-with-increase aggregates by deterministic id
109- $ this ->assertEquals (1 , count ($ results1h ));
110- $ this ->assertEquals (1 , count ($ results1d ));
111- $ this ->assertEquals (1 , count ($ resultsInf ));
112- });
76+ $ results1h = $ this ->usage ->getByPeriod ('requests ' , '1h ' );
77+ $ results1d = $ this ->usage ->getByPeriod ('requests ' , '1d ' );
78+ $ resultsInf = $ this ->usage ->getByPeriod ('storage ' , 'inf ' );
79+
80+ // SummingMergeTree / upsert-with-increase aggregates by deterministic id
81+ $ this ->assertEquals (1 , count ($ results1h ));
82+ $ this ->assertEquals (1 , count ($ results1d ));
83+ $ this ->assertEquals (1 , count ($ resultsInf ));
11384 }
11485
11586 public function testGetBetweenDates (): void
@@ -123,27 +94,23 @@ public function testGetBetweenDates(): void
12394
12495 public function testCountByPeriod (): void
12596 {
126- $ this ->assertEventually (function () {
127- $ count1h = $ this ->usage ->countByPeriod ('requests ' , '1h ' );
128- $ count1d = $ this ->usage ->countByPeriod ('requests ' , '1d ' );
129- $ countBandwidth = $ this ->usage ->countByPeriod ('bandwidth ' , '1h ' );
130-
131- // Aggregated by deterministic id: multiple logs in same period/time collapse
132- $ this ->assertEquals (1 , $ count1h );
133- $ this ->assertEquals (1 , $ count1d );
134- $ this ->assertEquals (1 , $ countBandwidth );
135- });
97+ $ count1h = $ this ->usage ->countByPeriod ('requests ' , '1h ' );
98+ $ count1d = $ this ->usage ->countByPeriod ('requests ' , '1d ' );
99+ $ countBandwidth = $ this ->usage ->countByPeriod ('bandwidth ' , '1h ' );
100+
101+ // Aggregated by deterministic id: multiple logs in same period/time collapse
102+ $ this ->assertEquals (1 , $ count1h );
103+ $ this ->assertEquals (1 , $ count1d );
104+ $ this ->assertEquals (1 , $ countBandwidth );
136105 }
137106
138107 public function testSumByPeriod (): void
139108 {
140- $ this ->assertEventually (function () {
141- $ sum = $ this ->usage ->sumByPeriod ('requests ' , '1h ' );
142- $ this ->assertEquals (250 , $ sum ); // 100 + 150
109+ $ sum = $ this ->usage ->sumByPeriod ('requests ' , '1h ' );
110+ $ this ->assertEquals (250 , $ sum ); // 100 + 150
143111
144- $ sumBandwidth = $ this ->usage ->sumByPeriod ('bandwidth ' , '1h ' );
145- $ this ->assertEquals (5000 , $ sumBandwidth );
146- });
112+ $ sumBandwidth = $ this ->usage ->sumByPeriod ('bandwidth ' , '1h ' );
113+ $ this ->assertEquals (5000 , $ sumBandwidth );
147114 }
148115
149116 public function testIncrementingDefaultBehavior (): void
@@ -154,15 +121,13 @@ public function testIncrementingDefaultBehavior(): void
154121 // Log the same metric twice with identical period and tags
155122 $ this ->assertTrue ($ this ->usage ->log ('increment-test ' , 5 , '1h ' , []));
156123 $ this ->assertTrue ($ this ->usage ->log ('increment-test ' , 7 , '1h ' , []));
157- $ this ->assertEventually (function () {
158- // Because adapters now aggregate by deterministic id/time/period (and tenant where applicable),
159- // there should be a single record and the summed value should be 12.
160- $ results = $ this ->usage ->getByPeriod ('increment-test ' , '1h ' );
161- $ this ->assertEquals (1 , count ($ results ));
162-
163- $ sum = $ this ->usage ->sumByPeriod ('increment-test ' , '1h ' );
164- $ this ->assertEquals (12 , $ sum );
165- }, 2 );
124+ // Because adapters now aggregate by deterministic id/time/period (and tenant where applicable),
125+ // there should be a single record and the summed value should be 12.
126+ $ results = $ this ->usage ->getByPeriod ('increment-test ' , '1h ' );
127+ $ this ->assertEquals (1 , count ($ results ));
128+
129+ $ sum = $ this ->usage ->sumByPeriod ('increment-test ' , '1h ' );
130+ $ this ->assertEquals (12 , $ sum );
166131 }
167132
168133 public function testWithQueries (): void
0 commit comments