FSTStreamTests.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Remote/FSTDatastore.h"
  17. #import <OCMock/OCMock.h>
  18. #import <XCTest/XCTest.h>
  19. #import "Auth/FSTEmptyCredentialsProvider.h"
  20. #import "Core/FSTDatabaseInfo.h"
  21. #import "Model/FSTDatabaseID.h"
  22. #import "Protos/objc/google/firestore/v1beta1/Firestore.pbrpc.h"
  23. #import "Util/FSTDispatchQueue.h"
  24. /** Expose otherwise private methods for testing. */
  25. @interface FSTStream (Testing)
  26. - (void)writesFinishedWithError:(NSError *_Nullable)error;
  27. @end
  28. @interface FSTStreamTests : XCTestCase
  29. @end
  30. @implementation FSTStreamTests {
  31. dispatch_queue_t _testQueue;
  32. FSTDatabaseInfo *_databaseInfo;
  33. FSTDispatchQueue *_workerDispatchQueue;
  34. id<FSTCredentialsProvider> _credentials;
  35. }
  36. - (void)setUp {
  37. [super setUp];
  38. FSTDatabaseID *databaseID =
  39. [FSTDatabaseID databaseIDWithProject:@"project" database:kDefaultDatabaseID];
  40. _databaseInfo = [FSTDatabaseInfo databaseInfoWithDatabaseID:databaseID
  41. persistenceKey:@"test"
  42. host:@"test-host"
  43. sslEnabled:NO];
  44. _testQueue = dispatch_queue_create("com.firebase.testing", DISPATCH_QUEUE_SERIAL);
  45. _workerDispatchQueue = [FSTDispatchQueue queueWith:_testQueue];
  46. _credentials = [[FSTEmptyCredentialsProvider alloc] init];
  47. }
  48. - (void)tearDown {
  49. [super tearDown];
  50. }
  51. - (void)testWatchStreamStop {
  52. id delegate = OCMStrictProtocolMock(@protocol(FSTWatchStreamDelegate));
  53. FSTWatchStream *stream =
  54. OCMPartialMock([[FSTWatchStream alloc] initWithDatabase:_databaseInfo
  55. workerDispatchQueue:_workerDispatchQueue
  56. credentials:_credentials
  57. responseMessageClass:[GCFSWriteResponse class]
  58. delegate:delegate]);
  59. OCMStub([stream createRPCWithRequestsWriter:[OCMArg any]]).andReturn(nil);
  60. // Start the stream up but that's not really the interesting bit. This is complicated by the fact
  61. // that startup involves redispatching after credentials are returned.
  62. dispatch_semaphore_t openCompleted = dispatch_semaphore_create(0);
  63. OCMStub([delegate watchStreamDidOpen]).andDo(^(NSInvocation *invocation) {
  64. dispatch_semaphore_signal(openCompleted);
  65. });
  66. dispatch_async(_testQueue, ^{
  67. [stream start];
  68. });
  69. dispatch_semaphore_wait(openCompleted, DISPATCH_TIME_FOREVER);
  70. OCMVerifyAll(delegate);
  71. // Stop must not call watchStreamDidClose because the full implementation of the delegate could
  72. // attempt to restart the stream in the event it had pending watches.
  73. dispatch_sync(_testQueue, ^{
  74. [stream stop];
  75. });
  76. OCMVerifyAll(delegate);
  77. // Simulate a final callback from GRPC
  78. [stream writesFinishedWithError:nil];
  79. // Drain queue
  80. dispatch_sync(_testQueue, ^{
  81. });
  82. OCMVerifyAll(delegate);
  83. }
  84. - (void)testWriteStreamStop {
  85. id delegate = OCMStrictProtocolMock(@protocol(FSTWriteStreamDelegate));
  86. FSTWriteStream *stream =
  87. OCMPartialMock([[FSTWriteStream alloc] initWithDatabase:_databaseInfo
  88. workerDispatchQueue:_workerDispatchQueue
  89. credentials:_credentials
  90. responseMessageClass:[GCFSWriteResponse class]
  91. delegate:delegate]);
  92. OCMStub([stream createRPCWithRequestsWriter:[OCMArg any]]).andReturn(nil);
  93. // Start the stream up but that's not really the interesting bit.
  94. dispatch_semaphore_t openCompleted = dispatch_semaphore_create(0);
  95. OCMStub([delegate writeStreamDidOpen]).andDo(^(NSInvocation *invocation) {
  96. dispatch_semaphore_signal(openCompleted);
  97. });
  98. dispatch_async(_testQueue, ^{
  99. [stream start];
  100. });
  101. dispatch_semaphore_wait(openCompleted, DISPATCH_TIME_FOREVER);
  102. OCMVerifyAll(delegate);
  103. // Stop must not call writeStreamDidClose because the full implementation of this delegate could
  104. // attempt to restart the stream in the event it had pending writes.
  105. dispatch_sync(_testQueue, ^{
  106. [stream stop];
  107. });
  108. OCMVerifyAll(delegate);
  109. // Simulate a final callback from GRPC
  110. [stream writesFinishedWithError:nil];
  111. // Drain queue
  112. dispatch_sync(_testQueue, ^{
  113. });
  114. OCMVerifyAll(delegate);
  115. }
  116. @end