33import java .io .IOException ;
44import java .net .InetSocketAddress ;
55import java .net .Socket ;
6+ import java .time .Duration ;
67import java .util .List ;
78import java .util .UUID ;
89
1314import io .weaviate .ConcurrentTest ;
1415import io .weaviate .client6 .v1 .api .Authentication ;
1516import io .weaviate .client6 .v1 .internal .TokenProvider ;
17+ import io .weaviate .client6 .v1 .internal .TokenProvider .Token ;
1618import io .weaviate .client6 .v1 .internal .rest .RestTransport ;
1719import io .weaviate .containers .Weaviate ;
1820
@@ -88,9 +90,10 @@ public void test_resourceOwnerPassword() throws Exception {
8890 // Get the token obtained by the wrapped TokenProvider.
8991 var t = auth .getToken ();
9092
91- // Now make all tokens expire immediately, forcing the client to refresh..
92- // Verify the new token is different from the one before.
93- auth .setExpiresIn (0 );
93+ // Wait for the token to expire. Because we're testing against a live IdP
94+ // and cannot control it at test, all other "mocking" options are more
95+ // intrusive. Verify the new token is different from the one before.
96+ waitForExpiry (t );
9497 pingWeaviate (wcsContainer , auth );
9598
9699 var newT = auth .getToken ();
@@ -113,9 +116,10 @@ public void test_clientCredentials() throws Exception {
113116 // Get the token obtained by the wrapped TokenProvider.
114117 var t = auth .getToken ();
115118
116- // Now make all tokens expire immediately, forcing the client to refresh..
117- // Verify the new token is different from the one before.
118- auth .setExpiresIn (0 );
119+ // Wait for the token to expire. Because we're testing against a live IdP
120+ // and cannot control it at test, all other "mocking" options are more
121+ // intrusive. Verify the new token is different from the one before.
122+ waitForExpiry (t );
119123 pingWeaviate (oktaContainer , auth );
120124
121125 var newT = auth .getToken ();
@@ -170,6 +174,20 @@ private static boolean ping(String site) {
170174 }
171175 }
172176
177+ private static void waitForExpiry (Token t ) {
178+ if (!t .isValid ()) {
179+ return ;
180+ }
181+ if (t .neverExpires ()) {
182+ throw new IllegalStateException ("token never expires" );
183+ }
184+ try {
185+ Thread .sleep (Duration .ofSeconds (t .expiresIn ()).toMillis ());
186+ } catch (InterruptedException e ) {
187+ Thread .currentThread ().interrupt ();
188+ }
189+ }
190+
173191 /**
174192 * SpyTokenProvider is an Authentication implementation that spies on the
175193 * TokenProvider it creates and can expose tokens generated by it.
@@ -188,7 +206,6 @@ static Token stealToken(Authentication auth) throws Exception {
188206 return spy .getToken ();
189207 }
190208
191- private Long expiresIn ;
192209 private Authentication authentication ;
193210 private TokenProvider tokenProvider ;
194211
@@ -204,17 +221,7 @@ public TokenProvider getTokenProvider(RestTransport transport) {
204221
205222 @ Override
206223 public Token getToken () {
207- var t = tokenProvider .getToken ();
208- if (expiresIn != null ) {
209- t = Token .expireAfter (t .accessToken (), t .refreshToken (), expiresIn );
210- }
211- return t ;
212- }
213-
214- /** Expire all tokens in {@code expiresIn} seconds. */
215- void setExpiresIn (long expiresIn ) {
216- this .expiresIn = expiresIn ;
224+ return tokenProvider .getToken ();
217225 }
218-
219226 }
220227}
0 commit comments