@@ -20,26 +20,30 @@ import { SettingsService } from './settings_service';
20
20
import { CONFIG_EXPIRY_LOCAL_STORAGE_KEY } from '../constants' ;
21
21
import { setupApi , Api } from './api_service' ;
22
22
import * as iidService from './iid_service' ;
23
- import { getConfig } from './remote_config_service' ;
23
+ import { getConfig , isDestFl } from './remote_config_service' ;
24
24
import { FirebaseApp } from '@firebase/app-types' ;
25
25
import '../../test/setup' ;
26
26
27
27
describe ( 'Performance Monitoring > remote_config_service' , ( ) => {
28
28
const IID = 'asd123' ;
29
29
const AUTH_TOKEN = 'auth_token' ;
30
30
const LOG_URL = 'https://firebaselogging.test.com' ;
31
+ const TRANSPORT_KEY = 'pseudo-transport-key' ;
31
32
const LOG_SOURCE = 2 ;
32
33
const NETWORK_SAMPLIG_RATE = 0.25 ;
33
34
const TRACE_SAMPLING_RATE = 0.5 ;
34
35
const GLOBAL_CLOCK_NOW = 1556524895326 ;
35
36
const STRINGIFIED_CONFIG = `{"entries":{"fpr_enabled":"true",\
36
- "fpr_log_endpoint_url":"https://firebaselogging.test.com",\
37
- "fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
38
- "fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
39
- "state":"UPDATE"}` ;
37
+ "fpr_log_endpoint_url":"https://firebaselogging.test.com",\
38
+ "fpr_log_transport_key":"pseudo-transport-key",\
39
+ "fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
40
+ "fpr_log_transport_web_percent":"100.0",\
41
+ "fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
42
+ "state":"UPDATE"}` ;
40
43
const PROJECT_ID = 'project1' ;
41
44
const APP_ID = '1:23r:web:fewq' ;
42
45
const API_KEY = 'asdfghjk' ;
46
+ const NOT_VALID_CONFIG = 'not a valid config and should not be used' ;
43
47
44
48
let clock : SinonFakeTimers ;
45
49
@@ -127,7 +131,11 @@ describe('Performance Monitoring > remote_config_service', () => {
127
131
expect ( getItemStub ) . to . be . called ;
128
132
expect ( SettingsService . getInstance ( ) . loggingEnabled ) . to . be . true ;
129
133
expect ( SettingsService . getInstance ( ) . logEndPointUrl ) . to . equal ( LOG_URL ) ;
134
+ expect ( SettingsService . getInstance ( ) . transportKey ) . to . equal (
135
+ TRANSPORT_KEY
136
+ ) ;
130
137
expect ( SettingsService . getInstance ( ) . logSource ) . to . equal ( LOG_SOURCE ) ;
138
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . true ;
131
139
expect (
132
140
SettingsService . getInstance ( ) . networkRequestsSamplingRate
133
141
) . to . equal ( NETWORK_SAMPLIG_RATE ) ;
@@ -164,7 +172,11 @@ describe('Performance Monitoring > remote_config_service', () => {
164
172
expect ( getItemStub ) . to . be . calledOnce ;
165
173
expect ( SettingsService . getInstance ( ) . loggingEnabled ) . to . be . true ;
166
174
expect ( SettingsService . getInstance ( ) . logEndPointUrl ) . to . equal ( LOG_URL ) ;
175
+ expect ( SettingsService . getInstance ( ) . transportKey ) . to . equal (
176
+ TRANSPORT_KEY
177
+ ) ;
167
178
expect ( SettingsService . getInstance ( ) . logSource ) . to . equal ( LOG_SOURCE ) ;
179
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . true ;
168
180
expect (
169
181
SettingsService . getInstance ( ) . networkRequestsSamplingRate
170
182
) . to . equal ( NETWORK_SAMPLIG_RATE ) ;
@@ -180,7 +192,7 @@ describe('Performance Monitoring > remote_config_service', () => {
180
192
setup (
181
193
{
182
194
expiry : EXPIRY_LOCAL_STORAGE_VALUE ,
183
- config : 'not a valid config and should not be used'
195
+ config : NOT_VALID_CONFIG
184
196
} ,
185
197
{ reject : true }
186
198
) ;
@@ -201,7 +213,7 @@ describe('Performance Monitoring > remote_config_service', () => {
201
213
setup (
202
214
{
203
215
expiry : EXPIRY_LOCAL_STORAGE_VALUE ,
204
- config : 'not a valid config and should not be used'
216
+ config : NOT_VALID_CONFIG
205
217
} ,
206
218
{ reject : false , value : new Response ( STRINGIFIED_PARTIAL_CONFIG ) }
207
219
) ;
@@ -214,18 +226,145 @@ describe('Performance Monitoring > remote_config_service', () => {
214
226
it ( 'uses secondary configs if the response does not have any fields' , async ( ) => {
215
227
// Expired local config.
216
228
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320' ;
217
- const STRINGIFIED_PARTIAL_CONFIG = '{"state":"NO TEMPLATE "}' ;
229
+ const STRINGIFIED_PARTIAL_CONFIG = '{"state":"NO_TEMPLATE "}' ;
218
230
219
231
setup (
220
232
{
221
233
expiry : EXPIRY_LOCAL_STORAGE_VALUE ,
222
- config : 'not a valid config and should not be used'
234
+ config : NOT_VALID_CONFIG
223
235
} ,
224
236
{ reject : false , value : new Response ( STRINGIFIED_PARTIAL_CONFIG ) }
225
237
) ;
226
238
await getConfig ( IID ) ;
227
239
228
240
expect ( SettingsService . getInstance ( ) . loggingEnabled ) . to . be . true ;
229
241
} ) ;
242
+
243
+ it ( 'marks event destination to cc if there is no template' , async ( ) => {
244
+ setup (
245
+ {
246
+ // Expired local config.
247
+ expiry : '1556524895320' ,
248
+ config : NOT_VALID_CONFIG
249
+ } ,
250
+ { reject : false , value : new Response ( '{"state":"NO_TEMPLATE"}' ) }
251
+ ) ;
252
+ await getConfig ( IID ) ;
253
+
254
+ // If no template, will send to cc.
255
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . false ;
256
+ } ) ;
257
+
258
+ it ( 'marks event destination to cc if instance state unspecified' , async ( ) => {
259
+ setup (
260
+ {
261
+ // Expired local config.
262
+ expiry : '1556524895320' ,
263
+ config : NOT_VALID_CONFIG
264
+ } ,
265
+ {
266
+ reject : false ,
267
+ value : new Response ( '{"state":"INSTANCE_STATE_UNSPECIFIED"}' )
268
+ }
269
+ ) ;
270
+ await getConfig ( IID ) ;
271
+
272
+ // If instance state unspecified, will send to cc.
273
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . false ;
274
+ } ) ;
275
+
276
+ it ( "marks event destination to cc if state doesn't exist" , async ( ) => {
277
+ setup (
278
+ {
279
+ // Expired local config.
280
+ expiry : '1556524895320' ,
281
+ config : NOT_VALID_CONFIG
282
+ } ,
283
+ { reject : false , value : new Response ( '{}' ) }
284
+ ) ;
285
+ await getConfig ( IID ) ;
286
+
287
+ // If "state" doesn't exist, will send to cc.
288
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . false ;
289
+ } ) ;
290
+
291
+ it ( 'marks event destination to Fl if template exists but no rollout flag' , async ( ) => {
292
+ const CONFIG_WITHOUT_ROLLOUT_FLAG = `{"entries":{"fpr_enabled":"true",\
293
+ "fpr_log_endpoint_url":"https://firebaselogging.test.com",\
294
+ "fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
295
+ "fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
296
+ "state":"UPDATE"}` ;
297
+ setup (
298
+ {
299
+ // Expired local config.
300
+ expiry : '1556524895320' ,
301
+ config : NOT_VALID_CONFIG
302
+ } ,
303
+ { reject : false , value : new Response ( CONFIG_WITHOUT_ROLLOUT_FLAG ) }
304
+ ) ;
305
+ await getConfig ( IID ) ;
306
+
307
+ // If template exists but no rollout flag, will send to Fl.
308
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . true ;
309
+ } ) ;
310
+
311
+ it ( 'marks event destination to cc when instance is outside of rollout range' , async ( ) => {
312
+ const CONFIG_WITH_ROLLOUT_FLAG_10 = `{"entries":{"fpr_enabled":"true",\
313
+ "fpr_log_endpoint_url":"https://firebaselogging.test.com",\
314
+ "fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
315
+ "fpr_log_transport_web_percent":"10.0",\
316
+ "fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
317
+ "state":"UPDATE"}` ;
318
+ setup (
319
+ {
320
+ // Expired local config.
321
+ expiry : '1556524895320' ,
322
+ config : NOT_VALID_CONFIG
323
+ } ,
324
+ { reject : false , value : new Response ( CONFIG_WITH_ROLLOUT_FLAG_10 ) }
325
+ ) ;
326
+ await getConfig ( IID ) ;
327
+
328
+ // If rollout flag exists, will send to cc when this instance is out of rollout scope.
329
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . false ;
330
+ } ) ;
331
+
332
+ it ( 'marks event destination to Fl when instance is within rollout range' , async ( ) => {
333
+ const CONFIG_WITH_ROLLOUT_FLAG_40 = `{"entries":{"fpr_enabled":"true",\
334
+ "fpr_log_endpoint_url":"https://firebaselogging.test.com",\
335
+ "fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
336
+ "fpr_log_transport_web_percent":"40.0",\
337
+ "fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
338
+ "state":"UPDATE"}` ;
339
+ setup (
340
+ {
341
+ // Expired local config.
342
+ expiry : '1556524895320' ,
343
+ config : NOT_VALID_CONFIG
344
+ } ,
345
+ { reject : false , value : new Response ( CONFIG_WITH_ROLLOUT_FLAG_40 ) }
346
+ ) ;
347
+ await getConfig ( IID ) ;
348
+
349
+ // If rollout flag exists, will send to Fl when this instance is within rollout scope.
350
+ expect ( SettingsService . getInstance ( ) . shouldSendToFl ) . to . be . true ;
351
+ } ) ;
352
+ } ) ;
353
+
354
+ describe ( 'isDestFl' , ( ) => {
355
+ it ( 'marks traffic to cc when rollout percentage is 0' , ( ) => {
356
+ const shouldSendToFl = isDestFl ( 'abc' , 0 ) ; // Hash percentage of "abc" is 38%.
357
+ expect ( shouldSendToFl ) . to . be . false ;
358
+ } ) ;
359
+
360
+ it ( 'marks traffic to Fl when rollout percentage is 100' , ( ) => {
361
+ const shouldSendToFl = isDestFl ( 'abc' , 100 ) ; // Hash percentage of "abc" is 38%.
362
+ expect ( shouldSendToFl ) . to . be . true ;
363
+ } ) ;
364
+
365
+ it ( 'marks traffic to Fl if hash percentage is lower than rollout percentage 50%' , ( ) => {
366
+ const shouldSendToFl = isDestFl ( 'abc' , 50 ) ; // Hash percentage of "abc" is 38%.
367
+ expect ( shouldSendToFl ) . to . be . true ;
368
+ } ) ;
230
369
} ) ;
231
370
} ) ;
0 commit comments