aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Database/Tests/Integration/FPersist.m
blob: 9f8bf56215c84731b78fe6ada1e44b38e309e969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <XCTest/XCTest.h>
#import <Foundation/Foundation.h>
#import "FPersist.h"
#import "FIRDatabaseReference.h"
#import "FIRDatabaseReference_Private.h"
#import "FRepo_Private.h"
#import "FTestHelpers.h"
#import "FDevice.h"
#import "FIRDatabaseQuery_Private.h"

@implementation FPersist

- (void) setUp {
    [super setUp];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *baseDir = [FPersist getFirebaseDir];
    // HACK: We want to clean up old persistence files from previous test runs, but on OSX, baseDir is going to be something
    // like /Users/michael/Documents/firebase, and we probably shouldn't blindly delete it, since somebody might have actual
    // documents there.  We should probably change the directory where we store persistence on OSX to .firebase or something
    // to avoid colliding with real files, but for now, we'll leave it and just manually delete each of the /0, /1, /2, etc.
    // directories that may exist from previous test runs.  As of now (2014/09/07), these directories only go up to ~50, but
    // if we add a ton more tests, we may need to increase the 100.  But I'm guessing we'll rewrite persistence and move the
    // persistence folder before then though.
    for(int i = 0; i < 100; i++) {
        // TODO: This hack is uneffective because the format now follows different rules. Persistence really needs a purge
        // option
        NSString *dir = [NSString stringWithFormat:@"%@/%d", baseDir, i];
        if ([fileManager fileExistsAtPath:dir]) {
            NSError *error;
            [[NSFileManager defaultManager] removeItemAtPath:dir error:&error];
            if (error) {
                XCTFail(@"Failed to clear persisted data at %@: %@", dir, error);
            }
        }
    }
}

- (void) testSetIsResentAfterRestart {
    FIRDatabaseReference *readerRef = [FTestHelpers getRandomNode];
    NSString *url = [readerRef description];
    FDevice* device = [[FDevice alloc] initOfflineWithUrl:url];

    // Monitor the data at this location.
    __block FIRDataSnapshot *readSnapshot = nil;
    [readerRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
        readSnapshot = snapshot;
    }];

    // Do some sets while offline and then "kill" the app, so it doesn't get sent to Firebase.
    [device do:^(FIRDatabaseReference *ref) {
        [ref setValue:@{ @"a": @42, @"b": @3.1415, @"c": @"hello", @"d": @{ @"dd": @"dd-val", @".priority": @"d-pri"} }];
        [[ref child:@"a"] setValue:@"a-val"];
        [[ref child:@"c"] setPriority:@"c-pri"];
        [ref updateChildValues:@{ @"b": @"b-val"}];
    }];

    // restart and wait for "idle" (so all pending puts should have been sent).
    [device restartOnline];
    [device waitForIdleUsingWaiter:self];

    // Pending sets should have gone through.
    id expected = @{
            @"a": @"a-val",
            @"b": @"b-val",
            @"c": @{ @".value": @"hello", @".priority": @"c-pri" },
            @"d": @{ @"dd": @"dd-val", @".priority": @"d-pri" }
    };
    [self waitForExportValueOf:readerRef toBe:expected];

    // Set the value to something else (12).
    [readerRef setValue:@12];

    // "restart" the app again and make sure it doesn't set it to 42 again.
    [device restartOnline];
    [device waitForIdleUsingWaiter:self];

    // Make sure data is still 12.
    [self waitForRoundTrip:readerRef];
    XCTAssertEqual(readSnapshot.value, @12, @"Read data should still be 12.");
    [device dispose];
}

- (void) testSetIsReappliedAfterRestart {
    FDevice* device = [[FDevice alloc] initOffline];

    // Do some sets while offline and then "kill" the app, so it doesn't get sent to Firebase.
    [device do:^(FIRDatabaseReference *ref) {
        [ref setValue:@{ @"a": @42, @"b": @3.1415, @"c": @"hello" }];
        [[ref child:@"a"] setValue:@"a-val"];
        [[ref child:@"c"] setPriority:@"c-pri"];
        [ref updateChildValues:@{ @"b": @"b-val"}];
    }];

    // restart the app offline and observe the data.
    [device restartOffline];

    // Pending sets should be visible
    id expected = @{
            @"a": @"a-val",
            @"b": @"b-val",
            @"c": @{ @".value": @"hello", @".priority": @"c-pri" }
    };
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForExportValueOf:ref toBe:expected];
    }];
    [device dispose];
}

- (void) testServerDataCachedOffline1 {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];
    __block BOOL done = NO;
    id data = @{@"a": @1, @"b": @2};
    [writerRef setValue:data withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
        done = YES;
    }];
    WAIT_FOR(done);

    // Wait for the data to get it cached.
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:ref toBe:data];
    }];

    // Should still be there after restart, offline.
    [device restartOffline];
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:ref toBe:data];
    }];

    // Children should be there too.
    [device restartOffline];
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:[ref child:@"a"] toBe:@1];
    }];
    [device dispose];
}

- (void) testServerDataCompleteness1 {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];
    id data = @{@"child": @{@"a": @1, @"b": @2 }, @"other": @"blah"};
    [self waitForCompletionOf:writerRef setValue:data];

    // Wait for each child to get it cached (but not the parent).
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:[ref child:@"child/a"] toBe:@1];
        [self waitForValueOf:[ref child:@"child/b"] toBe:@2];
        [self waitForValueOf:[ref child:@"other"] toBe:@"blah"];
    }];

    // Restart, offline, should get child_added events, but not value.
    [device restartOffline];
    __block BOOL gotA, gotB;
    [device do:^(FIRDatabaseReference *ref) {
        FIRDatabaseReference *childRef = [ref child:@"child"];
        [childRef observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
            if ([snapshot.key isEqualToString:@"a"]) {
                XCTAssertEqualObjects(snapshot.value, @1, @"Got a");
                gotA = YES;
            } else if ([snapshot.key isEqualToString:@"b"]) {
                XCTAssertEqualObjects(snapshot.value, @2, @"Got a");
                gotB = YES;
            } else {
                XCTFail(@"Unexpected child event.");
            }
        }];

        // Listen for value events (which we should *not* get).
        [childRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            XCTFail(@"Got a value event with incomplete data!");
        }];

        // Wait for another location just to make sure we wait long enough that we /would/ get a value event if it
        // was coming.
        [self waitForValueOf:[ref child:@"other"] toBe:@"blah"];
    }];

    XCTAssertTrue(gotA && gotB, @"Got a and b.");
    [device dispose];
}

- (void) testServerDataCompleteness2 {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];
    id data = @{@"a": @1, @"b": @2};
    [self waitForCompletionOf:writerRef setValue:data];

    // Wait for the children individually.
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:[ref child:@"a"] toBe:@1];
        [self waitForValueOf:[ref child:@"b"] toBe:@2];
    }];

    // Should still be there after restart, offline.
    [device restartOffline];
    [device do:^(FIRDatabaseReference *ref) {
        [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            // No-op.  Just triggering a listen at this location.
        }];
        [self waitForValueOf:[ref child:@"a"] toBe:@1];
        [self waitForValueOf:[ref child:@"b"] toBe:@2];
    }];
    [device dispose];
}

- (void)testServerDataLimit {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];
    [self waitForCompletionOf:writerRef setValue:@{@"a": @1, @"b": @2, @"c": @3}];

    // Cache limit(2) of the data.
    [device do:^(FIRDatabaseReference *ref) {
        FIRDatabaseQuery *limitRef = [ref queryLimitedToLast:2];
        [self waitForValueOf:limitRef toBe:@{@"b": @2, @"c": @3 }];
    }];

    // We should be able to get limit(2) data offline, but not the whole node.
    [device restartOffline];
    [device do:^(FIRDatabaseReference *ref) {
        [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            XCTFail(@"Got value event for whole node!");
        }];

        FIRDatabaseQuery *limitRef = [ref queryLimitedToLast:2];
        [self waitForValueOf:limitRef toBe:@{@"b": @2, @"c": @3 }];
    }];
    [device dispose];
}

- (void)testRemoveWhileOfflineAndRestart {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];

    [[writerRef child:@"test"] setValue:@"test"];
    [device do:^(FIRDatabaseReference *ref) {
        // Cache this location.
        __block id val = nil;
        [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            val = snapshot.value;
        }];
        [self waitUntil:^BOOL {
            return [val isEqual:@{@"test": @"test"}];
        }];
    }];
    [device restartOffline];

    __block BOOL done = NO;
    [writerRef removeValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
        done = YES;
    }];
    WAIT_FOR(done);

    [device goOnline];
    [device waitForIdleUsingWaiter:self];
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:ref toBe:[NSNull null]];
    }];
    [device dispose];
}


- (void)testDeltaSyncAfterRestart {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];

    [writerRef setValue:@"test"];

    [device do:^(FIRDatabaseReference *ref) {
        // Cache this location.
        __block id val = nil;
        [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            val = snapshot.value;
        }];
        [self waitUntil:^BOOL {
            return [val isEqual:@"test"];
        }];
        XCTAssertEqual(ref.repo.dataUpdateCount, 1L, @"Should have gotten one update.");
    }];
    [device restartOnline];

    [device waitForIdleUsingWaiter:self];
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForValueOf:ref toBe:@"test"];
        XCTAssertEqual(ref.repo.dataUpdateCount, 0L, @"Should have gotten no updates.");
    }];
    [device dispose];
}

- (void)testDeltaSyncWorksWithUnfilteredQuery {
    FIRDatabaseReference *writerRef = [FTestHelpers getRandomNode];
    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writerRef description] ];

    // List must be large enough to trigger delta sync.
    NSMutableDictionary *longList = [[NSMutableDictionary alloc] init];
    for(NSInteger i = 0; i < 50; i++) {
        NSString *key = [[writerRef childByAutoId] key];
        longList[key] = @{ @"order": @1, @"text": @"This is an awesome message!" };
    }

    [writerRef setValue:longList];

    [device do:^(FIRDatabaseReference *ref) {
        // Cache this location.
        [self waitForValueOf:[ref queryOrderedByChild:@"order"] toBe:longList];
        XCTAssertEqual(ref.repo.dataUpdateCount, 1L, @"Should have gotten one update.");
    }];
    [device restartOffline];

    // Add a new child while the device is offline.
    FIRDatabaseReference *newChildRef = [writerRef childByAutoId];
    NSDictionary *newChild = @{ @"order": @50, @"text": @"This is a new appended child!" };

    [self waitForCompletionOf:newChildRef setValue:newChild];
    longList[[newChildRef key]] = newChild;

    [device goOnline];
    [device do:^(FIRDatabaseReference *ref) {
        // Wait for updated value with new child.
        [self waitForValueOf:[ref queryOrderedByChild:@"order"] toBe:longList];
        XCTAssertEqual(ref.repo.rangeMergeUpdateCount, 1L, @"Should have gotten a range merge update.");
    }];
    [device dispose];
}

- (void) testPutsAreRestoredInOrder {
    FDevice *device = [[FDevice alloc] initOffline];

    // Store puts which should have a putId with 10 which is lexiographical small than 9
    [device do:^(FIRDatabaseReference *ref) {
        for (int i = 0; i < 11; i++) {
            [ref setValue:[NSNumber numberWithInt:i]];
        }
    }];

    // restart the app offline and observe the data.
    [device restartOffline];

    // Make sure that the write with putId 10 wins, not 9
    id expected = @10;
    [device do:^(FIRDatabaseReference *ref) {
        [self waitForExportValueOf:ref toBe:expected];
    }];
    [device dispose];
}

- (void) testStoreSetsPerf1 {
    if (!runPerfTests) return;
    // Disable persistence in FDevice for comparison without persistence
    FDevice *device = [[FDevice alloc] initOnline];

    __block BOOL done = NO;
    [device do:^(FIRDatabaseReference *ref) {
        NSDate *start = [NSDate date];
        [self writeChildren:ref count:1000 size:100 waitForComplete:NO];

        [self waitForQueue:ref];

        NSLog(@"Elapsed: %f", [[NSDate date] timeIntervalSinceDate:start]);
        done = YES;
    }];

    WAIT_FOR(done);
    [device dispose];
}

- (void) testStoreListenPerf1 {
    if (!runPerfTests) return;
    // Disable persistence in FDevice for comparison without persistence

    // Write 1000 x 100-byte children, to read back.
    unsigned int count = 1000;
    FIRDatabaseReference *writer = [FTestHelpers getRandomNode];
    [self writeChildren:writer count:count size:100];

    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writer description]];

    __block BOOL done = NO;
    [device do:^(FIRDatabaseReference *ref) {
        NSDate *start = [NSDate date];
        [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            // Wait to make sure we're done persisting everything.
            [self waitForQueue:ref];
            XCTAssertEqual(snapshot.childrenCount, count, @"Got correct data.");
            NSLog(@"Elapsed: %f", [[NSDate date] timeIntervalSinceDate:start]);
            done = YES;
        }];
    }];

    WAIT_FOR(done);
    [device dispose];
}

- (void) testRestoreListenPerf1 {
    if (!runPerfTests) return;

    // NOTE: Since this is testing restoration of data from cache after restarting, it only works with persistence on.

    // Write 1000 * 100-byte children, to read back.
    unsigned int count = 1000;
    FIRDatabaseReference *writer = [FTestHelpers getRandomNode];
    [self writeChildren:writer count:count size:100];

    FDevice *device = [[FDevice alloc] initOnlineWithUrl:[writer description]];

    // Get the data cached.
    __block BOOL done = NO;
    [device do:^(FIRDatabaseReference *ref) {
        [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            XCTAssertEqual(snapshot.childrenCount, count, @"Got correct data.");
            done = YES;
        }];
    }];
    WAIT_FOR(done);

    // Restart offline and see how long it takes to restore the data from cache.
    [device restartOffline];
    done = NO;
    [device do:^(FIRDatabaseReference *ref) {
        NSDate *start = [NSDate date];
        [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
            // Wait to make sure we're done persisting everything.
            XCTAssertEqual(snapshot.childrenCount, count, @"Got correct data.");
            [self waitForQueue:ref];
            NSLog(@"Elapsed: %f", [[NSDate date] timeIntervalSinceDate:start]);
            done = YES;
        }];
    }];

    WAIT_FOR(done);
    [device dispose];
}

- (void)writeChildren:(FIRDatabaseReference *)writer count:(unsigned int)count size:(unsigned int)size {
    [self writeChildren:writer count:count size:size waitForComplete:YES];
}

- (void)writeChildren:(FIRDatabaseReference *)writer count:(unsigned int)count size:(unsigned int)size waitForComplete:(BOOL)waitForComplete {
    __block BOOL done = NO;

    NSString *data = [self randomStringOfLength:size];
    for(int i = 0; i < count; i++) {
        [[writer childByAutoId] setValue:data withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
            if (i == (count - 1)) {
                done = YES;
            }
        }];
    }
    if (waitForComplete) {
        WAIT_FOR(done);
    }
}

NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- (NSString*) randomStringOfLength:(unsigned int)len {
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];

    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }
    return randomString;
}

+ (NSString *) getFirebaseDir {
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [dirPaths objectAtIndex:0];
    NSString *firebaseDir = [documentsDir stringByAppendingPathComponent:@"firebase"];

    return firebaseDir;
}

@end