Skip to content

Commit

Permalink
fix: [TMP] Try fix flaky tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Demaria <[email protected]>
  • Loading branch information
fabriziodemaria committed Jan 12, 2024
1 parent 81bbf68 commit 2fd8824
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,73 @@ 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()
}

// MARK: private

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)

guard appliesToSend.isEmpty == false else {
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)
}
}
}
Expand All @@ -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,
Expand All @@ -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)
}
}
}
}
Expand Down Expand Up @@ -168,3 +175,15 @@ final class FlagApplierWithRetries: FlagApplier {
}
}
}

extension Sequence {
func async<T>(
_ transform: (Element) async throws -> T
) async rethrows {
var values = [T]()

for element in self {
try await values.append(transform(element))
}
}
}

0 comments on commit 2fd8824

Please sign in to comment.