You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2024/07/26 16:58:44 UTC

(spark-kubernetes-operator) branch main updated: [SPARK-49019] Use `try-with-resources` to test `KubernetesClientFactory.buildKubernetesClient`

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

dongjoon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-kubernetes-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 30f1b6c  [SPARK-49019] Use `try-with-resources` to test `KubernetesClientFactory.buildKubernetesClient`
30f1b6c is described below

commit 30f1b6c448d3630d5b1a9684c4b7ea432307b7fb
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Fri Jul 26 09:58:38 2024 -0700

    [SPARK-49019] Use `try-with-resources` to test `KubernetesClientFactory.buildKubernetesClient`
    
    ### What changes were proposed in this pull request?
    
    This PR aims to use `try-with-resources` to test `KubernetesClientFactory.buildKubernetesClient`.
    
    ### Why are the changes needed?
    
    To be safe during the testing.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Pass the CIs.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No.
    
    Closes #25 from dongjoon-hyun/SPARK-49019.
    
    Authored-by: Dongjoon Hyun <do...@apache.org>
    Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
 .../source/KubernetesMetricsInterceptorTest.java   | 87 +++++++++++-----------
 1 file changed, 45 insertions(+), 42 deletions(-)

diff --git a/spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java b/spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java
index bd34e33..a1d9665 100644
--- a/spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java
+++ b/spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java
@@ -68,36 +68,38 @@ class KubernetesMetricsInterceptorTest {
   void testMetricsEnabled() {
     KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor();
     List<Interceptor> interceptors = Collections.singletonList(metricsInterceptor);
-    KubernetesClient client =
+    try (KubernetesClient client =
         KubernetesClientFactory.buildKubernetesClient(
-            interceptors, kubernetesClient.getConfiguration());
-    SparkApplication sparkApplication = createSparkApplication();
-    ConfigMap configMap = createConfigMap();
+            interceptors, kubernetesClient.getConfiguration())) {
+      SparkApplication sparkApplication = createSparkApplication();
+      ConfigMap configMap = createConfigMap();
 
-    Map<String, Metric> metrics = new HashMap<>(metricsInterceptor.metricRegistry().getMetrics());
-    Assertions.assertEquals(9, metrics.size());
-    client.resource(sparkApplication).create();
-    client.resource(configMap).get();
-    Map<String, Metric> metrics2 = new HashMap<>(metricsInterceptor.metricRegistry().getMetrics());
-    Assertions.assertEquals(17, metrics2.size());
-    List<String> expectedMetricsName =
-        Arrays.asList(
-            "http.response.201",
-            "http.request.post",
-            "sparkapplications.post",
-            "spark-test.sparkapplications.post",
-            "spark-test.sparkapplications.post",
-            "configmaps.get",
-            "spark-system.configmaps.get",
-            "2xx",
-            "4xx");
-    expectedMetricsName.stream()
-        .forEach(
-            name -> {
-              Meter metric = (Meter) metrics2.get(name);
-              Assertions.assertEquals(metric.getCount(), 1);
-            });
-    client.resource(sparkApplication).delete();
+      Map<String, Metric> metrics = new HashMap<>(metricsInterceptor.metricRegistry().getMetrics());
+      Assertions.assertEquals(9, metrics.size());
+      client.resource(sparkApplication).create();
+      client.resource(configMap).get();
+      Map<String, Metric> metrics2 =
+          new HashMap<>(metricsInterceptor.metricRegistry().getMetrics());
+      Assertions.assertEquals(17, metrics2.size());
+      List<String> expectedMetricsName =
+          Arrays.asList(
+              "http.response.201",
+              "http.request.post",
+              "sparkapplications.post",
+              "spark-test.sparkapplications.post",
+              "spark-test.sparkapplications.post",
+              "configmaps.get",
+              "spark-system.configmaps.get",
+              "2xx",
+              "4xx");
+      expectedMetricsName.stream()
+          .forEach(
+              name -> {
+                Meter metric = (Meter) metrics2.get(name);
+                Assertions.assertEquals(metric.getCount(), 1);
+              });
+      client.resource(sparkApplication).delete();
+    }
   }
 
   @Test
@@ -105,22 +107,23 @@ class KubernetesMetricsInterceptorTest {
   void testWhenKubernetesServerNotWorking() {
     KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor();
     List<Interceptor> interceptors = Collections.singletonList(metricsInterceptor);
-    KubernetesClient client =
+    try (KubernetesClient client =
         KubernetesClientFactory.buildKubernetesClient(
-            interceptors, kubernetesClient.getConfiguration());
-    int retry = client.getConfiguration().getRequestRetryBackoffLimit();
-    mockServer.shutdown();
-    SparkApplication sparkApplication = createSparkApplication();
-    assertThrows(
-        Exception.class,
-        () -> {
-          client.resource(sparkApplication).create();
-        });
+            interceptors, kubernetesClient.getConfiguration())) {
+      int retry = client.getConfiguration().getRequestRetryBackoffLimit();
+      mockServer.shutdown();
+      SparkApplication sparkApplication = createSparkApplication();
+      assertThrows(
+          Exception.class,
+          () -> {
+            client.resource(sparkApplication).create();
+          });
 
-    Map<String, Metric> map = metricsInterceptor.metricRegistry().getMetrics();
-    Assertions.assertEquals(12, map.size());
-    Meter metric = (Meter) map.get("failed");
-    Assertions.assertEquals(metric.getCount(), retry + 1);
+      Map<String, Metric> map = metricsInterceptor.metricRegistry().getMetrics();
+      Assertions.assertEquals(12, map.size());
+      Meter metric = (Meter) map.get("failed");
+      Assertions.assertEquals(metric.getCount(), retry + 1);
+    }
   }
 
   private static SparkApplication createSparkApplication() {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org