Skip to content

Commit

Permalink
Warn once per custom data types
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Mar 29, 2023
1 parent d870490 commit 15b2575
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/tools/heavyCustomerDataWarning.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { display } from './display'
import { CustomerDataType, CUSTOMER_DATA_BYTES_LIMIT, warnIfCustomerDataLimitReached } from './heavyCustomerDataWarning'
import {
CustomerDataType,
CUSTOMER_DATA_BYTES_LIMIT,
resetWarnings,
warnIfCustomerDataLimitReached,
} from './heavyCustomerDataWarning'

describe('warnIfCustomerDataLimitReached', () => {
let displaySpy: jasmine.Spy<typeof display.warn>
beforeEach(() => {
displaySpy = spyOn(display, 'warn')
resetWarnings()
})

it('should warn when the customer data reach the limit', () => {
it('should warn once when the customer data reach the limit', () => {
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.User)
expect(displaySpy).toHaveBeenCalledWith(
"The user data is over 3KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth."
)
})

it('should not warn when the customer data reach the limit', () => {
it('should not warn when the customer data does not reach the limit', () => {
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT - 1, CustomerDataType.User)
expect(displaySpy).not.toHaveBeenCalled()
})

it('should warn once per customer data types', () => {
for (let i = 0; i < 2; i++) {
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.User)
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.FeatureFlag)
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.GlobalContext)
warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.LoggerContext)
}

expect(displaySpy).toHaveBeenCalledTimes(4)
})
})
9 changes: 8 additions & 1 deletion packages/core/src/tools/heavyCustomerDataWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ export const enum CustomerDataType {
LoggerContext = 'logger context',
}

let alreadyWarned: { [key: string]: boolean } = {}

export function warnIfCustomerDataLimitReached(bytesCount: number, customerDataType: CustomerDataType) {
if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {
if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT && !alreadyWarned[customerDataType]) {
alreadyWarned[customerDataType] = true
display.warn(
`The ${customerDataType} data is over ${
CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE
}KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.`
)
}
}

export function resetWarnings() {
alreadyWarned = {}
}

0 comments on commit 15b2575

Please sign in to comment.