Skip to content

Tests: replaced toEventually with new waitUntilValue to simplify tests#2071

Merged
NachoSoto merged 5 commits into
mainfrom
wait-until
Nov 28, 2022
Merged

Tests: replaced toEventually with new waitUntilValue to simplify tests#2071
NachoSoto merged 5 commits into
mainfrom
wait-until

Conversation

@NachoSoto

Copy link
Copy Markdown
Contributor

Example:

-        var result: Result<Offerings, OfferingsManager.Error>?
-        offeringsManager.offerings(appUserID: MockData.anyAppUserID) {
-            result = $0
+        let result = waitUntilValue { completed in
+            self.offeringsManager.offerings(appUserID: MockData.anyAppUserID) {
+                completed($0)
+            }
         }
-        expect(result).toEventuallyNot(beNil())

This removes the need to poll for when the value is returned, and instead it gets detected immediately, potentially providing a significant boost in test execution speed.

I've also used Nimble's own waitUntil for cases where we don't need to return a value.

toEventually usage:

  • Before this PR: 353
  • After: 229

There's still a lot of legitimate usage of toEventually, but I'm sure I missed other places where this can also simplify the code. This is a good start for now.

@NachoSoto NachoSoto requested a review from a team November 18, 2022 00:30

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No longer need to specify types every time, now they're inferred by the method we call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Never mind, that only works in Xcode 14.

@vegaro vegaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

still have some files to check but looking good so far

Comment on lines 650 to 652

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be:

let receivedError: Error? = waitUntilValue { completed in
	self.orchestrator.showManageSubscription(completion: completed)
}

Correct? Not sure if that's more legible though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not this because of @Sendable shenanigans 🙃

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why has this been changed to 3?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The 1 was passing because it is 1 at some point. But in practice once the operation changes this became 3. I suppose I should change this to >= 1 to match the same semantics as before, what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could >= 1 give a false positive? Right now, there are 3 invokes on the main thread, one of them being the restoring one, which is the one we are testing for. If that one stops being called on the main thread (total of 2 invokes on main thread), the test will still pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Probably, but that was an issue with the existing test as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I looked into this more. This just looks like a bad test. The same operation dispatcher is used for all of Purchases, so other background tasks are calling dispatchOnMainThread as well. The test has always passed because it was effectively checking >= 1 with that toEventually. Eventually it was 1, even before restorePurchases was called, but that's not how the test was written.

Unfortunately can't think of a way to improve the test since we have no way to identify which operation was invoked.

Scratch that, of course there's a simple way haha

@NachoSoto NachoSoto Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since the test is testRestorePurchasesCallsCompletionOnMainThreadWhenMissingReceipts I added this in the completion block:

expect(Thread.isMainThread) == true

@vegaro vegaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just that question about the >= 1

@NachoSoto NachoSoto enabled auto-merge (squash) November 28, 2022 15:55
…y tests

Example:
```diff
-        var result: Result<Offerings, OfferingsManager.Error>?
-        offeringsManager.offerings(appUserID: MockData.anyAppUserID) {
-            result = $0
+        let result = waitUntilValue { completed in
+            self.offeringsManager.offerings(appUserID: MockData.anyAppUserID) {
+                completed($0)
+            }
         }
-        expect(result).toEventuallyNot(beNil())
```

This removes the need to _poll_ for when the value is returned, and instead it gets detected immediately, potentially providing a significant boost in test execution speed.
I've also used `Nimble`'s own `waitUntil` for cases where we don't need to return a value.

 #### `toEventually` usage:
- Before this PR: 353
- After: 229

There's still a lot of legitimate usage of `toEventually`, but I'm sure I missed other places where this can also simplify the code.
This is a good start for now.
@NachoSoto NachoSoto merged commit dd60d6c into main Nov 28, 2022
@NachoSoto NachoSoto deleted the wait-until branch November 28, 2022 16:47
NachoSoto added a commit that referenced this pull request Dec 13, 2022
NachoSoto added a commit that referenced this pull request Dec 13, 2022
NachoSoto added a commit that referenced this pull request Dec 19, 2022
See also #2071 and #2144.

I broke this tests recently, and because we're using `toEventually` they each take 1 second to fail, instead of failing immediately after the request finishes.
NachoSoto added a commit that referenced this pull request Dec 20, 2022
NachoSoto added a commit that referenced this pull request Dec 21, 2022
See also #2071 and #2144.

I broke these tests recently, and because we're using `toEventually`
they each take over 1 second to fail, instead of failing immediately
after the request finishes.
NachoSoto added a commit that referenced this pull request Feb 2, 2023
See also #2168 and #2071.

I was looking into randomness in tests that might lead to test coverage fluctuations and saw that these tests could be simplified to avoid use of `toEventually`.
NachoSoto added a commit that referenced this pull request Feb 3, 2023
See also #2168 and #2071.

I was looking into randomness in tests that might lead to test coverage
fluctuations and saw that these tests could be simplified to avoid use
of `toEventually`.
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.

2 participants