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

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new ef2bc53  Average time in MethodExecStats can integer overflow.
ef2bc53 is described below

commit ef2bc535c8d6d4e6bd11422eac19132d9a018894
Author: JamesBognar <[email protected]>
AuthorDate: Fri Jan 31 17:26:40 2020 -0500

    Average time in MethodExecStats can integer overflow.
---
 .../org/apache/juneau/utils/MethodExecStats.java   |  4 +---
 .../org/apache/juneau/utils/WeightedAverage.java   |  6 ++---
 .../org/apache/juneau/rest/mock2/MockRest.java     | 28 +++++++++++++++++++++-
 .../juneau/rest/mock2/MockServletRequest.java      | 18 ++++++++++++--
 4 files changed, 47 insertions(+), 9 deletions(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
index 883f48a..f27c6a3 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
@@ -28,7 +28,6 @@ import org.apache.juneau.marshall.*;
 public class MethodExecStats implements Comparable<MethodExecStats> {
 
        private String method;
-       private WeightedAverage avgTime = new WeightedAverage();
        private volatile int minTime = -1, maxTime;
 
        private AtomicInteger
@@ -76,7 +75,6 @@ public class MethodExecStats implements 
Comparable<MethodExecStats> {
                finishes.incrementAndGet();
                int milliTime = (int)(nanoTime/1_000_000);
                totalTime.addAndGet(nanoTime);
-               avgTime.add(1, nanoTime);
                minTime = minTime == -1 ? milliTime : Math.min(minTime, 
milliTime);
                maxTime = Math.max(maxTime, milliTime);
        }
@@ -150,7 +148,7 @@ public class MethodExecStats implements 
Comparable<MethodExecStats> {
         * @return The average execution time in milliseconds.
         */
        public int getAvgTime() {
-               return (int)avgTime.getValue() / 1_000_000;
+               return (int)(getTotalTime() / getRuns());
        }
 
        /**
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
index 7355d01..560b1c2 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
@@ -16,7 +16,7 @@ package org.apache.juneau.utils;
  * A simple weighted average of numbers.
  */
 public class WeightedAverage {
-       private Float value = 0f;
+       private Double value = 0d;
        private int weight = 0;
 
        /**
@@ -29,7 +29,7 @@ public class WeightedAverage {
        public WeightedAverage add(int w, Number v) {
                if (v != null) {
                        try {
-                               float w1 = weight, w2 = w;
+                               double w1 = weight, w2 = w;
                                weight = Math.addExact(weight, w);
                                if (weight != 0) {
                                        value = (value * (w1/weight)) + 
(v.floatValue() * (w2/weight));
@@ -46,7 +46,7 @@ public class WeightedAverage {
         *
         * @return The weighted average of all numbers.
         */
-       public float getValue() {
+       public double getValue() {
                return value;
        }
 }
diff --git 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
index 9576131..dfdb118 100644
--- 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
+++ 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
@@ -74,6 +74,8 @@ public class MockRest implements MockHttpConnection {
 
        final String contextPath, servletPath;
 
+       private final String[] roles;
+
        /**
         * Constructor.
         *
@@ -104,6 +106,7 @@ public class MockRest implements MockHttpConnection {
                        headers = new LinkedHashMap<>(b.headers);
                        contextPath = b.contextPath;
                        servletPath = b.servletPath;
+                       roles = b.roles;
                } catch (Exception e) {
                        throw new RuntimeException(e);
                }
@@ -211,6 +214,7 @@ public class MockRest implements MockHttpConnection {
                boolean debug;
                Map<String,Object> headers = new LinkedHashMap<>();
                String contextPath, servletPath;
+               String[] roles = new String[0];
 
                Builder(Object impl) {
                        this.impl = impl;
@@ -451,6 +455,28 @@ public class MockRest implements MockHttpConnection {
                }
 
                /**
+                * Adds the specified security roles for all requests.
+                *
+                * @param values The role names to add to all requests (e.g. 
<js>"ROLE_ADMIN"</js>).
+                * @return This object (for method chaining).
+                */
+               public Builder roles(String...values) {
+                       this.roles = values;
+                       return this;
+               }
+
+               /**
+                * Adds the specified security role for all requests.
+                *
+                * @param value The role name to add to all requests (e.g. 
<js>"ROLE_ADMIN"</js>).
+                * @return This object (for method chaining).
+                */
+               public Builder role(String value) {
+                       this.roles = new String[]{value};
+                       return this;
+               }
+
+               /**
                 * Create a new {@link MockRest} object based on the settings 
on this builder.
                 *
                 * @return A new {@link MockRest} object.
@@ -481,7 +507,7 @@ public class MockRest implements MockHttpConnection {
        @Override /* MockHttpConnection */
        public MockServletRequest request(String method, String path, 
Map<String,Object> headers, Object body) {
                String p = RestUtils.trimContextPath(ctx.getPath(), path);
-               return MockServletRequest.create(method, 
p).contextPath(emptyIfNull(contextPath)).servletPath(emptyIfNull(servletPath)).body(body).headers(this.headers).headers(headers).debug(debug).restContext(ctx);
+               return MockServletRequest.create(method, 
p).roles(roles).contextPath(emptyIfNull(contextPath)).servletPath(emptyIfNull(servletPath)).body(body).headers(this.headers).headers(headers).debug(debug).restContext(ctx);
        }
 
        /**
diff --git 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
index 97dc3d4..e67574b 100644
--- 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
+++ 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
@@ -249,11 +249,25 @@ public class MockServletRequest implements 
HttpServletRequest, MockHttpRequest {
        /**
         * Adds the specified roles on this request.
         *
-        * @param roles The roles to add to this request.
+        * @param roles The roles to add to this request (e.g. 
<js>"ROLE_ADMIN"</js>).
         * @return This object (for method chaining).
         */
        public MockServletRequest roles(String...roles) {
-               this.roles.addAll(Arrays.asList(roles));
+               this.roles = ASet.create(roles);
+               return this;
+       }
+
+       /**
+        * Adds the specified role on this request.
+        *
+        * <p>
+        * Note that {@link MockRest.Builder#roles(String...)} can be used to 
set the roles for all requests.
+        *
+        * @param role The role to add to this request (e.g. 
<js>"ROLE_ADMIN"</js>).
+        * @return This object (for method chaining).
+        */
+       public MockServletRequest role(String role) {
+               this.roles = ASet.create(role);
                return this;
        }
 

Reply via email to