Skip to content

Commit dabc3d8

Browse files
committed
Update spec reference to TP3i and add end-to-end test for extras
- Updated spec reference from TP3l to TP3i for PresenceMessage extras field - Added test that enters presence with extras.headers.foo set to "bar" - Verifies extras field is correctly transmitted and received on the other side - Test follows same pattern as existing presence tests https://claude.ai/code/session_01PcbKjcZbnXhnTfaE1P71tG
1 parent 3b24ac4 commit dabc3d8

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

lib/src/main/java/io/ably/lib/types/PresenceMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public enum Action {
7979
* A MessageExtras object of arbitrary key-value pairs that may contain metadata, and/or ancillary payloads.
8080
* Valid payloads include {@link DeltaExtras}, {@link JsonObject}.
8181
* <p>
82-
* Spec: TP3l
82+
* Spec: TP3i
8383
*/
8484
public MessageExtras extras;
8585

lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,6 +3625,79 @@ public void messages_from_encoded_json_array() throws AblyException {
36253625
}
36263626
}
36273627

3628+
/**
3629+
* Enter presence with extras field and verify it comes back on the other side
3630+
* Test TP3i
3631+
*/
3632+
@Test
3633+
public void presence_enter_with_extras() {
3634+
AblyRealtime clientAbly1 = null;
3635+
TestChannel testChannel = new TestChannel();
3636+
try {
3637+
/* subscribe for presence events in the anonymous connection */
3638+
PresenceWaiter presenceWaiter = new PresenceWaiter(testChannel.realtimeChannel);
3639+
/* set up a connection with specific clientId */
3640+
ClientOptions client1Opts = new ClientOptions() {{
3641+
tokenDetails = token1;
3642+
clientId = testClientId1;
3643+
}};
3644+
fillInOptions(client1Opts);
3645+
clientAbly1 = new AblyRealtime(client1Opts);
3646+
3647+
/* wait until connected */
3648+
(new ConnectionWaiter(clientAbly1.connection)).waitFor(ConnectionState.connected);
3649+
assertEquals("Verify connected state reached", clientAbly1.connection.state, ConnectionState.connected);
3650+
3651+
/* get channel and attach */
3652+
Channel client1Channel = clientAbly1.channels.get(testChannel.channelName);
3653+
client1Channel.attach();
3654+
(new ChannelWaiter(client1Channel)).waitFor(ChannelState.attached);
3655+
assertEquals("Verify attached state reached", client1Channel.state, ChannelState.attached);
3656+
3657+
/* create extras with headers.foo */
3658+
JsonObject extrasJson = new JsonObject();
3659+
JsonObject headers = new JsonObject();
3660+
headers.addProperty("foo", "bar");
3661+
extrasJson.add("headers", headers);
3662+
io.ably.lib.types.MessageExtras extras = new io.ably.lib.types.MessageExtras(extrasJson);
3663+
3664+
/* create presence message with extras */
3665+
String enterString = "Test data (presence_enter_with_extras)";
3666+
PresenceMessage presenceMsg = new PresenceMessage(PresenceMessage.Action.enter, null, enterString);
3667+
presenceMsg.extras = extras;
3668+
3669+
/* let client1 enter the channel with extras and wait for the entered event to be delivered */
3670+
CompletionWaiter enterComplete = new CompletionWaiter();
3671+
client1Channel.presence.updatePresence(presenceMsg, enterComplete);
3672+
presenceWaiter.waitFor(testClientId1, Action.enter);
3673+
PresenceMessage receivedMessage = presenceWaiter.contains(testClientId1, Action.enter);
3674+
assertNotNull("Verify presence message received", receivedMessage);
3675+
assertEquals("Verify data matches", enterString, receivedMessage.data);
3676+
3677+
/* verify extras field is present and correct */
3678+
assertNotNull("Verify extras is not null", receivedMessage.extras);
3679+
JsonObject receivedExtrasJson = receivedMessage.extras.asJsonObject();
3680+
assertNotNull("Verify extras JSON is not null", receivedExtrasJson);
3681+
assertTrue("Verify headers exists in extras", receivedExtrasJson.has("headers"));
3682+
JsonObject receivedHeaders = receivedExtrasJson.getAsJsonObject("headers");
3683+
assertNotNull("Verify headers object is not null", receivedHeaders);
3684+
assertTrue("Verify foo exists in headers", receivedHeaders.has("foo"));
3685+
assertEquals("Verify foo value matches", "bar", receivedHeaders.get("foo").getAsString());
3686+
3687+
/* verify enter callback called on completion */
3688+
enterComplete.waitFor();
3689+
assertTrue("Verify enter callback called on completion", enterComplete.success);
3690+
} catch(AblyException e) {
3691+
e.printStackTrace();
3692+
fail("Unexpected exception running test: " + e.getMessage());
3693+
} finally {
3694+
if(clientAbly1 != null)
3695+
clientAbly1.close();
3696+
if(testChannel != null)
3697+
testChannel.dispose();
3698+
}
3699+
}
3700+
36283701
static class MessagesData {
36293702
public PresenceMessage[] messages;
36303703
}

0 commit comments

Comments
 (0)