@@ -154,7 +154,10 @@ describe("sqs", () => {
154154 processChildSpan . end ( ) ;
155155 } ;
156156
157- const expectReceiver2ProcessWithOneChildEach = ( spans : ReadableSpan [ ] ) => {
157+ const expectReceiver2ProcessWithNChildrenEach = (
158+ spans : ReadableSpan [ ] ,
159+ numChildPerProcessSpan : number
160+ ) => {
158161 const awsReceiveSpan = spans . filter (
159162 ( s ) => s . attributes [ SqsAttributeNames . MESSAGING_OPERATION ] === "receive"
160163 ) ;
@@ -174,13 +177,23 @@ describe("sqs", () => {
174177 const processChildSpans = spans . filter (
175178 ( s ) => s . kind === SpanKind . INTERNAL
176179 ) ;
177- expect ( processChildSpans . length ) . toBe ( 2 ) ;
178- expect ( processChildSpans [ 0 ] . parentSpanId ) . toStrictEqual (
179- processSpans [ 0 ] . spanContext . spanId
180- ) ;
181- expect ( processChildSpans [ 1 ] . parentSpanId ) . toStrictEqual (
182- processSpans [ 1 ] . spanContext . spanId
183- ) ;
180+ expect ( processChildSpans . length ) . toBe ( 2 * numChildPerProcessSpan ) ;
181+ for ( let i = 0 ; i < numChildPerProcessSpan ; i ++ ) {
182+ expect ( processChildSpans [ 2 * i + 0 ] . parentSpanId ) . toStrictEqual (
183+ processSpans [ 0 ] . spanContext . spanId
184+ ) ;
185+ expect ( processChildSpans [ 2 * i + 1 ] . parentSpanId ) . toStrictEqual (
186+ processSpans [ 1 ] . spanContext . spanId
187+ ) ;
188+ }
189+ } ;
190+
191+ const expectReceiver2ProcessWith1ChildEach = ( spans : ReadableSpan [ ] ) => {
192+ expectReceiver2ProcessWithNChildrenEach ( spans , 1 ) ;
193+ } ;
194+
195+ const expectReceiver2ProcessWith2ChildEach = ( spans : ReadableSpan [ ] ) => {
196+ expectReceiver2ProcessWithNChildrenEach ( spans , 2 ) ;
184197 } ;
185198
186199 beforeEach ( async ( ) => {
@@ -197,54 +210,110 @@ describe("sqs", () => {
197210 receivedMessages . forEach ( ( msg ) => {
198211 createProcessChildSpan ( msg . Body ) ;
199212 } ) ;
200- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
213+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
201214 } ) ;
202215
203216 it ( "should create processing child with map" , async ( ) => {
204217 receivedMessages . map ( ( msg ) => {
205218 createProcessChildSpan ( msg . Body ) ;
206219 } ) ;
207- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
220+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
221+ } ) ;
222+
223+ it ( "should create one processing child when throws in map" , async ( ) => {
224+ try {
225+ receivedMessages . map ( ( msg ) => {
226+ createProcessChildSpan ( msg . Body ) ;
227+ throw Error ( "error from array.map" ) ;
228+ } ) ;
229+ } catch ( err ) { }
230+
231+ const processChildSpans = memoryExporter
232+ . getFinishedSpans ( )
233+ . filter ( ( s ) => s . kind === SpanKind . INTERNAL ) ;
234+ expect ( processChildSpans . length ) . toBe ( 1 ) ;
235+ } ) ;
236+
237+ it ( "should create processing child with two forEach" , async ( ) => {
238+ receivedMessages . forEach ( ( msg ) => {
239+ createProcessChildSpan ( msg . Body ) ;
240+ } ) ;
241+ receivedMessages . forEach ( ( msg ) => {
242+ createProcessChildSpan ( msg . Body ) ;
243+ } ) ;
244+ expectReceiver2ProcessWith2ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
245+ } ) ;
246+
247+ it ( "should forward all parameters to forEach callback" , async ( ) => {
248+ const objectForThis = { } ;
249+ receivedMessages . forEach ( function ( msg , index , array ) {
250+ expect ( msg ) . not . toBeUndefined ( ) ;
251+ expect ( index ) . toBeLessThan ( 2 ) ;
252+ expect ( index ) . toBeGreaterThanOrEqual ( 0 ) ;
253+ expect ( array ) . toBe ( receivedMessages ) ;
254+ expect ( this ) . toBe ( objectForThis ) ;
255+ } , objectForThis ) ;
256+ } ) ;
257+
258+ it ( "should create one processing child with forEach that throws" , async ( ) => {
259+ try {
260+ receivedMessages . forEach ( ( msg ) => {
261+ createProcessChildSpan ( msg . Body ) ;
262+ throw Error ( "error from forEach" ) ;
263+ } ) ;
264+ } catch ( err ) { }
265+ const processChildSpans = memoryExporter
266+ . getFinishedSpans ( )
267+ . filter ( ( s ) => s . kind === SpanKind . INTERNAL ) ;
268+ expect ( processChildSpans . length ) . toBe ( 1 ) ;
208269 } ) ;
209270
210271 it . skip ( "should create processing child with array index access" , async ( ) => {
211272 for ( let i = 0 ; i < receivedMessages . length ; i ++ ) {
212273 const msg = receivedMessages [ i ] ;
213274 createProcessChildSpan ( msg . Body ) ;
214275 }
215- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
276+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
216277 } ) ;
217278
218- it . skip ( "should create processing child with map and forEach calls" , async ( ) => {
279+ it ( "should create processing child with map and forEach calls" , async ( ) => {
219280 receivedMessages
220281 . map ( ( msg ) => JSON . parse ( msg . Body ) )
221282 . forEach ( ( msgBody ) => {
222283 createProcessChildSpan ( msgBody ) ;
223284 } ) ;
224- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
285+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
225286 } ) ;
226287
227- it . skip ( "should create processing child with filter and forEach" , async ( ) => {
288+ it ( "should create processing child with filter and forEach" , async ( ) => {
228289 receivedMessages
229290 . filter ( ( msg ) => msg )
230291 . forEach ( ( msgBody ) => {
231292 createProcessChildSpan ( msgBody ) ;
232293 } ) ;
233- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
294+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
234295 } ) ;
235296
236297 it . skip ( "should create processing child with for(msg of messages)" , ( ) => {
237298 for ( const msg of receivedMessages ) {
238299 createProcessChildSpan ( msg . Body ) ;
239300 }
240- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
301+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
241302 } ) ;
242303
243304 it . skip ( "should create processing child with array.values() for loop" , ( ) => {
244305 for ( const msg of receivedMessages . values ( ) ) {
245306 createProcessChildSpan ( msg . Body ) ;
246307 }
247- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
308+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
309+ } ) ;
310+
311+ it . skip ( "should create processing child with array.values() for loop and awaits in process" , async ( ) => {
312+ for ( const msg of receivedMessages . values ( ) ) {
313+ await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
314+ createProcessChildSpan ( msg . Body ) ;
315+ }
316+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
248317 } ) ;
249318 } ) ;
250319} ) ;
0 commit comments