|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | import { describeSpec, specTest } from './describe_spec';
|
19 |
| -import { client } from './spec_builder'; |
| 19 | +import { client, spec } from './spec_builder'; |
20 | 20 | import { TimerId } from '../../../src/util/async_queue';
|
21 | 21 | import { Query } from '../../../src/core/query';
|
22 |
| -import { path } from '../../util/helpers'; |
| 22 | +import { doc, path } from '../../util/helpers'; |
23 | 23 |
|
24 |
| -describeSpec( |
25 |
| - 'Persistence Recovery', |
26 |
| - ['durable-persistence', 'no-ios', 'no-android'], |
27 |
| - () => { |
28 |
| - specTest( |
29 |
| - 'Write is acknowledged by primary client (with recovery)', |
30 |
| - ['multi-client'], |
31 |
| - () => { |
32 |
| - return ( |
33 |
| - client(0) |
34 |
| - .expectPrimaryState(true) |
35 |
| - .client(1) |
36 |
| - .expectPrimaryState(false) |
37 |
| - .userSets('collection/a', { v: 1 }) |
38 |
| - .failDatabase() |
39 |
| - .client(0) |
40 |
| - .writeAcks('collection/a', 1, { expectUserCallback: false }) |
41 |
| - .client(1) |
42 |
| - // Client 1 has received the WebStorage notification that the write |
43 |
| - // has been acknowledged, but failed to process the change. Hence, |
44 |
| - // we did not get a user callback. We schedule the first retry and |
45 |
| - // make sure that it also does not get processed until |
46 |
| - // `recoverDatabase` is called. |
47 |
| - .runTimer(TimerId.AsyncQueueRetry) |
48 |
| - .recoverDatabase() |
49 |
| - .runTimer(TimerId.AsyncQueueRetry) |
50 |
| - .expectUserCallbacks({ |
51 |
| - acknowledged: ['collection/a'] |
52 |
| - }) |
53 |
| - ); |
54 |
| - } |
55 |
| - ); |
| 24 | +describeSpec('Persistence Recovery', ['no-ios', 'no-android'], () => { |
| 25 | + specTest( |
| 26 | + 'Write is acknowledged by primary client (with recovery)', |
| 27 | + ['multi-client'], |
| 28 | + () => { |
| 29 | + return ( |
| 30 | + client(0) |
| 31 | + .expectPrimaryState(true) |
| 32 | + .client(1) |
| 33 | + .expectPrimaryState(false) |
| 34 | + .userSets('collection/a', { v: 1 }) |
| 35 | + .failDatabase() |
| 36 | + .client(0) |
| 37 | + .writeAcks('collection/a', 1, { expectUserCallback: false }) |
| 38 | + .client(1) |
| 39 | + // Client 1 has received the WebStorage notification that the write |
| 40 | + // has been acknowledged, but failed to process the change. Hence, |
| 41 | + // we did not get a user callback. We schedule the first retry and |
| 42 | + // make sure that it also does not get processed until |
| 43 | + // `recoverDatabase` is called. |
| 44 | + .runTimer(TimerId.AsyncQueueRetry) |
| 45 | + .recoverDatabase() |
| 46 | + .runTimer(TimerId.AsyncQueueRetry) |
| 47 | + .expectUserCallbacks({ |
| 48 | + acknowledged: ['collection/a'] |
| 49 | + }) |
| 50 | + ); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + specTest( |
| 55 | + 'Query raises events in secondary client (with recovery)', |
| 56 | + ['multi-client'], |
| 57 | + () => { |
| 58 | + const query = Query.atPath(path('collection')); |
56 | 59 |
|
57 |
| - specTest( |
58 |
| - 'Query raises events in secondary client (with recovery)', |
59 |
| - ['multi-client'], |
60 |
| - () => { |
61 |
| - const query = Query.atPath(path('collection')); |
| 60 | + return client(0) |
| 61 | + .expectPrimaryState(true) |
| 62 | + .client(1) |
| 63 | + .expectPrimaryState(false) |
| 64 | + .userListens(query) |
| 65 | + .failDatabase() |
| 66 | + .client(0) |
| 67 | + .expectListen(query) |
| 68 | + .watchAcksFull(query, 1000) |
| 69 | + .client(1) |
| 70 | + .recoverDatabase() |
| 71 | + .runTimer(TimerId.AsyncQueueRetry) |
| 72 | + .expectEvents(query, {}); |
| 73 | + } |
| 74 | + ); |
62 | 75 |
|
63 |
| - return client(0) |
| 76 | + specTest( |
| 77 | + 'Query is listened to by primary (with recovery)', |
| 78 | + ['multi-client'], |
| 79 | + () => { |
| 80 | + const query = Query.atPath(path('collection')); |
| 81 | + |
| 82 | + return ( |
| 83 | + client(0) |
64 | 84 | .expectPrimaryState(true)
|
| 85 | + .failDatabase() |
65 | 86 | .client(1)
|
66 |
| - .expectPrimaryState(false) |
67 | 87 | .userListens(query)
|
68 |
| - .failDatabase() |
69 | 88 | .client(0)
|
| 89 | + // The primary client 0 receives a WebStorage notification about the |
| 90 | + // new query, but it cannot load the target from IndexedDB. The |
| 91 | + // query will only be listened to once we recover the database. |
| 92 | + .recoverDatabase() |
| 93 | + .runTimer(TimerId.AsyncQueueRetry) |
70 | 94 | .expectListen(query)
|
71 |
| - .watchAcksFull(query, 1000) |
| 95 | + .failDatabase() |
72 | 96 | .client(1)
|
| 97 | + .userUnlistens(query) |
| 98 | + .client(0) |
| 99 | + // The primary client 0 receives a notification that the query can |
| 100 | + // be released, but it can only process the change after we recover |
| 101 | + // the database. |
| 102 | + .expectActiveTargets({ query }) |
73 | 103 | .recoverDatabase()
|
74 | 104 | .runTimer(TimerId.AsyncQueueRetry)
|
75 |
| - .expectEvents(query, {}); |
76 |
| - } |
77 |
| - ); |
| 105 | + .expectActiveTargets() |
| 106 | + ); |
| 107 | + } |
| 108 | + ); |
78 | 109 |
|
79 |
| - specTest( |
80 |
| - 'Query is listened to by primary (with recovery)', |
81 |
| - ['multi-client'], |
82 |
| - () => { |
83 |
| - const query = Query.atPath(path('collection')); |
| 110 | + specTest('Recovers when write cannot be persisted', [], () => { |
| 111 | + return spec() |
| 112 | + .userSets('collection/key1', { foo: 'a' }) |
| 113 | + .expectNumOutstandingWrites(1) |
| 114 | + .failDatabase() |
| 115 | + .userSets('collection/key2', { bar: 'b' }) |
| 116 | + .expectUserCallbacks({ rejected: ['collection/key2'] }) |
| 117 | + .recoverDatabase() |
| 118 | + .expectNumOutstandingWrites(1) |
| 119 | + .userSets('collection/key3', { baz: 'c' }) |
| 120 | + .expectNumOutstandingWrites(2) |
| 121 | + .writeAcks('collection/key1', 1) |
| 122 | + .writeAcks('collection/key3', 2) |
| 123 | + .expectNumOutstandingWrites(0); |
| 124 | + }); |
84 | 125 |
|
85 |
| - return ( |
86 |
| - client(0) |
87 |
| - .expectPrimaryState(true) |
88 |
| - .failDatabase() |
89 |
| - .client(1) |
90 |
| - .userListens(query) |
91 |
| - .client(0) |
92 |
| - // The primary client 0 receives a WebStorage notification about the |
93 |
| - // new query, but it cannot load the target from IndexedDB. The |
94 |
| - // query will only be listened to once we recover the database. |
95 |
| - .recoverDatabase() |
96 |
| - .runTimer(TimerId.AsyncQueueRetry) |
97 |
| - .expectListen(query) |
98 |
| - .failDatabase() |
99 |
| - .client(1) |
100 |
| - .userUnlistens(query) |
101 |
| - .client(0) |
102 |
| - // The primary client 0 receives a notification that the query can |
103 |
| - // be released, but it can only process the change after we recover |
104 |
| - // the database. |
105 |
| - .expectActiveTargets({ query }) |
106 |
| - .recoverDatabase() |
107 |
| - .runTimer(TimerId.AsyncQueueRetry) |
108 |
| - .expectActiveTargets() |
109 |
| - ); |
110 |
| - } |
| 126 | + specTest('Does not surface non-persisted writes', [], () => { |
| 127 | + const query = Query.atPath(path('collection')); |
| 128 | + const doc1Local = doc( |
| 129 | + 'collection/key1', |
| 130 | + 0, |
| 131 | + { foo: 'a' }, |
| 132 | + { hasLocalMutations: true } |
| 133 | + ); |
| 134 | + const doc1 = doc('collection/key1', 1, { foo: 'a' }); |
| 135 | + const doc3Local = doc( |
| 136 | + 'collection/key3', |
| 137 | + 0, |
| 138 | + { foo: 'c' }, |
| 139 | + { hasLocalMutations: true } |
111 | 140 | );
|
112 |
| - } |
113 |
| -); |
| 141 | + const doc3 = doc('collection/key3', 2, { foo: 'c' }); |
| 142 | + return spec() |
| 143 | + .userListens(query) |
| 144 | + .userSets('collection/key1', { foo: 'a' }) |
| 145 | + .expectEvents(query, { |
| 146 | + added: [doc1Local], |
| 147 | + fromCache: true, |
| 148 | + hasPendingWrites: true |
| 149 | + }) |
| 150 | + .failDatabase() |
| 151 | + .userSets('collection/key2', { foo: 'b' }) |
| 152 | + .expectUserCallbacks({ rejected: ['collection/key2'] }) |
| 153 | + .recoverDatabase() |
| 154 | + .userSets('collection/key3', { foo: 'c' }) |
| 155 | + .expectEvents(query, { |
| 156 | + added: [doc3Local], |
| 157 | + fromCache: true, |
| 158 | + hasPendingWrites: true |
| 159 | + }) |
| 160 | + .writeAcks('collection/key1', 1) |
| 161 | + .writeAcks('collection/key3', 2) |
| 162 | + .watchAcksFull(query, 2, doc1, doc3) |
| 163 | + .expectEvents(query, { metadata: [doc1, doc3] }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments