From 3b8208cdb33ff3482b4680cf87c103cad28e3ee1 Mon Sep 17 00:00:00 2001 From: Shriram Kumar Date: Wed, 23 Aug 2017 16:29:23 -0700 Subject: [PATCH 1/4] PubSubException can have a cause, PubSubConfig removed, Convenience method for sending id, content added to Publisher --- .../java/com/yahoo/bullet/BulletConfig.java | 3 ++ .../com/yahoo/bullet/pubsub/Metadata.java | 6 +-- .../java/com/yahoo/bullet/pubsub/PubSub.java | 28 +++++----- .../com/yahoo/bullet/pubsub/PubSubConfig.java | 21 -------- .../yahoo/bullet/pubsub/PubSubException.java | 15 +++++- .../yahoo/bullet/pubsub/PubSubMessage.java | 51 ++++++++++++------- .../com/yahoo/bullet/pubsub/Publisher.java | 11 ++++ .../com/yahoo/bullet/pubsub/Subscriber.java | 4 +- .../com/yahoo/bullet/pubsub/MockPubSub.java | 3 +- .../yahoo/bullet/pubsub/PubSubConfigTest.java | 23 --------- .../bullet/pubsub/PubSubMessageTest.java | 19 +++---- .../com/yahoo/bullet/pubsub/PubSubTest.java | 29 ++++++++--- 12 files changed, 115 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/com/yahoo/bullet/pubsub/PubSubConfig.java delete mode 100644 src/test/java/com/yahoo/bullet/pubsub/PubSubConfigTest.java diff --git a/src/main/java/com/yahoo/bullet/BulletConfig.java b/src/main/java/com/yahoo/bullet/BulletConfig.java index a2cf0ae2..512884f1 100644 --- a/src/main/java/com/yahoo/bullet/BulletConfig.java +++ b/src/main/java/com/yahoo/bullet/BulletConfig.java @@ -46,6 +46,9 @@ public class BulletConfig extends Config { public static final String RESULT_METADATA_METRICS_MAPPING = "bullet.result.metadata.metrics.mapping"; + public static final String PUBSUB_CONTEXT_NAME = "bullet.pubsub.context.name"; + public static final String PUBSUB_CLASS_NAME = "bullet.pubsub.class.name"; + public static final String DEFAULT_CONFIGURATION_NAME = "bullet_defaults.yaml"; /** diff --git a/src/main/java/com/yahoo/bullet/pubsub/Metadata.java b/src/main/java/com/yahoo/bullet/pubsub/Metadata.java index 6baf3cbd..e7ebdc51 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Metadata.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Metadata.java @@ -1,13 +1,13 @@ package com.yahoo.bullet.pubsub; -import lombok.AllArgsConstructor; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; import java.io.Serializable; -@Getter @NoArgsConstructor @Setter @AllArgsConstructor +@Getter @Setter @AllArgsConstructor @NoArgsConstructor public class Metadata implements Serializable { public enum Signal { ACKNOWLEDGE, diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java index b2c46c21..e98d25b1 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java @@ -1,5 +1,7 @@ package com.yahoo.bullet.pubsub; +import com.yahoo.bullet.BulletConfig; + import java.io.Serializable; import java.lang.reflect.Constructor; import java.util.List; @@ -7,7 +9,7 @@ /** * Notation: Partition is a unit of parallelism in the Pub/Sub queue. * - * Implementations of PubSub should take in a {@link PubSubConfig} and use the information to wire up and return + * Implementations of PubSub should take in a {@link BulletConfig} and use the information to wire up and return * Publishers and Subscribers. */ public abstract class PubSub implements Serializable { @@ -28,12 +30,12 @@ public enum Context { protected Context context; /** - * Instantiate a PubSub using parameters from {@link PubSubConfig}. + * Instantiate a PubSub using parameters from {@link BulletConfig}. * - * @param config The {@link PubSubConfig} containing all required PubSub parameters. + * @param config The {@link BulletConfig} containing all required PubSub parameters. */ - public PubSub(PubSubConfig config) { - context = Context.valueOf(config.get(PubSubConfig.CONTEXT_NAME).toString()); + public PubSub(BulletConfig config) { + context = Context.valueOf(config.get(BulletConfig.PUBSUB_CONTEXT_NAME).toString()); } /** @@ -49,7 +51,7 @@ public PubSub(PubSubConfig config) { * (See {@link PubSub#context}) split as evenly as possible among them. * * @param n The number of Publishers requested. - * @return The {@link List} of n Publishers wired as required. + * @return List of n Publishers wired as required. */ public abstract List getPublishers(int n); @@ -57,7 +59,7 @@ public PubSub(PubSubConfig config) { * Get a {@link Subscriber} instance wired to read from all allocated partitions in the appropriate queue (See * {@link PubSub#context}). * - * @return The {@link Subscriber} wired as required. + * @return {@link Subscriber} wired as required. */ public abstract Subscriber getSubscriber(); @@ -66,25 +68,25 @@ public PubSub(PubSubConfig config) { * (See {@link PubSub#context}) split as evenly as possible among them. * * @param n The number of Subscribers requested. - * @return The {@link List} of n Subscribers wired as required. + * @return List of n Subscribers wired as required. */ public abstract List getSubscribers(int n); /** * Create a PubSub instance using the class specified in the config file. * - * @param config The {@link PubSubConfig} containing the class name and PubSub settings. + * @param config The {@link BulletConfig} containing the class name and PubSub settings. * @return an instance of specified class initialized with settings from the input file and defaults. * @throws PubSubException if PubSub creation fails. */ - public static PubSub from(PubSubConfig config) throws PubSubException { + public static PubSub from(BulletConfig config) throws PubSubException { try { - String pubSubClassName = (String) config.get(PubSubConfig.PUBSUB_CLASS_NAME); + String pubSubClassName = (String) config.get(BulletConfig.PUBSUB_CLASS_NAME); Class pubSubClass = (Class) Class.forName(pubSubClassName); - Constructor constructor = pubSubClass.getConstructor(PubSubConfig.class); + Constructor constructor = pubSubClass.getConstructor(BulletConfig.class); return constructor.newInstance(config); } catch (Exception e) { - throw new PubSubException("Cannot create PubSub instance. Error: " + e.toString()); + throw new PubSubException("Cannot create PubSub instance.", e); } } } diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSubConfig.java b/src/main/java/com/yahoo/bullet/pubsub/PubSubConfig.java deleted file mode 100644 index a167d378..00000000 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSubConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.yahoo.bullet.pubsub; - -import com.yahoo.bullet.BulletConfig; - -import java.io.IOException; - -public class PubSubConfig extends BulletConfig { - public static final String CONTEXT_NAME = "bullet.pubsub.context.name"; - public static final String PUBSUB_CLASS_NAME = "bullet.pubsub.class.name"; - - /** - * Constructor that loads configuration parameters from a specific file augmented by defaults. - * - * @param file YAML file to load. - * @throws IOException if an error occurs when loading the file. - */ - public PubSubConfig(String file) throws IOException { - // Load settings and merge with bullet defaults. - super(file); - } -} diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSubException.java b/src/main/java/com/yahoo/bullet/pubsub/PubSubException.java index d3fdcc30..240bb131 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSubException.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSubException.java @@ -1,12 +1,25 @@ package com.yahoo.bullet.pubsub; +/** + * Exception to be thrown if there is an error in {@link PubSub}, {@link Publisher} or {@link Subscriber}. + */ public class PubSubException extends Exception { /** - * Exception to be thrown if there is an error in {@link PubSub}, {@link Publisher} or {@link Subscriber}. + * Constructor to initialize PubSubException with a message. * * @param message The error message to be associated with the PubSubException. */ public PubSubException(String message) { super(message); } + + /** + * Constructor to initialize PubSubException with a message and a {@link Throwable} cause. + * + * @param message The error message to be associated with the PubSubException. + * @param cause The reason for the PubSubException. + */ + public PubSubException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java b/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java index 1002f5bd..0f66e3ab 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java @@ -1,6 +1,8 @@ package com.yahoo.bullet.pubsub; import java.io.Serializable; +import java.util.Objects; + import com.yahoo.bullet.pubsub.Metadata.Signal; import lombok.Getter; @@ -17,62 +19,73 @@ public class PubSubMessage implements Serializable { private Metadata metadata; /** - * Constructor for a message having only content. + * Constructor for a message that contains an id and content. * * @param id The ID associated with the message. * @param content The content of the message. */ public PubSubMessage(String id, String content) { - this(id, content, -1, new Metadata()); + this(id, content, (Metadata) null, -1); } /** - * Constructor for a message having content and a sequence number. + * Constructor for a message that contains an id, content and a sequence number. * * @param id The ID associated with the message. * @param content The content of the message. - * @param sequence The sequence number of the message. + * @param sequence The integer sequence number of the message. */ public PubSubMessage(String id, String content, int sequence) { - this(id, content, sequence, new Metadata()); + this(id, content, (Metadata) null, sequence); } /** - * Constructor for a PubSubMessage having content and Metadata. + * Constructor for a message that contains an id, {@link Metadata} and content. * * @param id The ID associated with the message. * @param content The content of the message. - * @param metadata The {@link Metadata} associated with the message. + * @param metadata The Metadata associated with the message. */ public PubSubMessage(String id, String content, Metadata metadata) { - this(id, content, -1, metadata); + this(id, content, metadata, -1); + } + + /** + * Constructor for a message that contains an id, {@link Metadata.Signal} and may contain content. + * + * @param id The ID associated with the message. + * @param content The content of the message. + * @param signal The Signal to be sent with the message. + */ + public PubSubMessage(String id, String content, Signal signal) { + this(id, content, signal, -1); } /** - * Constructor for a message having content, a {@link Metadata.Signal} and a sequence number. + * Constructor for a message that contains an id, {@link Metadata.Signal}, a sequence number and may contain content. * * @param id The ID associated with the message. * @param content The content of the message. - * @param sequence The sequence number of the message. - * @param signal The Metadata.Signal of the message. + * @param signal The Signal to be sent with the message. + * @param sequence The integer sequence number of the message. */ - public PubSubMessage(String id, String content, int sequence, Signal signal) { - this(id, content, sequence, new Metadata(signal, null)); + public PubSubMessage(String id, String content, Signal signal, int sequence) { + this(id, content, new Metadata(signal, null), sequence); } /** - * Constructor for a PubSubMessage having content, Metadata and a sequence number. + * Constructor for a message that contains an id, {@link Metadata}, a sequence number and content. * * @param id The ID associated with the message. * @param content The content of the message. - * @param sequence The sequence number associated with the message. - * @param metadata The {@link Metadata} associated with the message. + * @param metadata The Metadata associated with the message. + * @param sequence The integer sequence number of the message. */ - public PubSubMessage(String id, String content, int sequence, Metadata metadata) { - this.id = id; + public PubSubMessage(String id, String content, Metadata metadata, int sequence) { + this.id = Objects.requireNonNull(id, "ID cannot be null"); this.content = content; - this.sequence = sequence; this.metadata = metadata; + this.sequence = sequence; } /** diff --git a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java index 6bed546b..916668ab 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java @@ -1,6 +1,17 @@ package com.yahoo.bullet.pubsub; public interface Publisher { + /** + * Send a message with an ID and content. + * + * @param id The ID associated with the message. + * @param content The content of the message. + * @throws PubSubException if the messaging system throws an error. + */ + default void send(String id, String content) throws PubSubException { + send(new PubSubMessage(id, content)); + } + /** * Sends a {@link PubSubMessage}. Messages with the same ID should be received in order. * diff --git a/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java b/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java index bba801eb..f5e2d8b2 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java @@ -22,8 +22,8 @@ public interface Subscriber { * 1. Ack all received messages. * 2. Commit current read offset to persistent/fault tolerant storage. * - * @param id The ID of the message to be marked as committed. - * @param sequence The sequence number of the message to be committed. + * @param id The ID of the message to be marked as committed. + * @param sequence The sequence number of the message to be committed. */ void commit(String id, int sequence); diff --git a/src/test/java/com/yahoo/bullet/pubsub/MockPubSub.java b/src/test/java/com/yahoo/bullet/pubsub/MockPubSub.java index 21f67712..a22fe805 100644 --- a/src/test/java/com/yahoo/bullet/pubsub/MockPubSub.java +++ b/src/test/java/com/yahoo/bullet/pubsub/MockPubSub.java @@ -1,5 +1,6 @@ package com.yahoo.bullet.pubsub; +import com.yahoo.bullet.BulletConfig; import org.mockito.Mockito; import java.util.List; @@ -10,7 +11,7 @@ class MockPubSub extends PubSub { public static final String MOCK_MESSAGE_NAME = "MOCK_MESSAGE"; private String mockMessage; - public MockPubSub(PubSubConfig config) { + public MockPubSub(BulletConfig config) { super(config); mockMessage = config.get(MOCK_MESSAGE_NAME).toString(); } diff --git a/src/test/java/com/yahoo/bullet/pubsub/PubSubConfigTest.java b/src/test/java/com/yahoo/bullet/pubsub/PubSubConfigTest.java deleted file mode 100644 index 283f6cbf..00000000 --- a/src/test/java/com/yahoo/bullet/pubsub/PubSubConfigTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.yahoo.bullet.pubsub; - -import com.yahoo.bullet.BulletConfig; -import org.testng.Assert; -import org.testng.annotations.Test; - -import java.io.IOException; - -public class PubSubConfigTest { - public static final String CONTEXT_PROCESSING = PubSub.Context.QUERY_PROCESSING.toString(); - public static final String RECORD_INJECT_TIMESTAMP_KEY_VALUE = "bullet_project_timestamp"; - - @Test - public void testCustomFileCreate() throws IOException { - PubSubConfig config = new PubSubConfig("src/test/resources/test_config.yaml"); - //Test custom properties - Assert.assertEquals((long) config.get(BulletConfig.AGGREGATION_MAX_SIZE), 100); - Assert.assertEquals((long) config.get(BulletConfig.SPECIFICATION_MAX_DURATION), 10000); - Assert.assertTrue(config.get(PubSubConfig.CONTEXT_NAME).toString().equals(CONTEXT_PROCESSING)); - //Test default bullet properties - Assert.assertTrue(config.get(BulletConfig.RECORD_INJECT_TIMESTAMP_KEY).toString().equals(RECORD_INJECT_TIMESTAMP_KEY_VALUE)); - } -} diff --git a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java index 7f776498..92bc5828 100644 --- a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java +++ b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java @@ -20,9 +20,7 @@ public void testNoMetadataCreation() { Assert.assertTrue(messageId.equals(message.getId())); Assert.assertEquals(message.getSequence(), -1); Assert.assertTrue(messageContent.equals(message.getContent())); - Assert.assertNotNull(message.getMetadata()); - Assert.assertNull(message.getMetadata().getSignal()); - Assert.assertNull(message.getMetadata().getContent()); + Assert.assertNull(message.getMetadata()); } @Test @@ -34,9 +32,7 @@ public void testWithSequenceCreation() { Assert.assertTrue(messageId.equals(message.getId())); Assert.assertTrue(messageContent.equals(message.getContent())); Assert.assertEquals(message.getSequence(), 0); - Assert.assertNotNull(message.getMetadata()); - Assert.assertNull(message.getMetadata().getSignal()); - Assert.assertNull(message.getMetadata().getContent()); + Assert.assertNull(message.getMetadata()); } @Test @@ -45,7 +41,7 @@ public void testWithSignalCreation() { String messageContent = getRandomString(); Signal signal = Signal.ACKNOWLEDGE; - PubSubMessage message = new PubSubMessage(messageId, messageContent, 0, signal); + PubSubMessage message = new PubSubMessage(messageId, messageContent, signal, 0); Assert.assertTrue(messageId.equals(message.getId())); Assert.assertTrue(messageContent.equals(message.getContent())); Assert.assertEquals(message.getSequence(), 0); @@ -78,7 +74,7 @@ public void testWithMetadataAndSequenceCreation() { String metadataContent = getRandomString(); Signal signal = Signal.ACKNOWLEDGE; //Test creation with a sequence number. - PubSubMessage message = new PubSubMessage(messageId, messageContent, 0, new Metadata(signal, metadataContent)); + PubSubMessage message = new PubSubMessage(messageId, messageContent, new Metadata(signal, metadataContent), 0); Assert.assertTrue(messageId.equals(message.getId())); Assert.assertTrue(messageContent.equals(message.getContent())); Assert.assertEquals(message.getSequence(), 0); @@ -96,7 +92,12 @@ public void testHasContent() { message = new PubSubMessage(messageId, messageContent); Assert.assertTrue(message.hasContent()); - message = new PubSubMessage(messageId, null); + message = new PubSubMessage(messageId, null, Signal.COMPLETE); Assert.assertFalse(message.hasContent()); } + + @Test(expectedExceptions = NullPointerException.class) + public void testNoIdIllegalCreation() { + new PubSubMessage(null, ""); + } } diff --git a/src/test/java/com/yahoo/bullet/pubsub/PubSubTest.java b/src/test/java/com/yahoo/bullet/pubsub/PubSubTest.java index a72d0608..2fe02662 100644 --- a/src/test/java/com/yahoo/bullet/pubsub/PubSubTest.java +++ b/src/test/java/com/yahoo/bullet/pubsub/PubSubTest.java @@ -1,15 +1,16 @@ package com.yahoo.bullet.pubsub; +import com.yahoo.bullet.BulletConfig; import org.testng.Assert; import org.testng.annotations.Test; -import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.UUID; public class PubSubTest { @Test - public void testMockPubSubCreation() throws IOException, PubSubException { - PubSubConfig config = new PubSubConfig("src/test/resources/test_config.yaml"); + public void testMockPubSubCreation() throws Exception { + BulletConfig config = new BulletConfig("src/test/resources/test_config.yaml"); String mockMessage = UUID.randomUUID().toString(); config.set(MockPubSub.MOCK_MESSAGE_NAME, mockMessage); PubSub testPubSub = PubSub.from(config); @@ -19,8 +20,24 @@ public void testMockPubSubCreation() throws IOException, PubSubException { } @Test(expectedExceptions = PubSubException.class) - public void testIllegalPubSubCreation() throws IOException, PubSubException { - PubSubConfig config = new PubSubConfig(null); - PubSub testPubSub = PubSub.from(config); + public void testIllegalPubSubParameter() throws Exception { + BulletConfig config = new BulletConfig("src/test/resources/test_config.yaml"); + try { + PubSub.from(config); + } catch (Exception e) { + Assert.assertEquals(e.getCause().getClass(), InvocationTargetException.class); + throw e; + } + } + + @Test(expectedExceptions = PubSubException.class) + public void testIllegalPubSubClassName() throws Exception { + BulletConfig config = new BulletConfig(null); + try { + PubSub.from(config); + } catch (Exception e) { + Assert.assertEquals(e.getCause().getClass(), NullPointerException.class); + throw e; + } } } From cd50da07cce652e3ba4de6624f0186aef208f074 Mon Sep 17 00:00:00 2001 From: Shriram Kumar Date: Wed, 23 Aug 2017 17:28:20 -0700 Subject: [PATCH 2/4] Adding Publisher test, fixing javadocs --- .../com/yahoo/bullet/pubsub/Metadata.java | 4 +-- .../yahoo/bullet/pubsub/PubSubMessage.java | 26 +++++++-------- .../bullet/pubsub/PubSubMessageTest.java | 2 +- .../yahoo/bullet/pubsub/PublisherTest.java | 33 +++++++++++++++++++ 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/yahoo/bullet/pubsub/PublisherTest.java diff --git a/src/main/java/com/yahoo/bullet/pubsub/Metadata.java b/src/main/java/com/yahoo/bullet/pubsub/Metadata.java index e7ebdc51..e6342f15 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Metadata.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Metadata.java @@ -1,9 +1,9 @@ package com.yahoo.bullet.pubsub; -import lombok.Getter; -import lombok.Setter; import lombok.AllArgsConstructor; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.io.Serializable; diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java b/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java index 0f66e3ab..44e9c475 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSubMessage.java @@ -1,11 +1,11 @@ package com.yahoo.bullet.pubsub; -import java.io.Serializable; -import java.util.Objects; - import com.yahoo.bullet.pubsub.Metadata.Signal; import lombok.Getter; +import java.io.Serializable; +import java.util.Objects; + /** * The class of messages that can be understood by the PubSub package. The id should be set to the query ID generated * by the web service for Bullet queries. The sequence identifies individual segments if a multi-part response is @@ -19,7 +19,7 @@ public class PubSubMessage implements Serializable { private Metadata metadata; /** - * Constructor for a message that contains an id and content. + * Constructor for a message having only content. * * @param id The ID associated with the message. * @param content The content of the message. @@ -29,18 +29,18 @@ public PubSubMessage(String id, String content) { } /** - * Constructor for a message that contains an id, content and a sequence number. + * Constructor for a message having content and a sequence number. * * @param id The ID associated with the message. * @param content The content of the message. - * @param sequence The integer sequence number of the message. + * @param sequence The sequence number of the message. */ public PubSubMessage(String id, String content, int sequence) { this(id, content, (Metadata) null, sequence); } /** - * Constructor for a message that contains an id, {@link Metadata} and content. + * Constructor for a message having content and {@link Metadata}. * * @param id The ID associated with the message. * @param content The content of the message. @@ -51,35 +51,35 @@ public PubSubMessage(String id, String content, Metadata metadata) { } /** - * Constructor for a message that contains an id, {@link Metadata.Signal} and may contain content. + * Constructor for a message having content and a {@link Metadata.Signal}. * * @param id The ID associated with the message. * @param content The content of the message. - * @param signal The Signal to be sent with the message. + * @param signal The Metadata.Signal to be sent with the message. */ public PubSubMessage(String id, String content, Signal signal) { this(id, content, signal, -1); } /** - * Constructor for a message that contains an id, {@link Metadata.Signal}, a sequence number and may contain content. + * Constructor for a message having content, a {@link Metadata.Signal} and a sequence number. * * @param id The ID associated with the message. * @param content The content of the message. * @param signal The Signal to be sent with the message. - * @param sequence The integer sequence number of the message. + * @param sequence The sequence number of the message. */ public PubSubMessage(String id, String content, Signal signal, int sequence) { this(id, content, new Metadata(signal, null), sequence); } /** - * Constructor for a message that contains an id, {@link Metadata}, a sequence number and content. + * Constructor for a message having content, {@link Metadata} and a sequence number. * * @param id The ID associated with the message. * @param content The content of the message. * @param metadata The Metadata associated with the message. - * @param sequence The integer sequence number of the message. + * @param sequence The sequence number of the message. */ public PubSubMessage(String id, String content, Metadata metadata, int sequence) { this.id = Objects.requireNonNull(id, "ID cannot be null"); diff --git a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java index 92bc5828..ce208b89 100644 --- a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java +++ b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java @@ -1,10 +1,10 @@ package com.yahoo.bullet.pubsub; +import com.yahoo.bullet.pubsub.Metadata.Signal; import org.testng.Assert; import org.testng.annotations.Test; import java.util.UUID; -import com.yahoo.bullet.pubsub.Metadata.Signal; public class PubSubMessageTest { private String getRandomString() { diff --git a/src/test/java/com/yahoo/bullet/pubsub/PublisherTest.java b/src/test/java/com/yahoo/bullet/pubsub/PublisherTest.java new file mode 100644 index 00000000..17ec4a54 --- /dev/null +++ b/src/test/java/com/yahoo/bullet/pubsub/PublisherTest.java @@ -0,0 +1,33 @@ +package com.yahoo.bullet.pubsub; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.UUID; + +public class PublisherTest { + private class MockPublisher implements Publisher { + PubSubMessage sentMessage; + + @Override + public void send(PubSubMessage message) { + sentMessage = message; + } + + @Override + public void close() { + throw new UnsupportedOperationException(); + } + } + + @Test + public void testDefaultSend() throws PubSubException { + String randomId = UUID.randomUUID().toString(); + String randomMessage = UUID.randomUUID().toString(); + MockPublisher mockPublisher = new MockPublisher(); + mockPublisher.send(randomId, randomMessage); + + Assert.assertTrue(mockPublisher.sentMessage.getContent().equals(randomMessage)); + Assert.assertTrue(mockPublisher.sentMessage.getId().equals(randomId)); + } +} From 7ecb06dfd89d2f6ab7a14426418c3136823fac08 Mon Sep 17 00:00:00 2001 From: Shriram Kumar Date: Wed, 23 Aug 2017 19:38:52 -0700 Subject: [PATCH 3/4] Fixing some javadocs and function names --- src/main/java/com/yahoo/bullet/pubsub/PubSub.java | 6 +++--- src/main/java/com/yahoo/bullet/pubsub/Publisher.java | 4 ++-- src/main/java/com/yahoo/bullet/pubsub/Subscriber.java | 4 ++-- .../java/com/yahoo/bullet/pubsub/PubSubMessageTest.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java index e98d25b1..c3d6eb21 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java @@ -51,7 +51,7 @@ public PubSub(BulletConfig config) { * (See {@link PubSub#context}) split as evenly as possible among them. * * @param n The number of Publishers requested. - * @return List of n Publishers wired as required. + * @return The {@link List} of n Publishers wired as required. */ public abstract List getPublishers(int n); @@ -68,7 +68,7 @@ public PubSub(BulletConfig config) { * (See {@link PubSub#context}) split as evenly as possible among them. * * @param n The number of Subscribers requested. - * @return List of n Subscribers wired as required. + * @return The {@link List} of n Subscribers wired as required. */ public abstract List getSubscribers(int n); @@ -77,7 +77,7 @@ public PubSub(BulletConfig config) { * * @param config The {@link BulletConfig} containing the class name and PubSub settings. * @return an instance of specified class initialized with settings from the input file and defaults. - * @throws PubSubException if PubSub creation fails. + * @throws {@link PubSubException} if PubSub creation fails. */ public static PubSub from(BulletConfig config) throws PubSubException { try { diff --git a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java index 916668ab..9d6cf1a8 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java @@ -6,7 +6,7 @@ public interface Publisher { * * @param id The ID associated with the message. * @param content The content of the message. - * @throws PubSubException if the messaging system throws an error. + * @throws {@link PubSubException} if the messaging system throws an error. */ default void send(String id, String content) throws PubSubException { send(new PubSubMessage(id, content)); @@ -16,7 +16,7 @@ default void send(String id, String content) throws PubSubException { * Sends a {@link PubSubMessage}. Messages with the same ID should be received in order. * * @param message The {@link PubSubMessage} to be sent. - * @throws PubSubException if the messaging system throws an error. + * @throws {@link PubSubException} if the messaging system throws an error. */ void send(PubSubMessage message) throws PubSubException; diff --git a/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java b/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java index f5e2d8b2..bba801eb 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Subscriber.java @@ -22,8 +22,8 @@ public interface Subscriber { * 1. Ack all received messages. * 2. Commit current read offset to persistent/fault tolerant storage. * - * @param id The ID of the message to be marked as committed. - * @param sequence The sequence number of the message to be committed. + * @param id The ID of the message to be marked as committed. + * @param sequence The sequence number of the message to be committed. */ void commit(String id, int sequence); diff --git a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java index ce208b89..d965d1ed 100644 --- a/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java +++ b/src/test/java/com/yahoo/bullet/pubsub/PubSubMessageTest.java @@ -97,7 +97,7 @@ public void testHasContent() { } @Test(expectedExceptions = NullPointerException.class) - public void testNoIdIllegalCreation() { + public void testNoIDIllegalCreation() { new PubSubMessage(null, ""); } } From a645fb05c87e71b338595a90f9fb74c2d806dc07 Mon Sep 17 00:00:00 2001 From: Shriram Kumar Date: Wed, 23 Aug 2017 19:43:39 -0700 Subject: [PATCH 4/4] Fixing checkstyle violation --- src/main/java/com/yahoo/bullet/pubsub/PubSub.java | 2 +- src/main/java/com/yahoo/bullet/pubsub/Publisher.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java index c3d6eb21..8d42e463 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/PubSub.java +++ b/src/main/java/com/yahoo/bullet/pubsub/PubSub.java @@ -77,7 +77,7 @@ public PubSub(BulletConfig config) { * * @param config The {@link BulletConfig} containing the class name and PubSub settings. * @return an instance of specified class initialized with settings from the input file and defaults. - * @throws {@link PubSubException} if PubSub creation fails. + * @throws PubSubException if PubSub creation fails. */ public static PubSub from(BulletConfig config) throws PubSubException { try { diff --git a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java index 9d6cf1a8..916668ab 100644 --- a/src/main/java/com/yahoo/bullet/pubsub/Publisher.java +++ b/src/main/java/com/yahoo/bullet/pubsub/Publisher.java @@ -6,7 +6,7 @@ public interface Publisher { * * @param id The ID associated with the message. * @param content The content of the message. - * @throws {@link PubSubException} if the messaging system throws an error. + * @throws PubSubException if the messaging system throws an error. */ default void send(String id, String content) throws PubSubException { send(new PubSubMessage(id, content)); @@ -16,7 +16,7 @@ default void send(String id, String content) throws PubSubException { * Sends a {@link PubSubMessage}. Messages with the same ID should be received in order. * * @param message The {@link PubSubMessage} to be sent. - * @throws {@link PubSubException} if the messaging system throws an error. + * @throws PubSubException if the messaging system throws an error. */ void send(PubSubMessage message) throws PubSubException;