Skip to content

Commit

Permalink
Implement comparable on EventData (Azure#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko authored and sjkwak committed Dec 21, 2018
1 parent d1b68d1 commit 9d04a98
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventData> {

/**
* Construct EventData to Send to EventHubs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Symbol, Object> 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<EventData> 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);
}
}

0 comments on commit 9d04a98

Please sign in to comment.