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

Test suite: avoid calling the fulfill after the wait context has ended #303

Merged
merged 2 commits into from
Mar 17, 2016

Conversation

ricardopereira
Copy link
Contributor

I declared each expectation variable as __weak to avoid API violations. The expectation will be released when the timeout expires. So if the task takes longer than the timeout, the expectation variable will be nil when the task completion handler is called. Thus the fulfill method will be called on nil, doing nothing.

@tcard
Copy link
Contributor

tcard commented Mar 16, 2016

Doesn't assigning a freshly allocated object to a weak variable just deallocate the object right away?

From a quick test:

 10> func test() { 
 11.     weak var x = NSString(string: "not nil") 
 12.     print(x) 
 13. }    
 14> test()
nil

@ricardopereira
Copy link
Contributor Author

Yes, that's my point but it will wait because of the waitForExpectationsWithTimeout. So the expectation variable is available until the fullfill or the timeout.

@ricardopereira
Copy link
Contributor Author

Can I merge?

@ricardopereira ricardopereira force-pushed the fix-tests-api-violation branch from 7f4a1f4 to 0fe7e92 Compare March 16, 2016 19:08
@ricardopereira ricardopereira force-pushed the fix-tests-api-violation branch from 0fe7e92 to 43fde2b Compare March 16, 2016 19:18
@tcard
Copy link
Contributor

tcard commented Mar 16, 2016

I mean that, unless I'm mistaken, the expectation variable will never be not nil. I think the snippet I pasted above proves that; x is immediately nil after assigning it.

@ricardopereira
Copy link
Contributor Author

Yes, correct. In your example the instance has no strong reference but when you create an expectation __weak XCTestExpectation *expectation = [self expectationWithDescription:@"testCase"]; it will have a strong reference. Although not clearly documented, the expectation will be released when the timeout expires. You can check the reference count with [expectation retainCount]:

2016-03-17 08:25:34.155 xctest[59800:9879713] ------- testSubscribeToName
(lldb) po [exp retainCount]
2

Thus there are two references to it. I don't know if one of them is a strong but I tested it and it's working as expected:

- (void)testSubscribeToName {
    __weak XCTestExpectation *expectation = [self expectationWithDescription:@"testSubscribeToName"];
    NSString *channelName = @"channel";
    [ARTTestUtil testRealtime:^(ARTRealtime *realtime) {
        NSLog(@"------- %@", expectation);
        _realtime = realtime;
        ARTRealtimeChannel *channel = [realtime.channels get:channelName];
        [expectation fulfill];
    }];
    [self waitForExpectationsWithTimeout:[ARTTestUtil timeout] handler:nil];
    NSLog(@"------- %@", expectation);
}
2016-03-17 08:25:34.155 xctest[59800:9879713] ------- testSubscribeToName
2016-03-17 08:25:34.156 xctest[59800:9879713] ------- testSubscribeToName

@tcard
Copy link
Contributor

tcard commented Mar 17, 2016

Ah, OK, of course, self should be retaining a reference to the expectation so that waitForExpectationsWithTimeout works. Sorry, just wanted to understand what's going on. LGTM then!

ricardopereira added a commit that referenced this pull request Mar 17, 2016
Test suite: avoid calling the `fulfill` after the wait context has ended
@ricardopereira ricardopereira merged commit 9024e47 into master Mar 17, 2016
@ricardopereira ricardopereira deleted the fix-tests-api-violation branch March 17, 2016 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants