Skip to content

Commit

Permalink
[INF-500] Fix libs tests (#6228)
Browse files Browse the repository at this point in the history
  • Loading branch information
sliptype authored Oct 4, 2023
1 parent 9d46861 commit c6a90c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
48 changes: 25 additions & 23 deletions packages/libs/src/utils/getNStorageNodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ describe('isNodeHealthy', () => {
})

it('should return true when node is healthy', async () => {
nock('http://node1.com').get('/status').reply(200)
nock('http://node1.com').get('/health_check').reply(200)

const result = await isNodeHealthy('http://node1.com')
assert.equal(result, true)
})

it('should return false when node is not healthy', async () => {
nock('http://node1.com').get('/status').reply(500)
nock('http://node1.com').get('/health_check').reply(500)

const result = await isNodeHealthy('http://node1.com')
assert.equal(result, false)
})

it('should return false when an error occurs', async () => {
nock('http://invalid-url').get('/status').replyWithError('Request failed')
nock('http://invalid-url')
.get('/health_check')
.replyWithError('Request failed')

const result = await isNodeHealthy('http://invalid-url')
assert.equal(result, false)
Expand All @@ -65,18 +67,18 @@ describe('getNStorageNodes', () => {
})

it('should return healthy nodes when no rendezvousKey is provided', async () => {
nock('http://node1.com').get('/status').reply(500)
nock('http://node2.com').get('/status').reply(500)
nock('http://node3.com').get('/status').reply(200)
nock('http://node1.com').get('/health_check').reply(500)
nock('http://node2.com').get('/health_check').reply(500)
nock('http://node3.com').get('/health_check').reply(200)

const result = await getNStorageNodes(sampleNodes, 2)
assert.deepEqual(result, ['http://node3.com'])
})

it('should return all healthy nodes when no numNodes is not specified and all nodes are healthy', async () => {
nock('http://node1.com').get('/status').reply(200)
nock('http://node2.com').get('/status').reply(200)
nock('http://node3.com').get('/status').reply(200)
nock('http://node1.com').get('/health_check').reply(200)
nock('http://node2.com').get('/health_check').reply(200)
nock('http://node3.com').get('/health_check').reply(200)

const result = await getNStorageNodes(sampleNodes)
assert.deepEqual(result, [
Expand All @@ -87,40 +89,40 @@ describe('getNStorageNodes', () => {
})

it('should return all healthy nodes when no numNodes is not specified and 1 node is unhealthy', async () => {
nock('http://node1.com').get('/status').reply(200)
nock('http://node2.com').get('/status').reply(200)
nock('http://node3.com').get('/status').reply(500)
nock('http://node1.com').get('/health_check').reply(200)
nock('http://node2.com').get('/health_check').reply(200)
nock('http://node3.com').get('/health_check').reply(500)

const result = await getNStorageNodes(sampleNodes)
assert.deepEqual(result, ['http://node1.com', 'http://node2.com'])
})

it('should return nodes sorted by rendezvous score when a rendezvousKey is provided', async () => {
nock('http://node1.com').get('/status').reply(200)
nock('http://node2.com').get('/status').reply(200)
nock('http://node3.com').get('/status').reply(200)
nock('http://node1.com').get('/health_check').reply(200)
nock('http://node2.com').get('/health_check').reply(200)
nock('http://node3.com').get('/health_check').reply(200)

const result = await getNStorageNodes(sampleNodes, 3, 'test-rendezvous-key')
assert.deepEqual(result, [
'http://node3.com',
'http://node2.com',
'http://node1.com',
'http://node3.com'
'http://node1.com'
])
})

it('should return only the healthy nodes sorted by rendezvous score when a rendezvousKey is provided', async () => {
nock('http://node1.com').get('/status').reply(200)
nock('http://node2.com').get('/status').reply(200)
nock('http://node3.com').get('/status').reply(500)
nock('http://node1.com').get('/health_check').reply(200)
nock('http://node2.com').get('/health_check').reply(200)
nock('http://node3.com').get('/health_check').reply(500)

const result = await getNStorageNodes(sampleNodes, 3, 'test-rendezvous-key')
assert.deepEqual(result, ['http://node2.com', 'http://node1.com'])
})

it('should return an empty array when there are no healthy nodes and numNodes is specified', async () => {
nock('http://node1.com').get('/status').reply(500)
nock('http://node2.com').get('/status').reply(500)
nock('http://node3.com').get('/status').reply(500)
nock('http://node1.com').get('/health_check').reply(500)
nock('http://node2.com').get('/health_check').reply(500)
nock('http://node3.com').get('/health_check').reply(500)

const result = await getNStorageNodes(sampleNodes, 2)
assert.deepEqual(result, [])
Expand Down
3 changes: 2 additions & 1 deletion packages/libs/src/utils/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert'

import { describe, it } from 'mocha'
import nock from 'nock'

import { timeRequests } from './network'
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('timeRequests()', () => {

// Make sure there is some variance
assert(!allResults.every((val) => val === allResults[0]))
}, 10000)
}).timeout(10000)

it('filterNonResponsive = true', async () => {
const requests = [
Expand Down

0 comments on commit c6a90c4

Please sign in to comment.