66
77import static org .assertj .core .api .Assertions .assertThat ;
88
9- import io .opentelemetry .sdk .internal .TestClock ;
9+ import io .opentelemetry .sdk .testing .time .TestClock ;
10+ import java .time .Duration ;
1011import java .util .ArrayList ;
1112import java .util .List ;
1213import java .util .concurrent .ExecutionException ;
@@ -32,18 +33,18 @@ void testRateLimiterWholeNumber() {
3233 assertThat (limiter .trySpend (1.0 )).isTrue ();
3334 assertThat (limiter .trySpend (1.0 )).isFalse ();
3435 // move time 250ms forward, not enough credits to pay for 1.0 item
35- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (250 ));
36+ clock .advance ( Duration . ofMillis (250 ));
3637 assertThat (limiter .trySpend (1.0 )).isFalse ();
3738
3839 // move time 500ms forward, now enough credits to pay for 1.0 item
39- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (500 ));
40+ clock .advance ( Duration . ofMillis (500 ));
4041
4142 assertThat (limiter .trySpend (1.0 )).isTrue ();
4243 assertThat (limiter .trySpend (1.0 )).isFalse ();
4344
4445 // move time 5s forward, enough to accumulate credits for 10 messages, but it should still be
4546 // capped at 2
46- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (5000 ));
47+ clock .advance ( Duration . ofMillis (5000 ));
4748
4849 assertThat (limiter .trySpend (1.0 )).isTrue ();
4950 assertThat (limiter .trySpend (1.0 )).isTrue ();
@@ -58,7 +59,7 @@ void testRateLimiterSteadyRate() {
5859 RateLimiter limiter = new RateLimiter (5.0 / 60.0 , 5.0 , clock );
5960 for (int i = 0 ; i < 100 ; i ++) {
6061 assertThat (limiter .trySpend (1.0 )).isTrue ();
61- clock .advanceNanos ( TimeUnit . SECONDS . toNanos (20 ));
62+ clock .advance ( Duration . ofSeconds (20 ));
6263 }
6364 }
6465
@@ -78,18 +79,18 @@ void testRateLimiterLessThanOne() {
7879 assertThat (limiter .trySpend (0.25 )).isTrue ();
7980 assertThat (limiter .trySpend (0.25 )).isFalse ();
8081 // move time 250ms forward, not enough credits to pay for 1.0 item
81- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (250 ));
82+ clock .advance ( Duration . ofMillis (250 ));
8283 assertThat (limiter .trySpend (0.25 )).isFalse ();
8384
8485 // move time 500ms forward, now enough credits to pay for 1.0 item
85- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (500 ));
86+ clock .advance ( Duration . ofMillis (500 ));
8687
8788 assertThat (limiter .trySpend (0.25 )).isTrue ();
8889 assertThat (limiter .trySpend (0.25 )).isFalse ();
8990
9091 // move time 5s forward, enough to accumulate credits for 10 messages, but it should still be
9192 // capped at 2
92- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (5000 ));
93+ clock .advance ( Duration . ofMillis (5000 ));
9394
9495 assertThat (limiter .trySpend (0.25 )).isTrue ();
9596 assertThat (limiter .trySpend (0.25 )).isTrue ();
@@ -103,13 +104,13 @@ void testRateLimiterMaxBalance() {
103104 TestClock clock = TestClock .create ();
104105 RateLimiter limiter = new RateLimiter (0.1 , 1.0 , clock );
105106
106- clock .advanceNanos ( TimeUnit .MICROSECONDS .toNanos (100 ));
107+ clock .advance ( Duration . ofNanos ( TimeUnit .MICROSECONDS .toNanos (100 ) ));
107108 assertThat (limiter .trySpend (1.0 )).isTrue ();
108109 assertThat (limiter .trySpend (1.0 )).isFalse ();
109110
110111 // move time 20s forward, enough to accumulate credits for 2 messages, but it should still be
111112 // capped at 1
112- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (20000 ));
113+ clock .advance ( Duration . ofMillis (20000 ));
113114
114115 assertThat (limiter .trySpend (1.0 )).isTrue ();
115116 assertThat (limiter .trySpend (1.0 )).isFalse ();
@@ -127,19 +128,17 @@ void testRateLimiterInitial() {
127128 assertThat (limiter .trySpend (100 )).isTrue (); // consume initial (max) balance
128129 assertThat (limiter .trySpend (1 )).isFalse ();
129130
130- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (49 )); // add 49 credits
131+ clock .advance ( Duration . ofMillis (49 )); // add 49 credits
131132 assertThat (limiter .trySpend (50 )).isFalse ();
132133
133- clock .advanceNanos ( TimeUnit . MILLISECONDS . toNanos (1 )); // add one credit
134+ clock .advance ( Duration . ofMillis (1 )); // add one credit
134135 assertThat (limiter .trySpend (50 )).isTrue (); // consume accrued balance
135136 assertThat (limiter .trySpend (1 )).isFalse ();
136137
137- clock .advanceNanos (
138- TimeUnit .MILLISECONDS .toNanos (1_000_000 )); // add a lot of credits (max out balance)
138+ clock .advance (Duration .ofMillis (1_000_000 )); // add a lot of credits (max out balance)
139139 assertThat (limiter .trySpend (1 )).isTrue (); // take one credit
140140
141- clock .advanceNanos (
142- TimeUnit .MILLISECONDS .toNanos (1_000_000 )); // add a lot of credits (max out balance)
141+ clock .advance (Duration .ofMillis (1_000_000 )); // add a lot of credits (max out balance)
143142 assertThat (limiter .trySpend (101 )).isFalse (); // can't consume more than max balance
144143 assertThat (limiter .trySpend (100 )).isTrue (); // consume max balance
145144 assertThat (limiter .trySpend (1 )).isFalse ();
0 commit comments