This is an automated email from the ASF dual-hosted git repository.

mmiller pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 7715766  Replacing MockReateLimiter with mocking object to improve 
test design (#2254)
7715766 is described below

commit 77157661af4e6de790e4b0cd34debd02f69bdce2
Author: wx930910 <[email protected]>
AuthorDate: Wed Sep 8 10:40:58 2021 -0400

    Replacing MockReateLimiter with mocking object to improve test design 
(#2254)
---
 .../core/file/streams/MockRateLimiter.java         | 41 ----------------------
 .../file/streams/RateLimitedInputStreamTest.java   | 29 +++++++++------
 .../file/streams/RateLimitedOutputStreamTest.java  | 17 +++++++--
 3 files changed, 33 insertions(+), 54 deletions(-)

diff --git 
a/core/src/test/java/org/apache/accumulo/core/file/streams/MockRateLimiter.java 
b/core/src/test/java/org/apache/accumulo/core/file/streams/MockRateLimiter.java
deleted file mode 100644
index 905224b..0000000
--- 
a/core/src/test/java/org/apache/accumulo/core/file/streams/MockRateLimiter.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.accumulo.core.file.streams;
-
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.apache.accumulo.core.util.ratelimit.RateLimiter;
-
-public class MockRateLimiter implements RateLimiter {
-  private final AtomicLong permitsAcquired = new AtomicLong();
-
-  @Override
-  public long getRate() {
-    return 0;
-  }
-
-  @Override
-  public void acquire(long permits) {
-    permitsAcquired.addAndGet(permits);
-  }
-
-  public long getPermitsAcquired() {
-    return permitsAcquired.get();
-  }
-}
diff --git 
a/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedInputStreamTest.java
 
b/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedInputStreamTest.java
index d23ee31..18bf228 100644
--- 
a/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedInputStreamTest.java
+++ 
b/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedInputStreamTest.java
@@ -23,8 +23,11 @@ import static org.junit.Assert.assertEquals;
 import java.io.InputStream;
 import java.security.SecureRandom;
 import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.accumulo.core.util.ratelimit.RateLimiter;
 import org.apache.hadoop.fs.Seekable;
+import org.easymock.EasyMock;
 import org.junit.Test;
 
 public class RateLimitedInputStreamTest {
@@ -32,7 +35,17 @@ public class RateLimitedInputStreamTest {
   @Test
   public void permitsAreProperlyAcquired() throws Exception {
     Random randGen = new SecureRandom();
-    MockRateLimiter rateLimiter = new MockRateLimiter();
+    // Create variables for tracking behaviors of mock object
+    AtomicLong rateLimiterPermitsAcquired = new AtomicLong();
+    // Construct mock object
+    RateLimiter rateLimiter = EasyMock.niceMock(RateLimiter.class);
+    // Stub Mock Method
+    rateLimiter.acquire(EasyMock.anyLong());
+    EasyMock.expectLastCall()
+        .andAnswer(() -> 
rateLimiterPermitsAcquired.addAndGet(EasyMock.getCurrentArgument(0)))
+        .anyTimes();
+    EasyMock.replay(rateLimiter);
+
     long bytesRetrieved = 0;
     try (InputStream is = new RateLimitedInputStream(new RandomInputStream(), 
rateLimiter)) {
       for (int i = 0; i < 100; ++i) {
@@ -42,7 +55,7 @@ public class RateLimitedInputStreamTest {
         bytesRetrieved += count;
       }
     }
-    assertEquals(bytesRetrieved, rateLimiter.getPermitsAcquired());
+    assertEquals(bytesRetrieved, rateLimiterPermitsAcquired.get());
   }
 
   private static class RandomInputStream extends InputStream implements 
Seekable {
@@ -55,23 +68,17 @@ public class RateLimitedInputStreamTest {
 
     @Override
     public void seek(long pos) {
-      throw new UnsupportedOperationException("Not supported yet."); // To 
change body of generated
-                                                                     // 
methods, choose Tools |
-                                                                     // 
Templates.
+      throw new UnsupportedOperationException("Not supported yet.");
     }
 
     @Override
     public long getPos() {
-      throw new UnsupportedOperationException("Not supported yet."); // To 
change body of generated
-                                                                     // 
methods, choose Tools |
-                                                                     // 
Templates.
+      throw new UnsupportedOperationException("Not supported yet.");
     }
 
     @Override
     public boolean seekToNewSource(long targetPos) {
-      throw new UnsupportedOperationException("Not supported yet."); // To 
change body of generated
-                                                                     // 
methods, choose Tools |
-                                                                     // 
Templates.
+      throw new UnsupportedOperationException("Not supported yet.");
     }
 
   }
diff --git 
a/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedOutputStreamTest.java
 
b/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedOutputStreamTest.java
index e009c8b..0472bae 100644
--- 
a/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedOutputStreamTest.java
+++ 
b/core/src/test/java/org/apache/accumulo/core/file/streams/RateLimitedOutputStreamTest.java
@@ -23,8 +23,11 @@ import static org.junit.Assert.assertEquals;
 import java.io.OutputStream;
 import java.security.SecureRandom;
 import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.accumulo.core.util.ratelimit.RateLimiter;
 import org.apache.hadoop.fs.FSDataOutputStream;
+import org.easymock.EasyMock;
 import org.junit.Test;
 
 import com.google.common.io.CountingOutputStream;
@@ -34,7 +37,17 @@ public class RateLimitedOutputStreamTest {
   @Test
   public void permitsAreProperlyAcquired() throws Exception {
     Random randGen = new SecureRandom();
-    MockRateLimiter rateLimiter = new MockRateLimiter();
+    // Create variables for tracking behaviors of mock object
+    AtomicLong rateLimiterPermitsAcquired = new AtomicLong();
+    // Construct mock object
+    RateLimiter rateLimiter = EasyMock.niceMock(RateLimiter.class);
+    // Stub Mock Method
+    rateLimiter.acquire(EasyMock.anyLong());
+    EasyMock.expectLastCall()
+        .andAnswer(() -> 
rateLimiterPermitsAcquired.addAndGet(EasyMock.getCurrentArgument(0)))
+        .anyTimes();
+    EasyMock.replay(rateLimiter);
+
     long bytesWritten = 0;
     try (RateLimitedOutputStream os =
         new RateLimitedOutputStream(new NullOutputStream(), rateLimiter)) {
@@ -45,7 +58,7 @@ public class RateLimitedOutputStreamTest {
       }
       assertEquals(bytesWritten, os.position());
     }
-    assertEquals(bytesWritten, rateLimiter.getPermitsAcquired());
+    assertEquals(bytesWritten, rateLimiterPermitsAcquired.get());
   }
 
   public static class NullOutputStream extends FSDataOutputStream {

Reply via email to