Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 119 additions & 25 deletions packages/datafile-manager/__test__/httpPollingDatafileManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import HttpPollingDatafileManager from '../src/httpPollingDatafileManager'
import { Headers, AbortableRequest, Response } from '../src/http'
import { DatafileManagerConfig } from '../src/datafileManager';
import { advanceTimersByTime, getTimerCount } from './testUtils'
import PersistentKeyValueCache from '../src/persistentKeyValueCache'

jest.mock('../src/backoffController', () => {
return jest.fn().mockImplementation(() => {
Expand All @@ -39,6 +40,8 @@ class TestDatafileManager extends HttpPollingDatafileManager {

responsePromises: Promise<Response>[] = []

simulateResponseDelay: boolean = false

makeGetRequest(url: string, headers: Headers): AbortableRequest {
const nextResponse: Error | Response | undefined = this.queuedResponses.pop()
let responsePromise: Promise<Response>
Expand All @@ -47,7 +50,12 @@ class TestDatafileManager extends HttpPollingDatafileManager {
} else if (nextResponse instanceof Error) {
responsePromise = Promise.reject(nextResponse)
} else {
responsePromise = Promise.resolve(nextResponse)
if (this.simulateResponseDelay) {
// Actual response will have some delay. This is required to get expected behavior for caching.
responsePromise = new Promise((resolve) => setTimeout(() => resolve(nextResponse), 50))
} else {
responsePromise = Promise.resolve(nextResponse)
}
}
this.responsePromises.push(responsePromise)
return { responsePromise, abort: jest.fn() }
Expand All @@ -58,6 +66,30 @@ class TestDatafileManager extends HttpPollingDatafileManager {
}
}

const testCache : PersistentKeyValueCache = {
get(key: string): Promise<any | null> {
let val = null
switch(key) {
case 'opt-datafile-keyThatExists':
val = { name: 'keyThatExists' }
break
}
return Promise.resolve(val)
},

set(key: string, val: any): Promise<void> {
return Promise.resolve()
},

contains(key: string): Promise<Boolean> {
return Promise.resolve(false)
},

remove(key: string): Promise<void> {
return Promise.resolve()
}
}

describe('httpPollingDatafileManager', () => {
beforeEach(() => {
jest.useFakeTimers()
Expand All @@ -82,13 +114,7 @@ describe('httpPollingDatafileManager', () => {
expect(manager.get()).toEqual({ foo: 'abcd' })
})

it('resolves onReady immediately', async () => {
manager.start()
await manager.onReady()
expect(manager.get()).toEqual({ foo: 'abcd' })
})

it('after being started, fetches the datafile, updates itself, emits an update event, and updates itself again after a timeout', async () => {
it('after being started, fetches the datafile, updates itself, and updates itself again after a timeout', async () => {
manager.queuedResponses.push(
{
statusCode: 200,
Expand All @@ -106,10 +132,6 @@ describe('httpPollingDatafileManager', () => {
manager.start()
expect(manager.responsePromises.length).toBe(1)
await manager.responsePromises[0]
expect(updateFn).toBeCalledTimes(1)
expect(updateFn).toBeCalledWith({
datafile: { foo: 'bar' }
})
Comment on lines -109 to -112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this deleted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see why this behavior changed. We are only emitting update events after ready is resolved. Deleting this is OK.

expect(manager.get()).toEqual({ foo: 'bar' })
updateFn.mockReset()

Expand All @@ -134,27 +156,15 @@ describe('httpPollingDatafileManager', () => {
expect(manager.get()).toEqual({ foo: 'abcd' })
})

it('after being started, resolves onReady immediately', async () => {
manager.start()
await manager.onReady()
expect(manager.get()).toEqual({ foo: 'abcd' })
})

it('after being started, fetches the datafile, updates itself once, and emits an update event, but does not schedule a future update', async () => {
it('after being started, fetches the datafile, updates itself once, but does not schedule a future update', async () => {
manager.queuedResponses.push({
statusCode: 200,
body: '{"foo": "bar"}',
headers: {}
})
const updateFn = jest.fn()
manager.on('update', updateFn)
manager.start()
expect(manager.responsePromises.length).toBe(1)
await manager.responsePromises[0]
expect(updateFn).toBeCalledTimes(1)
expect(updateFn).toBeCalledWith({
datafile: { foo: 'bar' }
})
expect(manager.get()).toEqual({ foo: 'bar' })
expect(getTimerCount()).toBe(0)
})
Expand Down Expand Up @@ -634,4 +644,88 @@ describe('httpPollingDatafileManager', () => {
expect(makeGetRequestSpy).toBeCalledTimes(2)
})
})

describe('when constructed with a cache implementation having an already cached datafile', () => {
beforeEach(() => {
manager = new TestDatafileManager({
sdkKey: 'keyThatExists',
updateInterval: 500,
autoUpdate: true,
cache: testCache,
})
manager.simulateResponseDelay = true
})

it('uses cached version of datafile first and resolves the promise while network throws error and no update event is triggered', async () => {
manager.queuedResponses.push(new Error('Connection Error'))
const updateFn = jest.fn()
manager.on('update', updateFn)
manager.start()
await manager.onReady()
expect(manager.get()).toEqual({name: 'keyThatExists'})
await advanceTimersByTime(50)
expect(manager.get()).toEqual({name: 'keyThatExists'})
expect(updateFn).toBeCalledTimes(0)
})

it('uses cached datafile, resolves ready promise, fetches new datafile from network and triggers update event', async() => {
manager.queuedResponses.push({
statusCode: 200,
body: '{"foo": "bar"}',
headers: {}
})

const updateFn = jest.fn()
manager.on('update', updateFn)
manager.start()
await manager.onReady()
expect(manager.get()).toEqual({ name: 'keyThatExists' })
expect(updateFn).toBeCalledTimes(0)
await advanceTimersByTime(50)
expect(manager.get()).toEqual({ foo: 'bar' })
expect(updateFn).toBeCalledTimes(1)
})

it('sets newly recieved datafile in to cache', async() => {
const cacheSetSpy = jest.spyOn(testCache, 'set')
manager.queuedResponses.push({
statusCode: 200,
body: '{"foo": "bar"}',
headers: {}
})
manager.start()
await manager.onReady()
await advanceTimersByTime(50)
expect(manager.get()).toEqual({ foo: 'bar' })
expect(cacheSetSpy).toBeCalledWith('opt-datafile-keyThatExists', {"foo": "bar"})
})
})

describe('when constructed with a cache implementation without an already cached datafile', () => {
beforeEach(() => {
manager = new TestDatafileManager({
sdkKey: 'keyThatDoesExists',
updateInterval: 500,
autoUpdate: true,
cache: testCache,
})
manager.simulateResponseDelay = true
})

it('does not find cached datafile, fetches new datafile from network, resolves promise and does not trigger update event', async() => {
manager.queuedResponses.push({
statusCode: 200,
body: '{"foo": "bar"}',
headers: {}
})

const updateFn = jest.fn()
manager.on('update', updateFn)
manager.start()
await advanceTimersByTime(50)
await manager.onReady()
expect(manager.get()).toEqual({ foo: 'bar' })
expect(updateFn).toBeCalledTimes(0)
})
})
})
1 change: 1 addition & 0 deletions packages/datafile-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"main": "lib/index.node.js",
"browser": "lib/index.browser.js",
"react-native": "lib/index.react_native.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib",
Expand Down
8 changes: 5 additions & 3 deletions packages/datafile-manager/src/datafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import PersistentKeyValueCache from './persistentKeyValueCache'

export interface DatafileUpdate {
datafile: object
}
export interface DatafileUpdate {
datafile: object
}

export interface DatafileUpdateListener {
(datafileUpdate: DatafileUpdate): void
Expand All @@ -41,4 +42,5 @@ export interface DatafileManagerConfig {
sdkKey: string
updateInterval?: number
urlTemplate?: string
cache?: PersistentKeyValueCache
}
49 changes: 45 additions & 4 deletions packages/datafile-manager/src/httpPollingDatafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

import { getLogger } from '@optimizely/js-sdk-logging'
import { sprintf } from '@optimizely/js-sdk-utils';
import { DatafileManager, DatafileManagerConfig, DatafileUpdate } from './datafileManager';
import { DatafileManager, DatafileManagerConfig, DatafileUpdate } from './datafileManager'
import EventEmitter from './eventEmitter'
import { AbortableRequest, Response, Headers } from './http';
import { AbortableRequest, Response, Headers } from './http'
import { DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL, DEFAULT_URL_TEMPLATE } from './config'
import BackoffController from './backoffController';
import BackoffController from './backoffController'
import PersistentKeyValueCache from './persistentKeyValueCache'

const logger = getLogger('DatafileManager')

Expand All @@ -34,6 +35,24 @@ function isSuccessStatusCode(statusCode: number): boolean {
return statusCode >= 200 && statusCode < 400
}

const noOpKeyValueCache: PersistentKeyValueCache = {
get(key: string): Promise<any | null> {
return Promise.resolve(null)
},

set(key: string, val: any): Promise<void> {
return Promise.resolve()
},

contains(key: string): Promise<Boolean> {
return Promise.resolve(false)
},

remove(key: string): Promise<void> {
return Promise.resolve()
}
}

export default abstract class HttpPollingDatafileManager implements DatafileManager {
// Make an HTTP get request to the given URL with the given headers
// Return an AbortableRequest, which has a promise for a Response.
Expand Down Expand Up @@ -72,6 +91,10 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana

private backoffController: BackoffController

private cacheKey: string

private cache: PersistentKeyValueCache

// When true, this means the update interval timeout fired before the current
// sync completed. In that case, we should sync again immediately upon
// completion of the current request, instead of waiting another update
Expand All @@ -89,8 +112,11 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
sdkKey,
updateInterval = DEFAULT_UPDATE_INTERVAL,
urlTemplate = DEFAULT_URL_TEMPLATE,
cache = noOpKeyValueCache,
} = configWithDefaultsApplied

this.cache = cache
this.cacheKey = 'opt-datafile-' + sdkKey
this.isReadyPromiseSettled = false
this.readyPromiseResolver = () => {}
this.readyPromiseRejecter = () => {}
Expand All @@ -101,7 +127,9 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana

if (datafile) {
this.currentDatafile = datafile
this.resolveReadyPromise()
if (!sdkKey) {
this.resolveReadyPromise()
}
} else {
this.currentDatafile = null
}
Expand Down Expand Up @@ -133,6 +161,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
logger.debug('Datafile manager started')
this.isStarted = true
this.backoffController.reset()
this.setDatafileFromCacheIfAvailable()
this.syncDatafile()
}
}
Expand Down Expand Up @@ -197,6 +226,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
if (datafile !== null) {
logger.info('Updating datafile from response')
this.currentDatafile = datafile
this.cache.set(this.cacheKey, datafile)
if (!this.isReadyPromiseSettled) {
this.resolveReadyPromise()
} else {
Expand Down Expand Up @@ -314,4 +344,15 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
logger.debug('Saved last modified header value from response: %s', this.lastResponseLastModified)
}
}

setDatafileFromCacheIfAvailable(): void {
this.cache.get(this.cacheKey)
.then(datafile => {
if (this.isStarted && !this.isReadyPromiseSettled && datafile) {
logger.debug('Using datafile from cache')
this.currentDatafile = datafile
this.resolveReadyPromise()
}
})
}
}
18 changes: 18 additions & 0 deletions packages/datafile-manager/src/index.react_native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './datafileManager'
export { default as HttpPollingDatafileManager } from './reactNativeDatafileManager'
35 changes: 35 additions & 0 deletions packages/datafile-manager/src/reactNativeDatafileManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { makeGetRequest } from './browserRequest'
import HttpPollingDatafileManager from './httpPollingDatafileManager'
import { Headers, AbortableRequest } from './http'
import { DatafileManagerConfig } from './datafileManager'
import ReactNativeAsyncStorageCache from './reactNativeAsyncStorageCache'

export default class ReactNativeDatafileManager extends HttpPollingDatafileManager {

protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest {
return makeGetRequest(reqUrl, headers)
}

protected getConfigDefaults(): Partial<DatafileManagerConfig> {
return {
autoUpdate: true,
cache: new ReactNativeAsyncStorageCache(),
}
}
}