Remove axios-cached-dns-resolve-test

This commit is contained in:
Louis Lam 2022-06-13 18:52:08 +08:00
parent f570d41142
commit 7f432bd916
3 changed files with 742 additions and 352 deletions

900
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,7 @@
"start-server": "node server/server.js",
"start-server-dev": "cross-env NODE_ENV=development node server/server.js",
"build": "vite build --config ./config/vite.config.js",
"test": "ava test/axios-cached-dns-resolve-test.js -timeout=10m && node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test",
"test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test",
"test-with-build": "npm run build && npm test",
"jest": "node test/prepare-jest.js && npm run jest-frontend && npm run jest-backend",
"jest-frontend": "cross-env TEST_FRONTEND=1 jest --config=./config/jest-frontend.config.js",
@ -140,7 +140,6 @@
"@vitejs/plugin-vue": "~2.3.3",
"@vue/compiler-sfc": "~3.2.36",
"aedes": "^0.46.3",
"ava": "^3.15.0",
"babel-plugin-rewire": "~1.2.0",
"concurrently": "^7.1.0",
"core-js": "~3.18.3",

View File

@ -1,191 +0,0 @@
/*
* From: https://github.com/tcollinsworth/axios-cached-dns-resolve/blob/master/__tests__/test.js
* But it converted to CommonJS
*/
const ava = require("ava");
const delay = require("delay");
const LRUCache = require("lru-cache");
const axios = require("axios");
const axiosCachingDns = require("esm")(module)("axios-cached-dns-resolve");
const test = ava.serial
let axiosClient
test.beforeEach(() => {
axiosCachingDns.config.dnsTtlMs = 1000
axiosCachingDns.config.dnsIdleTtlMs = 5000
axiosCachingDns.config.cacheGraceExpireMultiplier = 2
axiosCachingDns.config.backgroundScanMs = 100
axiosCachingDns.cacheConfig.ttl = (axiosCachingDns.config.dnsTtlMs * axiosCachingDns.config.cacheGraceExpireMultiplier)
axiosCachingDns.config.cache = new LRUCache(axiosCachingDns.cacheConfig)
axiosClient = axios.create({
timeout: 5000,
// maxRedirects: 0,
})
axiosCachingDns.registerInterceptor(axiosClient)
axiosCachingDns.startBackgroundRefresh()
axiosCachingDns.startPeriodicCachePrune()
})
test.after.always(() => {
axiosCachingDns.config.cache.clear()
})
test('query google with baseURL and relative url', async (t) => {
axiosCachingDns.registerInterceptor(axios)
const { data } = await axios.get('/finance', {
baseURL: 'http://www.google.com',
// headers: { Authorization: `Basic ${basicauth}` },
})
t.truthy(data)
t.is(1, axiosCachingDns.getStats().dnsEntries)
t.is(1, axiosCachingDns.getStats().misses)
})
test('query google caches and after idle delay uncached', async (t) => {
const resp = await axiosClient.get('http://amazon.com')
t.truthy(resp.data)
t.truthy(axiosCachingDns.config.cache.get('amazon.com'))
await delay(6000)
t.falsy(axiosCachingDns.config.cache.get('amazon.com'))
const expectedStats = {
dnsEntries: 0,
// refreshed: 4, variable
hits: 0,
misses: 2,
idleExpired: 1,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('query google caches and refreshes', async (t) => {
await axiosClient.get('http://amazon.com')
const { updatedTs } = axiosCachingDns.config.cache.get('amazon.com')
const timeoutTime = Date.now() + 5000
// eslint-disable-next-line no-constant-condition
while (true) {
const dnsEntry = axiosCachingDns.config.cache.get('amazon.com')
if (!dnsEntry) t.fail('dnsEntry missing or expired')
// console.log(dnsEntry)
if (updatedTs !== dnsEntry.updatedTs) break
if (Date.now() > timeoutTime) t.fail()
// eslint-disable-next-line no-await-in-loop
await delay(10)
}
const expectedStats = {
dnsEntries: 1,
// refreshed: 5, variable
hits: 0,
misses: 3,
idleExpired: 1,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('query two services, caches and after one idle delay uncached', async (t) => {
await axiosClient.get('http://amazon.com')
await axiosClient.get('http://microsoft.com')
const { lastUsedTs } = axiosCachingDns.config.cache.get('microsoft.com')
t.is(1, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
await axiosClient.get('http://microsoft.com')
t.is(2, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
t.truthy(lastUsedTs < axiosCachingDns.config.cache.get('microsoft.com').lastUsedTs)
t.is(2, axiosCachingDns.config.cache.size)
await axiosClient.get('http://microsoft.com')
t.is(3, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
t.falsy(lastUsedTs === axiosCachingDns.config.cache.get('microsoft.com').lastUsedTs)
t.is(2, axiosCachingDns.config.cache.size)
await delay(4000)
t.is(1, axiosCachingDns.config.cache.size)
await delay(2000)
t.is(0, axiosCachingDns.config.cache.size)
const expectedStats = {
dnsEntries: 0,
// refreshed: 17, variable
hits: 2,
misses: 5,
idleExpired: 3,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('validate axios config not altered', async (t) => {
const baseURL = 'http://microsoft.com'
const axiosConfig = { baseURL }
const custAxiosClient = axios.create(axiosConfig)
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.get('/')
t.is(baseURL, axiosConfig.baseURL)
await custAxiosClient.get('/')
t.is(baseURL, axiosConfig.baseURL)
})
test('validate axios get config not altered', async (t) => {
const url = 'http://microsoft.com'
const custAxiosClient = axios.create()
const reqConfig = {
method: 'get',
url,
}
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.get(url, reqConfig)
t.is(url, reqConfig.url)
await custAxiosClient.get(url, reqConfig)
t.is(url, reqConfig.url)
})
test('validate axios request config not altered', async (t) => {
const url = 'http://microsoft.com'
const custAxiosClient = axios.create()
const reqConfig = {
method: 'get',
url,
}
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.request(reqConfig)
t.is(url, reqConfig.url)
await custAxiosClient.request(reqConfig)
t.is(url, reqConfig.url)
})