Skip to content

Commit

Permalink
Simplify the selection of the dynamic port in the nodejs sdk. (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberthbailey authored and pooneh-m committed Oct 21, 2019
1 parent 82f0936 commit 15ce0a1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
24 changes: 6 additions & 18 deletions sdks/nodejs/spec/agonesSDK.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,16 @@ describe('agones', () => {
expect(port).toEqual('59357');
});

it('returns the default port if $AGONES_SDK_GRPC_PORT is not an integer', async () => {
process.env.AGONES_SDK_GRPC_PORT = "random string";
let port = agonesSDK.port;
expect(port).toEqual('59357');
});

it('returns the default port if $AGONES_SDK_GRPC_PORT is to large of an integer', async () => {
process.env.AGONES_SDK_GRPC_PORT = '4455667788';
let port = agonesSDK.port;
expect(port).toEqual('59357');
});

it('returns the default port if $AGONES_SDK_GRPC_PORT is to small of an integer', async () => {
process.env.AGONES_SDK_GRPC_PORT = '0';
it('returns a valid port set in $AGONES_SDK_GRPC_PORT', async () => {
process.env.AGONES_SDK_GRPC_PORT = '6789';
let port = agonesSDK.port;
expect(port).toEqual('59357');
expect(port).toEqual('6789');
});

it('returns the port set in $AGONES_SDK_GRPC_PORT', async () => {
process.env.AGONES_SDK_GRPC_PORT = '6789';
it('returns an invalid port set in $AGONES_SDK_GRPC_PORT', async () => {
process.env.AGONES_SDK_GRPC_PORT = 'foo';
let port = agonesSDK.port;
expect(port).toEqual('6789');
expect(port).toEqual('foo');
});
});

Expand Down
16 changes: 1 addition & 15 deletions sdks/nodejs/src/agonesSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,8 @@ class AgonesSDK {
}

get port() {
const defaultPort = '59357';
const port = process.env.AGONES_SDK_GRPC_PORT;
if (port === undefined) {
console.log(`Environment variable AGONES_SDK_GRPC_PORT not defined, using default port ${defaultPort}`);
return defaultPort;
}
const portNum = parseInt(port, 10);
if (isNaN(portNum)) {
console.log(`Unable to parse '${port}' defined in AGONES_SDK_GRPC_PORT into an integer`);
return defaultPort;
}
if (portNum < 1 || portNum > 65535) {
console.log(`Invalid port ${portNum} defined in AGONES_SDK_GRPC_PORT. It must be between 1 and 65535`);
return defaultPort;
}
return port;
return port === undefined ? '59357' : port;
}

async connect() {
Expand Down

0 comments on commit 15ce0a1

Please sign in to comment.