@@ -4,10 +4,14 @@ import cats.syntax.all._
4
4
import hedgehog ._
5
5
import hedgehog .runner ._
6
6
import monix .eval .{Task , TaskLocal }
7
- import org .slf4j .MDC
7
+ import org .slf4j .spi .MDCAdapter
8
+ import org .slf4j .{MDC , SetMdcAdapter }
8
9
10
+ import java .io .{PrintWriter , StringWriter }
9
11
import java .time .Instant
12
+ import java .util
10
13
import scala .jdk .CollectionConverters ._
14
+ import scala .util .control .NonFatal
11
15
12
16
/** @author Kevin Lee
13
17
* @since 2023-07-03
@@ -20,7 +24,23 @@ object Monix3MdcAdapterSpec extends Properties {
20
24
*/
21
25
implicit val opts : Task .Options = Task .defaultOptions.enableLocalContextPropagation
22
26
23
- private val monixMdcAdapter : Monix3MdcAdapter = Monix3MdcAdapter .initialize()
27
+ @ SuppressWarnings (Array (" org.wartremover.warts.Throw" ))
28
+ private val monixMdcAdapter : Monix3MdcAdapter =
29
+ try {
30
+ Monix3MdcAdapter .initialize()
31
+ } catch {
32
+ case NonFatal (ex) =>
33
+ val writer = new StringWriter ()
34
+ val out = new PrintWriter (writer)
35
+ ex.printStackTrace(out)
36
+ System
37
+ .err
38
+ .println(
39
+ s """ Error when initializing Monix3MdcAdapter: ${writer.toString}
40
+ | """ .stripMargin
41
+ )
42
+ throw ex // scalafix:ok DisableSyntax.throw
43
+ }
24
44
25
45
override def tests : List [Test ] = List (
26
46
property(" Task - MDC should be able to put and get a value" , testPutAndGet),
@@ -39,6 +59,7 @@ object Monix3MdcAdapterSpec extends Properties {
39
59
property(" Task - MDC: It should return context map for getCopyOfContextMap" , testGetCopyOfContextMap),
40
60
property(" Task - MDC: It should return context map for getPropertyMap" , testGetPropertyMap),
41
61
property(" Task - MDC: It should return context map for getKeys" , testGetKeys),
62
+ property(" test SetMdcAdapter" , testSetMdcAdapter),
42
63
)
43
64
44
65
def before (): Unit =
@@ -484,4 +505,48 @@ object Monix3MdcAdapterSpec extends Properties {
484
505
}.toMap
485
506
}
486
507
508
+ @ SuppressWarnings (Array (" org.wartremover.warts.Equals" ))
509
+ def testSetMdcAdapter : Property = for {
510
+ mdcAdapterName1 <- Gen .string(Gen .ascii, Range .linear(5 , 5 )).log(" mdcAdapterName1" )
511
+ mdcAdapterName2 <- Gen .string(Gen .ascii, Range .linear(5 , 5 )).log(" mdcAdapterName2" )
512
+ mdcAdapterName <- Gen .constant(mdcAdapterName1 + mdcAdapterName2).log(" mdcAdapterName" )
513
+ } yield {
514
+ val mdcAdapter = TestMdcAdapter (mdcAdapterName)
515
+ val before = MDC .getMDCAdapter()
516
+
517
+ SetMdcAdapter (mdcAdapter)
518
+
519
+ val after = MDC .getMDCAdapter()
520
+
521
+ Result .all(
522
+ List (
523
+ Result .diffNamed(" before should not be equal to the new MDCAdapter" , before, mdcAdapter)(_ != _),
524
+ (after ==== mdcAdapter).log(" after SetMdcAdapter should be equal to the new MDCAdapter" ),
525
+ )
526
+ )
527
+ }
528
+
529
+ @ SuppressWarnings (Array (" org.wartremover.warts.Var" ))
530
+ final case class TestMdcAdapter (name : String ) extends MDCAdapter {
531
+
532
+ override def put (key : String , `val` : String ): Unit = ???
533
+
534
+ override def get (key : String ): String = ???
535
+
536
+ override def remove (key : String ): Unit = ???
537
+
538
+ override def clear (): Unit = ???
539
+
540
+ override def getCopyOfContextMap : util.Map [String , String ] = ???
541
+
542
+ override def setContextMap (contextMap : util.Map [String , String ]): Unit = ???
543
+
544
+ override def pushByKey (key : String , value : String ): Unit = ???
545
+
546
+ override def popByKey (key : String ): String = ???
547
+
548
+ override def getCopyOfDequeByKey (key : String ): util.Deque [String ] = ???
549
+
550
+ override def clearDequeByKey (key : String ): Unit = ???
551
+ }
487
552
}
0 commit comments