Skip to content

Commit

Permalink
fix: [TMP] Try fix flaky tests, part 2
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 2fd8824 commit 52d4ad8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 59 deletions.
38 changes: 14 additions & 24 deletions Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ final class FlagApplierWithRetries: FlagApplier {
return
}

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(
Expand Down Expand Up @@ -131,27 +125,25 @@ final class FlagApplierWithRetries: FlagApplier {
sdk: Sdk(id: metadata.name, version: metadata.version)
)

performRequest(request: request) { result in
Task {
switch result {
case .success:
await completion(true)
case .failure(let error):
self.logApplyError(error: error)
await completion(false)
}
await performRequest(request: request) { result in
switch result {
case .success:
await completion(true)
case .failure(let error):
self.logApplyError(error: error)
await completion(false)
}
}
}

private func performRequest(
request: ApplyFlagsRequest,
completion: @escaping (ApplyFlagResult) -> Void
) {
completion: @escaping (ApplyFlagResult) async -> Void
) async {
do {
try httpClient.post(path: ":apply", data: request, completion: completion)
try await httpClient.post(path: ":apply", data: request, completion: completion)
} catch {
completion(.failure(handleError(error: error)))
await completion(.failure(handleError(error: error)))
}
}

Expand All @@ -177,13 +169,11 @@ final class FlagApplierWithRetries: FlagApplier {
}

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

for element in self {
try await values.append(transform(element))
try await transform(element)
}
}
}
4 changes: 2 additions & 2 deletions Sources/ConfidenceProvider/Http/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ protocol HttpClient {
func post<T: Decodable>(
path: String,
data: Codable,
completion: @escaping (HttpClientResult<T>) -> Void
) throws
completion: @escaping (HttpClientResult<T>) async -> Void
) async throws

func post<T: Decodable>(path: String, data: Codable) async throws -> HttpClientResponse<T>
}
Expand Down
32 changes: 14 additions & 18 deletions Sources/ConfidenceProvider/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,23 @@ final class NetworkClient: HttpClient {
func post<T: Decodable>(
path: String,
data: Codable,
completion: @escaping (HttpClientResult<T>) -> Void
) throws {
completion: @escaping (HttpClientResult<T>) async -> Void
) async throws {
let request = try buildRequest(path: path, data: data)
perform(request: request, retry: self.retry) { response, data, error in
if let error {
completion(.failure(error))
return
}

guard let response, let data else {
completion(.failure(ConfidenceError.internalError(message: "Bad response")))
return
}

do {
let httpClientResult: HttpClientResponse<T> =
try self.buildResponse(response: response, data: data)
completion(.success(httpClientResult))
} catch {
completion(.failure(error))
}
let (data, response) = try await URLSession.shared.data(for: request)
guard let response = response as? HTTPURLResponse, response.statusCode == 202 else {
await completion(.failure(HttpClientError.internalError))
return
}

do {
let httpClientResult: HttpClientResponse<T> =
try self.buildResponse(response: response, data: data)
await completion(.success(httpClientResult))
} catch {
await completion(.failure(error))
}
}

Expand Down
20 changes: 9 additions & 11 deletions Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ final class CacheDataInteractorTests: XCTestCase {
func testCacheDataInteractor_addEventToEmptyCache() async throws {
// Given cache data interactor with no previously stored data
let cacheDataInteractor = CacheDataInteractor(cacheData: .empty())
Task {
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 0)
}

Task {
// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 0)

// Then event is added with
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 1)
}

// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())

// Then event is added with
let cache2 = await cacheDataInteractor.cache
XCTAssertEqual(cache2.resolveEvents.count, 1)
}

func testCacheDataInteractor_addEventToPreFilledCache() async throws {
Expand Down
8 changes: 4 additions & 4 deletions Tests/ConfidenceProviderTests/Helpers/HttpClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ final class HttpClientMock: HttpClient {
func post<T>(
path: String,
data: Codable,
completion: @escaping (ConfidenceProvider.HttpClientResult<T>) -> Void
) throws where T: Decodable {
completion: @escaping (ConfidenceProvider.HttpClientResult<T>) async -> Void
) async throws where T: Decodable {
do {
let result: HttpClientResponse<T> = try handlePost(path: path, data: data)
completion(.success(result))
await completion(.success(result))
} catch {
completion(.failure(error))
await completion(.failure(error))
}
}

Expand Down

0 comments on commit 52d4ad8

Please sign in to comment.