Refactoring test function

This commit is contained in:
freearhey 2019-07-20 10:33:16 +03:00
parent 4dc6ac3632
commit c8412527eb
1 changed files with 24 additions and 27 deletions

View File

@ -1,24 +1,26 @@
const parsers = require('playlist-parser') const util = require('../helpers/util')
const M3U = parsers.M3U
const fs = require("fs")
const axios = require('axios') const axios = require('axios')
const path = require('path') const path = require('path')
const errorLog = 'error.log' const errorLog = 'error.log'
const timeout = 60000 const config = {
const delay = 200 timeout: 60000,
delay: 200
}
let tests = 0 let stats = {
let channels = 0 tests: 0,
let failures = 0 channels: 0,
failures: 0
}
const http = axios.create({ timeout }) const http = axios.create({ timeout: config.timeout })
http.defaults.headers.common["User-Agent"] = "VLC/2.2.4 LibVLC/2.2.4" http.defaults.headers.common["User-Agent"] = "VLC/2.2.4 LibVLC/2.2.4"
function writeToLog(test, country, msg, url) { function writeToLog(test, country, msg, url) {
var now = new Date() var now = new Date()
var line = `${test}(): ${country}: ${msg} '${url}'` var line = `${test}(): ${country}: ${msg} '${url}'`
fs.appendFileSync(path.resolve(__dirname) + '/../' + errorLog, now.toISOString() + ' ' + line + '\n') util.writeToFile(errorLog, now.toISOString() + ' ' + line + '\n')
console.log(line) console.log(line)
} }
@ -30,16 +32,11 @@ function skipPlaylist(filename) {
return false; return false;
} }
function loadPlaylist(filename) { async function test() {
return M3U.parse(fs.readFileSync(path.resolve(__dirname) + "/../" + filename, { encoding: "utf8" }))
}
async function testAllLinksIsWorking() { stats.tests++
tests++ let countries = util.parsePlaylist('index.m3u')
let countries = loadPlaylist('index.m3u')
// countries = countries.slice(0, 2)
for(let country of countries) { for(let country of countries) {
@ -47,15 +44,15 @@ async function testAllLinksIsWorking() {
continue; continue;
} }
const playlist = loadPlaylist(country.file) const playlist = util.parsePlaylist(country.file)
for(let channel of playlist) { for(let channel of playlist) {
await new Promise(resolve => { await new Promise(resolve => {
setTimeout(resolve, delay) setTimeout(resolve, config.delay)
}) })
channels++ stats.channels++
try { try {
@ -65,22 +62,22 @@ async function testAllLinksIsWorking() {
} catch (err) { } catch (err) {
failures++ stats.failures++
writeToLog('testAllLinksIsWorking', country.file, err.message, channel.file) writeToLog('test', country.file, err.message, channel.file)
} }
} }
} }
if(failures === 0) { if(stats.failures === 0) {
console.log(`OK (${tests} tests, ${channels} channels)`) console.log(`OK (${stats.tests} tests, ${stats.channels} channels)`)
} else { } else {
console.log(`FAILURES! (${tests} tests, ${channels} channels, ${failures} failures)`) console.log(`FAILURES! (${stats.tests} tests, ${stats.channels} channels, ${stats.failures} failures)`)
} }
@ -88,4 +85,4 @@ async function testAllLinksIsWorking() {
console.log('Test is running...') console.log('Test is running...')
testAllLinksIsWorking() test()