diff --git a/build/includes/sdk.mk b/build/includes/sdk.mk index 0654efe820..8ca9d25c06 100644 --- a/build/includes/sdk.mk +++ b/build/includes/sdk.mk @@ -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 diff --git a/examples/nodejs-simple/src/index.js b/examples/nodejs-simple/src/index.js index 270b4b0fa1..c735e27932 100644 --- a/examples/nodejs-simple/src/index.js +++ b/examples/nodejs-simple/src/index.js @@ -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(); @@ -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(); @@ -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(); diff --git a/sdks/nodejs/spec/agonesSDK.spec.js b/sdks/nodejs/spec/agonesSDK.spec.js index fc78bb3b27..52b992b232 100644 --- a/sdks/nodejs/spec/agonesSDK.spec.js +++ b/sdks/nodejs/spec/agonesSDK.spec.js @@ -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'); + } + }); + }); }); diff --git a/sdks/nodejs/src/agonesSDK.js b/sdks/nodejs/src/agonesSDK.js index 02791bc537..40bb950316 100644 --- a/sdks/nodejs/src/agonesSDK.js +++ b/sdks/nodejs/src/agonesSDK.js @@ -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; diff --git a/site/content/en/docs/Guides/Client SDKs/nodejs.md b/site/content/en/docs/Guides/Client SDKs/nodejs.md index d5c379b8ed..31815df24c 100644 --- a/site/content/en/docs/Guides/Client SDKs/nodejs.md +++ b/site/content/en/docs/Guides/Client SDKs/nodejs.md @@ -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 >}}. @@ -64,4 +64,11 @@ agonesSDK.watchGameServer((result) => { }); ``` +{{% feature publishVersion="0.12.0" %}} + + +To mark the game server as reserved 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{{< / >}}. diff --git a/test/sdk/nodejs/testSDKClient.js b/test/sdk/nodejs/testSDKClient.js index c99bf10431..f7d449ad74 100644 --- a/test/sdk/nodejs/testSDKClient.js +++ b/test/sdk/nodejs/testSDKClient.js @@ -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(); \ No newline at end of file +connect();