Skip to content

Commit

Permalink
EKSemaphore: improve reliability and semantics of counting semaphore …
Browse files Browse the repository at this point in the history
…test

See https://github.com/allending/Kiwi/issues/321. Adding verifiers from
multiple threads is not supported in Kiwi.

Also changed the semantics of the test case to be about waiting on a
non-boolean counting semaphore (i.e., with a value of 2, the third
thread to wait will block until the semaphore is signalled).
  • Loading branch information
Adam Sharp committed Jul 8, 2013
1 parent a37cf13 commit 7323d18
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Tests/EKSemaphoreSpec.m
Expand Up @@ -50,26 +50,19 @@
[[expectFutureValue(theValue(waitSuccess)) shouldEventually] beYes];
});

it(@"allows multiple threads to wait", ^{
it(@"blocks when the semaphore count is exceeded", ^{
EKSemaphore *countedSem = [EKSemaphore semaphoreWithValue:2];

NSMutableArray *startedFlags = @[@NO, @NO, @NO].mutableCopy;
NSMutableArray *finishedFlags = @[@NO, @NO, @NO].mutableCopy;
[queue addOperationWithBlock:^{
startedFlags[0] = @YES;
NSOperation *firstOperation = [NSBlockOperation blockOperationWithBlock:^{
[countedSem wait];
finishedFlags[0] = @YES;
}];
[queue addOperationWithBlock:^{
startedFlags[1] = @YES;
NSOperation *secondOperation = [NSBlockOperation blockOperationWithBlock:^{
[countedSem wait];
finishedFlags[1] = @YES;
}];

NSOperation *thirdOperation = [NSBlockOperation blockOperationWithBlock:^{
startedFlags[2] = @YES;
[[startedFlags should] equal:@[@YES, @YES, @YES]];

// as the semaphore value is 2, this will block until the
// semaphore is signalled
[countedSem wait];
Expand All @@ -81,10 +74,13 @@
[countedSem signal];
[countedSem signal];
}];
[queue addOperation:thirdOperation];

[firstOperation start];
[secondOperation start];
[queue addOperation:thirdOperation]; // execute the third operation asynchronously

// wait here until the third operation starts executing
while (!thirdOperation.isExecuting) continue;
[[expectFutureValue(theValue(thirdOperation.isExecuting)) shouldEventually] beYes];

// third operation has now started, but will not finish until
// the semaphore is signalled
Expand Down

0 comments on commit 7323d18

Please sign in to comment.