Skip to content

Commit bad5b4f

Browse files
committed
feat: added game server object and tests
1 parent 9790f15 commit bad5b4f

6 files changed

Lines changed: 557 additions & 34 deletions

File tree

.github/workflows/ci.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: "⚙️ CI & Release Workflow"
22
on:
33
push:
44
pull_request:
5-
types: [ opened, reopened ]
5+
types: [ opened, synchronize, reopened ]
66

77
env:
88
REGISTRY: ghcr.io
@@ -14,8 +14,8 @@ permissions:
1414
packages: write
1515

1616
jobs:
17-
test:
18-
name: Run Tests
17+
build:
18+
name: Build and Test
1919
runs-on: ubuntu-latest
2020
steps:
2121
- name: 📥 Checkout Code
@@ -30,13 +30,16 @@ jobs:
3030
- name: "⚡ Setup Gradle with Cache"
3131
uses: gradle/actions/setup-gradle@v5
3232

33-
- name: "🧪 Run Unit Tests"
33+
- name: "🧪 Run Tests"
3434
run: ./gradlew test
35+
36+
- name: "🏗️ Build"
37+
run: ./gradlew build
3538
releaser:
3639
name: "🏁 Release"
3740
if: startsWith(github.ref, 'refs/tags/')
3841
needs:
39-
- test
42+
- build
4043
runs-on: ubuntu-latest
4144
steps:
4245
- name: "📥 Checkout Code (Full History)"
@@ -64,4 +67,4 @@ jobs:
6467
VERSION="${RAW_TAG#v}"
6568
6669
[[ "$VERSION" =~ ^[0-9]+(\.[0-9]+)*$ ]] || exit -1
67-
./gradlew publishToCentralPortal -Pversion="$VERSION"
70+
./gradlew publishToCentralPortal -Pversion="$VERSION"

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ information {
4040
dependencies {
4141
implementation("com.squareup.okhttp3:okhttp:5.1.0")
4242
implementation("com.google.code.gson:gson:2.13.1")
43+
testImplementation("org.junit.jupiter:junit-jupiter:5.13.4")
44+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
4345
}
4446

4547
java.toolchain.languageVersion.set(JavaLanguageVersion.of(21))
48+
49+
tasks.test {
50+
useJUnitPlatform()
51+
}

src/main/java/studio/o7/agones/sdk/AgonesSDK.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ static AgonesSDK NewSDK() {
1515

1616
AgonesBeta beta();
1717

18+
void deallocate() throws IOException;
19+
20+
GameServer gameServer() throws IOException;
21+
1822
void health() throws IOException;
1923

2024
void ready() throws IOException;

src/main/java/studio/o7/agones/sdk/AgonesSDKImpl.java

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,110 @@ final class AgonesSDKImpl implements AgonesSDK {
2020
private static final String DEFAULT_URL = "http://localhost:9358/";
2121
private static final Gson GSON = new GsonBuilder().create();
2222

23-
private final String url = getUrl();
24-
private final OkHttpClient client = new OkHttpClient();
23+
private final String url;
24+
private final OkHttpClient client;
2525

26-
private final Request allocateRequest = new Request.Builder()
27-
.url(url + "allocate")
28-
.addHeader("Content-Type", "application/json")
29-
.post(RequestBody.create("{}", MediaType.get("application/json")))
30-
.build();
26+
AgonesSDKImpl() {
27+
this(getUrl());
28+
}
29+
30+
AgonesSDKImpl(@NonNull String url) {
31+
this.url = url.endsWith("/") ? url : url + "/";
32+
this.client = new OkHttpClient();
33+
}
3134

3235
@Override
3336
public void allocate() throws IOException {
34-
try (Response response = client.newCall(allocateRequest).execute()) {
37+
Request request = new Request.Builder()
38+
.url(url + "allocate")
39+
.addHeader("Content-Type", "application/json")
40+
.post(RequestBody.create("{}", MediaType.get("application/json")))
41+
.build();
42+
43+
try (Response response = client.newCall(request).execute()) {
3544
if (response.isSuccessful()) return;
3645
int code = response.code();
3746
String message = response.message();
3847
throw new IOException("Allocate request failed: " + code + " - " + message);
3948
}
4049
}
4150

42-
private final Request healthRequest = new Request.Builder()
43-
.url(url + "health")
44-
.addHeader("Content-Type", "application/json")
45-
.post(RequestBody.create("{}", MediaType.get("application/json")))
46-
.build();
51+
@Override
52+
public void deallocate() throws IOException {
53+
Request request = readyRequest();
54+
try (Response response = client.newCall(request).execute()) {
55+
if (response.isSuccessful()) return;
56+
int code = response.code();
57+
String message = response.message();
58+
throw new IOException("Deallocate request failed: " + code + " - " + message);
59+
}
60+
}
61+
62+
@Override
63+
public GameServer gameServer() throws IOException {
64+
Request request = new Request.Builder()
65+
.url(url + "gameserver")
66+
.addHeader("Content-Type", "application/json")
67+
.get()
68+
.build();
69+
70+
try (Response response = client.newCall(request).execute()) {
71+
if (response.isSuccessful()) {
72+
if (response.body() == null) {
73+
throw new IOException("GameServer request failed: empty response body");
74+
}
75+
return GSON.fromJson(response.body().string(), GameServer.class);
76+
}
77+
int code = response.code();
78+
String message = response.message();
79+
throw new IOException("GameServer request failed: " + code + " - " + message);
80+
}
81+
}
4782

4883
@Override
4984
public void health() throws IOException {
50-
try (Response response = client.newCall(healthRequest).execute()) {
85+
Request request = new Request.Builder()
86+
.url(url + "health")
87+
.addHeader("Content-Type", "application/json")
88+
.post(RequestBody.create("{}", MediaType.get("application/json")))
89+
.build();
90+
91+
try (Response response = client.newCall(request).execute()) {
5192
if (response.isSuccessful()) return;
5293
int code = response.code();
5394
String message = response.message();
5495
throw new IOException("Health request failed: " + code + " - " + message);
5596
}
5697
}
5798

58-
private final Request readyRequest = new Request.Builder()
59-
.url(url + "ready")
60-
.addHeader("Content-Type", "application/json")
61-
.post(RequestBody.create("{}", MediaType.get("application/json")))
62-
.build();
63-
6499
@Override
65100
public void ready() throws IOException {
66-
try (Response response = client.newCall(readyRequest).execute()) {
101+
Request request = readyRequest();
102+
try (Response response = client.newCall(request).execute()) {
67103
if (response.isSuccessful()) return;
68104
int code = response.code();
69105
String message = response.message();
70106
throw new IOException("Ready request failed: " + code + " - " + message);
71107
}
72108
}
73109

74-
private final Request shutdownRequest = new Request.Builder()
75-
.url(url + "shutdown")
76-
.addHeader("Content-Type", "application/json")
77-
.post(RequestBody.create("{}", MediaType.get("application/json")))
78-
.build();
110+
private Request readyRequest() {
111+
return new Request.Builder()
112+
.url(url + "ready")
113+
.addHeader("Content-Type", "application/json")
114+
.post(RequestBody.create("{}", MediaType.get("application/json")))
115+
.build();
116+
}
79117

80118
@Override
81119
public void shutdown() throws IOException {
82-
try (Response response = client.newCall(shutdownRequest).execute()) {
120+
Request request = new Request.Builder()
121+
.url(url + "shutdown")
122+
.addHeader("Content-Type", "application/json")
123+
.post(RequestBody.create("{}", MediaType.get("application/json")))
124+
.build();
125+
126+
try (Response response = client.newCall(request).execute()) {
83127
if (response.isSuccessful()) return;
84128
int code = response.code();
85129
String message = response.message();
@@ -238,7 +282,7 @@ private Counter getCounter(@NonNull String key) throws IOException {
238282
};
239283
}
240284

241-
private String getUrl() {
285+
private static String getUrl() {
242286
String port = System.getenv("AGONES_SDK_HTTP_PORT");
243287
if (port == null || port.isEmpty()) return DEFAULT_URL;
244288
return "http://localhost:" + port + "/";

0 commit comments

Comments
 (0)