From 9d04a9816b24eb935b060f24561d6e8007bbc82e Mon Sep 17 00:00:00 2001 From: Fokko Driesprong Date: Fri, 21 Dec 2018 09:40:25 +0100 Subject: [PATCH] Implement comparable on EventData (#395) --- .../microsoft/azure/eventhubs/EventData.java | 2 +- .../azure/eventhubs/impl/EventDataImpl.java | 8 +++ .../eventdata/InteropEventBodyTest.java | 1 - .../eventhubs/impl/EventDataOrderTest.java | 52 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/impl/EventDataOrderTest.java diff --git a/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/EventData.java b/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/EventData.java index f2d7ca49d6802..d9884bf248c53 100755 --- a/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/EventData.java +++ b/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/EventData.java @@ -30,7 +30,7 @@ * Section (iii) is used for advanced scenarios, where the sending application uses third-party AMQP library to send the message to EventHubs and the receiving application * uses this client library to receive {@link EventData}. */ -public interface EventData extends Serializable { +public interface EventData extends Serializable, Comparable { /** * Construct EventData to Send to EventHubs. diff --git a/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/impl/EventDataImpl.java b/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/impl/EventDataImpl.java index 940ca8aa9f3f5..69986268623c9 100755 --- a/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/impl/EventDataImpl.java +++ b/azure-eventhubs/src/main/java/com/microsoft/azure/eventhubs/impl/EventDataImpl.java @@ -258,4 +258,12 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE this.bodyData = new Binary(data, 0, length); } } + + @Override + public int compareTo(EventData other) { + return Long.compare( + this.getSystemProperties().getSequenceNumber(), + other.getSystemProperties().getSequenceNumber() + ); + } } diff --git a/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/eventdata/InteropEventBodyTest.java b/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/eventdata/InteropEventBodyTest.java index f1f2e2c9beaab..42c012cbf6af8 100644 --- a/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/eventdata/InteropEventBodyTest.java +++ b/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/eventdata/InteropEventBodyTest.java @@ -37,7 +37,6 @@ public class InteropEventBodyTest extends ApiTestBase { static PartitionSender partitionSender; static EventData receivedEvent; static EventData reSentAndReceivedEvent; - static Message reSendAndReceivedMessage; @BeforeClass public static void initialize() throws EventHubException, IOException, InterruptedException, ExecutionException { diff --git a/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/impl/EventDataOrderTest.java b/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/impl/EventDataOrderTest.java new file mode 100644 index 0000000000000..f36066a1fee1c --- /dev/null +++ b/azure-eventhubs/src/test/java/com/microsoft/azure/eventhubs/impl/EventDataOrderTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) Microsoft. All rights reserved. + * Licensed under the MIT license. See LICENSE file in the project root for full license information. + */ +package com.microsoft.azure.eventhubs.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +import com.microsoft.azure.eventhubs.EventData; +import org.apache.qpid.proton.amqp.Symbol; +import org.apache.qpid.proton.amqp.messaging.MessageAnnotations; +import org.apache.qpid.proton.message.Message; +import org.junit.Assert; +import org.junit.Test; + +public class EventDataOrderTest { + + private EventData constructMessage(long seqNumber) { + HashMap properties = new HashMap<>(); + properties.put(AmqpConstants.SEQUENCE_NUMBER, seqNumber); + + Message message = Message.Factory.create(); + + message.setMessageAnnotations(new MessageAnnotations(properties)); + + return new EventDataImpl(message); + } + + @Test + public void eventDataEmptyByteArray() { + ArrayList messages = new ArrayList<>(); + + EventData first = constructMessage(19); + EventData second = constructMessage(22); + EventData third = constructMessage(25); + EventData last = constructMessage(88); + + messages.add(second); + messages.add(first); + messages.add(last); + messages.add(third); + + Collections.sort(messages); + + Assert.assertEquals(messages.get(0), first); + Assert.assertEquals(messages.get(1), second); + Assert.assertEquals(messages.get(2), third); + Assert.assertEquals(messages.get(3), last); + } +}