Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tman.suite('chaos test', function () {
const client = redis.createClient()
const clientP = redis.createClient({
usePromise: true
})
const len = 10000
const tasks = []
for (let i = 0; i < len; i++) tasks.push(i)
function getClient () {
// use thunk API or promise API randomly
return Math.floor(Math.random() * 10) % 2 ? client : clientP
}
tman.it('create 10000 users', function * () {
assert((yield client.flushall()) === 'OK')
yield tasks.map((value, index) => createUser('U' + index))
assert((yield client.zcard('userScore')) === len)
function createUser (id) {
const time = Date.now()
const user = {
id: id,
name: 'user_' + id,
email: id + '@test.com',
score: 0,
issues: [],
createdAt: time,
updatedAt: time
}
client.multi(),
client.set(i, i),
client.get(i),
client.exec()
]
console.log(111, res)
assert.strictEqual(res[0], 'OK')
assert.strictEqual(res[1], 'QUEUED')
assert.strictEqual(res[2], 'QUEUED')
assert.strictEqual(res[3][0], 'OK')
assert.strictEqual(res[3][1], i + '')
if (!(i % 500)) process.stdout.write('.')
}
})
tman.it('evalauto', function * () {
const task = []
let len = count
while (len--) addTask(len)
yield thunk.all(task)
function addTask (index) {
task.push(function * () {
const res = yield client.evalauto('return KEYS[1]', 1, index)
assert.strictEqual(+res, index)
if (!(index % 500)) process.stdout.write('.')
return +res
})
}
})
tman.it.skip('kill a master', function * () {
tman.suite('when no type is set', function () {
tman.it('should return false', function () {
let res = context().response
assert.strictEqual(res.is(), false)
assert.strictEqual(res.is('html'), false)
})
})
tman.suite('if no types match', function () {
tman.it('should return false', function () {
let ctx = context()
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'
assert.strictEqual(ctx.acceptsCharsets('utf-16'), false)
})
})
})
tman.suite('if no types match', function () {
tman.it('should return false', function () {
let ctx = context()
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'
assert.strictEqual(ctx.acceptsLanguages('fr', 'au'), false)
})
})
})
tman.suite('when the request method is not idempotent', function () {
tman.it('should return false', function () {
let req = request()
req.method = 'POST'
assert.strictEqual(req.idempotent, false)
})
})
})
tman.suite('when no content type is given', function () {
tman.it('should return false', function () {
let ctx = context()
ctx.header['transfer-encoding'] = 'chunked'
assert.strictEqual(ctx.is(), false)
assert.strictEqual(ctx.is('image/*'), false)
assert.strictEqual(ctx.is('text/*', 'image/*'), false)
})
})
}
return new Promise((resolve, reject) => setTimeout(resolve, Math.floor(Math.random() * 5)))
.then(() => {
return Promise.all([
clientP.multi(),
clientP.set(id, JSON.stringify(user)),
clientP.zadd('userScore', user.score, id),
clientP.exec()
])
})
.then((result) => assert.deepStrictEqual(result, ['OK', 'QUEUED', 'QUEUED', ['OK', 1]]))
}
})
tman.it('update 10000 users', function * () {
yield tasks.map(function (value, index) {
return updateUser('U' + index, Math.floor(Math.random() * 1000))
})
assert((yield client.pfcount('scoreLog')) > 5)
function * updateUser (id, score) {
const cli = getClient()
let user = yield cli.get(id)
user = JSON.parse(user)
user.score = score
user.updatedAt = Date.now()
const result = yield [
cli.multi(),
cli.set(id, JSON.stringify(user)),
cli.zadd('userScore', user.score, id),
user = JSON.parse(user)
user.score = score
user.updatedAt = Date.now()
const result = yield [
cli.multi(),
cli.set(id, JSON.stringify(user)),
cli.zadd('userScore', user.score, id),
cli.pfadd('scoreLog', Math.floor(score / 100)),
cli.exec()
]
result.pop()
assert.deepStrictEqual(result, ['OK', 'QUEUED', 'QUEUED', 'QUEUED'])
}
})
tman.it('create 10000 issues for some users', function * () {
yield tasks.map(function (value, index) {
return createIssue('I' + index, 'U' + Math.floor(Math.random() * len))
})
assert((yield client.pfcount('scoreLog')) > 5)
function * createIssue (id, uid) {
const cli = getClient()
const time = Date.now()
const issue = {
id: id,
creatorId: uid,
content: 'issue_' + id,
createdAt: time,
updatedAt: time
}
tman.suite('messed up whitespace', function () {
tman.it('work', function () {
let mejs = fixtureMejs('messed.ejs')
assert.strictEqual(mejs.render('messed', {users: users}), fixtureFile('messed.html'))
})
})