@@ -6,7 +6,9 @@ import io.prometheus.client.CollectorRegistry
66import org .scalatest .matchers .should .Matchers
77import org .scalatest .wordspec .AnyWordSpec
88
9+ import java .util .concurrent .atomic .AtomicBoolean
910import scala .jdk .CollectionConverters .*
11+ import scala .util .Try
1012
1113class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
1214
@@ -18,7 +20,6 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
1820
1921 val metrics = factory(" test-name" )
2022
21- metrics should not be null
2223 // Verify it has the expected methods by calling queue
2324 val queueResult = metrics.queue(System .nanoTime())
2425 queueResult shouldBe a[IO [_]]
@@ -80,7 +81,7 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
8081 Array (" test-queue" , " queue" )
8182 )
8283
83- summary should not be null
84+ Option ( summary).isDefined shouldBe true
8485 summary.doubleValue() should be >= 0.0
8586 }
8687
@@ -106,8 +107,8 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
106107 Array (" name2" , " queue" )
107108 )
108109
109- count1 should not be null
110- count2 should not be null
110+ Option ( count1).isDefined shouldBe true
111+ Option ( count2).isDefined shouldBe true
111112 count1.doubleValue() should be >= 0.0
112113 count2.doubleValue() should be >= 0.0
113114 }
@@ -141,7 +142,7 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
141142 Array (" test-run" , " run" )
142143 )
143144
144- count should not be null
145+ Option ( count).isDefined shouldBe true
145146 count.doubleValue() should be >= 0.0
146147 }
147148
@@ -166,8 +167,8 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
166167 Array (" name2" , " run" )
167168 )
168169
169- count1 should not be null
170- count2 should not be null
170+ Option ( count1).isDefined shouldBe true
171+ Option ( count2).isDefined shouldBe true
171172 count1.doubleValue() should be >= 0.0
172173 count2.doubleValue() should be >= 0.0
173174 }
@@ -192,45 +193,40 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
192193
193194 val task = IO .raiseError[String ](new RuntimeException (" test error" ))
194195
195- // Attempt to run and catch exception
196- try {
197- metrics.run(task).unsafeRunSync()
198- } catch {
199- case _ : RuntimeException => // Expected
200- }
196+ // Attempt to run and catch exception using Try
197+ val result = Try (metrics.run(task).unsafeRunSync())
198+ result.isFailure shouldBe true
201199
202200 // Note: The current implementation records metrics only on success
203201 // This test documents the current behavior
204- val count = registry.getSampleValue(
202+ val count = Option ( registry.getSampleValue(
205203 " sequentially_time_count" ,
206204 Array (" name" , " operation" ),
207205 Array (" test-error" , " run" )
208- )
206+ ))
209207
210208 // If exception occurs before flatMap completes, metric may not be recorded
211209 // This is expected behavior based on the implementation
212- if (count != null ) {
213- count.doubleValue() shouldEqual 0.0
214- }
210+ count.foreach(_.doubleValue() shouldEqual 0.0 )
215211 }
216212
217213 " execute task lazily" in {
218214 val registry = new CollectorRegistry ()
219215 val factory = SequentiallyMetricsF .Factory [IO ](registry)
220216 val metrics = factory(" test-lazy" )
221217
222- var executed = false
218+ val executed = new AtomicBoolean ( false )
223219 val task = IO {
224- executed = true
220+ executed.set( true )
225221 " result"
226222 }
227223
228224 // Task should not execute until run is called
229- executed shouldBe false
225+ executed.get() shouldBe false
230226
231227 val result = metrics.run(task).unsafeRunSync()
232228
233- executed shouldBe true
229+ executed.get() shouldBe true
234230 result shouldEqual " result"
235231 }
236232
@@ -258,9 +254,9 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
258254 Array (" test-duration" , " run" )
259255 )
260256
261- summary should not be null
257+ Option ( summary).isDefined shouldBe true
262258 summary.doubleValue() should be > 0.0
263- summary.doubleValue() should be < duration + 0.1 // Allow some margin
259+ summary.doubleValue() should be < duration + 0.5
264260 }
265261 }
266262
@@ -287,8 +283,8 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
287283 Array (" test-integration" , " run" )
288284 )
289285
290- queueCount should not be null
291- runCount should not be null
286+ Option ( queueCount).isDefined shouldBe true
287+ Option ( runCount).isDefined shouldBe true
292288 queueCount.doubleValue() should be >= 0.0
293289 runCount.doubleValue() should be >= 0.0
294290 }
@@ -317,6 +313,8 @@ class SequentiallyMetricsFSpec extends AnyWordSpec with Matchers {
317313 Array (" test-multiple" , " run" )
318314 )
319315
316+ Option (queueCount).isDefined shouldBe true
317+ Option (runCount).isDefined shouldBe true
320318 queueCount.doubleValue() shouldEqual 10.0
321319 runCount.doubleValue() shouldEqual 10.0
322320 }
0 commit comments