22
33import static org .junit .jupiter .api .Assertions .*;
44
5+ import com .databricks .sdk .core .DatabricksException ;
56import java .time .Duration ;
7+ import java .util .HashMap ;
8+ import java .util .Map ;
69import java .util .Optional ;
10+ import java .util .concurrent .CountDownLatch ;
11+ import java .util .concurrent .TimeUnit ;
712import org .junit .jupiter .api .Test ;
813
914public class ConsentTest {
@@ -22,7 +27,6 @@ public void testConsentWithBrowserAuthTimeout() {
2227 .withBrowserTimeout (Optional .of (Duration .ofSeconds (30 )))
2328 .build ();
2429
25- // Verify that the timeout is properly set
2630 assertEquals (Optional .of (Duration .ofSeconds (30 )), consent .getBrowserTimeout ());
2731 }
2832
@@ -39,7 +43,90 @@ public void testConsentWithoutBrowserAuthTimeout() {
3943 .withVerifier ("test-verifier" )
4044 .build ();
4145
42- // Verify that the timeout is empty when not set
4346 assertEquals (Optional .empty (), consent .getBrowserTimeout ());
4447 }
48+
49+ @ Test
50+ public void testTimeoutLogicWithShortTimeout () throws InterruptedException {
51+ // Test that timeout is enforced correctly.
52+ Consent .CallbackResponseHandler handler =
53+ new Consent .CallbackResponseHandler (Optional .of (Duration .ofMillis (100 ))); // 100ms timeout
54+
55+ long startTime = System .currentTimeMillis ();
56+
57+ try {
58+ handler .getParams ();
59+ fail ("Expected timeout exception" );
60+ } catch (DatabricksException e ) {
61+ long elapsedTime = System .currentTimeMillis () - startTime ;
62+ assertTrue (
63+ elapsedTime >= 100 , "Timeout should have taken at least 100ms, but took " + elapsedTime );
64+ assertTrue (e .getMessage ().contains ("timed out after 0 seconds" ));
65+ }
66+ }
67+
68+ @ Test
69+ public void testTimeoutLogicWithNoTimeout () throws InterruptedException {
70+ // Test that no timeout means indefinite wait.
71+ Consent .CallbackResponseHandler handler = new Consent .CallbackResponseHandler (Optional .empty ());
72+
73+ CountDownLatch latch = new CountDownLatch (1 );
74+
75+ Thread setterThread =
76+ new Thread (
77+ () -> {
78+ try {
79+ Thread .sleep (50 );
80+ synchronized (handler .lock ) {
81+ Map <String , String > params = new HashMap <>();
82+ params .put ("code" , "test-code" );
83+ params .put ("state" , "test-state" );
84+ handler .setParams (params );
85+ }
86+ latch .countDown ();
87+ } catch (InterruptedException e ) {
88+ Thread .currentThread ().interrupt ();
89+ }
90+ });
91+
92+ setterThread .start ();
93+
94+ Map <String , String > result = handler .getParams ();
95+ assertNotNull (result );
96+ assertEquals ("test-code" , result .get ("code" ));
97+ assertEquals ("test-state" , result .get ("state" ));
98+ assertTrue (latch .await (1 , TimeUnit .SECONDS ));
99+ }
100+
101+ @ Test
102+ public void testTimeoutLogicWithParamsSetBeforeTimeout () throws InterruptedException {
103+ // Test that if params are set before timeout, no exception is thrown.
104+ Consent .CallbackResponseHandler handler =
105+ new Consent .CallbackResponseHandler (Optional .of (Duration .ofSeconds (1 )));
106+
107+ CountDownLatch latch = new CountDownLatch (1 );
108+
109+ Thread setterThread =
110+ new Thread (
111+ () -> {
112+ try {
113+ Thread .sleep (50 );
114+ synchronized (handler .lock ) {
115+ Map <String , String > params = new HashMap <>();
116+ params .put ("code" , "test-code" );
117+ handler .setParams (params );
118+ }
119+ latch .countDown ();
120+ } catch (InterruptedException e ) {
121+ Thread .currentThread ().interrupt ();
122+ }
123+ });
124+
125+ setterThread .start ();
126+
127+ Map <String , String > result = handler .getParams ();
128+ assertNotNull (result );
129+ assertEquals ("test-code" , result .get ("code" ));
130+ assertTrue (latch .await (1 , TimeUnit .SECONDS ));
131+ }
45132}
0 commit comments