From b39c9e499c2f99d0fc0a6488d47cc815ada86c54 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Mon, 2 Dec 2019 23:25:35 +0100 Subject: [PATCH] fix a concurrency issue in NSArray enumeration test --- .travis.yml | 1 - Tests/base/NSArray/blocks.m | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 042c9088d..6d85c0b65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,6 @@ matrix: env: LIBRARY_COMBO=gnu-gnu-gnu sudo: required before_install: - - ls /usr/local/bin - sudo apt-get -qq update - sudo apt-get install -y cmake pkg-config libgnutls28-dev libgmp-dev libffi-dev libicu-dev libxml2-dev libxslt1-dev libssl-dev libavahi-client-dev zlib1g-dev libblocksruntime-dev - > diff --git a/Tests/base/NSArray/blocks.m b/Tests/base/NSArray/blocks.m index 9112e39d5..ec8a20a9b 100644 --- a/Tests/base/NSArray/blocks.m +++ b/Tests/base/NSArray/blocks.m @@ -4,10 +4,12 @@ #import #import #import +#import #import static NSUInteger fooCount = 0; static NSUInteger lastIndex = NSNotFound; +static BOOL reverse = NO; int main() { START_SET("NSArray Blocks") @@ -16,10 +18,20 @@ int main() # endif # if __has_feature(blocks) NSAutoreleasePool *arp = [NSAutoreleasePool new]; - + NSLock *lock = [[[NSLock alloc] init] autorelease]; NSArray *array = [NSArray arrayWithObjects: @"foo", @"bar", @"foo", nil]; - void(^enumBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger index, BOOL *stop){ - if ([obj isEqual: @"foo"]){ fooCount++;} lastIndex = index;}; + void(^enumBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger index, BOOL *stop) { + [lock lock]; + if ([obj isEqual: @"foo"]) { + fooCount++; + } + if (lastIndex == NSNotFound) { + lastIndex = index; + } else { + lastIndex = reverse ? MIN(lastIndex, index) : MAX(lastIndex, index); + } + [lock unlock]; + }; [array enumerateObjectsUsingBlock: enumBlock]; PASS((2 == fooCount) && (lastIndex == 2), "Can forward enumerate array using a block"); @@ -31,9 +43,11 @@ int main() "Can forward enumerate array concurrently using a block"); fooCount = 0; lastIndex = NSNotFound; + reverse = YES; [array enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: enumBlock]; PASS((0 == lastIndex), "Can enumerate array in reverse using a block"); + reverse = NO; fooCount = 0; lastIndex = NSNotFound; enumBlock = ^(id obj, NSUInteger index, BOOL *stop){if ([obj isEqual: @"foo"]){