GPBConcurrencyTests.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #import "google/protobuf/UnittestObjc.pbobjc.h"
  33. static const int kNumThreads = 100;
  34. static const int kNumMessages = 100;
  35. // NOTE: Most of these tests don't "fail" in the sense that the XCTAsserts
  36. // trip. Rather, the asserts simply exercise the apis, and if there is
  37. // a concurancy issue, the NSAsserts in the runtime code fire and/or the
  38. // code just crashes outright.
  39. @interface ConcurrencyTests : GPBTestCase
  40. @end
  41. @implementation ConcurrencyTests
  42. - (NSArray *)createThreadsWithSelector:(SEL)selector object:(id)object {
  43. NSMutableArray *array = [NSMutableArray array];
  44. for (NSUInteger i = 0; i < kNumThreads; i++) {
  45. NSThread *thread =
  46. [[NSThread alloc] initWithTarget:self selector:selector object:object];
  47. [array addObject:thread];
  48. [thread release];
  49. }
  50. return array;
  51. }
  52. - (NSArray *)createMessagesWithType:(Class)msgType {
  53. NSMutableArray *array = [NSMutableArray array];
  54. for (NSUInteger i = 0; i < kNumMessages; i++) {
  55. [array addObject:[msgType message]];
  56. }
  57. return array;
  58. }
  59. - (void)startThreads:(NSArray *)threads {
  60. for (NSThread *thread in threads) {
  61. [thread start];
  62. }
  63. }
  64. - (void)joinThreads:(NSArray *)threads {
  65. for (NSThread *thread in threads) {
  66. while (![thread isFinished])
  67. ;
  68. }
  69. }
  70. - (void)readForeignMessage:(NSArray *)messages {
  71. for (NSUInteger i = 0; i < 10; i++) {
  72. for (TestAllTypes *message in messages) {
  73. XCTAssertEqual(message.optionalForeignMessage.c, 0);
  74. }
  75. }
  76. }
  77. - (void)testConcurrentReadOfUnsetMessageField {
  78. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  79. NSArray *threads =
  80. [self createThreadsWithSelector:@selector(readForeignMessage:)
  81. object:messages];
  82. [self startThreads:threads];
  83. [self joinThreads:threads];
  84. for (TestAllTypes *message in messages) {
  85. XCTAssertFalse(message.hasOptionalForeignMessage);
  86. }
  87. }
  88. - (void)readRepeatedInt32:(NSArray *)messages {
  89. for (int i = 0; i < 10; i++) {
  90. for (TestAllTypes *message in messages) {
  91. XCTAssertEqual([message.repeatedInt32Array count], (NSUInteger)0);
  92. }
  93. }
  94. }
  95. - (void)testConcurrentReadOfUnsetRepeatedIntField {
  96. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  97. NSArray *threads =
  98. [self createThreadsWithSelector:@selector(readRepeatedInt32:)
  99. object:messages];
  100. [self startThreads:threads];
  101. [self joinThreads:threads];
  102. for (TestAllTypes *message in messages) {
  103. XCTAssertEqual([message.repeatedInt32Array count], (NSUInteger)0);
  104. }
  105. }
  106. - (void)readRepeatedString:(NSArray *)messages {
  107. for (int i = 0; i < 10; i++) {
  108. for (TestAllTypes *message in messages) {
  109. XCTAssertEqual([message.repeatedStringArray count], (NSUInteger)0);
  110. }
  111. }
  112. }
  113. - (void)testConcurrentReadOfUnsetRepeatedStringField {
  114. NSArray *messages = [self createMessagesWithType:[TestAllTypes class]];
  115. NSArray *threads =
  116. [self createThreadsWithSelector:@selector(readRepeatedString:)
  117. object:messages];
  118. [self startThreads:threads];
  119. [self joinThreads:threads];
  120. for (TestAllTypes *message in messages) {
  121. XCTAssertEqual([message.repeatedStringArray count], (NSUInteger)0);
  122. }
  123. }
  124. - (void)readInt32Int32Map:(NSArray *)messages {
  125. for (int i = 0; i < 10; i++) {
  126. for (TestRecursiveMessageWithRepeatedField *message in messages) {
  127. XCTAssertEqual([message.iToI count], (NSUInteger)0);
  128. }
  129. }
  130. }
  131. - (void)testConcurrentReadOfUnsetInt32Int32MapField {
  132. NSArray *messages =
  133. [self createMessagesWithType:[TestRecursiveMessageWithRepeatedField class]];
  134. NSArray *threads =
  135. [self createThreadsWithSelector:@selector(readInt32Int32Map:)
  136. object:messages];
  137. [self startThreads:threads];
  138. [self joinThreads:threads];
  139. for (TestRecursiveMessageWithRepeatedField *message in messages) {
  140. XCTAssertEqual([message.iToI count], (NSUInteger)0);
  141. }
  142. }
  143. - (void)readStringStringMap:(NSArray *)messages {
  144. for (int i = 0; i < 10; i++) {
  145. for (TestRecursiveMessageWithRepeatedField *message in messages) {
  146. XCTAssertEqual([message.strToStr count], (NSUInteger)0);
  147. }
  148. }
  149. }
  150. - (void)testConcurrentReadOfUnsetStringStringMapField {
  151. NSArray *messages =
  152. [self createMessagesWithType:[TestRecursiveMessageWithRepeatedField class]];
  153. NSArray *threads =
  154. [self createThreadsWithSelector:@selector(readStringStringMap:)
  155. object:messages];
  156. [self startThreads:threads];
  157. [self joinThreads:threads];
  158. for (TestRecursiveMessageWithRepeatedField *message in messages) {
  159. XCTAssertEqual([message.strToStr count], (NSUInteger)0);
  160. }
  161. }
  162. - (void)readOptionalForeignMessageExtension:(NSArray *)messages {
  163. for (int i = 0; i < 10; i++) {
  164. for (TestAllExtensions *message in messages) {
  165. ForeignMessage *foreign =
  166. [message getExtension:[UnittestRoot optionalForeignMessageExtension]];
  167. XCTAssertEqual(foreign.c, 0);
  168. }
  169. }
  170. }
  171. - (void)testConcurrentReadOfUnsetExtensionField {
  172. NSArray *messages = [self createMessagesWithType:[TestAllExtensions class]];
  173. SEL sel = @selector(readOptionalForeignMessageExtension:);
  174. NSArray *threads = [self createThreadsWithSelector:sel object:messages];
  175. [self startThreads:threads];
  176. [self joinThreads:threads];
  177. GPBExtensionDescriptor *extension =
  178. [UnittestRoot optionalForeignMessageExtension];
  179. for (TestAllExtensions *message in messages) {
  180. XCTAssertFalse([message hasExtension:extension]);
  181. }
  182. }
  183. @end