agones.dev/agones@v1.53.0/test/sdk/nodejs/testSDKClient.js (about) 1 // Copyright 2019 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 const AgonesSDK = require('@google-cloud/agones-sdk'); 16 const {setTimeout} = require('timers/promises'); 17 18 const agonesSDK = new AgonesSDK(); 19 20 const connect = async () => { 21 let UID = ''; 22 try { 23 console.log("attempting to connect"); 24 await agonesSDK.connect(); 25 console.log("connected!"); 26 let once = true; 27 agonesSDK.watchGameServer((result) => { 28 console.log('watch', result); 29 UID = result.objectMeta.uid.toString(); 30 if (once) { 31 console.log('Setting annotation ', UID); 32 agonesSDK.setAnnotation('annotation', UID); 33 once = false; 34 } 35 }, (error) => { 36 console.error('Watch ERROR', error); 37 }); 38 await agonesSDK.ready(); 39 40 let result = await agonesSDK.getGameServer(); 41 await agonesSDK.setLabel('label', result.objectMeta.creationTimestamp.toString()); 42 console.log('gameServer', result); 43 console.log('creation Timestamp', result.objectMeta.creationTimestamp); 44 result = await agonesSDK.health(); 45 console.log('health', result); 46 47 await agonesSDK.reserve(5); 48 49 await setTimeout(1000); 50 console.log('send allocate request'); 51 agonesSDK.allocate(); 52 53 await testCounts(agonesSDK); 54 await testLists(agonesSDK); 55 56 await setTimeout(1000); 57 console.log('send shutdown request'); 58 agonesSDK.shutdown(); 59 60 await setTimeout(2000); 61 console.log('closing agones SDK'); 62 // Closing Agones SDK and all event emitters 63 agonesSDK.close() 64 65 await setTimeout(2000); 66 process.exit(0); 67 } catch (error) { 68 console.error(error); 69 } 70 }; 71 72 const testCounts = async(sdk) => { 73 // LocalSDKServer starting "rooms": {Count: 1, Capacity: 10} 74 const counter = "rooms"; 75 76 try { 77 let count = await sdk.beta.getCounterCount(counter); 78 if (count !== 1) { 79 throw new Error(`Counter count should be 1, but is ${count}`); 80 } 81 } catch (error) { 82 throw new Error(`Error getting Counter count: ${error}`); 83 } 84 85 try { 86 await sdk.beta.incrementCounter(counter, 9); 87 } catch (error) { 88 throw new Error(`Error incrementing Counter: ${error}`); 89 } 90 91 try { 92 await sdk.beta.decrementCounter(counter, 10); 93 } catch (error) { 94 throw new Error(`Error decrementing Counter: ${error}`); 95 } 96 97 try { 98 await sdk.beta.setCounterCount(counter, 10); 99 } catch (error) { 100 throw new Error(`Error setting Counter count: ${error}`); 101 } 102 103 try { 104 let capacity = await sdk.beta.getCounterCapacity(counter); 105 if (capacity !== 10) { 106 throw new Error(`Counter capacity should be 10, but is ${capacity}`); 107 } 108 } catch (error) { 109 throw new Error(`Error getting Counter capacity: ${error}`); 110 } 111 112 try { 113 await sdk.beta.setCounterCapacity(counter, 1); 114 } catch (error) { 115 throw new Error(`Error setting Counter capacity: ${error}`); 116 } 117 } 118 119 const testLists = async(sdk) => { 120 // LocalSDKServer starting "players": {Values: []string{"test0", "test1", "test2"}, Capacity: 100}} 121 const list = "players" 122 const listValues = ["test0", "test1", "test2"] 123 124 let contains = await sdk.beta.listContains(list, "test1"); 125 if (!contains) { 126 throw new Error("List should contain value \"test1\""); 127 } 128 129 try { 130 let length = await sdk.beta.getListLength(list); 131 if (length !== 3) { 132 throw new Error(`List length should be 3, but is ${length}`); 133 } 134 } catch (error) { 135 throw new Error(`Error getting List length: ${error}`); 136 } 137 138 try { 139 let values = await sdk.beta.getListValues(list); 140 if (JSON.stringify(values) !== JSON.stringify(listValues)) { 141 throw new Error(`List values should be ${listValues}, but is ${values}`); 142 } 143 } catch (error) { 144 throw new Error(`Error getting List values: ${error}`); 145 } 146 147 await sdk.beta.appendListValue(list, "test3"); 148 149 await sdk.beta.deleteListValue(list, "test2"); 150 151 try { 152 let capacity = await sdk.beta.getListCapacity(list); 153 if (capacity !== 100) { 154 throw new Error(`List capacity should be 100, but is ${capacity}`); 155 } 156 } catch (error) { 157 throw new Error(`Error getting List capacity: ${error}`); 158 } 159 160 await sdk.beta.setListCapacity(list, 2); 161 } 162 163 connect();