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

Make expectation recording thread-safe. #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions Source/OCMock/OCMockObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -408,44 +408,40 @@ - (BOOL)handleInvocation:(NSInvocation *)anInvocation
[self assertInvocationsArrayIsPresent];
[self addInvocation:anInvocation];

OCMInvocationStub *stub = [self stubForInvocation:anInvocation];
if(stub == nil)
return NO;
OCMInvocationStub *stub = nil;
@synchronized(stubs)
{
stub = [self stubForInvocation:anInvocation];
if(stub == nil)
return NO;

// Retain the stub in case it ends up being removed because we still need it at the end for handleInvocation:
[stub retain];
// Retain the stub in case it ends up being removed because we still need it at the end for handleInvocation:
[stub retain];

BOOL removeStub = NO;
@synchronized(expectations)
{
if([expectations containsObject:stub])
@synchronized(expectations)
{
OCMInvocationExpectation *expectation = [self _nextExpectedInvocation];
if(expectationOrderMatters && (expectation != stub))
if([expectations containsObject:stub])
{
[NSException raise:NSInternalInconsistencyException
format:@"%@: unexpected method invoked: %@\n\texpected:\t%@",
[self description], [stub description], [[expectations objectAtIndex:0] description]];
}
OCMInvocationExpectation *expectation = [self _nextExpectedInvocation];
if(expectationOrderMatters && (expectation != stub))
{
[NSException raise:NSInternalInconsistencyException
format:@"%@: unexpected method invoked: %@\n\texpected:\t%@",
[self description], [stub description], [[expectations objectAtIndex:0] description]];
}

// We can't check isSatisfied yet, since the stub won't be satisfied until we call
// handleInvocation: since we'll still have the current expectation in the expectations array, which
// will cause an exception if expectationOrderMatters is YES and we're not ready for any future
// expected methods to be called yet
if(![(OCMInvocationExpectation *)stub isMatchAndReject])
{
[expectations removeObject:stub];
removeStub = YES;
// We can't check isSatisfied yet, since the stub won't be satisfied until we call
// handleInvocation: since we'll still have the current expectation in the expectations array, which
// will cause an exception if expectationOrderMatters is YES and we're not ready for any future
// expected methods to be called yet
if(![(OCMInvocationExpectation *)stub isMatchAndReject])
{
[expectations removeObject:stub];
[stubs removeObject:stub];
}
}
}
}
if(removeStub)
{
@synchronized(stubs)
{
[stubs removeObject:stub];
}
}

@try
{
Expand Down
19 changes: 19 additions & 0 deletions Source/OCMockTests/OCMockObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,23 @@ - (void)testTryingToCreateAnInstanceOfOCMockObjectRaisesAnException
XCTAssertThrows([[OCMockObject alloc] init]);
}

#pragma mark thread safety

- (void)testExpectationsAreThreadSafe
{
size_t concurrentTaskCount = 10000;

for (size_t i = 0; i < concurrentTaskCount; i++)
{
[[mock expect] lowercaseString];
}

dispatch_apply(concurrentTaskCount, dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^(size_t iteration)
{
[mock lowercaseString];
});

[mock verify];
}

@end