Skip to content

Commit 7e7b82e

Browse files
Addressing review comments
1 parent 7280673 commit 7e7b82e

File tree

5 files changed

+201
-274
lines changed

5 files changed

+201
-274
lines changed

Firestore/Example/Tests/Integration/FSTStreamTests.m

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#import <XCTest/XCTest.h>
1818

19-
#import <FirebaseCommunity/FIRLogger.h>
2019
#import <Firestore/FIRFirestoreSettings.h>
2120

2221
#import "Auth/FSTEmptyCredentialsProvider.h"
@@ -42,61 +41,61 @@ @interface FSTStreamStatusDelegate : NSObject <FSTWatchStreamDelegate, FSTWriteS
4241
@property(nonatomic, readonly) NSMutableArray<NSString *> *states;
4342
@property(atomic, readwrite) BOOL invokeCallbacks;
4443
@property(nonatomic, weak) XCTestExpectation *expectation;
45-
@property(nonatomic, weak, readonly) FSTStream *stream;
4644

47-
- (instancetype)initWithStream:(FSTStream *)stream NS_DESIGNATED_INITIALIZER;
48-
- (instancetype)init NS_UNAVAILABLE;
4945
@end
5046

5147
@implementation FSTStreamStatusDelegate
5248

53-
- (instancetype)initWithStream:(FSTStream *)stream {
49+
- (instancetype)init {
5450
if (self = [super init]) {
5551
_states = [NSMutableArray new];
56-
_stream = stream;
5752
}
5853

5954
return self;
6055
}
6156

62-
- (void)streamDidReceiveChange:(FSTWatchChange *)change
63-
snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
64-
[_states addObject:@"didReceiveChange"];
57+
- (void)watchStreamDidOpen {
58+
[_states addObject:@"watchStreamDidOpen"];
6559
[_expectation fulfill];
6660
_expectation = nil;
6761
}
6862

69-
- (void)streamDidOpen {
70-
[_states addObject:@"didOpen"];
63+
- (void)writeStreamDidOpen {
64+
[_states addObject:@"writeStreamDidOpen"];
7165
[_expectation fulfill];
7266
_expectation = nil;
7367
}
7468

75-
- (void)streamDidClose:(NSError *_Nullable)error {
76-
[_states addObject:@"didClose"];
69+
- (void)writeStreamDidCompleteHandshake {
70+
[_states addObject:@"writeStreamDidCompleteHandshake"];
7771
[_expectation fulfill];
7872
_expectation = nil;
7973
}
8074

81-
- (void)streamDidCompleteHandshake {
82-
[_states addObject:@"didCompleteHandshake"];
75+
- (void)writeStreamWasInterrupted:(NSError *_Nullable)error {
76+
[_states addObject:@"writeStreamWasInterrupted"];
8377
[_expectation fulfill];
8478
_expectation = nil;
8579
}
8680

87-
- (void)streamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
88-
mutationResults:(NSArray<FSTMutationResult *> *)results {
89-
[_states addObject:@"didReceiveResponse"];
81+
- (void)watchStreamWasInterrupted:(NSError *_Nullable)error {
82+
[_states addObject:@"watchStreamWasInterrupted"];
9083
[_expectation fulfill];
9184
_expectation = nil;
9285
}
9386

94-
- (void)writeValue:(id)value {
95-
[self.stream writeValue:value];
87+
- (void)watchStreamDidChange:(FSTWatchChange *)change
88+
snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
89+
[_states addObject:@"watchStreamDidChange"];
90+
[_expectation fulfill];
91+
_expectation = nil;
9692
}
9793

98-
- (void)writesFinishedWithError:(NSError *)errorOrNil {
99-
[self.stream writesFinishedWithError:errorOrNil];
94+
- (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
95+
mutationResults:(NSArray<FSTMutationResult *> *)results {
96+
[_states addObject:@"writeStreamDidReceiveResponseWithVersion"];
97+
[_expectation fulfill];
98+
_expectation = nil;
10099
}
101100

102101
- (void)fulfillOnCallback:(XCTestExpectation *)expectation {
@@ -113,9 +112,9 @@ @interface FSTStreamTests : XCTestCase
113112
@implementation FSTStreamTests {
114113
dispatch_queue_t _testQueue;
115114
FSTDatabaseInfo *_databaseInfo;
116-
FSTTestDispatchQueue *_workerDispatchQueue;
117115
FSTEmptyCredentialsProvider *_credentials;
118116
FSTStreamStatusDelegate *_delegate;
117+
FSTTestDispatchQueue *_workerDispatchQueue;
119118

120119
/** Single mutation to send to the write stream. */
121120
NSArray<FSTMutation *> *_mutations;
@@ -149,20 +148,16 @@ - (FSTWriteStream *)setUpWriteStream {
149148
workerDispatchQueue:_workerDispatchQueue
150149
credentials:_credentials];
151150

152-
FSTWriteStream *stream = [datastore createWriteStream];
153-
_delegate = [[FSTStreamStatusDelegate alloc] initWithStream:stream];
154-
155-
return stream;
151+
_delegate = [FSTStreamStatusDelegate new];
152+
return [datastore createWriteStream];
156153
}
157154

158155
- (FSTWatchStream *)setUpWatchStream {
159156
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:_databaseInfo
160157
workerDispatchQueue:_workerDispatchQueue
161158
credentials:_credentials];
162-
FSTWatchStream *stream = [datastore createWatchStream];
163-
_delegate = [[FSTStreamStatusDelegate alloc] initWithStream:stream];
164-
165-
return stream;
159+
_delegate = [FSTStreamStatusDelegate new];
160+
return [datastore createWatchStream];
166161
}
167162

168163
- (void)verifyDelegate:(NSArray<NSString *> *)expectedStates {
@@ -193,7 +188,7 @@ - (void)testWatchStreamStopBeforeHandshake {
193188
// Simulate a final callback from GRPC
194189
[watchStream writesFinishedWithError:nil];
195190

196-
[self verifyDelegate:@[ @"didOpen" ]];
191+
[self verifyDelegate:@[ @"watchStreamDidOpen" ]];
197192
}
198193

199194
/** Verifies that the write stream does not issue an onClose callback after a call to stop(). */
@@ -218,7 +213,7 @@ - (void)testWriteStreamStopBeforeHandshake {
218213
// Simulate a final callback from GRPC
219214
[writeStream writesFinishedWithError:nil];
220215

221-
[self verifyDelegate:@[ @"didOpen" ]];
216+
[self verifyDelegate:@[ @"writeStreamDidOpen" ]];
222217
}
223218

224219
- (void)testWriteStreamStopAfterHandshake {
@@ -255,7 +250,7 @@ - (void)testWriteStreamStopAfterHandshake {
255250
[writeStream stop];
256251
}];
257252

258-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didReceiveResponse" ]];
253+
[self verifyDelegate:@[ @"writeStreamDidOpen", @"writeStreamDidCompleteHandshake", @"writeStreamDidReceiveResponseWithVersion" ]];
259254
}
260255

261256
- (void)testStreamClosesWhenIdle {
@@ -286,7 +281,7 @@ - (void)testStreamClosesWhenIdle {
286281
XCTAssertFalse([writeStream isOpen]);
287282
});
288283

289-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didClose" ]];
284+
[self verifyDelegate:@[ @"writeStreamDidOpen", @"writeStreamDidCompleteHandshake", @"writeStreamWasInterrupted" ]];
290285
}
291286

292287
- (void)testStreamCancelsIdleOnWrite {
@@ -327,7 +322,7 @@ - (void)testStreamCancelsIdleOnWrite {
327322
[writeStream stop];
328323
}];
329324

330-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didReceiveResponse" ]];
325+
[self verifyDelegate:@[ @"writeStreamDidOpen", @"writeStreamDidCompleteHandshake", @"writeStreamDidReceiveResponseWithVersion" ]];
331326
}
332327

333328
@end

Firestore/Example/Tests/SpecTests/FSTMockDatastore.m

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ - (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
5151
@property(nonatomic, strong, readonly)
5252
NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *activeTargets;
5353

54-
@property(nonatomic, strong, readonly) id<FSTWatchStreamDelegate> delegate;
55-
5654
@end
5755

5856
@implementation FSTMockWatchStream
5957

60-
@synthesize delegate = _delegate;
61-
6258
- (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
6359
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
6460
credentials:(id<FSTCredentialsProvider>)credentials
@@ -79,12 +75,12 @@ - (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
7975
- (void)start:(id<FSTWatchStreamDelegate>)delegate {
8076
FSTAssert(!self.open, @"Trying to start already started watch stream");
8177
self.open = YES;
82-
_delegate = delegate;
83-
[self.delegate streamDidOpen];
78+
self.delegate = delegate;
79+
[self.delegate watchStreamDidOpen];
8480
}
8581

8682
- (void)stop {
87-
_delegate = nil;
83+
self.delegate = nil;
8884
}
8985

9086
- (BOOL)isOpen {
@@ -95,8 +91,8 @@ - (BOOL)isStarted {
9591
return self.open;
9692
}
9793

98-
- (void)streamDidOpen {
99-
[self.delegate streamDidOpen];
94+
- (void)notifyWatchStreamDidOpen {
95+
[self.delegate watchStreamDidOpen];
10096
}
10197

10298
- (void)watchQuery:(FSTQueryData *)query {
@@ -115,7 +111,7 @@ - (void)unwatchTargetID:(FSTTargetID)targetID {
115111

116112
- (void)failStreamWithError:(NSError *)error {
117113
self.open = NO;
118-
[self.delegate streamDidClose:error];
114+
[self.delegate watchStreamWasInterrupted:error];
119115
}
120116

121117
#pragma mark - Helper methods.
@@ -135,7 +131,7 @@ - (void)writeWatchChange:(FSTWatchChange *)change snapshotVersion:(FSTSnapshotVe
135131
}
136132
}
137133
}
138-
[self.delegate streamDidReceiveChange:change snapshotVersion:snap];
134+
[self.delegate watchStreamDidChange:change snapshotVersion:snap];
139135
}
140136

141137
@end
@@ -156,14 +152,10 @@ - (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
156152

157153
@property(nonatomic, assign) BOOL open;
158154
@property(nonatomic, strong, readonly) NSMutableArray<NSArray<FSTMutation *> *> *sentMutations;
159-
@property(nonatomic, strong, readonly) id<FSTWriteStreamDelegate> delegate;
160-
161155
@end
162156

163157
@implementation FSTMockWriteStream
164158

165-
@synthesize delegate = _delegate;
166-
167159
- (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
168160
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
169161
credentials:(id<FSTCredentialsProvider>)credentials
@@ -184,12 +176,12 @@ - (void)start:(id<FSTWriteStreamDelegate>)delegate {
184176
FSTAssert(!self.open, @"Trying to start already started write stream");
185177
self.open = YES;
186178
[self.sentMutations removeAllObjects];
187-
_delegate = delegate;
188-
[self.delegate streamDidOpen];
179+
self.delegate = delegate;
180+
[self.delegate writeStreamDidOpen];
189181
}
190182

191183
- (void)stop {
192-
_delegate = nil;
184+
self.delegate = nil;
193185
}
194186

195187
- (BOOL)isOpen {
@@ -202,29 +194,29 @@ - (BOOL)isStarted {
202194

203195
- (void)writeHandshake {
204196
self.handshakeComplete = YES;
205-
[self.delegate streamDidCompleteHandshake];
197+
[self.delegate writeStreamDidCompleteHandshake];
206198
}
207199

208200
- (void)writeMutations:(NSArray<FSTMutation *> *)mutations {
209201
[self.sentMutations addObject:mutations];
210202
}
211203

212-
- (void)streamDidOpen {
213-
[self.delegate streamDidOpen];
204+
- (void)notifyStreamDidOpen {
205+
[self.delegate writeStreamDidOpen];
214206
}
215207

216208
#pragma mark - Helper methods.
217209

218210
/** Injects a write ack as though it had come from the backend in response to a write. */
219211
- (void)ackWriteWithVersion:(FSTSnapshotVersion *)commitVersion
220212
mutationResults:(NSArray<FSTMutationResult *> *)results {
221-
[self.delegate streamDidReceiveResponseWithVersion:commitVersion mutationResults:results];
213+
[self.delegate writeStreamDidReceiveResponseWithVersion:commitVersion mutationResults:results];
222214
}
223215

224216
/** Injects a failed write response as though it had come from the backend. */
225217
- (void)failStreamWithError:(NSError *)error {
226218
self.open = NO;
227-
[self.delegate streamDidClose:error];
219+
[self.delegate writeStreamWasInterrupted:error];
228220
}
229221

230222
/**

0 commit comments

Comments
 (0)