@@ -79,4 +79,51 @@ describe("TraceContextService", () => {
79
79
expect ( currentTraceContext ?. sampleMode ( ) ) . toBe ( 1 ) ;
80
80
expect ( currentTraceContext ?. source ) . toBe ( "xray" ) ;
81
81
} ) ;
82
+
83
+ it ( "resets rootTraceContext to prevent caching between invocations" , ( ) => {
84
+ // Initial trace context
85
+ traceContextService [ "rootTraceContext" ] = {
86
+ toTraceId : ( ) => "123456" ,
87
+ toSpanId : ( ) => "abcdef" ,
88
+ sampleMode : ( ) => 1 ,
89
+ source : TraceSource . Event ,
90
+ spanContext : spanContext ,
91
+ } ;
92
+
93
+ expect ( traceContextService . currentTraceContext ) . not . toBeNull ( ) ;
94
+ expect ( traceContextService . traceSource ) . toBe ( "event" ) ;
95
+
96
+ traceContextService . reset ( ) ;
97
+
98
+ expect ( traceContextService . currentTraceContext ) . toBeNull ( ) ;
99
+ expect ( traceContextService . traceSource ) . toBeNull ( ) ;
100
+ } ) ;
101
+
102
+ it ( "automatically resets trace context at the beginning of extract" , async ( ) => {
103
+ // Mock the extractor to return a specific context
104
+ const mockExtract = jest . fn ( ) . mockResolvedValue ( {
105
+ toTraceId : ( ) => "newTraceId" ,
106
+ toSpanId : ( ) => "newSpanId" ,
107
+ sampleMode : ( ) => 1 ,
108
+ source : TraceSource . Event ,
109
+ spanContext : { } ,
110
+ } ) ;
111
+ traceContextService [ "traceExtractor" ] = { extract : mockExtract } as any ;
112
+
113
+ // Set up old trace context (simulating previous invocation)
114
+ traceContextService [ "rootTraceContext" ] = {
115
+ toTraceId : ( ) => "oldTraceId" ,
116
+ toSpanId : ( ) => "oldSpanId" ,
117
+ sampleMode : ( ) => 0 ,
118
+ source : TraceSource . Xray ,
119
+ spanContext : { } ,
120
+ } ;
121
+
122
+ // Extract should reset and set new context
123
+ const result = await traceContextService . extract ( { } , { } as any ) ;
124
+
125
+ // Verify old context was cleared and new context was set
126
+ expect ( result ?. toTraceId ( ) ) . toBe ( "newTraceId" ) ;
127
+ expect ( traceContextService . traceSource ) . toBe ( "event" ) ;
128
+ } ) ;
82
129
} ) ;
0 commit comments