From 2fd88246c95b6a3f3528b32ea68e2c0229ea8e70 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Thu, 11 Jan 2024 16:09:08 +0100 Subject: [PATCH] fix: [TMP] Try fix flaky tests Signed-off-by: Fabrizio Demaria --- .../Apply/FlagApplierWithRetries.swift | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift b/Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift index e37c7fe9..17fca728 100644 --- a/Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift +++ b/Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift @@ -37,19 +37,19 @@ final class FlagApplierWithRetries: FlagApplier { public func apply(flagName: String, resolveToken: String) async { let applyTime = Date.backport.now - let (data, added) = await cacheDataInteractor.add( + async let (data, added) = await cacheDataInteractor.add( resolveToken: resolveToken, flagName: flagName, applyTime: applyTime ) - guard added == true else { + guard await added == true else { // If record is found in the cache, early return (de-duplication). // Triggerring batch apply in case if there are any unsent events stored await triggerBatch() return } - self.writeToFile(data: data) + await self.writeToFile(data: data) await triggerBatch() } @@ -57,7 +57,7 @@ final class FlagApplierWithRetries: FlagApplier { private func triggerBatch() async { async let cacheData = await cacheDataInteractor.cache - await cacheData.resolveEvents.forEach { resolveEvent in + await cacheData.resolveEvents.async { resolveEvent in let appliesToSend = resolveEvent.events.filter { $0.status == .created } .chunk(size: 20) @@ -65,40 +65,45 @@ final class FlagApplierWithRetries: FlagApplier { return } - appliesToSend.forEach { chunk in - self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .sending) - executeApply( + let a = resolveEvent.events.count + let b = resolveEvent.events.filter { $0.status == .created }.count + if (a != b) { + print("JAHA") + } + + await appliesToSend.async { chunk in + await self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .sending) + await executeApply( resolveToken: resolveEvent.resolveToken, items: chunk ) { success in guard success else { - self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .created) + await self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .created) return } // Set 'sent' property of apply events to true - self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .sent) + await self.writeStatus(resolveToken: resolveEvent.resolveToken, events: chunk, status: .sent) } } } } - private func writeStatus(resolveToken: String, events: [FlagApply], status: ApplyEventStatus) { + private func writeStatus(resolveToken: String, events: [FlagApply], status: ApplyEventStatus) async { let lastIndex = events.count - 1 - events.enumerated().forEach { index, event in - Task(priority: .medium) { - var data = await self.cacheDataInteractor.setEventStatus( - resolveToken: resolveToken, - name: event.name, - status: status - ) - - if index == lastIndex { - let unsentFlagApplies = data.resolveEvents.filter { - $0.isSent == false - } - data.resolveEvents = unsentFlagApplies - try? self.storage.save(data: data) + await events.enumerated().async { index, event in + print("<< Setting Status \(status) to \(event.name)") + var data = await self.cacheDataInteractor.setEventStatus( + resolveToken: resolveToken, + name: event.name, + status: status + ) + + if index == lastIndex { + let unsentFlagApplies = data.resolveEvents.filter { + $0.isSent == false } + data.resolveEvents = unsentFlagApplies + try? self.storage.save(data: data) } } } @@ -110,8 +115,8 @@ final class FlagApplierWithRetries: FlagApplier { private func executeApply( resolveToken: String, items: [FlagApply], - completion: @escaping (Bool) -> Void - ) { + completion: @escaping (Bool) async -> Void + ) async { let applyFlagRequestItems = items.map { applyEvent in AppliedFlagRequestItem( flag: applyEvent.name, @@ -127,12 +132,14 @@ final class FlagApplierWithRetries: FlagApplier { ) performRequest(request: request) { result in - switch result { - case .success: - completion(true) - case .failure(let error): - self.logApplyError(error: error) - completion(false) + Task { + switch result { + case .success: + await completion(true) + case .failure(let error): + self.logApplyError(error: error) + await completion(false) + } } } } @@ -168,3 +175,15 @@ final class FlagApplierWithRetries: FlagApplier { } } } + +extension Sequence { + func async( + _ transform: (Element) async throws -> T + ) async rethrows { + var values = [T]() + + for element in self { + try await values.append(transform(element)) + } + } +}