Skip to content

Commit dced517

Browse files
ttypicclaude
andcommitted
Give the door builders an options-accepting entry point
The device and server doors exposed only no-arg fluent builders, but the branch's own callers — the UTS ClientFactories seam and the device instrumentation test — call realtimeClientBuilder(options), httpClientBuilder(options) and clientBuilder(options). That mismatch broke :uts:compileKotlin and :device:compileDebugAndroidTestJavaWithJavac, and with them every CI job that builds either module. The fluent setters cannot express a pre-assembled DebugOptions (transportFactory, webSocketEngineFactory, clock), so the UTS server leg has to hand the door an options object rather than replay it setter by setter. Each door gains one overload taking ClientOptions, and the builder wraps that object instead of its own empty one, so a DebugOptions subclass and its mock transports reach the client intact. A key or token still goes through the builder's own key()/token() setter: there is no key-string overload. Also drops shared/src/main/java/io/ably/pubsub/internal/Side.java, a second copy of the side-stamping helper that no module compiled — settings.gradle.kts never included it and no source set added it as a srcDir, so io.ably.lib.util.Side is the one the doors actually use. The two references to the dead class, SideModesTest's import and a Defaults javadoc pointer, now name the surviving one. The instrumentation test asserts non-mutation the way PubSubServerTest does, on the caller's agents map rather than on the identity of options.agents, because build() stamps that field in place. :device had no androidTest dependencies at all, so add JUnit, a runner and the testInstrumentationRunner the emulator matrix needs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent b4ed0a9 commit dced517

7 files changed

Lines changed: 114 additions & 114 deletions

File tree

‎device/build.gradle.kts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ android {
5252

5353
defaultConfig {
5454
minSdk = 24
55+
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
5556
}
5657

5758
compileOptions {
@@ -79,6 +80,17 @@ android {
7980
}
8081
}
8182

83+
dependencies {
84+
/*
85+
* The instrumentation test is Java under src/androidTest/java, so AGP compiles it rather
86+
* than a Kotlin source set and its dependencies belong on AGP's androidTest configuration.
87+
* It needs only JUnit and a runner: unlike :core-android's push tests it mocks nothing, so
88+
* the instrumental-android bundle (dexmaker, mockito, retrostreams) would be dead weight.
89+
*/
90+
androidTestImplementation(libs.junit)
91+
androidTestImplementation(libs.android.test.runner)
92+
}
93+
8294
mavenPublishing {
8395
configure(KotlinMultiplatform(javadocJar = JavadocJar.Empty(), androidVariantsToPublish = listOf("release")))
8496
}

‎device/src/androidTest/java/io/ably/pubsub/device/PubSubDeviceTest.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotSame;
56
import static org.junit.Assert.assertNull;
67
import static org.junit.Assert.assertTrue;
78

89
import io.ably.lib.realtime.AblyRealtime;
910
import io.ably.lib.types.ClientOptions;
10-
import io.ably.pubsub.internal.Side;
11+
import io.ably.lib.util.Side;
1112
import java.util.HashMap;
1213
import java.util.Map;
1314
import org.junit.Test;
@@ -45,8 +46,12 @@ public void client_stampsDeviceAgent() throws Exception {
4546
}
4647

4748
@Test
48-
public void keyString_isAcceptedAndDisambiguatedAsKey() throws Exception {
49-
ClientOptions builtOptions = PubSubDevice.clientBuilder(FAKE_KEY).build().options;
49+
public void key_isCarriedThroughToTheClient() throws Exception {
50+
ClientOptions builtOptions = PubSubDevice.clientBuilder()
51+
.key(FAKE_KEY)
52+
.autoConnect(false)
53+
.build()
54+
.options;
5055
assertEquals(FAKE_KEY, builtOptions.key);
5156
assertNull(builtOptions.token);
5257
assertDeviceFlag(builtOptions.agents);
@@ -65,8 +70,9 @@ public void callerAgentEntries_arePreserved_andCannotOverrideTheSideEntry() thro
6570
// The stamp replaces the caller's value: the flag is present and back to versionless.
6671
assertDeviceFlag(client.options.agents);
6772

68-
// the caller's own map is untouched
69-
assertTrue(options.agents == callerAgents);
73+
// the caller's own map is untouched: the stamp goes into a fresh one
74+
assertNotSame("the stamp must go into a copy, not the caller's map",
75+
callerAgents, client.options.agents);
7076
assertEquals("not-the-real-form", callerAgents.get(Side.DEVICE_AGENT_IDENTIFIER));
7177
}
7278
}

‎device/src/commonMain/kotlin/io/ably/pubsub/device/PubSubDevice.kt‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,34 @@ public object PubSubDevice {
4242
public fun clientBuilder(): ClientBuilder = ClientBuilder()
4343

4444
/**
45-
* Builds a device client. Obtain one from [PubSubDevice.clientBuilder].
45+
* As [clientBuilder], but starting from options the caller has already assembled, rather
46+
* than from the builder's own empty set.
47+
*
48+
* This is for callers holding a [ClientOptions] they did not build here: an SDK layered on
49+
* this package that takes options from its own user, or a test harness passing a
50+
* [io.ably.lib.debug.DebugOptions] subclass. To supply an API key or token, prefer
51+
* [ClientBuilder.key] or [ClientBuilder.token] on the no-arg builder over assembling
52+
* options for it.
53+
*
54+
* The builder's setters still apply on top of what is passed here, and [ClientBuilder.build]
55+
* stamps the side-declaring agent entry as usual. The options object is handed to the
56+
* client rather than copied, so later changes to it are not isolated from the built client,
57+
* and [ClientBuilder.build] replaces its `agents` map with the stamped one; pass
58+
* [ClientOptions.copy] if either matters.
59+
*
60+
* @param options the options to start from.
61+
* @return a new builder over [options].
4662
*/
47-
public class ClientBuilder internal constructor() {
63+
@JvmStatic
64+
public fun clientBuilder(options: ClientOptions): ClientBuilder = ClientBuilder(options)
4865

66+
/**
67+
* Builds a device client. Obtain one from [PubSubDevice.clientBuilder].
68+
*/
69+
public class ClientBuilder internal constructor(
4970
/** Accumulates the calls made on this builder; handed to the client as-is by [build]. */
50-
private val options = ClientOptions()
71+
private val options: ClientOptions = ClientOptions(),
72+
) {
5173

5274
/**
5375
* Sets [Auth.AuthOptions.authCallback].

‎lib/src/main/java/io/ably/lib/transport/Defaults.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Defaults {
1919
* split, so the identifier alone partitions the fleet: {@code ably-java/*} is legacy-package
2020
* traffic, {@code ably-pubsub-java/*} is new-package traffic. It names the family rather than
2121
* any one published artifact; the side a client declares travels as a separate versionless
22-
* agent entry (see io.ably.pubsub.internal.Side and the agents registry in ably-common).
22+
* agent entry (see {@link io.ably.lib.util.Side} and the agents registry in ably-common).
2323
*/
2424
public static final String ABLY_AGENT_VERSION = String.format("%s/%s", "ably-pubsub-java", BuildConfig.VERSION);
2525

‎server/src/main/java/io/ably/pubsub/server/PubSubServer.java‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ public static HttpClientBuilder httpClientBuilder() {
5353
return new HttpClientBuilder();
5454
}
5555

56+
/**
57+
* As {@link #httpClientBuilder()}, but starting from options the caller has already
58+
* assembled, rather than from the builder's own empty set.
59+
* <p>
60+
* This is for callers holding a {@link ClientOptions} they did not build here: an SDK
61+
* layered on this package that takes options from its own user, or a test harness passing
62+
* a {@link io.ably.lib.debug.DebugOptions} subclass. To supply an API key or token, prefer
63+
* {@link ClientBuilder#key(String)} or {@link ClientBuilder#token(String)} on the no-arg
64+
* builder over assembling options for it.
65+
* <p>
66+
* The builder's setters still apply on top of what is passed here, and {@link
67+
* HttpClientBuilder#build()} stamps the side-declaring agent entry as usual. The
68+
* options object is handed to the client rather than copied, so later changes to it are not
69+
* isolated from the built client, and {@code build()} replaces its {@code agents} map with
70+
* the stamped one; pass {@link ClientOptions#copy()} if either matters. A {@code null} is
71+
* carried through to {@code build()}, which surfaces the core constructor's own
72+
* "no options provided" error.
73+
*
74+
* @param options the options to start from.
75+
* @return a new builder over {@code options}.
76+
*/
77+
public static HttpClientBuilder httpClientBuilder(ClientOptions options) {
78+
return new HttpClientBuilder(options);
79+
}
80+
5681
/**
5782
* Creates a builder for a realtime client, which holds a persistent connection to Ably and can
5883
* subscribe to messages and presence as they happen.
@@ -66,6 +91,31 @@ public static RealtimeClientBuilder realtimeClientBuilder() {
6691
return new RealtimeClientBuilder();
6792
}
6893

94+
/**
95+
* As {@link #realtimeClientBuilder()}, but starting from options the caller has already
96+
* assembled, rather than from the builder's own empty set.
97+
* <p>
98+
* This is for callers holding a {@link ClientOptions} they did not build here: an SDK
99+
* layered on this package that takes options from its own user, or a test harness passing
100+
* a {@link io.ably.lib.debug.DebugOptions} subclass. To supply an API key or token, prefer
101+
* {@link ClientBuilder#key(String)} or {@link ClientBuilder#token(String)} on the no-arg
102+
* builder over assembling options for it.
103+
* <p>
104+
* The builder's setters still apply on top of what is passed here, and {@link
105+
* RealtimeClientBuilder#build()} stamps the side-declaring agent entry as usual. The
106+
* options object is handed to the client rather than copied, so later changes to it are not
107+
* isolated from the built client, and {@code build()} replaces its {@code agents} map with
108+
* the stamped one; pass {@link ClientOptions#copy()} if either matters. A {@code null} is
109+
* carried through to {@code build()}, which surfaces the core constructor's own
110+
* "no options provided" error.
111+
*
112+
* @param options the options to start from.
113+
* @return a new builder over {@code options}.
114+
*/
115+
public static RealtimeClientBuilder realtimeClientBuilder(ClientOptions options) {
116+
return new RealtimeClientBuilder(options);
117+
}
118+
69119
/**
70120
* The options common to both server-side clients. One method per {@link ClientOptions}
71121
* property that can affect a client of either kind; {@link RealtimeClientBuilder} adds the
@@ -76,9 +126,14 @@ public static RealtimeClientBuilder realtimeClientBuilder() {
76126
public abstract static class ClientBuilder<T extends ClientBuilder<T>> {
77127

78128
/** Accumulates the calls made on this builder; handed to the client as-is by build(). */
79-
final ClientOptions options = new ClientOptions();
129+
final ClientOptions options;
80130

81131
ClientBuilder() {
132+
this(new ClientOptions());
133+
}
134+
135+
ClientBuilder(ClientOptions options) {
136+
this.options = options;
82137
}
83138

84139
@SuppressWarnings("unchecked")
@@ -486,6 +541,10 @@ public static final class HttpClientBuilder extends ClientBuilder<HttpClientBuil
486541
HttpClientBuilder() {
487542
}
488543

544+
HttpClientBuilder(ClientOptions options) {
545+
super(options);
546+
}
547+
489548
/**
490549
* Builds the client.
491550
*
@@ -507,6 +566,10 @@ public static final class RealtimeClientBuilder extends ClientBuilder<RealtimeCl
507566
RealtimeClientBuilder() {
508567
}
509568

569+
RealtimeClientBuilder(ClientOptions options) {
570+
super(options);
571+
}
572+
510573
/**
511574
* Sets {@link ClientOptions#realtimeHost}.
512575
*

‎shared/src/main/java/io/ably/pubsub/internal/Side.java‎

Lines changed: 0 additions & 103 deletions
This file was deleted.

‎uts/src/test/kotlin/io/ably/lib/uts/unit/SideModesTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ package io.ably.lib.uts.unit
1717
*/
1818

1919
import io.ably.lib.rest.AblyBase
20+
import io.ably.lib.util.Side
2021
import io.ably.lib.uts.infra.unit.MockHttpClient
2122
import io.ably.lib.uts.infra.unit.TestRealtimeClient
2223
import io.ably.lib.uts.infra.unit.TestRestClient
2324
import io.ably.lib.uts.infra.unit.utsSide
24-
import io.ably.pubsub.internal.Side
2525
import kotlin.test.Test
2626
import kotlin.test.assertFalse
2727
import kotlin.test.assertNotNull

0 commit comments

Comments
 (0)