Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix FlagApplier async behaviour #70

Merged
merged 4 commits into from
Jan 16, 2024
Merged

Conversation

fabriziodemaria
Copy link
Member

@fabriziodemaria fabriziodemaria commented Jan 11, 2024

Issues with flaky tests seem to be caused by the async apply function in FlagApplier not really waiting for the entire triggerBatch operation to finish (including network + updating the cache for apply status), before returning.

Note: older context around the existing asynchronous setup: #35 (not sure how much this is relevant for this PR)

@fabriziodemaria fabriziodemaria changed the title tests: Update expectation APIs test: Update expectation APIs Jan 11, 2024
@fabriziodemaria fabriziodemaria marked this pull request as draft January 11, 2024 10:19
Signed-off-by: Fabrizio Demaria <[email protected]>
@fabriziodemaria fabriziodemaria force-pushed the expectations-tests branch 2 times, most recently from 957f127 to 52d4ad8 Compare January 12, 2024 11:03
@fabriziodemaria fabriziodemaria changed the title test: Update expectation APIs fix: Fix FlagApplier async behaviour Jan 12, 2024
@fabriziodemaria fabriziodemaria force-pushed the expectations-tests branch 2 times, most recently from 63fa498 to 3b1a06a Compare January 12, 2024 14:08
DispatchQueue.main.asyncAfter(deadline: .now() + retryWait) {
self.perform(request: request, retry: retry, completion: completion)
}
try? await Task.sleep(nanoseconds: UInt64(retryWait * 1_000_000_000))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure there is a better way to achieve this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like sleep in with Task is a fine way to schedule tasks in the newest Swift: https://www.swiftbysundell.com/articles/delaying-an-async-swift-task/

@fabriziodemaria fabriziodemaria marked this pull request as ready for review January 12, 2024 14:37
guard let httpResponse = response as? HTTPURLResponse else {
completion(nil, nil, HttpClientError.invalidResponse)
do {
async let (data, response) = await self.session.data(for: request)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataTask is not async, but this data function is, hence this change

@@ -37,68 +37,66 @@ 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't here the problem that the add function in the cacheDataInteractor is not defined async? then if it's defined as async we can just await it normally here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try making the cache interactor add funciton async and using await when accessing cache in here ?

Signed-off-by: Fabrizio Demaria <[email protected]>
Comment on lines +79 to +80
do {
let (data, response) = try await self.session.data(for: request)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error is not returned from .data(), while that was the case with .dataTask(). However, .data() is marked as throwing, so the do/catch wrapping should in practice replicate the exact same behaviour of .dataTask()

@nicklasl
Copy link
Member

Looks good 👍

@fabriziodemaria fabriziodemaria merged commit f169d90 into main Jan 16, 2024
3 checks passed
@fabriziodemaria fabriziodemaria deleted the expectations-tests branch January 16, 2024 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants