Skip to content

Commit ce61a1b

Browse files
SimonWoolfttypic
authored andcommitted
Make releasing a realtime channel that isn't detached an error (RTS4c-e)
Spec 6.3.0 replaces RTS4a, under which release() detaches the channel and then removes it, with RTS4c-e: release() of a non-existent channel is a no-op, a channel in the INITIALIZED, DETACHED or FAILED state is removed synchronously, and release() of a channel in any other state throws 90011 and does nothing else. Channels#release now declares AblyException. Users must call channel.detach() and wait for it to complete before releasing. Since a released channel can no longer be mid-detach, drop the released-channel special cases from the detach path, the test of the old behaviour, and the liveobjects test teardown's release of channels that may still be attached (close() already disposes of them). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> � Conflicts: � lib/src/test/java/io/ably/pubsub/test/realtime/RealtimeChannelTest.java � lib/src/test/kotlin/io/ably/pubsub/uts/unit/realtime/ChannelsCollectionTest.kt � liveobjects/src/test/kotlin/io/ably/pubsub/liveobjects/integration/setup/IntegrationTest.kt � Conflicts: � lib/src/main/java/io/ably/pubsub/realtime/PubSubRealtimeClient.java � pubsub-adapter/src/main/kotlin/com/ably/pubsub/Channels.kt
1 parent 8321b91 commit ce61a1b

6 files changed

Lines changed: 156 additions & 68 deletions

File tree

‎lib/src/main/java/io/ably/pubsub/realtime/ChannelBase.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ private void sendDetachMessage(CompletionListener listener) throws AblyException
400400
}
401401

402402
this.attachResume = false;
403-
if (released.get()) {
404-
setDetached(null);
405-
} else {
406-
setState(ChannelState.detaching, null);
407-
}
403+
setState(ChannelState.detaching, null);
408404
ably.connection.connectionManager.send(detachMessage, true, null);
409405
} catch(AblyException e) {
410406
throw e;
@@ -630,7 +626,7 @@ synchronized private void detachWithTimeout(final CompletionListener listener) {
630626
final ChannelState originalState = state;
631627
AblyTimer currentDetachTimer;
632628
try {
633-
currentDetachTimer = released.get() ? null : clock.newTimer("detach-timer");
629+
currentDetachTimer = clock.newTimer("detach-timer");
634630
} catch(Throwable t) {
635631
/* an exception instancing the timer can arise because the runtime is exiting */
636632
callCompletionListenerError(listener, ErrorInfo.fromThrowable(t));
@@ -639,8 +635,7 @@ synchronized private void detachWithTimeout(final CompletionListener listener) {
639635
attachTimer = currentDetachTimer;
640636

641637
try {
642-
// If channel has been released, completionListener won't be invoked anyway
643-
CompletionListener completionListener = released.get() ? null : new CompletionListener() {
638+
CompletionListener completionListener = new CompletionListener() {
644639
@Override
645640
public void onSuccess() {
646641
clearAttachTimers();

‎lib/src/main/java/io/ably/pubsub/realtime/PubSubRealtimeClient.java‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,14 @@ public interface Channels extends ReadOnlyMap<String, Channel> {
171171
/**
172172
* Releases a {@link Channel} object, deleting it, and enabling it to be garbage collected.
173173
* It also removes any listeners associated with the channel.
174-
* A realtime channel should only be released when it is in the INITIALIZED, DETACHED, or FAILED state;
175-
* releasing a realtime channel in any other state is deprecated and will throw an error in the next major version.
174+
* A realtime channel can only be released when it is in the INITIALIZED, DETACHED, or FAILED state.
176175
* <p>
177176
* Spec: RSN4, RTS4
178177
* @param channelName The channel name.
178+
* @throws AblyException If the channel is in any other state. Call {@link Channel#detach(CompletionListener)}
179+
* and wait for it to complete before calling release().
179180
*/
180-
void release(String channelName);
181+
void release(String channelName) throws AblyException;
181182
}
182183

183184
private class InternalChannels extends InternalMap<String, Channel> implements Channels, ConnectionManager.Channels {
@@ -216,28 +217,25 @@ public Channel get(final String channelName, final ChannelOptions channelOptions
216217
}
217218

218219
@Override
219-
public void release(String channelName) {
220+
public void release(String channelName) throws AblyException {
220221
Channel channel = map.get(channelName);
221-
if(channel != null) {
222-
ChannelState state;
223-
// The channel's state transitions hold its monitor, so holding it here stops the channel
224-
// from starting to attach between the state check and it being marked as released.
225-
synchronized (channel) {
226-
if (!map.remove(channelName, channel)) {
227-
return;
228-
}
229-
state = channel.state;
230-
channel.markAsReleased();
231-
}
222+
if (channel == null) {
223+
return;
224+
}
225+
// The channel's state transitions hold its monitor, so holding it here stops the channel
226+
// from starting to attach between the state check and it being marked as released.
227+
synchronized (channel) {
228+
ChannelState state = channel.state;
232229
if (state != ChannelState.initialized && state != ChannelState.detached && state != ChannelState.failed) {
233-
// RTS4b
234-
Log.w(TAG, "Calling `channels.release()` on a channel in the " + state + " state is deprecated, and will throw an error in the next major version. Call `channel.detach()` and wait for it to complete before calling `channels.release(channelName)`.");
230+
// RTS4e
231+
throw AblyException.fromErrorInfo(new ErrorInfo(
232+
"Can only release a channel in a state where there is no possibility of further updates from the server being received (initialized, detached, or failed). The current state is " + state,
233+
400, 90011));
235234
}
236-
try {
237-
channel.detach();
238-
} catch (AblyException e) {
239-
Log.e(TAG, "Unexpected exception detaching channel; channelName = " + channelName, e);
235+
if (!map.remove(channelName, channel)) {
236+
return;
240237
}
238+
channel.markAsReleased();
241239
}
242240
if (liveObjectsPlugin != null) {
243241
liveObjectsPlugin.dispose(channelName);

‎lib/src/test/java/io/ably/pubsub/test/realtime/RealtimeChannelTest.java‎

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,41 +2443,6 @@ public void no_messages_when_channel_state_not_attached() {
24432443
}
24442444
}
24452445

2446-
/*
2447-
* Checks that the DETACHED message sent by the server when a channel is released is dropped.
2448-
*/
2449-
@Test
2450-
public void detach_message_to_released_channel_is_dropped() throws AblyException {
2451-
PubSubRealtimeClient ably = null;
2452-
long oldRealtimeTimeout = Defaults.realtimeRequestTimeout;
2453-
final String channelName = "detach_message_to_released_channel_is_dropped";
2454-
2455-
try {
2456-
DebugOptions opts = createOptions(testVars.keys[0].keyStr);
2457-
Helpers.RawProtocolMonitor monitor = Helpers.RawProtocolMonitor.createReceiver(ProtocolMessage.Action.detached);
2458-
opts.protocolListener = monitor;
2459-
2460-
/* Make test faster */
2461-
Defaults.realtimeRequestTimeout = 1000;
2462-
opts.channelRetryTimeout = 1000;
2463-
2464-
ably = RealtimeClientFactory.create(opts);
2465-
Channel channel = ably.channels.get(channelName);
2466-
channel.attach();
2467-
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
2468-
2469-
// Listen for detach messages and release the channel
2470-
ably.channels.release(channelName);
2471-
monitor.waitForRecv(1, 10000);
2472-
2473-
assertFalse(ably.channels.containsKey("messages_to_non_existent_channels_are_dropped"));
2474-
} finally {
2475-
if (ably != null)
2476-
ably.close();
2477-
Defaults.realtimeRequestTimeout = oldRealtimeTimeout;
2478-
}
2479-
}
2480-
24812446
/*
24822447
* Spec: RTN11d
24832448
* Checks that all channels become if the state is CLOSED transitions all the channels to
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.ably.pubsub.uts.unit.realtime
2+
3+
import io.ably.pubsub.uts.infra.unit.*
4+
import io.ably.pubsub.realtime.ChannelState
5+
import io.ably.pubsub.realtime.ConnectionState
6+
import io.ably.pubsub.types.AblyException
7+
import io.ably.pubsub.types.ProtocolMessage
8+
import io.ably.pubsub.uts.infra.awaitChannelState
9+
import io.ably.pubsub.uts.infra.awaitState
10+
import kotlinx.coroutines.test.runTest
11+
import java.util.UUID
12+
import java.util.concurrent.CopyOnWriteArrayList
13+
import kotlin.test.*
14+
15+
class ChannelsCollectionTest {
16+
17+
/**
18+
* @UTS realtime/unit/RTS4c/release-nonexistent-noop-0
19+
*/
20+
@Test
21+
fun `RTS4c - release on non-existent channel is no-op`() {
22+
val channelName = "test-RTS4c-nonexistent-${UUID.randomUUID()}"
23+
val client = TestRealtimeClient { autoConnect = false }
24+
25+
client.channels.release(channelName)
26+
27+
assertFalse(client.channels.containsKey(channelName))
28+
client.close()
29+
}
30+
31+
/**
32+
* @UTS realtime/unit/RTS4d/release-removes-channel-0
33+
*/
34+
@Test
35+
fun `RTS4d - release removes an initialized channel`() {
36+
val channelName = "test-RTS4d-${UUID.randomUUID()}"
37+
val client = TestRealtimeClient { autoConnect = false }
38+
39+
val channel = client.channels.get(channelName)
40+
assertEquals(ChannelState.initialized, channel.state)
41+
assertTrue(client.channels.containsKey(channelName))
42+
43+
client.channels.release(channelName)
44+
45+
assertFalse(client.channels.containsKey(channelName))
46+
client.close()
47+
}
48+
49+
/**
50+
* @UTS realtime/unit/RTS4d/release-after-detach-1
51+
*/
52+
@Test
53+
fun `RTS4d - release removes a channel once detached`() = runTest {
54+
val channelName = "test-RTS4d-detached-${UUID.randomUUID()}"
55+
lateinit var mockWs: MockWebSocket
56+
mockWs = MockWebSocket {
57+
onConnectionAttempt = { conn -> conn.respondWithSuccess(CONNECTED_MESSAGE) }
58+
onMessageFromClient = { msg ->
59+
when (msg.action) {
60+
ProtocolMessage.Action.attach ->
61+
mockWs.sendToClient(ProtocolMessage(ProtocolMessage.Action.attached, msg.channel))
62+
ProtocolMessage.Action.detach ->
63+
mockWs.sendToClient(ProtocolMessage(ProtocolMessage.Action.detached, msg.channel))
64+
else -> Unit
65+
}
66+
}
67+
}
68+
val client = TestRealtimeClient {
69+
autoConnect = false
70+
install(mockWs)
71+
}
72+
val channel = client.channels.get(channelName)
73+
74+
client.connect()
75+
awaitState(client, ConnectionState.connected)
76+
77+
channel.attach()
78+
awaitChannelState(channel, ChannelState.attached)
79+
channel.detach()
80+
awaitChannelState(channel, ChannelState.detached)
81+
82+
client.channels.release(channelName)
83+
84+
assertFalse(client.channels.containsKey(channelName))
85+
client.close()
86+
}
87+
88+
/**
89+
* @UTS realtime/unit/RTS4e/release-attached-fails-0
90+
*/
91+
@Test
92+
fun `RTS4e - release of an attached channel fails`() = runTest {
93+
val channelName = "test-RTS4e-attached-${UUID.randomUUID()}"
94+
val capturedDetachMessages = CopyOnWriteArrayList<ProtocolMessage>()
95+
lateinit var mockWs: MockWebSocket
96+
mockWs = MockWebSocket {
97+
onConnectionAttempt = { conn -> conn.respondWithSuccess(CONNECTED_MESSAGE) }
98+
onMessageFromClient = { msg ->
99+
when (msg.action) {
100+
ProtocolMessage.Action.attach ->
101+
mockWs.sendToClient(ProtocolMessage(ProtocolMessage.Action.attached, msg.channel))
102+
ProtocolMessage.Action.detach -> capturedDetachMessages.add(msg)
103+
else -> Unit
104+
}
105+
}
106+
}
107+
val client = TestRealtimeClient {
108+
autoConnect = false
109+
install(mockWs)
110+
}
111+
val channel = client.channels.get(channelName)
112+
113+
client.connect()
114+
awaitState(client, ConnectionState.connected)
115+
116+
channel.attach()
117+
awaitChannelState(channel, ChannelState.attached)
118+
119+
val error = assertFailsWith<AblyException> { client.channels.release(channelName) }.errorInfo
120+
121+
assertEquals(90011, error.code)
122+
assertEquals(400, error.statusCode)
123+
assertEquals(ChannelState.attached, channel.state)
124+
assertTrue(client.channels.containsKey(channelName))
125+
assertSame(channel, client.channels.get(channelName))
126+
assertEquals(0, capturedDetachMessages.size)
127+
client.close()
128+
}
129+
}

‎liveobjects/src/test/kotlin/io/ably/pubsub/liveobjects/integration/setup/IntegrationTest.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ abstract class IntegrationTest {
5959
@After
6060
fun afterEach() {
6161
for (realtimeClient in realtimeClients.values) {
62-
for ((channelName, channel) in realtimeClient.channels.entrySet()) {
62+
for (channel in realtimeClient.channels.values()) {
6363
channel.off()
64-
realtimeClient.channels.release(channelName)
6564
}
6665
realtimeClient.close()
6766
}

‎pubsub-adapter/src/main/kotlin/com/ably/pubsub/Channels.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ably.pubsub
22

33
import io.ably.pubsub.realtime.ChannelState
4+
import io.ably.pubsub.types.AblyException
45
import io.ably.pubsub.types.ChannelOptions
56

67
/**
@@ -39,11 +40,12 @@ public interface Channels<ChannelType> : Iterable<ChannelType> {
3940
/**
4041
* Releases a [Channel] object, deleting it, and enabling it to be garbage collected.
4142
* It also removes any listeners associated with the channel.
42-
* A realtime channel should only be released when it is in the `INITIALIZED`, `DETACHED`, or `FAILED` state;
43-
* releasing a realtime channel in any other state is deprecated and will throw an error in the next major version.
43+
* A realtime channel can only be released when it is in the `INITIALIZED`, `DETACHED`, or `FAILED` state.
4444
* <p>
4545
* Spec: RSN4, RTS4
4646
* @param name The channel name.
47+
* @throws AblyException With code 90011 if a realtime channel is in any other state. Detach the channel
48+
* and wait for the detach to complete before calling release().
4749
*/
4850
public fun release(name: String)
4951
}

0 commit comments

Comments
 (0)