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

Added isEnabled getters for OHHTTPStubs. #159

Merged
merged 4 commits into from
Mar 13, 2016

Conversation

jzucker2
Copy link
Contributor

@jzucker2 jzucker2 commented Mar 8, 2016

Reflects current implementation. This branch does not update the version or run pod update for the examples. There is a minor bug in Cocoapods for building projects for different platforms. Not sure how you build your projects so I can add that to the branch when I know what to do. This is in response to #139

Reflects current implementation
@@ -149,9 +149,11 @@ +(void)removeAllStubs

#pragma mark > Disabling & Re-Enabling stubs

static BOOL currentEnabledState = NO;
Copy link
Owner

Choose a reason for hiding this comment

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

global variables are not thread safe, so this is not an acceptable solution. You could instead add an atomic property on the shared instance for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion!

@AliSoftware
Copy link
Owner

What's the CP issue/error message you're encountering?

@jzucker2
Copy link
Contributor Author

jzucker2 commented Mar 9, 2016

This is a thread about the issue. Supposedly fixed in the next beta. Definitely present in 0.39 CocoaPods/CocoaPods#4478

@AliSoftware
Copy link
Owner

Oh I see. Never had that issue before, it passed when running OHHTTPStubs tests on CI before which runs the checks for each platform, so will take a look later 👍

(PS: a bit busy this week so I might not be able to test this PR until this weekend)

@jzucker2
Copy link
Contributor Author

jzucker2 commented Mar 9, 2016

No rush on this pull request. Just saw an opportunity to help and wanted to contribute.

@AliSoftware
Copy link
Owner

👍

@jzucker2
Copy link
Contributor Author

jzucker2 commented Mar 9, 2016

There's not a ton of great documentation on @synchronized, seems it's slightly out of favor. I tried to follow your pattern in this implementation. Also open to using a NSLock or a mutex, if you'd prefer. Just figured I'd try out your original suggestion first.

-(BOOL)isEnabled
{
BOOL enabled = NO;
@synchronized(self)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using self instead of _enabledStateNumber because that might change and cause multiple locks to be enabled for this property

@AliSoftware
Copy link
Owner

Yeah, OHHTTPStubs starts having history, that code was written a while back when @synchronized was the way to go. Nowadays if I were to re-do it, I'd probably use NSLock or OSSpinLock or something. But as this is not the purpose of this PR, I think it's better to keep @synhronized here for now. We can still decide to make the switch for NSLock or OSSpinLock in a separate and dedicated PR if we feel the need, but that's a separate issue 😉

@@ -44,6 +44,7 @@ @interface OHHTTPStubsProtocol : NSURLProtocol @end
@interface OHHTTPStubs()
+ (instancetype)sharedInstance;
@property(atomic, copy) NSMutableArray* stubDescriptors;
@property(atomic, copy) NSNumber* enabledStateNumber;
Copy link
Owner

Choose a reason for hiding this comment

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

Why not using a BOOL, as it will be initialized to YES at init time so you don't really need the YES/NO/nil tri-state anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that seems obvious now. I originally wrote it intending to do @synchronized(_enabledState) when I didn't fully understand the @synchronized() directive. Anyways, updated with another commit.

{
NSMutableArray * urlProtocolClasses = [NSMutableArray arrayWithArray:sessionConfig.protocolClasses];
Class protoCls = OHHTTPStubsProtocol.class;
if ([urlProtocolClasses containsObject:protoCls])
Copy link
Owner

Choose a reason for hiding this comment

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

Why not directly return [urlProtocolClasses containsObject:protoCls];?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

@jzucker2
Copy link
Contributor Author

Fixed!

@AliSoftware
Copy link
Owner

Awesome, thanks again for the PR 👌

AliSoftware added a commit that referenced this pull request Mar 13, 2016
Added isEnabled getters for OHHTTPStubs.
@AliSoftware AliSoftware merged commit cd1020a into AliSoftware:master Mar 13, 2016
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