Skip to content

Commit

Permalink
jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Geczy committed Aug 31, 2023
1 parent c4aa511 commit f60bf37
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ services:
- BUILD_CONTEXT=packages/dota
hostname: dota
ports:
- "81:5000"
- "81:5120"
- "9229:9229"
environment:
- D2PT_TOKEN
Expand Down
7 changes: 5 additions & 2 deletions packages/dota/src/__tests__/setup-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ beforeAll((done) => {

const interval = setInterval(() => {
apiClient
.post('/')
.get('/')
.then((response) => {
if (response?.data) {
console.log('response.data', response.data)
clearInterval(interval) // Stop the interval
done() // Continue with the tests
setTimeout(() => {
done() // Continue with the tests
}, 3000)
}
})
.catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ describe('aegis denied', () => {

// check memory usage
it('should not leak memory', () => {
const used = process.memoryUsage()
console.log('memory', used.heapUsed / 1000000)
const { heapUsed } = process.memoryUsage()
const mb = heapUsed / 1000000
// value in megabytes
expect(used.heapUsed / 1000000).toBeLessThan(350_000)
expect(mb).toBeLessThan(350)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DotaEventTypes } from '../../../../types.js'

describe('aegis picked up', () => {
// might be less than 100 if some users are offline
let USER_COUNT = 200
let USER_COUNT = 50

const promises: Promise<any>[] = []

Expand Down Expand Up @@ -74,9 +74,13 @@ describe('aegis picked up', () => {
const callLength = twitchChatSpy.mock.calls.filter(([, message]) =>
message.match(/(?:picked|подбирает)/i),
).length

const { heapUsed, rss } = process.memoryUsage()
const mb = rss / 1000000
try {
expect(callLength).toBe(USER_COUNT)

expect(mb).toBeLessThan(400)

clearInterval(interval)
done()
} catch (e) {
Expand All @@ -89,10 +93,10 @@ describe('aegis picked up', () => {
consecutiveSameCount = 0
}

if (consecutiveSameCount > 5) {
if (consecutiveSameCount > 2) {
clearInterval(interval)
done(
`call count ${USER_COUNT} ${currentCallCount} ${twitchChatSpy.mock.calls.length} did not change for 5 intervals`,
`call count ${USER_COUNT} ${currentCallCount} ${twitchChatSpy.mock.calls.length} and mb use ${mb}`,
)
}

Expand All @@ -103,11 +107,23 @@ describe('aegis picked up', () => {
USER_COUNT * 1000,
)

// check memory usage
it('should not leak memory', () => {
const used = process.memoryUsage()
console.log('memory', used.heapUsed / 1000000)
// value in megabytes
expect(used.heapUsed / 1000000).toBeLessThan(350_000)
})
it('should still access server', (done) => {
const interval = setInterval(() => {
apiClient
.get('/')
.then((response) => {
if (response?.data) {
console.log('response.data', response.data)
clearInterval(interval) // Stop the interval
setTimeout(() => {
done() // Continue with the tests
}, 3000)
}
})
.catch((error) => {
// Handle error, perhaps log it but don't call done(error) here
// because we're inside an interval and it will keep calling
})
}, 500) // Check every 1 second
}, 5000)
})
2 changes: 1 addition & 1 deletion services/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ server {
}

location / {
proxy_pass http://dota:5000;
proxy_pass http://dota:5120;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
Expand Down

0 comments on commit f60bf37

Please sign in to comment.