Skip to content

Commit fe6a630

Browse files
fix: revert event batching (#197)
1 parent e8e0a91 commit fe6a630

File tree

2 files changed

+39
-65
lines changed

2 files changed

+39
-65
lines changed

DevCycle/Networking/DevCycleService.swift

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class DevCycleService: DevCycleServiceProtocol {
105105
return completion((nil, nil, ClientError.InvalidUser))
106106
}
107107

108-
self.batchEventsPayload(events: eventPayload, user: userBody, completion: completion)
108+
self.sendEventsPayload(events: eventPayload, user: userBody, completion: completion)
109109
}
110110

111111
func saveEntity(user: DevCycleUser, completion: @escaping SaveEntityCompletionHandler) {
@@ -293,41 +293,28 @@ class DevCycleService: DevCycleServiceProtocol {
293293
return eventsJSON
294294
}
295295

296-
private func batchEventsPayload(events: [[String:Any]], user: Any, completion: @escaping PublishEventsCompletionHandler) {
296+
private func sendEventsPayload(events: [[String:Any]], user: Any, completion: @escaping PublishEventsCompletionHandler) {
297297
var eventsRequest = createEventsRequest()
298298
eventsRequest.httpMethod = "POST"
299299
eventsRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
300300
eventsRequest.addValue("application/json", forHTTPHeaderField: "Accept")
301301
eventsRequest.addValue(self.config.sdkKey, forHTTPHeaderField: "Authorization")
302-
303-
let totalEventsCount = events.count
304-
var startIndex = 0
305-
var endIndex = min(self.maxBatchSize, totalEventsCount)
306-
307-
while startIndex < totalEventsCount {
308-
let batchEvents = Array(events[startIndex..<endIndex])
309-
310-
let requestBody: [String: Any] = [
311-
"events": batchEvents,
312-
"user": user
313-
]
314302

315-
let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
316-
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
317-
eventsRequest.httpBody = jsonBody
318-
319-
self.makeRequest(request: eventsRequest) { data, response, error in
320-
if error != nil || data == nil {
321-
return completion((data, response, error))
322-
}
323-
// Continue with next batch
324-
startIndex = endIndex
325-
endIndex = min(endIndex + self.maxBatchSize, totalEventsCount)
303+
let requestBody: [String: Any] = [
304+
"events": events,
305+
"user": user
306+
]
326307

327-
if startIndex >= totalEventsCount {
328-
return completion((data, response, nil))
329-
}
308+
let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
309+
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
310+
eventsRequest.httpBody = jsonBody
311+
312+
self.makeRequest(request: eventsRequest) { data, response, error in
313+
if error != nil || data == nil {
314+
return completion((data, response, error))
330315
}
316+
317+
return completion((data, response, nil))
331318
}
332319
}
333320
}

DevCycleTests/Networking/DevCycleServiceTests.swift

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)