From b102ad2d07761d027ab49ca46c30e2625d90c36b Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 1 Feb 2019 17:52:29 +0800 Subject: [PATCH] test: route cache in middleware/cache --- lib/router.js | 31 ++---------------------------- lib/routes/test/index.js | 41 ++++++++++++++++++++++++++++++++++++++++ test/middleware/cache.js | 36 +++++++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 lib/routes/test/index.js diff --git a/lib/router.js b/lib/router.js index 679c06e71c..5f900edbf9 100644 --- a/lib/router.js +++ b/lib/router.js @@ -86,35 +86,8 @@ router.get('/', async (ctx) => { }); }); -router.get('/test/:id', (ctx) => { - if (ctx.params.id === '0') { - throw Error('Error test'); - } - const item = []; - if (ctx.params.id === 'long') { - item.push({ - title: `Long Title `.repeat(10), - description: `Long Description `.repeat(10), - pubDate: new Date(`2018-3-1`).toUTCString(), - link: `https://github.com/DIYgod/RSSHub/issues/0`, - author: `DIYgod0`, - }); - } - for (let i = 1; i < 6; i++) { - item.push({ - title: `Title${i}`, - description: `Description${i}`, - pubDate: new Date(`2018-4-${i}`).toUTCString(), - link: `https://github.com/DIYgod/RSSHub/issues/${i}`, - author: `DIYgod${i}`, - }); - } - ctx.state.data = { - title: `Test ${ctx.params.id}`, - link: 'https://github.com/DIYgod/RSSHub', - item: item, - }; -}); +// test +router.get('/test/:id', require('./routes/test')); // RSSHub router.get('/rsshub/rss', require('./routes/rsshub/rss')); diff --git a/lib/routes/test/index.js b/lib/routes/test/index.js new file mode 100644 index 0000000000..6584b92ad3 --- /dev/null +++ b/lib/routes/test/index.js @@ -0,0 +1,41 @@ +const config = require('../../config'); +let cacheIndex = 0; + +module.exports = async (ctx) => { + if (ctx.params.id === '0') { + throw Error('Error test'); + } + const item = []; + if (ctx.params.id === 'long') { + item.push({ + title: `Long Title `.repeat(10), + description: `Long Description `.repeat(10), + pubDate: new Date(`2018-3-1`).toUTCString(), + link: `https://github.com/DIYgod/RSSHub/issues/0`, + author: `DIYgod0`, + }); + } else if (ctx.params.id === 'cache') { + const description = await ctx.cache.tryGet('test', () => `Cache${++cacheIndex}`, config.cacheExpire * 2); + item.push({ + title: 'Cache Title', + description: description, + pubDate: new Date(`2018-3-1`).toUTCString(), + link: `https://github.com/DIYgod/RSSHub/issues/0`, + author: `DIYgod0`, + }); + } + for (let i = 1; i < 6; i++) { + item.push({ + title: `Title${i}`, + description: `Description${i}`, + pubDate: new Date(`2018-4-${i}`).toUTCString(), + link: `https://github.com/DIYgod/RSSHub/issues/${i}`, + author: `DIYgod${i}`, + }); + } + ctx.state.data = { + title: `Test ${ctx.params.id}`, + link: 'https://github.com/DIYgod/RSSHub', + item: item, + }; +}; diff --git a/test/middleware/cache.js b/test/middleware/cache.js index 044f28be88..2c97490dd2 100644 --- a/test/middleware/cache.js +++ b/test/middleware/cache.js @@ -24,10 +24,10 @@ describe('cache', () => { server = require('../../lib/index').server; const request = supertest(server); - const response1 = await request.get('/test/1'); + const response1 = await request.get('/test/cache'); const parsed1 = await parser.parseString(response1.text); - const response2 = await request.get('/test/1'); + const response2 = await request.get('/test/cache'); const parsed2 = await parser.parseString(response2.text); delete parsed1.lastBuildDate; @@ -38,10 +38,20 @@ describe('cache', () => { expect(response2.headers['x-koa-memory-cache']).toBe('true'); expect(response2.headers).not.toHaveProperty('x-koa-redis-cache'); - await wait(1 * 1000 + 10); - const response3 = await request.get('/test/1'); + await wait(1 * 1000 + 100); + const response3 = await request.get('/test/cache'); expect(response3.headers).not.toHaveProperty('x-koa-redis-cache'); expect(response3.headers).not.toHaveProperty('x-koa-memory-cache'); + const parsed3 = await parser.parseString(response3.text); + + await wait(1 * 1000 + 100); + const response4 = await request.get('/test/cache'); + const parsed4 = await parser.parseString(response4.text); + + expect(parsed1.items[0].content).toBe('Cache1'); + expect(parsed2.items[0].content).toBe('Cache1'); + expect(parsed3.items[0].content).toBe('Cache1'); + expect(parsed4.items[0].content).toBe('Cache1'); }); it('redis', async () => { @@ -49,10 +59,10 @@ describe('cache', () => { server = require('../../lib/index').server; const request = supertest(server); - const response1 = await request.get('/test/1'); + const response1 = await request.get('/test/cache'); const parsed1 = await parser.parseString(response1.text); - const response2 = await request.get('/test/1'); + const response2 = await request.get('/test/cache'); const parsed2 = await parser.parseString(response2.text); delete parsed1.lastBuildDate; @@ -63,9 +73,19 @@ describe('cache', () => { expect(response2.headers['x-koa-redis-cache']).toBe('true'); expect(response2.headers).not.toHaveProperty('x-koa-memory-cache'); - await wait(1 * 1000 + 10); - const response3 = await request.get('/test/1'); + await wait(1 * 1000 + 100); + const response3 = await request.get('/test/cache'); expect(response3.headers).not.toHaveProperty('x-koa-redis-cache'); expect(response3.headers).not.toHaveProperty('x-koa-memory-cache'); + const parsed3 = await parser.parseString(response3.text); + + await wait(1 * 1000 + 100); + const response4 = await request.get('/test/cache'); + const parsed4 = await parser.parseString(response4.text); + + expect(parsed1.items[0].content).toBe('Cache1'); + expect(parsed2.items[0].content).toBe('Cache1'); + expect(parsed3.items[0].content).toBe('Cache1'); + expect(parsed4.items[0].content).toBe('Cache1'); }); });