title: Unit tests for NSOperationQueue with maxConcurrentOperationCount
body: <p>I have a class which is a kind of wrapper for <code>NSOperationQueue</code>. It allows to enqueue network requests using blocks. Currently the requests are executed one after another, but this can be changed in the future.</p>
  
  <p>Here is the code of MyRequestsQueue class:</p>
  
  <pre><code>@interface MyRequestsQueue ()
  
  @property(nonatomic, strong) NSOperationQueue* queue;
  
  @end
  
  @implementation MyRequestsQueue
  
  -(instancetype)init
  {
      self = [super init];
      if(!self) {
          return nil;
      }
  
      self.queue = [[NSOperationQueue new] autorelease];
      self.queue.maxConcurrentOperationCount = 1;
  
      return self;
  }
  
  -(void)addRequestBlock:(void (^)())request
  {
      NSBlockOperation* operation = [NSBlockOperation blockOperationWithBlock:request];
      [self.queue addOperation:operation];
  }
  
  @end
  </code></pre>
  
  <p>In general, I know how to unit test asynchronous code using XCTest. But now I want to add a unit test for <code>MyRequestsQueue</code> that checks if queue executes only one operation at the time. Or even better - test that number of currently executing operations is not greater than <code>maxConcurrentOperationCount</code>. I tried to observe <code>operationCount</code> property of <code>self.queue</code>, but the <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/operationCount" rel="nofollow">documentation</a> says I shouldn't rely on it. How can I achieve it?</p>
  
  <p>EDIT: My tests use the following pattern: </p>
  
  <pre><code>@interface MessageRequestQueueTest : XCTestCase
  
  @property(nonatomic, strong) MessageRequestsQueue* reuqestQueue;
  @property(nonatomic, assign) NSInteger finishedRequestsCounter;
  
  @end
  // setUp method ommited - simply initializes self.requestQueue
  
  
  -(void)testAddedRequestIsExecuted
  {
      [self.reuqestQueue.queue setSuspended:YES];
  
      __weak __typeof(self) weakSelf = self;
      [self.reuqestQueue addRequestBlock:^{
          ++weakSelf.finishedRequestsCounter;
      } withName:kDummyRequestName];
  
      [self.reuqestQueue.queue setSuspended:NO];
  
      WAIT_WHILE(0 == self.finishedRequestsCounter, 0.1);
      XCTAssertEqual(self.finishedRequestsCounter, 1, @"request should be executed");
  }
  </code></pre>
  
  <p>the WAIT_WHILE macro is from <a href="https://github.com/hfossli/AGAsyncTestHelper" rel="nofollow">AGAsyncTestHelper</a>.</p>