Skip to content

Commit 589d398

Browse files
committed
test(e2e): consolidate cloud helper function calling
there were two places this was defined, and I just added 429 / quota-exceeded retries to one, both could use it though so focus all cloud helper function usage through a single code path
1 parent 7c862f8 commit 589d398

File tree

3 files changed

+45
-60
lines changed

3 files changed

+45
-60
lines changed

packages/remote-config/e2e/config.e2e.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*
1616
*/
1717

18-
const { updateTemplate } = require('./helpers');
19-
2018
describe('remoteConfig()', function () {
2119
describe('firebase v8 compatibility', function () {
2220
beforeEach(async function beforeEachTest() {
@@ -782,7 +780,7 @@ describe('remoteConfig()', function () {
782780

783781
// If there is orphaned data, delete it on the server and let that settle
784782
if (staleDeletes.length > 0) {
785-
const response = await updateTemplate({
783+
const response = await FirebaseHelpers.updateRemoteConfigTemplate({
786784
operations: {
787785
delete: staleDeletes,
788786
},
@@ -797,7 +795,7 @@ describe('remoteConfig()', function () {
797795

798796
after(async function () {
799797
// clean up our own test data after the whole suite runs
800-
const response = await updateTemplate({
798+
const response = await FirebaseHelpers.updateRemoteConfigTemplate({
801799
operations: {
802800
delete: ['rttest1' + timestamp, 'rttest2' + timestamp, 'rttest3' + timestamp],
803801
},
@@ -823,7 +821,7 @@ describe('remoteConfig()', function () {
823821
const unsubscribe = onConfigUpdated(config, (event, error) => callback(event, error));
824822
unsubscribers.push(unsubscribe);
825823
// Update the template using our cloud function, so our listeners are called
826-
let response = await updateTemplate({
824+
let response = await FirebaseHelpers.updateRemoteConfigTemplate({
827825
operations: {
828826
add: [{ name: 'rttest1' + timestamp, value: timestamp }],
829827
},
@@ -858,7 +856,7 @@ describe('remoteConfig()', function () {
858856
unsubscribers.push(unsubscribe3);
859857

860858
// Trigger an update that should call them all
861-
let response = await updateTemplate({
859+
let response = await FirebaseHelpers.updateRemoteConfigTemplate({
862860
operations: {
863861
add: [{ name: 'rttest1' + timestamp, value: Date.now() + '' }],
864862
},
@@ -887,7 +885,7 @@ describe('remoteConfig()', function () {
887885
const callback2Count = callback2.callCount;
888886

889887
// Trigger update that should call listener 1 and 3 for these values
890-
response = await updateTemplate({
888+
response = await FirebaseHelpers.updateRemoteConfigTemplate({
891889
operations: {
892890
add: [{ name: 'rttest2' + timestamp, value: Date.now() + '' }],
893891
},
@@ -916,7 +914,7 @@ describe('remoteConfig()', function () {
916914
const callback3Count = callback3.callCount;
917915

918916
// Trigger an update that should call no listeners
919-
response = await updateTemplate({
917+
response = await FirebaseHelpers.updateRemoteConfigTemplate({
920918
operations: {
921919
add: [{ name: 'rttest3' + timestamp, value: Date.now() + '' }],
922920
},

packages/remote-config/e2e/helpers.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/globals.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import * as mlModular from '@react-native-firebase/ml';
8989
import { Platform } from 'react-native';
9090
import NativeEventEmitter from '@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter';
9191
import { getReactNativeModule } from '@react-native-firebase/app/lib/internal/nativeModule';
92+
import { getE2eTestProject } from '@react-native-firebase/app/e2e/helpers';
9293

9394
global.should = shouldMatchers;
9495
global.sinon = require('sinon');
@@ -181,25 +182,47 @@ global.FirebaseHelpers = {
181182
};
182183
},
183184
},
184-
async fetchAppCheckToken() {
185-
const tokenRequest = await fetch(
186-
'https://us-central1-react-native-firebase-testing.cloudfunctions.net/fetchAppCheckTokenV2',
187-
{
188-
method: 'POST',
189-
headers: {
190-
'Content-Type': 'application/json',
185+
async callCloudHelperFunction(functioNName, postData) {
186+
let doc = undefined;
187+
let retries = 0;
188+
let maxRetries = 5;
189+
// We handle 429 errors in a retry loop
190+
while ((doc === undefined || doc.status === 429) && retries < maxRetries) {
191+
doc = await fetch(
192+
`https://us-central1-${getE2eTestProject()}.cloudfunctions.net/${functioNName}`,
193+
{
194+
method: 'POST',
195+
body: JSON.stringify({ data: postData }),
196+
headers: { 'Content-Type': 'application/json' },
191197
},
192-
body: JSON.stringify({
193-
data: {
194-
appId: global.FirebaseHelpers.app.config().appId,
195-
},
196-
}),
197-
redirect: 'follow',
198-
},
199-
);
200-
const { result } = await tokenRequest.json();
198+
);
199+
if (doc.status === 429) {
200+
// We have been delayed by concurrency limits or rate limits
201+
// We'll sleep for a little bit then try again.
202+
const delayRequired = 10;
203+
await new Promise(r => setTimeout(r, delayRequired * 1000));
204+
}
205+
retries++;
206+
}
207+
208+
// did we eventually have success? If not, error.
209+
if (retries === maxRetries && doc.status !== 200) {
210+
throw new Error('Unable to execute cloud remote config helper function');
211+
}
212+
const result = await doc.json();
213+
// console.error('received: ' + JSON.stringify(result));
214+
return result;
215+
},
216+
async fetchAppCheckToken() {
217+
const tokenRequset = await this.callCloudHelperFunction('fetchAppCheckTokenV2', {
218+
appId: global.FirebaseHelpers.app.config().appId,
219+
});
220+
const { result } = tokenRequset;
201221
return result;
202222
},
223+
async updateRemoteConfigTemplate(operations) {
224+
return await this.callCloudHelperFunction('testFunctionRemoteConfigUpdateV2', operations);
225+
},
203226
};
204227

205228
global.android = {

0 commit comments

Comments
 (0)