LOGGER.info(FULL_UPDATE_TRIGGER + "subscribing to topic " + PubSub.Topic.HDFS_SYNC_HMS.getName()); PubSub.getInstance().subscribe(PubSub.Topic.HDFS_SYNC_HMS, this);
LOGGER.info("Submitting topic " + topic + ", message " + message); try { PubSub.getInstance().publish(PubSub.Topic.fromString(topic), message); msg = "Submitted topic " + topic + ", message " + message; } catch (Exception e) { topics.append("<option>").append(t.getName()).append("</option>");
/** * Successfully publish notifications * @throws Exception */ @Test public void testPubSub() throws Exception { int count = 0; for (Topic topic : topics) { for (String message : messages) { URL url = new URL(String.format(REQUEST_URL, topic.getName(), message)); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); Assert.assertEquals("Unexpected response code", HttpURLConnection.HTTP_OK, conn.getResponseCode()); } finally { safeClose(conn); } Assert.assertEquals("Unexpected topic", topic, testSubscriber.topic); if (message.isEmpty()) { Assert.assertEquals("Unexpected message", null, testSubscriber.message); } else { Assert.assertEquals("Unexpected message", message, testSubscriber.message); } Assert.assertEquals("Unexpected number of PubSub.onMessage() callbacks", ++count, testSubscriber.count); } } }
/** * Submit topic that has no subscribers. * @throws Exception */ @Test public void testPubSubNonSubscribedTopic() throws Exception { // At this point all valid Topic values have been subscribed to // in before() method. // Unsubscribe from one topic and then try publishing to it. PubSub.getInstance().unsubscribe(Topic.HDFS_SYNC_HMS, testSubscriber); Assert.assertEquals("Unexpected number of registered topics", topics.length-1, PubSub.getInstance().getTopics().size()); URL url = new URL(String.format(REQUEST_URL, Topic.HDFS_SYNC_HMS.getName(), "message")); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); Assert.assertEquals("Unexpected response code", HttpURLConnection.HTTP_BAD_REQUEST, conn.getResponseCode()); } finally { safeClose(conn); } // re-subscribe, not to upset after() method which expects all topics to be subscribed to PubSub.getInstance().subscribe(Topic.HDFS_SYNC_HMS, testSubscriber); }