Skip to content

Commit

Permalink
Add Reserve to Node.js SDK (#955)
Browse files Browse the repository at this point in the history
* Add Reserve to Node.js SDK
Update tests and docs
* Add reserve() calls in example and conformance test
  • Loading branch information
steven-supersolid authored and markmandel committed Jul 30, 2019
1 parent 4fae9b5 commit 565431f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ run-sdk-conformance-test:

# Run a conformance test for all SDKs supported
run-sdk-conformance-tests:
$(MAKE) run-sdk-conformance-test SDK_FOLDER=node
$(MAKE) run-sdk-conformance-test SDK_FOLDER=node TESTS=ready,allocate,setlabel,setannotation,gameserver,health,shutdown,watch,reserve
$(MAKE) run-sdk-conformance-test SDK_FOLDER=go TESTS=ready,allocate,setlabel,setannotation,gameserver,health,shutdown,watch,reserve
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust
16 changes: 12 additions & 4 deletions examples/nodejs-simple/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

const AgonesSDK = require('agones');

const agonesSDK = new AgonesSDK();
const agonesSDK = new AgonesSDK();

const connect = async () => {
agonesSDK.watchGameServer((result) => {
console.log("GameServer Update:\n\tname:", result.objectMeta.name, "\n\tstate:", result.status.state);
console.log('GameServer Update:\n\tname:', result.objectMeta.name, '\n\tstate:', result.status.state);
});
let healthInterval = setInterval(() => {
agonesSDK.health();
Expand All @@ -29,9 +29,9 @@ const connect = async () => {
console.log('node.js Game Server has started!');

console.log('Setting a label');
await agonesSDK.setLabel("test-label", "test-value");
await agonesSDK.setLabel('test-label', 'test-value');
console.log('Setting an annotation');
await agonesSDK.setAnnotation("test-annotation", "test value");
await agonesSDK.setAnnotation('test-annotation', 'test value');

console.log('Marking server as ready...');
await agonesSDK.ready();
Expand All @@ -42,6 +42,14 @@ const connect = async () => {
count = count + 10;
console.log('Running for', count, 'seconds!');
}, 10000);
setTimeout(async () => {
console.log('Allocating');
await agonesSDK.allocate();
}, 15000);
setTimeout(async () => {
console.log('Reserving for 20 seconds');
await agonesSDK.reserve(20);
}, 25000);
setTimeout(() => {
console.log('Shutting down after 60 seconds...');
agonesSDK.shutdown();
Expand Down
29 changes: 29 additions & 0 deletions sdks/nodejs/spec/agonesSDK.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,33 @@ describe('agones', () => {
expect(serverEmitter.call.cancel).toHaveBeenCalled();
});
});

describe('reserve', () => {
it('calls the server with duration parameter and handles success', async () => {
spyOn(agonesSDK.client, 'reserve').and.callFake((request, callback) => {
let result = new messages.Empty();
callback(undefined, result);
});

let result = await agonesSDK.reserve(10);
expect(agonesSDK.client.reserve).toHaveBeenCalled();
expect(result).toEqual({});

let request = agonesSDK.client.reserve.calls.argsFor(0)[0];
expect(request.getSeconds()).toEqual(10);
});

it('calls the server and handles failure', async () => {
spyOn(agonesSDK.client, 'reserve').and.callFake((request, callback) => {
callback('error', undefined);
});
try {
await agonesSDK.reserve(10);
fail();
} catch (error) {
expect(agonesSDK.client.reserve).toHaveBeenCalled();
expect(error).toEqual('error');
}
});
});
});
14 changes: 14 additions & 0 deletions sdks/nodejs/src/agonesSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ class AgonesSDK {
});
});
}

async reserve(duration) {
const request = new messages.Duration();
request.setSeconds(duration);
return new Promise((resolve, reject) => {
this.client.reserve(request, (error, response) => {
if (error) {
reject(error);
} else {
resolve(response.toObject());
}
});
});
}
}

module.exports = AgonesSDK;
9 changes: 8 additions & 1 deletion site/content/en/docs/Guides/Client SDKs/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ To mark the game server as [ready to receive player connections]({{< relref "_in
let result = await agonesSDK.ready();
```

Similarly `shutdown()`, `setAnnotation(key, value)` and `setLabel(key, value)` are async methods that perform an action and return an empty result.
Similarly `shutdown()`, `allocate()`, `setAnnotation(key, value)` and `setLabel(key, value)` are async methods that perform an action and return an empty result.

To get [details of the backing GameServer]({{< relref "_index.md#gameserver" >}}) call the async method `getGameServer()`. The result will be an object representing `GameServer` defined in {{< ghlink href="sdk.proto" >}}`sdk.proto`{{< /ghlink >}}.

Expand All @@ -64,4 +64,11 @@ agonesSDK.watchGameServer((result) => {
});
```

{{% feature publishVersion="0.12.0" %}}

<!--TODO: Change the link to a relref once 0.12.0 has been published so that this passes link checking -->
To mark the game server as <a href="../#reserve-seconds" data-proofer-ignore>reserved</a> for a period of time, call the async method `reserve(seconds)`. The result will be an empty object.

{{% /feature %}}

For more information, please read the [SDK Overview]({{< relref "_index.md" >}}), check out {{< ghlink href="sdks/nodejs/src/agonesSDK.js" >}}agonesSDK.js{{< /ghlink >}} and also look at the {{< ghlink href="examples/nodejs-simple" >}}Node.js example{{< / >}}.
68 changes: 34 additions & 34 deletions test/sdk/nodejs/testSDKClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const AgonesSDK = require("agones");
const AgonesSDK = require('agones');
const agonesSDK = new AgonesSDK();

const connect = async function() {
var UID = ""
try {
var once = true;
agonesSDK.watchGameServer((result) => {
console.log("watch", result);
UID = result.objectMeta.uid.toString();
if (once) {
console.log("Setting annotation ", UID)
agonesSDK.setAnnotation("annotation", UID);
once = false;
}
});
await agonesSDK.ready();
setTimeout(() => {
console.log("send allocate request");
agonesSDK.allocate();
}, 1000);
const connect = async () => {
let UID = '';
try {
let once = true;
agonesSDK.watchGameServer((result) => {
console.log('watch', result);
UID = result.objectMeta.uid.toString();
if (once) {
console.log('Setting annotation ', UID);
agonesSDK.setAnnotation('annotation', UID);
once = false;
}
});
await agonesSDK.ready();
setTimeout(() => {
console.log('send allocate request');
agonesSDK.allocate();
}, 1000);

let result = await agonesSDK.getGameServer();
await agonesSDK.setLabel('label', result.objectMeta.creationTimestamp.toString());
console.log('gameServer', result);
console.log('creation Timestamp', result.objectMeta.creationTimestamp);
result = await agonesSDK.health();
console.log('health', result);

let result = await agonesSDK.getGameServer();
await agonesSDK.setLabel("label",
result.objectMeta.creationTimestamp.toString());
console.log("gameServer", result);
console.log("creation Timestamp", result.objectMeta.creationTimestamp)
result = await agonesSDK.health();
console.log("health", result);
await agonesSDK.reserve(5);

setTimeout(() => {
console.log("send shutdown request");
agonesSDK.shutdown();
}, 1000);
} catch (error) {
console.error(error);
}
setTimeout(() => {
console.log('send shutdown request');
agonesSDK.shutdown();
}, 1000);
} catch (error) {
console.error(error);
}
};

connect();
connect();

0 comments on commit 565431f

Please sign in to comment.