You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by GitBox <gi...@apache.org> on 2020/10/29 13:58:53 UTC
[GitHub] [zookeeper] belugabehr opened a new pull request #882: ZOOKEEPER-3342: Use StandardCharsets
belugabehr opened a new pull request #882:
URL: https://github.com/apache/zookeeper/pull/882
> Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. The behavior of this method when this string cannot be encoded in the default charset is unspecified.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#getBytes--
1. Since this is a distributed system, it is always possible that different nodes have different default charsets defined. I think it's most safe to specify it explicitly across all nodes for safety sake. You could for example see a situation where an upgrade JVM uses a different default and during a rolling upgrade of the JVM, different nodes now have a different default.
2. The default charset is usually "ISO-8859-1". UTF-8 covers more of our international friends.
3. Explicitly specifying the CharSet yields slight performance gains
4. Explicitly specifying the CharSet removes the need for try/catch blocks of UnsupportedEncodingException
https://blog.codecentric.de/en/2014/04/faster-cleaner-code-since-java-7/
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] eolivelli commented on pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
eolivelli commented on pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#issuecomment-719011839
Usually we don't have many flaky tests.
We must see a green build.
I will review the patch ASAP, hopefully we will be able to commit it as soon as CI passes
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr closed pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #882:
URL: https://github.com/apache/zookeeper/pull/882
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr closed pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #882:
URL: https://github.com/apache/zookeeper/pull/882
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr commented on pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr commented on pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#issuecomment-712913406
@ctubbsii Thanks for the review and re-sparking my attention on this PR. I have gone through, added your suggestions, and cleaned up all the weirdness. I look forward to your next review.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr commented on pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr commented on pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#issuecomment-719063749
@eolivelli Third time is a charm. Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr commented on pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr commented on pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#issuecomment-719010331
@ctubbsii @anmolnar Thanks for the +1's.
Unit tests seem to be failing repeatedly with transient errors, something different any time. Possible for a merge regardless?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] anmolnar commented on pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
anmolnar commented on pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#issuecomment-708362736
@belugabehr Sorry for ignoring this so long. Yes, I think it's good for 3.7, but you have to rebase again.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr closed pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #882:
URL: https://github.com/apache/zookeeper/pull/882
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr closed pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #882:
URL: https://github.com/apache/zookeeper/pull/882
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] belugabehr commented on a change in pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#discussion_r508548510
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
Review comment:
@ctubbsii Thanks for the review. I'm not sure how these changes came to be. I don't think I changed them manually and instead came from something I did merging this relatively old branch into master branch. I am working on taking into account some of your feedback now and these types of changes were reverted when I tried to rebase on master again. Thanks.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [zookeeper] ctubbsii commented on a change in pull request #882: ZOOKEEPER-3342: Use StandardCharsets
Posted by GitBox <gi...@apache.org>.
ctubbsii commented on a change in pull request #882:
URL: https://github.com/apache/zookeeper/pull/882#discussion_r504834541
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
Review comment:
Not sure why this was changed, but it should be changed back:
```suggestion
Assert.fail();
```
(or even better, use assertThrows instead, but that's outside the scope of this changeset)
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
- for (int i = 0; i < m; i++) {
- queueHandles[1].remove();
}
- Assert.assertEquals(new String(queueHandles[1].element()), testString + m);
- }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement1() throws Exception {
- createNremoveMelementTest("/testElement1", 1, 0);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+ } catch (KeeperException e) {
- @Test
- public void testElement2() throws Exception {
- createNremoveMelementTest("/testElement2", 10, 2);
- }
+ } catch (InterruptedException e) {
- @Test
- public void testElement3() throws Exception {
- createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+ }
+ };
+ offerThread.start();
+ offerThread.join();
+
+ takeThread.join();
+
+ Assert.assertTrue(takeResult[0] != null);
Review comment:
assertNotNull is better. This should go back to the way it was.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
Review comment:
Why separate out these exceptions instead of keeping the original multi-catch? This seems like a regression.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
- for (int i = 0; i < m; i++) {
- queueHandles[1].remove();
}
- Assert.assertEquals(new String(queueHandles[1].element()), testString + m);
- }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement1() throws Exception {
- createNremoveMelementTest("/testElement1", 1, 0);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+ } catch (KeeperException e) {
- @Test
- public void testElement2() throws Exception {
- createNremoveMelementTest("/testElement2", 10, 2);
- }
+ } catch (InterruptedException e) {
- @Test
- public void testElement3() throws Exception {
- createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+ }
+ };
+ offerThread.start();
+ offerThread.join();
+
+ takeThread.join();
+
+ Assert.assertTrue(takeResult[0] != null);
+ Assert.assertEquals(new String(takeResult[0], UTF_8), testString);
+ }
+
+ @Test
+ public void testTakeWait2() throws Exception {
+ String dir = "/testTakeWait2";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
+ int numAttempts = 2;
+ for (int i = 0; i < numAttempts; i++) {
+ final byte[] takeResult[] = new byte[1][];
Review comment:
The type of the variable is `byte[][]`, so this should go back to the way it was.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
- for (int i = 0; i < m; i++) {
- queueHandles[1].remove();
}
- Assert.assertEquals(new String(queueHandles[1].element()), testString + m);
- }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement1() throws Exception {
- createNremoveMelementTest("/testElement1", 1, 0);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+ } catch (KeeperException e) {
- @Test
- public void testElement2() throws Exception {
- createNremoveMelementTest("/testElement2", 10, 2);
- }
+ } catch (InterruptedException e) {
- @Test
- public void testElement3() throws Exception {
- createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+ }
+ };
+ offerThread.start();
+ offerThread.join();
+
+ takeThread.join();
+
+ Assert.assertTrue(takeResult[0] != null);
+ Assert.assertEquals(new String(takeResult[0], UTF_8), testString);
+ }
+
+ @Test
+ public void testTakeWait2() throws Exception {
+ String dir = "/testTakeWait2";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
+ int numAttempts = 2;
+ for (int i = 0; i < numAttempts; i++) {
+ final byte[] takeResult[] = new byte[1][];
+ final String threadTestString = testString + i;
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
+
+ } catch (InterruptedException e) {
+
+ }
+ }
+ };
Review comment:
Another conversion of lambda/multi-catch to the old style code that should go back to the way it was. There's more below. I'll stop calling them out, but they should be changed back everywhere.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
- for (int i = 0; i < m; i++) {
- queueHandles[1].remove();
}
- Assert.assertEquals(new String(queueHandles[1].element()), testString + m);
- }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement1() throws Exception {
- createNremoveMelementTest("/testElement1", 1, 0);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+ } catch (KeeperException e) {
- @Test
- public void testElement2() throws Exception {
- createNremoveMelementTest("/testElement2", 10, 2);
- }
+ } catch (InterruptedException e) {
- @Test
- public void testElement3() throws Exception {
- createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+ }
+ };
Review comment:
Same comments about keeping lambda and multi-catch
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
Review comment:
I think this creates an unused variable. You could remove the assignment, as it was before, or you can do something with it in a new Assert statement. A simple thing is just assert that it's not null.
##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/ServerAdminClient.java
##########
@@ -52,7 +53,7 @@ public static void ruok(String host, int port) {
byte[] resBytes = new byte[4];
int rc = is.read(resBytes);
- String retv = new String(resBytes);
+ String retv = new String(resBytes, StandardCharsets.UTF_8);
Review comment:
These would be great to use static imports for the `UTF_8`, as was done in some of the other classes.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ } catch (InterruptedException e) {
- for (int i = 0; i < m; i++) {
- queueHandles[1].remove();
}
- Assert.assertEquals(new String(queueHandles[1].element()), testString + m);
- }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement1() throws Exception {
- createNremoveMelementTest("/testElement1", 1, 0);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+ } catch (KeeperException e) {
- @Test
- public void testElement2() throws Exception {
- createNremoveMelementTest("/testElement2", 10, 2);
- }
+ } catch (InterruptedException e) {
- @Test
- public void testElement3() throws Exception {
- createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+ }
+ };
+ offerThread.start();
+ offerThread.join();
+
+ takeThread.join();
+
+ Assert.assertTrue(takeResult[0] != null);
+ Assert.assertEquals(new String(takeResult[0], UTF_8), testString);
+ }
+
+ @Test
+ public void testTakeWait2() throws Exception {
+ String dir = "/testTakeWait2";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
+ int numAttempts = 2;
+ for (int i = 0; i < numAttempts; i++) {
+ final byte[] takeResult[] = new byte[1][];
+ final String threadTestString = testString + i;
+ Thread takeThread = new Thread() {
+ public void run() {
+ try {
+ takeResult[0] = queueHandles[0].take();
+ } catch (KeeperException e) {
+
+ } catch (InterruptedException e) {
+
+ }
+ }
+ };
+ takeThread.start();
- @Test
- public void testElement4() throws Exception {
- createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
- }
+ Thread.sleep(1000);
+ Thread offerThread = new Thread() {
+ public void run() {
+ try {
+ queueHandles[0].offer(threadTestString.getBytes(UTF_8));
+ } catch (KeeperException e) {
+
+ } catch (InterruptedException e) {
- @Test
- public void testTakeWait1() throws Exception {
- String dir = "/testTakeWait1";
- final String testString = "Hello World";
- final int numClients = 1;
- final ZooKeeper[] clients = new ZooKeeper[numClients];
- final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
}
+ };
+ offerThread.start();
+ offerThread.join();
- final byte[][] takeResult = new byte[1][];
- Thread takeThread = new Thread(() -> {
- try {
- takeResult[0] = queueHandles[0].take();
- } catch (KeeperException | InterruptedException ignore) {
- // no op
- }
- });
- takeThread.start();
-
- Thread.sleep(1000);
- Thread offerThread = new Thread(() -> {
- try {
- queueHandles[0].offer(testString.getBytes());
- } catch (KeeperException | InterruptedException ignore) {
- // no op
- }
- });
- offerThread.start();
- offerThread.join();
-
- takeThread.join();
-
- Assert.assertNotNull(takeResult[0]);
- Assert.assertEquals(new String(takeResult[0]), testString);
- }
+ takeThread.join();
- @Test
- public void testTakeWait2() throws Exception {
- String dir = "/testTakeWait2";
- final String testString = "Hello World";
- final int numClients = 1;
- final ZooKeeper[] clients = new ZooKeeper[numClients];
- final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
- int numAttempts = 2;
- for (int i = 0; i < numAttempts; i++) {
- final byte[][] takeResult = new byte[1][];
- final String threadTestString = testString + i;
- Thread takeThread = new Thread(() -> {
- try {
- takeResult[0] = queueHandles[0].take();
- } catch (KeeperException | InterruptedException ignore) {
- // no op
- }
- });
- takeThread.start();
-
- Thread.sleep(1000);
- Thread offerThread = new Thread(() -> {
- try {
- queueHandles[0].offer(threadTestString.getBytes());
- } catch (KeeperException | InterruptedException ignore) {
- // no op
- }
- });
- offerThread.start();
- offerThread.join();
-
- takeThread.join();
-
- Assert.assertNotNull(takeResult[0]);
- Assert.assertEquals(new String(takeResult[0]), threadTestString);
- }
+ Assert.assertTrue(takeResult[0] != null);
Review comment:
should go back to assertNotNull
##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/cli/ReconfigCommand.java
##########
@@ -152,7 +153,7 @@ public boolean exec() throws CliException {
}
byte[] curConfig = ((ZooKeeperAdmin) zk).reconfigure(joining, leaving, members, version, stat);
- out.println("Committed new configuration:\n" + new String(curConfig));
+ out.println("Committed new configuration:\n" + new String(curConfig, StandardCharsets.UTF_8));
Review comment:
This should probably just be two `println` statements instead of appending a newline and using string concatenation.
##########
File path: zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java
##########
@@ -27,243 +28,260 @@
import org.junit.Test;
/**
- * Tests for {@link DistributedQueue}.
+ * DistributedQueueTest test.
*/
public class DistributedQueueTest extends ClientBase {
- @After
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOffer1() throws Exception {
+ String dir = "/testOffer1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer1() throws Exception {
- String dir = "/testOffer1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testOffer2() throws Exception {
+ String dir = "/testOffer2";
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testOffer2() throws Exception {
- String dir = "/testOffer2";
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[1].remove();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[1].remove();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testTake1() throws Exception {
+ String dir = "/testTake1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testTake1() throws Exception {
- String dir = "/testTake1";
- String testString = "Hello World";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- queueHandles[0].offer(testString.getBytes());
-
- byte[] dequeuedBytes = queueHandles[0].take();
- Assert.assertEquals(new String(dequeuedBytes), testString);
+ queueHandles[0].offer(testString.getBytes(UTF_8));
+
+ byte[] dequeuedBytes = queueHandles[0].take();
+ Assert.assertEquals(new String(dequeuedBytes, UTF_8), testString);
+ }
+
+ @Test
+ public void testRemove1() throws Exception {
+ String dir = "/testRemove1";
+ String testString = "Hello World";
+ final int numClients = 1;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- @Test
- public void testRemove1() throws Exception {
- String dir = "/testRemove1";
- final int numClients = 1;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- try {
- queueHandles[0].remove();
- } catch (NoSuchElementException e) {
- return;
- }
-
- Assert.fail();
+ try {
+ queueHandles[0].remove();
+ } catch (NoSuchElementException e) {
+ return;
+ }
+ Assert.assertTrue(false);
+ }
+
+ public void createNremoveMtest(String dir, int n, int m) throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMtest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
-
- for (int i = 0; i < n; i++) {
- String offerString = testString + i;
- queueHandles[0].offer(offerString.getBytes());
- }
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
+ }
- byte[] data = null;
- for (int i = 0; i < m; i++) {
- data = queueHandles[1].remove();
- }
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
+ }
+ Assert.assertEquals(new String(data, UTF_8), testString + (m - 1));
+ }
+
+ @Test
+ public void testRemove2() throws Exception {
+ createNremoveMtest("/testRemove2", 10, 2);
+ }
+
+ @Test
+ public void testRemove3() throws Exception {
+ createNremoveMtest("/testRemove3", 1000, 1000);
+ }
+
+ public void createNremoveMelementTest(String dir, int n, int m)
+ throws Exception {
+ String testString = "Hello World";
+ final int numClients = 2;
+ ZooKeeper[] clients = new ZooKeeper[numClients];
+ DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
+ }
- Assert.assertNotNull(data);
- Assert.assertEquals(new String(data), testString + (m - 1));
+ for (int i = 0; i < n; i++) {
+ String offerString = testString + i;
+ queueHandles[0].offer(offerString.getBytes(UTF_8));
}
- @Test
- public void testRemove2() throws Exception {
- createNremoveMtest("/testRemove2", 10, 2);
+ byte[] data = null;
+ for (int i = 0; i < m; i++) {
+ data = queueHandles[1].remove();
}
- @Test
- public void testRemove3() throws Exception {
- createNremoveMtest("/testRemove3", 1000, 1000);
+ Assert.assertEquals(new String(queueHandles[1].element(), UTF_8),
+ testString + m);
+ }
+
+ @Test
+ public void testElement1() throws Exception {
+ createNremoveMelementTest("/testElement1", 1, 0);
+ }
+
+ @Test
+ public void testElement2() throws Exception {
+ createNremoveMelementTest("/testElement2", 10, 2);
+ }
+
+ @Test
+ public void testElement3() throws Exception {
+ createNremoveMelementTest("/testElement3", 1000, 500);
+ }
+
+ @Test
+ public void testElement4() throws Exception {
+ createNremoveMelementTest("/testElement4", 1000, 1000 - 1);
+ }
+
+ @Test
+ public void testTakeWait1() throws Exception {
+ String dir = "/testTakeWait1";
+ final String testString = "Hello World";
+ final int numClients = 1;
+ final ZooKeeper[] clients = new ZooKeeper[numClients];
+ final DistributedQueue[] queueHandles = new DistributedQueue[numClients];
+ for (int i = 0; i < clients.length; i++) {
+ clients[i] = createClient();
+ queueHandles[i] = new DistributedQueue(clients[i], dir, null);
}
- public void createNremoveMelementTest(String dir, int n, int m) throws Exception {
- String testString = "Hello World";
- final int numClients = 2;
- ZooKeeper[] clients = new ZooKeeper[numClients];
- DistributedQueue[] queueHandles = new DistributedQueue[numClients];
- for (int i = 0; i < clients.length; i++) {
- clients[i] = createClient();
- queueHandles[i] = new DistributedQueue(clients[i], dir, null);
- }
+ final byte[] takeResult[] = new byte[1][];
+ Thread takeThread = new Thread() {
+ public void run() {
Review comment:
Why convert lambda to an inner class? This seems like a regression.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org