Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/@webex/webex-core/src/lib/services/service-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ const ServiceUrl = AmpState.extend({
? this.hosts.filter((host) => host.id === clusterId)
: this.hosts.filter((host) => host.homeCluster);

// Filter out hosts with non-positive priorities (including -1)
filteredHosts = filteredHosts.filter((host) => host.priority > 0);

// If no valid hosts remain after filtering, return the default URL
if (filteredHosts.length === 0) {
return this.defaultUrl;
}

const aliveHosts = filteredHosts.filter((host) => !host.failed);

filteredHosts =
Expand All @@ -76,7 +84,7 @@ const ServiceUrl = AmpState.extend({
filteredHosts.reduce(
(previous, current) =>
previous.priority > current.priority || !previous.homeCluster ? current : previous,
{}
filteredHosts[0]
).host
);
},
Expand Down
110 changes: 110 additions & 0 deletions packages/@webex/webex-core/test/unit/spec/services/service-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,116 @@ describe('webex-core', () => {

assert.isTrue(homeClusterUrls.every((host) => !host.failed));
});

describe('when hosts have negative priorities', () => {
it('should return defaultUrl when all hosts have negative priorities', () => {
const negativeServiceUrl = new ServiceUrl({
defaultUrl: 'https://default.example.com/api/v1',
hosts: [
{
host: 'example-host-neg1.com',
priority: -1,
ttl: -1,
id: '1',
homeCluster: true,
},
{
host: 'example-host-neg2.com',
priority: -1,
ttl: -1,
id: '2',
homeCluster: true,
},
],
name: 'negative-priority-test',
});

assert.equal(
negativeServiceUrl._getPriorityHostUrl(),
'https://default.example.com/api/v1'
);
});

it('should return defaultUrl when all hosts have zero priority', () => {
const zeroServiceUrl = new ServiceUrl({
defaultUrl: 'https://default.example.com/api/v1',
hosts: [
{
host: 'example-host-zero.com',
priority: 0,
ttl: -1,
id: '1',
homeCluster: true,
},
],
name: 'zero-priority-test',
});

assert.equal(zeroServiceUrl._getPriorityHostUrl(), 'https://default.example.com/api/v1');
});

it('should ignore hosts with negative priorities and return valid host', () => {
const mixedServiceUrl = new ServiceUrl({
defaultUrl: 'https://default.example.com/api/v1',
hosts: [
{
host: 'example-host-neg.com',
priority: -1,
ttl: -1,
id: '1',
homeCluster: true,
},
{
host: 'example-host-valid.com',
priority: 5,
ttl: -1,
id: '2',
homeCluster: true,
},
],
name: 'mixed-priority-test',
});

const result = mixedServiceUrl._getPriorityHostUrl();

assert.include(result, 'example-host-valid.com');
assert.notInclude(result, 'example-host-neg.com');
});

it('should select lowest positive priority host when mixed with negative priorities', () => {
const mixedServiceUrl = new ServiceUrl({
defaultUrl: 'https://default.example.com/api/v1',
hosts: [
{
host: 'example-host-neg.com',
priority: -1,
ttl: -1,
id: '1',
homeCluster: true,
},
{
host: 'example-host-p5.com',
priority: 5,
ttl: -1,
id: '2',
homeCluster: true,
},
{
host: 'example-host-p2.com',
priority: 2,
ttl: -1,
id: '3',
homeCluster: true,
},
],
name: 'mixed-priority-test',
});

const result = mixedServiceUrl._getPriorityHostUrl();

assert.include(result, 'example-host-p2.com');
});
});
});

describe('#failHost()', () => {
Expand Down
Loading