Skip to content

Commit a0217fb

Browse files
Add tests
1 parent c5057f2 commit a0217fb

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Consent.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ private Token exchange(String code, String state) {
297297

298298
static class CallbackResponseHandler implements HttpHandler {
299299
private final Logger LOG = LoggerFactory.getLogger(getClass().getName());
300-
// Protects params
301-
private final Object lock = new Object();
300+
protected final Object lock = new Object(); // protected for testing
302301
private volatile Map<String, String> params;
303302
private final Optional<Duration> timeout;
304303

@@ -343,10 +342,7 @@ public void handleInner(HttpExchange exchange) throws IOException {
343342
});
344343

345344
sendSuccess(exchange);
346-
synchronized (lock) {
347-
params = theseParams;
348-
lock.notify();
349-
}
345+
setParams(theseParams);
350346
}
351347

352348
private void sendError(
@@ -411,5 +407,12 @@ public Map<String, String> getParams() {
411407
return params;
412408
}
413409
}
410+
411+
void setParams(Map<String, String> params) {
412+
synchronized (lock) {
413+
this.params = params;
414+
lock.notify();
415+
}
416+
}
414417
}
415418
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ConsentTest.java

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import com.databricks.sdk.core.DatabricksException;
56
import java.time.Duration;
7+
import java.util.HashMap;
8+
import java.util.Map;
69
import java.util.Optional;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.TimeUnit;
712
import org.junit.jupiter.api.Test;
813

914
public 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

Comments
 (0)