m-trieu commented on code in PR #28537:
URL: https://github.com/apache/beam/pull/28537#discussion_r1335360374


##########
runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/streaming/WeightBoundedQueueTest.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.runners.dataflow.worker.streaming;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class WeightBoundedQueueTest {
+  private static final int MAX_WEIGHT = 10;
+
+  @Test
+  public void testPut_hasCapacity() {
+    WeightedBoundedQueue<Integer> queue =
+        WeightedBoundedQueue.create(MAX_WEIGHT, i -> Math.min(MAX_WEIGHT, i));
+
+    int insertedValue = 1;
+
+    queue.put(insertedValue);
+
+    assertEquals(insertedValue, queue.queuedElementsWeight());
+    assertEquals(1, queue.size());
+    assertEquals(insertedValue, (int) queue.poll());
+  }
+
+  @Test
+  public void testPut_noCapacity() throws InterruptedException {
+    WeightedBoundedQueue<Integer> queue =
+        WeightedBoundedQueue.create(MAX_WEIGHT, i -> Math.min(MAX_WEIGHT, i));
+
+    // Insert value that takes all the capacity into the queue.
+    Thread thread1 = new Thread(() -> queue.put(MAX_WEIGHT));
+    thread1.start();
+    thread1.join();
+
+    // Try to insert another value into the queue. This will block since there 
is no capacity in the
+    // queue.
+    Thread thread2 = new Thread(() -> queue.put(MAX_WEIGHT));
+    thread2.start();
+
+    // Should only see the first value in the queue, since the queue is at 
capacity.  thread2
+    // should be blocked.
+    assertEquals(MAX_WEIGHT, queue.queuedElementsWeight());
+    assertEquals(1, queue.size());
+
+    // Have another thread poll the queue, pulling off the only value inside 
and freeing up the

Review Comment:
   done



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to