GPBConcurrencyTests.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBTestUtilities.h"
  31. #import "google/protobuf/Unittest.pbobjc.h"
  32. static const int kNumThreads = 100;
  33. static const int kNumMessages = 100;
  34. @interface ConcurrencyTests : GPBTestCase
  35. @end
  36. @implementation ConcurrencyTests
  37. - (NSArray *)createThreadsWithSelector:(SEL)selector object:(id)object {
  38. NSMutableArray *array = [NSMutableArray array];
  39. for (NSUInteger i = 0; i < kNumThreads; i++) {
  40. NSThread *thread =
  41. [[NSThread alloc] initWithTarget:self selector:selector object:object];
  42. [array addObject:thread];
  43. [thread release];
  44. }
  45. return array;
  46. }
  47. - (NSArray *)createMessagesWithType:(Class)msgType {
  48. NSMutableArray *array = [NSMutableArray array];
  49. for (NSUInteger i = 0; i < kNumMessages; i++) {
  50. [array addObject:[msgType message]];
  51. }
  52. return array;
  53. }
  54. - (void)startThreads:(NSArray *)threads {
  55. for (NSThread *thread in threads) {
  56. [thread start];
  57. }
  58. }
  59. - (void)joinThreads:(NSArray *)threads {
  60. for (NSThread *thread in threads) {
  61. while (![thread isFinished])
  62. ;
  63. }
  64. }
  65. - (void)readForeignMessage:(NSArray *)messages {
  66. for (NSUInteger i = 0; i < 10; i++) {
  67. for (TestAllTypes *message in messages) {
  68. XCTAssertEqual(message.optionalForeignMessage.c, 0);
  69. }
  70. }
  71. }
  72. - (void)testConcurrentReadOfUnsetMessageField {
  73. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  74. NSArray *threads =
  75. [self createThreadsWithSelector:@selector(readForeignMessage:)
  76. object:messages];
  77. [self startThreads:threads];
  78. [self joinThreads:threads];
  79. for (TestAllTypes *message in messages) {
  80. XCTAssertFalse(message.hasOptionalForeignMessage);
  81. }
  82. }
  83. - (void)readRepeatedInt32:(NSArray *)messages {
  84. for (int i = 0; i < 10; i++) {
  85. for (TestAllTypes *message in messages) {
  86. XCTAssertEqual([message.repeatedInt32Array count], (NSUInteger)0);
  87. }
  88. }
  89. }
  90. - (void)testConcurrentReadOfUnsetRepeatedIntField {
  91. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  92. NSArray *threads =
  93. [self createThreadsWithSelector:@selector(readRepeatedInt32:)
  94. object:messages];
  95. [self startThreads:threads];
  96. [self joinThreads:threads];
  97. for (TestAllTypes *message in messages) {
  98. XCTAssertEqual([message.repeatedInt32Array count], (NSUInteger)0);
  99. }
  100. }
  101. - (void)readRepeatedString:(NSArray *)messages {
  102. for (int i = 0; i < 10; i++) {
  103. for (TestAllTypes *message in messages) {
  104. XCTAssertEqual([message.repeatedStringArray count], (NSUInteger)0);
  105. }
  106. }
  107. }
  108. - (void)testConcurrentReadOfUnsetRepeatedStringField {
  109. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  110. NSArray *threads =
  111. [self createThreadsWithSelector:@selector(readRepeatedString:)
  112. object:messages];
  113. [self startThreads:threads];
  114. [self joinThreads:threads];
  115. for (TestAllTypes *message in messages) {
  116. XCTAssertEqual([message.repeatedStringArray count], (NSUInteger)0);
  117. }
  118. }
  119. - (void)readOptionalForeignMessageExtension:(NSArray *)messages {
  120. for (int i = 0; i < 10; i++) {
  121. for (TestAllExtensions *message in messages) {
  122. ForeignMessage *foreign =
  123. [message getExtension:[UnittestRoot optionalForeignMessageExtension]];
  124. XCTAssertEqual(foreign.c, 0);
  125. }
  126. }
  127. }
  128. - (void)testConcurrentReadOfUnsetExtensionField {
  129. NSArray *messages = [self createMessagesWithType:[TestAllExtensions class]];
  130. SEL sel = @selector(readOptionalForeignMessageExtension:);
  131. NSArray *threads = [self createThreadsWithSelector:sel object:messages];
  132. [self startThreads:threads];
  133. [self joinThreads:threads];
  134. GPBExtensionField *extension = [UnittestRoot optionalForeignMessageExtension];
  135. for (TestAllExtensions *message in messages) {
  136. XCTAssertFalse([message hasExtension:extension]);
  137. }
  138. }
  139. @end