Skip to content

Commit

Permalink
Adds Counter to CSharp SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Jan 10, 2024
1 parent 9534c07 commit a878647
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
4 changes: 3 additions & 1 deletion build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ DEFAULT_CONFORMANCE_TESTS = ready,allocate,setlabel,setannotation,gameserver,hea
ALPHA_CONFORMANCE_TESTS = getplayercapacity,setplayercapacity,playerconnect,playerdisconnect,getplayercount,isplayerconnected,getconnectedplayers
# TODO: Move Counter and List tests into ALPHA_CONFORMANCE_TESTS once the they are written for all SDKs
COUNTS_AND_LISTS_TESTS = getcounter,updatecounter,setcountcounter,setcapacitycounter,getlist,updatelist,addlistvalue,removelistvalue
# TODO: Remove
COUNTERS_TESTS = getcounter

.PHONY: test-sdks test-sdk build-sdks build-sdk gen-all-sdk-grpc gen-sdk-grpc run-all-sdk-command run-sdk-command build-example

Expand Down Expand Up @@ -187,7 +189,7 @@ run-sdk-conformance-test-csharp:
# run without feature flags
$(MAKE) run-sdk-conformance-test SDK_FOLDER=csharp GRPC_PORT=9005 HTTP_PORT=9105
# run with feature flags enabled
$(MAKE) run-sdk-conformance-test SDK_FOLDER=csharp GRPC_PORT=9005 HTTP_PORT=9105 FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)
$(MAKE) run-sdk-conformance-test SDK_FOLDER=csharp GRPC_PORT=9005 HTTP_PORT=9105 FEATURE_GATES=$(ALPHA_FEATURE_GATES) TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS),$(COUNTERS_TESTS)

run-sdk-conformance-test-rest:
# (note: the restapi folder doesn't use GRPC_PORT but run-sdk-conformance-no-build defaults it, so we supply a unique value here)
Expand Down
30 changes: 26 additions & 4 deletions sdks/csharp/sdk/Alpha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Alpha(
{
_logger = logger;
RequestTimeoutSec = requestTimeoutSec;

if (cancellationTokenSource == null)
{
cts = new CancellationTokenSource();
Expand All @@ -60,7 +60,7 @@ public Alpha(
cts = cancellationTokenSource;
ownsCts = false;
}

ctoken = cts.Token;
client = new SDK.SDKClient(channel);
}
Expand Down Expand Up @@ -210,6 +210,28 @@ public async Task<List<string>> GetConnectedPlayersAsync()
}
}

/// <summary>
/// GetCounterCountAsync returns the Count for a Counter, given the Counter's key (name).
/// Will error if the key was not predefined in the GameServer resource on creation.
/// </summary>
/// <returns>The Counter's Count</returns>
public async Task<long> GetCounterCountAsync(string key)
{
try
{
var request = new GetCounterRequest();
request.Name = key;
var counter = await client.GetCounterAsync(request,
deadline: DateTime.UtcNow.AddSeconds(RequestTimeoutSec), cancellationToken: ctoken);
return counter.Count;
}
catch (RpcException ex)
{
LogError(ex, "Unable to invoke GetCounterCount.");
throw;
}
}

public void Dispose()
{
if (_disposed)
Expand All @@ -218,12 +240,12 @@ public void Dispose()
}

cts.Cancel();

if (ownsCts)
{
cts.Dispose();
}

_disposed = true;
GC.SuppressFinalize(this);
}
Expand Down
1 change: 1 addition & 0 deletions sdks/csharp/sdk/IAgonesAlphaSDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public interface IAgonesAlphaSDK : IDisposable
Task<long> GetPlayerCountAsync();
Task<bool> IsPlayerConnectedAsync(string id);
Task<List<string>> GetConnectedPlayersAsync();
Task<long> GetCounterCountAsync(string key);
}
}
14 changes: 14 additions & 0 deletions sdks/csharp/test/AgonesAlphaSDKClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ public async Task GetConnectedPlayers_Sends_OK()
CollectionAssert.AreEquivalent(expected, result);
}

[TestMethod]
public async Task GetCounterCount_Sends_OK()
{
var mockClient = new Mock<SDK.SDKClient>();
var mockSdk = new AgonesSDK();
var expected = new Count() { Count_ = 1 };
mockClient.Setup(m => m.GetCounterCountAsync(It.IsAny<Empty>(), It.IsAny<Metadata>(), It.IsAny<DateTime?>(), It.IsAny<CancellationToken>())).Returns(
(Empty _, Metadata _, DateTime? _, CancellationToken _) => new AsyncUnaryCall<Count>(Task.FromResult(expected), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
mockSdk.alpha.client = mockClient.Object;

var result = await mockSdk.Alpha().GetCounterCountAsync();
Assert.AreEqual(expected.Count_, result);
}

[TestMethod]
public void InstantiateWithParameters_OK()
{
Expand Down
18 changes: 17 additions & 1 deletion test/sdk/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@
}
}

if (featureGates.Contains("CountsAndLists"))
{
var alpha = sdk.Alpha();
{
var key = "conformanceTestCounter";
var wantCount = 1;
// TODO: catch expections
var gotCount = await alpha.GetCounterCountAsync(key);
if (wantCount != gotCount)
{
Console.Error.WriteLine($"Counter count should be {wantCount}, but is {gotCount}");
Environment.Exit(1);
}
}
}

var shutDownStatus = await sdk.ShutDownAsync();
if (shutDownStatus.StatusCode != StatusCode.OK)
{
Expand All @@ -174,4 +190,4 @@
Environment.Exit(1);
}

Console.WriteLine("Finish all tests");
Console.WriteLine("Finish all tests");

0 comments on commit a878647

Please sign in to comment.