@@ -5,207 +5,177 @@ import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/gener
5
5
6
6
const UPLOAD_TIMEOUT_MS = 3000 ;
7
7
8
- describe (
9
- 'Streaming' ,
10
- {
11
- sequential : true ,
12
- timeout : 30_000
13
- } ,
14
- ( ) => {
15
- /**
16
- * Declares a test to be executed with different generated db functions
17
- */
18
- const itWithGenerators = (
19
- name : string ,
20
- test : ( createConnectedDatabase : ( ) => ReturnType < typeof generateConnectedDatabase > ) => Promise < void >
21
- ) => {
22
- const funcWithWebWorker = generateConnectedDatabase ;
23
- const funcWithoutWebWorker = ( ) =>
24
- generateConnectedDatabase ( {
25
- powerSyncOptions : {
26
- dbFilename : 'test-stream-connection-no-worker.db' ,
27
- flags : {
28
- useWebWorker : false
29
- }
8
+ describe ( 'Streaming' , ( ) => {
9
+ /**
10
+ * Declares a test to be executed with different generated db functions
11
+ */
12
+ const itWithGenerators = (
13
+ name : string ,
14
+ test : ( createConnectedDatabase : ( ) => ReturnType < typeof generateConnectedDatabase > ) => Promise < void >
15
+ ) => {
16
+ const funcWithWebWorker = generateConnectedDatabase ;
17
+ const funcWithoutWebWorker = ( ) =>
18
+ generateConnectedDatabase ( {
19
+ powerSyncOptions : {
20
+ dbFilename : 'test-stream-connection-no-worker.db' ,
21
+ flags : {
22
+ useWebWorker : false
30
23
}
31
- } ) ;
32
-
33
- it . sequential ( `${ name } - with web worker` , ( ) => test ( funcWithWebWorker ) ) ;
34
- it . sequential ( `${ name } - without web worker` , ( ) => test ( funcWithoutWebWorker ) ) ;
35
- } ;
36
-
37
- beforeAll ( ( ) => Logger . useDefaults ( ) ) ;
38
-
39
- let connectionUtilities : ConnectedDatabaseUtils | null = null ;
40
-
41
- afterEach ( async ( ) => {
42
- await connectionUtilities ?. dispose ( ) ;
43
- connectionUtilities = null ;
44
- } ) ;
45
-
46
- itWithGenerators ( 'PowerSync reconnect on closed stream' , async ( createConnectedDatabase ) => {
47
- connectionUtilities = await createConnectedDatabase ( ) ;
48
- const { powersync, waitForStream, remote } = connectionUtilities ;
49
-
50
- expect ( powersync . connected ) . toBe ( true ) ;
51
-
52
- // Close the stream
53
- const newStream = waitForStream ( ) ;
54
- remote . streamController ?. close ( ) ;
55
-
56
- // A new stream should be requested
57
- await newStream ;
58
- } ) ;
59
-
60
- itWithGenerators ( 'PowerSync reconnect multiple connect calls' , async ( createConnectedDatabase ) => {
61
- // This initially performs a connect call
62
- connectionUtilities = await createConnectedDatabase ( ) ;
63
- const { powersync, waitForStream, remote } = connectionUtilities ;
64
- expect ( powersync . connected ) . toBe ( true ) ;
65
-
66
- // Call connect again, a new stream should be requested
67
- const newStream = waitForStream ( ) ;
68
- powersync . connect ( new TestConnector ( ) ) ;
69
-
70
- // A new stream should be requested
71
- await newStream ;
72
- } ) ;
73
-
74
- itWithGenerators ( 'Should trigger upload connector when connected' , async ( createConnectedDatabase ) => {
75
- console . log ( 'connecting' ) ;
76
- connectionUtilities = await createConnectedDatabase ( ) ;
77
- const { powersync, uploadSpy } = connectionUtilities ;
78
- console . log ( 'connected' ) ;
79
- expect ( powersync . connected ) . toBe ( true ) ;
80
-
81
- // do something which should trigger an upload
82
- await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
83
- console . log ( 'executed a statement' ) ;
84
- // It should try and upload
85
- await vi . waitFor (
86
- ( ) => {
87
- // to-have-been-called seems to not work after failing the first check
88
- console . log ( 'number of calls, ' , uploadSpy . mock . calls . length ) ;
89
- expect ( uploadSpy . mock . calls . length ) . equals ( 1 ) ;
90
- } ,
91
- {
92
- timeout : UPLOAD_TIMEOUT_MS ,
93
- interval : 500
94
24
}
95
- ) ;
96
- } ) ;
97
-
98
- itWithGenerators ( 'Should retry failed uploads when connected' , async ( createConnectedDatabase ) => {
99
- console . log ( 'connecting' ) ;
100
- connectionUtilities = await createConnectedDatabase ( ) ;
101
- console . log ( 'connected' ) ;
102
- const { powersync, uploadSpy } = connectionUtilities ;
25
+ } ) ;
103
26
104
- expect ( powersync . connected ) . toBe ( true ) ;
27
+ it ( `${ name } - with web worker` , ( ) => test ( funcWithWebWorker ) ) ;
28
+ it ( `${ name } - without web worker` , ( ) => test ( funcWithoutWebWorker ) ) ;
29
+ } ;
30
+
31
+ beforeAll ( ( ) => Logger . useDefaults ( ) ) ;
32
+
33
+ let connectionUtilities : ConnectedDatabaseUtils | null = null ;
34
+
35
+ afterEach ( async ( ) => {
36
+ await connectionUtilities ?. dispose ( ) ;
37
+ connectionUtilities = null ;
38
+ } ) ;
39
+
40
+ itWithGenerators ( 'PowerSync reconnect on closed stream' , async ( createConnectedDatabase ) => {
41
+ connectionUtilities = await createConnectedDatabase ( ) ;
42
+ const { powersync, waitForStream, remote } = connectionUtilities ;
43
+
44
+ expect ( powersync . connected ) . toBe ( true ) ;
45
+
46
+ // Close the stream
47
+ const newStream = waitForStream ( ) ;
48
+ remote . streamController ?. close ( ) ;
49
+
50
+ // A new stream should be requested
51
+ await newStream ;
52
+ } ) ;
53
+
54
+ itWithGenerators ( 'PowerSync reconnect multiple connect calls' , async ( createConnectedDatabase ) => {
55
+ // This initially performs a connect call
56
+ connectionUtilities = await createConnectedDatabase ( ) ;
57
+ const { powersync, waitForStream, remote } = connectionUtilities ;
58
+ expect ( powersync . connected ) . toBe ( true ) ;
59
+
60
+ // Call connect again, a new stream should be requested
61
+ const newStream = waitForStream ( ) ;
62
+ powersync . connect ( new TestConnector ( ) ) ;
63
+
64
+ // A new stream should be requested
65
+ await newStream ;
66
+ } ) ;
67
+
68
+ itWithGenerators ( 'Should trigger upload connector when connected' , async ( createConnectedDatabase ) => {
69
+ connectionUtilities = await createConnectedDatabase ( ) ;
70
+ const { powersync, uploadSpy } = connectionUtilities ;
71
+ expect ( powersync . connected ) . toBe ( true ) ;
72
+
73
+ // do something which should trigger an upload
74
+ await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
75
+ // It should try and upload
76
+ await vi . waitFor (
77
+ ( ) => {
78
+ // to-have-been-called seems to not work after failing the first check
79
+ expect ( uploadSpy . mock . calls . length ) . equals ( 1 ) ;
80
+ } ,
81
+ {
82
+ timeout : UPLOAD_TIMEOUT_MS ,
83
+ interval : 500
84
+ }
85
+ ) ;
86
+ } ) ;
87
+
88
+ itWithGenerators ( 'Should retry failed uploads when connected' , async ( createConnectedDatabase ) => {
89
+ connectionUtilities = await createConnectedDatabase ( ) ;
90
+ const { powersync, uploadSpy } = connectionUtilities ;
91
+
92
+ expect ( powersync . connected ) . toBe ( true ) ;
93
+
94
+ let uploadCounter = 0 ;
95
+ // This test will throw an exception a few times before uploading
96
+ const throwCounter = 2 ;
97
+ uploadSpy . mockImplementation ( async ( db ) => {
98
+ if ( uploadCounter ++ < throwCounter ) {
99
+ throw new Error ( 'No uploads yet' ) ;
100
+ }
101
+ // Now actually do the upload
102
+ const tx = await db . getNextCrudTransaction ( ) ;
103
+ await tx ?. complete ( ) ;
104
+ } ) ;
105
105
106
- let uploadCounter = 0 ;
107
- // This test will throw an exception a few times before uploading
108
- const throwCounter = 2 ;
106
+ // do something which should trigger an upload
107
+ await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
108
+
109
+ // It should try and upload
110
+ await vi . waitFor (
111
+ ( ) => {
112
+ // to-have-been-called seems to not work after failing a check
113
+ expect ( uploadSpy . mock . calls . length ) . equals ( throwCounter + 1 ) ;
114
+ } ,
115
+ {
116
+ timeout : UPLOAD_TIMEOUT_MS ,
117
+ interval : 500
118
+ }
119
+ ) ;
120
+ } ) ;
121
+
122
+ itWithGenerators ( 'Should upload after reconnecting' , async ( createConnectedDatabase ) => {
123
+ connectionUtilities = await createConnectedDatabase ( ) ;
124
+ const { powersync, connect, uploadSpy } = connectionUtilities ;
125
+ expect ( powersync . connected ) . toBe ( true ) ;
126
+
127
+ await powersync . disconnect ( ) ;
128
+
129
+ // do something (offline) which should trigger an upload
130
+ await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
131
+
132
+ await connect ( ) ;
133
+
134
+ // It should try and upload
135
+ await vi . waitFor (
136
+ ( ) => {
137
+ // to-have-been-called seems to not work after failing a check
138
+ expect ( uploadSpy . mock . calls . length ) . equals ( 1 ) ;
139
+ } ,
140
+ {
141
+ timeout : UPLOAD_TIMEOUT_MS ,
142
+ interval : 500
143
+ }
144
+ ) ;
145
+ } ) ;
146
+
147
+ itWithGenerators ( 'Should update status when uploading' , async ( createConnectedDatabase ) => {
148
+ connectionUtilities = await createConnectedDatabase ( ) ;
149
+ const { powersync, uploadSpy } = connectionUtilities ;
150
+
151
+ expect ( powersync . connected ) . toBe ( true ) ;
152
+
153
+ let uploadStartedPromise = new Promise < void > ( ( resolve ) => {
109
154
uploadSpy . mockImplementation ( async ( db ) => {
110
- if ( uploadCounter ++ < throwCounter ) {
111
- throw new Error ( 'No uploads yet' ) ;
112
- }
113
- console . log ( 'allowing the upload' ) ;
155
+ resolve ( ) ;
114
156
// Now actually do the upload
115
157
const tx = await db . getNextCrudTransaction ( ) ;
116
158
await tx ?. complete ( ) ;
117
159
} ) ;
118
-
119
- console . log ( 'creating an item' ) ;
120
- // do something which should trigger an upload
121
- await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
122
-
123
- console . log ( 'done creating an item' ) ;
124
-
125
- // It should try and upload
126
- await vi . waitFor (
127
- ( ) => {
128
- // to-have-been-called seems to not work after failing a check
129
- console . log ( 'the number of calls is ' , uploadSpy . mock . calls . length ) ;
130
- expect ( uploadSpy . mock . calls . length ) . equals ( throwCounter + 1 ) ;
131
- } ,
132
- {
133
- timeout : UPLOAD_TIMEOUT_MS ,
134
- interval : 500
135
- }
136
- ) ;
137
- } ) ;
138
-
139
- itWithGenerators ( 'Should upload after reconnecting' , async ( createConnectedDatabase ) => {
140
- console . log ( 'connecting' ) ;
141
- connectionUtilities = await createConnectedDatabase ( ) ;
142
- const { powersync, connect, uploadSpy } = connectionUtilities ;
143
- console . log ( 'connected' ) ;
144
- expect ( powersync . connected ) . toBe ( true ) ;
145
-
146
- console . log ( 'disconnecting' ) ;
147
- await powersync . disconnect ( ) ;
148
- console . log ( 'disconnected' ) ;
149
-
150
- // do something (offline) which should trigger an upload
151
- await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
152
-
153
- console . log ( 'connecting again' ) ;
154
- await connect ( ) ;
155
- console . log ( 'done connecting' ) ;
156
-
157
- // It should try and upload
158
- await vi . waitFor (
159
- ( ) => {
160
- // to-have-been-called seems to not work after failing a check
161
- console . log ( 'upload has been called times: ' , uploadSpy . mock . calls . length ) ;
162
- expect ( uploadSpy . mock . calls . length ) . equals ( 1 ) ;
163
- } ,
164
- {
165
- timeout : UPLOAD_TIMEOUT_MS ,
166
- interval : 500
167
- }
168
- ) ;
169
160
} ) ;
170
161
171
- itWithGenerators ( 'Should update status when uploading' , async ( createConnectedDatabase ) => {
172
- connectionUtilities = await createConnectedDatabase ( ) ;
173
- const { powersync, uploadSpy } = connectionUtilities ;
174
-
175
- expect ( powersync . connected ) . toBe ( true ) ;
176
-
177
- console . log ( 'database connected' ) ;
178
-
179
- let uploadStartedPromise = new Promise < void > ( ( resolve ) => {
180
- uploadSpy . mockImplementation ( async ( db ) => {
181
- resolve ( ) ;
182
- // Now actually do the upload
183
- const tx = await db . getNextCrudTransaction ( ) ;
184
- await tx ?. complete ( ) ;
185
- } ) ;
186
- } ) ;
187
-
188
- // do something which should trigger an upload
189
- await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
190
-
191
- console . log ( 'created item' ) ;
192
- await uploadStartedPromise ;
193
- console . log ( 'upload started' ) ;
194
-
195
- expect ( powersync . currentStatus . dataFlowStatus . uploading ) . true ;
196
-
197
- // Status should update after uploads are completed
198
- await vi . waitFor (
199
- ( ) => {
200
- // to-have-been-called seems to not work after failing a check
201
- console . log ( 'upload status is' , powersync . currentStatus . dataFlowStatus . uploading ) ;
202
- expect ( powersync . currentStatus . dataFlowStatus . uploading ) . false ;
203
- } ,
204
- {
205
- timeout : UPLOAD_TIMEOUT_MS ,
206
- interval : 500
207
- }
208
- ) ;
209
- } ) ;
210
- }
211
- ) ;
162
+ // do something which should trigger an upload
163
+ await powersync . execute ( 'INSERT INTO users (id, name) VALUES (uuid(), ?)' , [ 'name' ] ) ;
164
+
165
+ await uploadStartedPromise ;
166
+
167
+ expect ( powersync . currentStatus . dataFlowStatus . uploading ) . true ;
168
+
169
+ // Status should update after uploads are completed
170
+ await vi . waitFor (
171
+ ( ) => {
172
+ // to-have-been-called seems to not work after failing a check
173
+ expect ( powersync . currentStatus . dataFlowStatus . uploading ) . false ;
174
+ } ,
175
+ {
176
+ timeout : UPLOAD_TIMEOUT_MS ,
177
+ interval : 500
178
+ }
179
+ ) ;
180
+ } ) ;
181
+ } ) ;
0 commit comments