@@ -73,6 +73,21 @@ describe("sqs", () => {
7373 } ;
7474
7575 it ( "should set parent context in sqs receive callback" , async ( done ) => {
76+ const sqs = new AWS . SQS ( ) ;
77+ sqs . receiveMessage (
78+ {
79+ QueueUrl : "queue/url/for/unittests" ,
80+ } ,
81+ ( err : AWSError , data : AWS . SQS . Types . ReceiveMessageResult ) => {
82+ expect ( err ) . toBeFalsy ( ) ;
83+ createReceiveChildSpan ( ) ;
84+ expectReceiverWithChildSpan ( memoryExporter . getFinishedSpans ( ) ) ;
85+ done ( ) ;
86+ }
87+ ) ;
88+ } ) ;
89+
90+ it ( "should set parent context in sqs receive 'send' callback" , async ( done ) => {
7691 const sqs = new AWS . SQS ( ) ;
7792 sqs
7893 . receiveMessage ( {
@@ -139,7 +154,10 @@ describe("sqs", () => {
139154 processChildSpan . end ( ) ;
140155 } ;
141156
142- const expectReceiver2ProcessWithOneChildEach = ( spans : ReadableSpan [ ] ) => {
157+ const expectReceiver2ProcessWithNChildrenEach = (
158+ spans : ReadableSpan [ ] ,
159+ numChildPerProcessSpan : number
160+ ) => {
143161 const awsReceiveSpan = spans . filter (
144162 ( s ) => s . attributes [ SqsAttributeNames . MESSAGING_OPERATION ] === "receive"
145163 ) ;
@@ -159,13 +177,23 @@ describe("sqs", () => {
159177 const processChildSpans = spans . filter (
160178 ( s ) => s . kind === SpanKind . INTERNAL
161179 ) ;
162- expect ( processChildSpans . length ) . toBe ( 2 ) ;
163- expect ( processChildSpans [ 0 ] . parentSpanId ) . toStrictEqual (
164- processSpans [ 0 ] . spanContext . spanId
165- ) ;
166- expect ( processChildSpans [ 1 ] . parentSpanId ) . toStrictEqual (
167- processSpans [ 1 ] . spanContext . spanId
168- ) ;
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 ) ;
169197 } ;
170198
171199 beforeEach ( async ( ) => {
@@ -182,54 +210,110 @@ describe("sqs", () => {
182210 receivedMessages . forEach ( ( msg ) => {
183211 createProcessChildSpan ( msg . Body ) ;
184212 } ) ;
185- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
213+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
186214 } ) ;
187215
188216 it ( "should create processing child with map" , async ( ) => {
189217 receivedMessages . map ( ( msg ) => {
190218 createProcessChildSpan ( msg . Body ) ;
191219 } ) ;
192- 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 ) ;
193269 } ) ;
194270
195271 it . skip ( "should create processing child with array index access" , async ( ) => {
196272 for ( let i = 0 ; i < receivedMessages . length ; i ++ ) {
197273 const msg = receivedMessages [ i ] ;
198274 createProcessChildSpan ( msg . Body ) ;
199275 }
200- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
276+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
201277 } ) ;
202278
203- it . skip ( "should create processing child with map and forEach calls" , async ( ) => {
279+ it ( "should create processing child with map and forEach calls" , async ( ) => {
204280 receivedMessages
205281 . map ( ( msg ) => JSON . parse ( msg . Body ) )
206282 . forEach ( ( msgBody ) => {
207283 createProcessChildSpan ( msgBody ) ;
208284 } ) ;
209- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
285+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
210286 } ) ;
211287
212- it . skip ( "should create processing child with filter and forEach" , async ( ) => {
288+ it ( "should create processing child with filter and forEach" , async ( ) => {
213289 receivedMessages
214290 . filter ( ( msg ) => msg )
215291 . forEach ( ( msgBody ) => {
216292 createProcessChildSpan ( msgBody ) ;
217293 } ) ;
218- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
294+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
219295 } ) ;
220296
221297 it . skip ( "should create processing child with for(msg of messages)" , ( ) => {
222298 for ( const msg of receivedMessages ) {
223299 createProcessChildSpan ( msg . Body ) ;
224300 }
225- expectReceiver2ProcessWithOneChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
301+ expectReceiver2ProcessWith1ChildEach ( memoryExporter . getFinishedSpans ( ) ) ;
226302 } ) ;
227303
228304 it . skip ( "should create processing child with array.values() for loop" , ( ) => {
229305 for ( const msg of receivedMessages . values ( ) ) {
230306 createProcessChildSpan ( msg . Body ) ;
231307 }
232- 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 ( ) ) ;
233317 } ) ;
234318 } ) ;
235319} ) ;
0 commit comments