@@ -81,7 +81,7 @@ class DevCycleServiceTests: XCTestCase {
8181 let service = MockDevCycleService ( )
8282 let eventQueue = EventQueue ( )
8383 let user = try ! DevCycleUser . builder ( ) . userId ( " user1 " ) . build ( )
84- let expectation = XCTestExpectation ( description: " Events are flushed in a single batch " )
84+ let expectation = XCTestExpectation ( description: " 10 Events are flushed in a single batch" )
8585
8686 // Generate 205 custom events and add them to the queue
8787 for i in 0 ..< 10 {
@@ -98,11 +98,11 @@ class DevCycleServiceTests: XCTestCase {
9898 XCTAssertEqual ( service. makeRequestCallCount, 1 , " makeRequest should have been called 1 time " )
9999 }
100100
101- func testFlushingEventsInBatches ( ) {
101+ func testFlushingLargeNumberOfEvents ( ) {
102102 let service = MockDevCycleService ( )
103103 let eventQueue = EventQueue ( )
104104 let user = try ! DevCycleUser . builder ( ) . userId ( " user1 " ) . build ( )
105- let expectation = XCTestExpectation ( description: " Events are serially queued and flushed in multiple batches " )
105+ let expectation = XCTestExpectation ( description: " 205 Events are flushed in a single batch " )
106106
107107 // Generate 205 custom events and add them to the queue
108108 for i in 0 ..< 205 {
@@ -116,7 +116,7 @@ class DevCycleServiceTests: XCTestCase {
116116 }
117117 wait ( for: [ expectation] , timeout: 3.0 )
118118 XCTAssertTrue ( service. publishEventsCalled)
119- XCTAssertEqual ( service. makeRequestCallCount, 3 , " makeRequest should have been called 3 times " )
119+ XCTAssertEqual ( service. makeRequestCallCount, 1 , " makeRequest should have been called 1 times " )
120120 }
121121}
122122
@@ -223,43 +223,30 @@ extension DevCycleServiceTests {
223223 }
224224
225225 private func batchEventsPayload( events: [ [ String : Any ] ] , user: Any , completion: @escaping PublishEventsCompletionHandler ) {
226- let url = URL ( string: " http://test.com/v1/events " ) !
227- var eventsRequest = URLRequest ( url: url)
228- eventsRequest. httpMethod = " POST "
229- eventsRequest. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
230- eventsRequest. addValue ( " application/json " , forHTTPHeaderField: " Accept " )
231- eventsRequest. addValue ( self . sdkKey, forHTTPHeaderField: " Authorization " )
232-
233- let totalEventsCount = events. count
234- var startIndex = 0
235- var endIndex = min ( self . testMaxBatchSize, totalEventsCount)
236-
237- while startIndex < totalEventsCount {
238- let batchEvents = Array ( events [ startIndex..< endIndex] )
239-
240- let requestBody : [ String : Any ] = [
241- " events " : batchEvents,
242- " user " : user
243- ]
226+ let url = URL ( string: " http://test.com/v1/events " ) !
227+ var eventsRequest = URLRequest ( url: url)
228+ eventsRequest. httpMethod = " POST "
229+ eventsRequest. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
230+ eventsRequest. addValue ( " application/json " , forHTTPHeaderField: " Accept " )
231+ eventsRequest. addValue ( self . sdkKey, forHTTPHeaderField: " Authorization " )
232+
233+ let requestBody : [ String : Any ] = [
234+ " events " : events,
235+ " user " : user
236+ ]
244237
245- let jsonBody = try ? JSONSerialization . data ( withJSONObject: requestBody, options: . prettyPrinted)
246- Log . debug ( " Post Events Payload: \( String ( data: jsonBody!, encoding: . utf8) ?? " " ) " )
247- eventsRequest. httpBody = jsonBody
248-
249- self . makeRequest ( request: eventsRequest) { data, response, error in
250- if error != nil || data == nil {
251- return completion ( ( data, response, error) )
252- }
253- // Continue with next batch
254- startIndex = endIndex
255- endIndex = min ( endIndex + self . testMaxBatchSize, totalEventsCount)
238+ let jsonBody = try ? JSONSerialization . data ( withJSONObject: requestBody, options: . prettyPrinted)
239+ Log . debug ( " Post Events Payload: \( String ( data: jsonBody!, encoding: . utf8) ?? " " ) " )
240+ eventsRequest. httpBody = jsonBody
256241
257- if startIndex >= totalEventsCount {
258- return completion ( ( data, response, nil ) )
259- }
260- }
242+ self . makeRequest ( request: eventsRequest) { data, response, error in
243+ if error != nil || data == nil {
244+ return completion ( ( data, response, error) )
261245 }
246+
247+ return completion ( ( data, response, nil ) )
262248 }
249+ }
263250 }
264251
265252
0 commit comments