@@ -3,7 +3,7 @@ import { createEnvelope, serializeEnvelope } from '@sentry/utils';
33import { TextEncoder } from 'util' ;
44
55import type { EdgeTransportOptions } from '../../src/edge/transport' ;
6- import { makeEdgeTransport } from '../../src/edge/transport' ;
6+ import { IsolatedPromiseBuffer , makeEdgeTransport } from '../../src/edge/transport' ;
77
88const DEFAULT_EDGE_TRANSPORT_OPTIONS : EdgeTransportOptions = {
99 url : 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7' ,
@@ -51,6 +51,7 @@ describe('Edge Transport', () => {
5151
5252 expect ( mockFetch ) . toHaveBeenCalledTimes ( 0 ) ;
5353 await transport . send ( ERROR_ENVELOPE ) ;
54+ await transport . flush ( ) ;
5455 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
5556
5657 expect ( mockFetch ) . toHaveBeenLastCalledWith ( DEFAULT_EDGE_TRANSPORT_OPTIONS . url , {
@@ -77,6 +78,7 @@ describe('Edge Transport', () => {
7778
7879 expect ( headers . get ) . toHaveBeenCalledTimes ( 0 ) ;
7980 await transport . send ( ERROR_ENVELOPE ) ;
81+ await transport . flush ( ) ;
8082
8183 expect ( headers . get ) . toHaveBeenCalledTimes ( 2 ) ;
8284 expect ( headers . get ) . toHaveBeenCalledWith ( 'X-Sentry-Rate-Limits' ) ;
@@ -101,10 +103,61 @@ describe('Edge Transport', () => {
101103 const transport = makeEdgeTransport ( { ...DEFAULT_EDGE_TRANSPORT_OPTIONS , fetchOptions : REQUEST_OPTIONS } ) ;
102104
103105 await transport . send ( ERROR_ENVELOPE ) ;
106+ await transport . flush ( ) ;
104107 expect ( mockFetch ) . toHaveBeenLastCalledWith ( DEFAULT_EDGE_TRANSPORT_OPTIONS . url , {
105108 body : serializeEnvelope ( ERROR_ENVELOPE , new TextEncoder ( ) ) ,
106109 method : 'POST' ,
107110 ...REQUEST_OPTIONS ,
108111 } ) ;
109112 } ) ;
110113} ) ;
114+
115+ describe ( 'IsolatedPromiseBuffer' , ( ) => {
116+ it ( 'should not call tasks until drained' , async ( ) => {
117+ const ipb = new IsolatedPromiseBuffer ( ) ;
118+
119+ const task1 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
120+ const task2 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
121+
122+ await ipb . add ( task1 ) ;
123+ await ipb . add ( task2 ) ;
124+
125+ expect ( task1 ) . not . toHaveBeenCalled ( ) ;
126+ expect ( task2 ) . not . toHaveBeenCalled ( ) ;
127+
128+ await ipb . drain ( ) ;
129+
130+ expect ( task1 ) . toHaveBeenCalled ( ) ;
131+ expect ( task2 ) . toHaveBeenCalled ( ) ;
132+ } ) ;
133+
134+ it ( 'should not allow adding more items than the specified limit' , async ( ) => {
135+ const ipb = new IsolatedPromiseBuffer ( 3 ) ;
136+
137+ const task1 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
138+ const task2 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
139+ const task3 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
140+ const task4 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
141+
142+ await ipb . add ( task1 ) ;
143+ await ipb . add ( task2 ) ;
144+ await ipb . add ( task3 ) ;
145+
146+ await expect ( ipb . add ( task4 ) ) . rejects . toThrowError ( 'Not adding Promise because buffer limit was reached.' ) ;
147+ } ) ;
148+
149+ it ( 'should not throw when one of the tasks throws when drained' , async ( ) => {
150+ const ipb = new IsolatedPromiseBuffer ( ) ;
151+
152+ const task1 = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
153+ const task2 = jest . fn ( ( ) => Promise . reject ( new Error ( ) ) ) ;
154+
155+ await ipb . add ( task1 ) ;
156+ await ipb . add ( task2 ) ;
157+
158+ await expect ( ipb . drain ( ) ) . resolves . toEqual ( true ) ;
159+
160+ expect ( task1 ) . toHaveBeenCalled ( ) ;
161+ expect ( task2 ) . toHaveBeenCalled ( ) ;
162+ } ) ;
163+ } ) ;
0 commit comments