Skip to content

Commit

Permalink
test cache max size
Browse files Browse the repository at this point in the history
  • Loading branch information
tdesvenain committed Sep 24, 2017
1 parent c216865 commit 4641f14
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
49 changes: 47 additions & 2 deletions src/cache.service.spec.ts
Expand Up @@ -150,6 +150,8 @@ describe('CacheService', () => {
});
});

// because of timer we do not revoke cache in afterEach, which is executed in same event loop

it('should not use cache if delay is passed', inject([CacheService], (cache) => {
const http = TestBed.get(HttpTestingController);
let response = front_page_response;
Expand All @@ -162,12 +164,55 @@ describe('CacheService', () => {
cache.get('http://fake/Plone/').subscribe(() => {});
http.expectNone('http://fake/Plone/');

// we actually get content but request has not been sent again
Observable.timer(2).subscribe(() => {
cache.get('http://fake/Plone/').subscribe(() => {});
http.expectOne('http://fake/Plone/').flush(response);
expect(cache.hits['http://fake/Plone/']).toBe(1);
cache.revoke.emit()
})
}));
})
});

describe('CacheService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
APIService,
ConfigurationService,
AuthenticationService,
{
provide: 'CONFIGURATION', useValue: {
BACKEND_URL: 'http://fake/Plone',
CACHE_MAX_SIZE: 2,
}
},
CacheService,
]
});
});

afterEach(() => {
TestBed.get(CacheService).revoke.emit();
});

it('should refreshed store when cache max size is reached', inject([CacheService], (cache) => {
const http = TestBed.get(HttpTestingController);
let response = front_page_response;
expect(cache.maxSize).toBe(2);

cache.get('http://fake/Plone/').subscribe(() => {});
http.expectOne('http://fake/Plone/').flush(response);
expect(cache.cache['http://fake/Plone/']).toBeDefined();

cache.get('http://fake/Plone/1').subscribe(() => {});
http.expectOne('http://fake/Plone/1').flush(response);

cache.get('http://fake/Plone/2').subscribe(() => {});
http.expectOne('http://fake/Plone/2').flush(response);
expect(cache.cache['http://fake/Plone/2']).toBeDefined();
expect(cache.hits['http://fake/Plone/2']).toBe(1);
expect(cache.cache['http://fake/Plone/']).toBeUndefined();
expect(cache.hits['http://fake/Plone/']).toBeUndefined();
}));
});
2 changes: 1 addition & 1 deletion src/cache.service.ts
Expand Up @@ -47,7 +47,7 @@ export class CacheService {
public get<T>(url: string): Observable<T> {
const service = this;
if (!service.cache.hasOwnProperty(url)) {
if (Object.keys(service.cache).length > service.maxSize) {
if (Object.keys(service.cache).length >= service.maxSize) {
// TODO: do not revoke everything
this.revoke.emit();
}
Expand Down

0 comments on commit 4641f14

Please sign in to comment.