-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(notifier): create tests for notifier and its redis helper (#338)
* update lock file * added tests for worker and redisHelper * typo * imp(notifier): improved testcase of the redis helper * fix(notifier-tests): afterall quit from redis * test(notifier-validator): remove check what to receive validation * feat(notifier-types): what to reveive All changed to SeenMore * test(notifier): restore RedisHelper original implementation before each test Added new testcases - should send event to channels at most once in one threshold period for one fitted rule, no matter how much events have passed - should compute event count for period for each fitted rule * revert: remove check what to receive validator * test(notifier): add testcase for new events * feat(notifier): add check for new events * lint fix * fix tests * test(notifier-worker): improve readability * test(notifier-worker): improve testcase * test(notifier-worker): improve testacase description * test(notifier-worker): improve testcase readability * test(notifier-worker): improve test description * but(): all bugs fixed * feat(): all features added * chore(): restore all bugs * bug(notifier): fix bug with channel enabled validation * update yarn.lock * fix tests * imp(notifier): updater redis structure key format * Update extensions.ts * improve docs * remove empty lines * fix(notifier): fix notifier redisHelper tests * fix(notifer): notifier redisHelpers tests fix * fix(notifier): fix notifier redisHelper test --------- Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
- Loading branch information
Showing
12 changed files
with
348 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import RedisHelper from '../src/redisHelper'; | ||
import { createClient, RedisClientType } from 'redis'; | ||
|
||
describe('RedisHelper', () => { | ||
let redisHelper: RedisHelper; | ||
let redisClientMock: jest.Mocked<ReturnType<typeof createClient>>; | ||
let redisClient: RedisClientType; | ||
|
||
beforeAll(async () => { | ||
redisHelper = new RedisHelper(); | ||
redisClient = createClient({ url: process.env.REDIS_URL }); | ||
await redisClient.connect(); | ||
redisClientMock = Reflect.get(redisHelper, 'redisClient') as jest.Mocked<ReturnType<typeof createClient>>; | ||
}); | ||
|
||
afterAll(async () => { | ||
await redisClient.quit(); | ||
}); | ||
|
||
describe('initialize', () => { | ||
it('should connect to redis client', async () => { | ||
const connect = jest.spyOn(redisClientMock, 'connect'); | ||
|
||
await redisHelper.initialize(); | ||
|
||
expect(connect).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('close', () => { | ||
it('should close redis client', async () => { | ||
const quit = jest.spyOn(redisClientMock, 'quit'); | ||
|
||
await redisHelper.close(); | ||
|
||
expect(quit).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not throw error on close if client is already closed', async () => { | ||
const quit = jest.spyOn(redisClientMock, 'quit'); | ||
|
||
quit.mockClear(); | ||
|
||
await redisHelper.close(); | ||
await redisHelper.close(); | ||
await redisHelper.close(); | ||
|
||
expect(quit).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('computeEventCountForPeriod', () => { | ||
beforeEach(async () => { | ||
await redisHelper.initialize(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await redisHelper.close(); | ||
}); | ||
|
||
it('should return event count', async () => { | ||
const ruleId = 'ruleId'; | ||
const groupHash = 'groupHash'; | ||
const projectId = 'projectId'; | ||
const thresholdPeriod = 1000; | ||
|
||
const currentEventCount = await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod); | ||
|
||
expect(currentEventCount).toBe(1); | ||
}); | ||
|
||
it('should reset counter and timestamp if threshold period is expired', async () => { | ||
const ruleId = 'ruleId'; | ||
const currentDate = Date.now(); | ||
const groupHash = 'groupHash'; | ||
const projectId = 'projectId'; | ||
const thresholdPeriod = 1000; | ||
|
||
/** | ||
* Send several events to increment counter | ||
*/ | ||
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod); | ||
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod); | ||
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod); | ||
|
||
/** | ||
* Update current date for threshold period expiration | ||
*/ | ||
jest.spyOn(global.Date, 'now').mockImplementation(() => currentDate + 2 * thresholdPeriod + 1); | ||
|
||
const currentEventCount = await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod); | ||
const currentlyStoredTimestamp = await redisClient.hGet(`${projectId}:${ruleId}:${groupHash}:${thresholdPeriod}:times`, 'timestamp'); | ||
|
||
expect(currentEventCount).toBe(1); | ||
expect(currentlyStoredTimestamp).toBe(Date.now().toString()); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.