szhem commented on a change in pull request #24556: [SPARK-27641][CORE] Fix 
MetricsSystem to remove unregistered source correctly
URL: https://github.com/apache/spark/pull/24556#discussion_r283148086
 
 

 ##########
 File path: 
core/src/test/scala/org/apache/spark/metrics/MetricsSystemSuite.scala
 ##########
 @@ -65,6 +66,32 @@ class MetricsSystemSuite extends SparkFunSuite with 
BeforeAndAfter with PrivateM
     assert(metricsSystem.invokePrivate(sources()).length === 
StaticSources.allSources.length + 1)
   }
 
+  test("MetricsSystem with sources remove") {
+    val metricsSystem = MetricsSystem.createMetricsSystem("test", conf, 
securityMgr)
+    metricsSystem.start()
+    val mockContext = mock(classOf[SparkContext])
+    val mockEnvironment = mock(classOf[SparkEnv])
+    when(mockEnvironment.metricsSystem) thenReturn (metricsSystem)
+    when(mockContext.env) thenReturn (mockEnvironment)
+    val sources = PrivateMethod[ArrayBuffer[Source]]('sources)
+    val registry = PrivateMethod[MetricRegistry]('registry)
+    val laFirst = mockContext.longAccumulator
+    val laSecond = mockContext.longAccumulator
+    val laThird = mockContext.longAccumulator
+    val laSource =
+      LongAccumulatorSource.register(mockContext, Map("laF" -> laFirst, "laS" 
-> laSecond))
+    LongAccumulatorSource.register(mockContext, Map("laT" -> laThird))
+    assert(metricsSystem.invokePrivate(sources()).length === 
StaticSources.allSources.length + 2)
+    val notRegisteredSource = new LongAccumulatorSource
+    metricsSystem.removeSource(notRegisteredSource)
+    assert(metricsSystem.invokePrivate(sources()).length === 
StaticSources.allSources.length + 2)
+    metricsSystem.removeSource(laSource)
+    assert(metricsSystem.invokePrivate(sources()).length === 
StaticSources.allSources.length + 1)
+    
assert(metricsSystem.invokePrivate(registry()).getNames.contains("AccumulatorSource.laT"))
+    
assert(!metricsSystem.invokePrivate(registry()).getNames.contains("AccumulatorSource.laF"))
+    
assert(!metricsSystem.invokePrivate(registry()).getNames.contains("AccumulatorSource.laS"))
+  }
 
 Review comment:
   I'd suggest to pay an attention to how the other test cases in this test 
suite are organised by grouping the related blocks of code with the empty lines.
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to