/aws-java-sdk-core/src/test/java/com/amazonaws/metrics/AwsSdkMetricsTest.java

https://gitlab.com/github-cloud-corp/aws-sdk-java · Java · 186 lines · 136 code · 13 blank · 37 comment · 11 complexity · de3dfabb5064e7c50573aa54de998726 MD5 · raw file

  1. /*
  2. * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights
  3. * Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is
  12. * distributed
  13. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either
  15. * express or implied. See the License for the specific language
  16. * governing
  17. * permissions and limitations under the License.
  18. */
  19. package com.amazonaws.metrics;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertNotSame;
  23. import static org.junit.Assert.assertSame;
  24. import static org.junit.Assert.assertTrue;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.Set;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import com.amazonaws.util.AWSRequestMetrics.Field;
  31. public class AwsSdkMetricsTest {
  32. /**
  33. * By default the AWS SDK metric collection is disabled. Enabling it should
  34. * fail unless the necessary CloudWatch related jars are on the classpath.
  35. * Therefore, this test is expected to fail in enabling the default metric
  36. * collection, but have absolutely no impact otherwise.
  37. */
  38. @Test
  39. public void enableDefaultMetrics() {
  40. Assert.assertFalse(AwsSdkMetrics.enableDefaultMetrics());
  41. }
  42. @Test
  43. public void test() {
  44. // by default, it's disabled
  45. assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
  46. // won't be anble to enable unless the default impl library is on the classpath
  47. assertFalse(AwsSdkMetrics.enableDefaultMetrics());
  48. assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
  49. assertSame(RequestMetricCollector.NONE, AwsSdkMetrics.getRequestMetricCollector());
  50. assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
  51. // effectively no effect
  52. AwsSdkMetrics.disableMetrics();
  53. assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
  54. }
  55. @Test
  56. public void defaultMetricTypes() {
  57. // Default set of predefined metric types is not empty
  58. Set<MetricType> set = AwsSdkMetrics.getPredefinedMetrics();
  59. assertNotNull(set);
  60. assertTrue(set.size() > 0);
  61. // Clear out the default set of predefined metric types
  62. AwsSdkMetrics.set(Collections.<MetricType>emptyList());
  63. Set<MetricType> empty = AwsSdkMetrics.getPredefinedMetrics();
  64. assertNotNull(empty);
  65. assertTrue(empty.size() == 0);
  66. // Reconfigure the default set of predefined metric types back to the original
  67. AwsSdkMetrics.set(set);
  68. Set<MetricType> set2 = AwsSdkMetrics.getPredefinedMetrics();
  69. assertNotNull(set2);
  70. assertTrue(set2.size() > 0);
  71. // Not the same due to ensuring thread-safety
  72. assertNotSame(set, set2);
  73. }
  74. @Test
  75. public void setNullOrEmpty() {
  76. Set<MetricType> orig = AwsSdkMetrics.getPredefinedMetrics();
  77. assertTrue(orig.size() > 0);
  78. AwsSdkMetrics.set(null);
  79. Set<MetricType> empty = AwsSdkMetrics.getPredefinedMetrics();
  80. assertTrue(empty.size() == 0);
  81. AwsSdkMetrics.set(null);
  82. Set<MetricType> stillEmpty = AwsSdkMetrics.getPredefinedMetrics();
  83. assertSame(empty, stillEmpty);
  84. AwsSdkMetrics.set(Collections.<MetricType>emptySet());
  85. Set<MetricType> empty3 = AwsSdkMetrics.getPredefinedMetrics();
  86. assertSame(empty, empty3);
  87. AwsSdkMetrics.set(orig);
  88. }
  89. @Test
  90. public void addNull() {
  91. assertFalse(AwsSdkMetrics.add(null));
  92. }
  93. @Test
  94. public void addAllNull() {
  95. assertFalse(AwsSdkMetrics.addAll(null));
  96. assertFalse(AwsSdkMetrics.addAll(Collections.<MetricType>emptyList()));
  97. }
  98. @Test
  99. public void removeNull() {
  100. assertFalse(AwsSdkMetrics.remove(null));
  101. }
  102. @Test
  103. public void addAndRemove() {
  104. Set<MetricType> orig = AwsSdkMetrics.getPredefinedMetrics();
  105. AwsSdkMetrics.set(null);
  106. // Test add and remove
  107. assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
  108. AwsSdkMetrics.add(Field.ClientExecuteTime);
  109. assertFalse(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
  110. AwsSdkMetrics.remove(Field.ClientExecuteTime);
  111. assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
  112. // Test add more than one entry
  113. AwsSdkMetrics.add(Field.ClientExecuteTime);
  114. AwsSdkMetrics.add(Field.Exception);
  115. assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == 2);
  116. AwsSdkMetrics.set(null);
  117. assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
  118. // Test addAll
  119. AwsSdkMetrics.addAll(Arrays.asList(Field.Exception, Field.ClientExecuteTime));
  120. assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == 2);
  121. AwsSdkMetrics.set(orig);
  122. assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == orig.size());
  123. }
  124. @Test
  125. public void setJvmMetricsExcluded() {
  126. final boolean b = AwsSdkMetrics.isMachineMetricExcluded();
  127. AwsSdkMetrics.setMachineMetricsExcluded(b);
  128. assertTrue(b == AwsSdkMetrics.isMachineMetricExcluded());
  129. AwsSdkMetrics.setMachineMetricsExcluded(!b);
  130. assertFalse(b == AwsSdkMetrics.isMachineMetricExcluded());
  131. AwsSdkMetrics.setMachineMetricsExcluded(b);
  132. assertTrue(b == AwsSdkMetrics.isMachineMetricExcluded());
  133. }
  134. @Test
  135. public void setPerHostMetricsIncluded() {
  136. final boolean b = AwsSdkMetrics.isPerHostMetricIncluded();
  137. AwsSdkMetrics.setPerHostMetricsIncluded(b);
  138. assertTrue(b == AwsSdkMetrics.isPerHostMetricIncluded());
  139. AwsSdkMetrics.setPerHostMetricsIncluded(!b);
  140. assertFalse(b == AwsSdkMetrics.isPerHostMetricIncluded());
  141. AwsSdkMetrics.setPerHostMetricsIncluded(b);
  142. assertTrue(b == AwsSdkMetrics.isPerHostMetricIncluded());
  143. }
  144. @Test
  145. public void testEnableHttpSocketReadMetric() {
  146. assertFalse(AwsSdkMetrics.isHttpSocketReadMetricEnabled());
  147. AwsSdkMetrics.enableHttpSocketReadMetric();
  148. assertTrue(AwsSdkMetrics.isHttpSocketReadMetricEnabled());
  149. }
  150. @Test
  151. public void isMetricEnabled() {
  152. // originally disabled
  153. assertFalse(AwsSdkMetrics.isMetricsEnabled());
  154. // set to NONE, so still disabled
  155. AwsSdkMetrics.setMetricCollector(MetricCollector.NONE);
  156. assertFalse(AwsSdkMetrics.isMetricsEnabled());
  157. // set to a custom collector, so now considered enabled
  158. AwsSdkMetrics.setMetricCollector(new MetricCollector() {
  159. @Override public boolean start() { return true; }
  160. @Override public boolean stop() { return false; }
  161. @Override public boolean isEnabled() { return true; }
  162. @Override
  163. public RequestMetricCollector getRequestMetricCollector() {
  164. return RequestMetricCollector.NONE;
  165. }
  166. @Override
  167. public ServiceMetricCollector getServiceMetricCollector() {
  168. return ServiceMetricCollector.NONE;
  169. }
  170. });
  171. assertTrue(AwsSdkMetrics.isMetricsEnabled());
  172. }
  173. }