Add test for memory and redis store

This commit is contained in:
Jerko Steiner 2020-03-14 08:36:17 +01:00
parent d6104bae14
commit 80eb39b5b8
4 changed files with 90 additions and 1 deletions

View File

@ -5,7 +5,7 @@ import express from 'express'
import path from 'path'
import SocketIO from 'socket.io'
import { config } from './config'
import { configureStores } from './redis'
import { configureStores } from './configureStores'
import call from './routes/call'
import index from './routes/index'
import { createServer } from './server'

View File

@ -0,0 +1,42 @@
jest.mock('ioredis')
import Redis from 'ioredis'
import SocketIO from 'socket.io'
import { configureStores } from './configureStores'
import { MemoryStore, RedisStore } from './store'
describe('configureStores', () => {
describe('memory', () => {
it('should be in memory when no params specified', () => {
const io = SocketIO()
const stores = configureStores(io)
expect(stores.socketIdByUserId).toEqual(jasmine.any(MemoryStore))
expect(stores.userIdBySocketId).toEqual(jasmine.any(MemoryStore))
})
it('should be in memory when type="memory"', () => {
const io = SocketIO()
const stores = configureStores(io)
expect(stores.socketIdByUserId).toEqual(jasmine.any(MemoryStore))
expect(stores.userIdBySocketId).toEqual(jasmine.any(MemoryStore))
})
})
describe('redis', () => {
it('should be redis when type="redis"', () => {
const io = SocketIO()
const stores = configureStores(io, {
type: 'redis',
host: 'localhost',
port: 6379,
prefix: 'peercalls',
})
expect(io.adapter().pubClient).toEqual(jasmine.any(Redis))
expect(io.adapter().subClient).toEqual(jasmine.any(Redis))
expect(stores.socketIdByUserId).toEqual(jasmine.any(RedisStore))
expect(stores.userIdBySocketId).toEqual(jasmine.any(RedisStore))
})
})
})

View File

@ -0,0 +1,47 @@
import { MemoryStore, RedisStore } from './'
import Redis from 'ioredis'
import { Store } from './store'
describe('store', () => {
const redis = new Redis({
host: process.env.TEST_REDIS_HOST || 'localhost',
port: parseInt(process.env.TEST_REDIS_PORT!) || 6379,
enableOfflineQueue: true,
})
const testCases: Array<{name: string, store: Store}> = [{
name: MemoryStore.name,
store: new MemoryStore(),
}, {
name: RedisStore.name,
store: new RedisStore(redis, 'peercallstest'),
}]
afterAll(() => {
redis.disconnect()
})
testCases.forEach(({name, store}) => {
describe(name, () => {
afterEach(async () => {
await Promise.all([
store.remove('a'),
store.remove('b'),
])
})
describe('set, get, getMany', () => {
it('sets and retreives value(s)', async () => {
await store.set('a', 'A')
await store.set('b', 'B')
expect(await store.get('a')).toBe('A')
expect(await store.get('b')).toBe('B')
expect(await store.remove('b'))
expect(await store.get('c')).toBe(undefined)
expect(await store.getMany(['a', 'b', 'c']))
.toEqual(['A', undefined, undefined])
})
})
})
})
})