Skip to content

Commit b6796cb

Browse files
committed
test: adding tests for default values
1 parent 322a2a9 commit b6796cb

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Assets/Tests/Runtime/RateLimit/RpcRateLimitTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ public void UnattributedRpc()
4141
{
4242
UnattributedCount++;
4343
}
44+
45+
public int DefaultCount;
46+
47+
[ServerRpc(requireAuthority = false)]
48+
[RateLimit]
49+
public void RpcWithDefaultRateLimit()
50+
{
51+
DefaultCount++;
52+
}
53+
54+
public int PartialCount;
55+
56+
[ServerRpc(requireAuthority = false)]
57+
[RateLimit(Refill = 2, MaxTokens = 5)]
58+
public void RpcWithPartialRateLimit()
59+
{
60+
PartialCount++;
61+
}
4462
}
4563

4664
public class RpcRateLimitTests : ClientServerSetup<RpcRateLimitBehaviour>
@@ -195,6 +213,44 @@ public IEnumerator RpcWithoutAttributeIsNotRateLimited() => UniTask.ToCoroutine(
195213
Assert.That(serverComponent.UnattributedCount, Is.EqualTo(1000), "RPC without attribute should not be rate limited");
196214
Assert.That(serverPlayer.ErrorFlags, Is.Not.EqualTo(PlayerErrorFlags.RateLimit));
197215
});
216+
217+
[UnityTest]
218+
public IEnumerator RpcWithDefaultRateLimitUsesDefaults() => UniTask.ToCoroutine(async () =>
219+
{
220+
// Trigger bucket creation by calling it once
221+
clientComponent.RpcWithDefaultRateLimit();
222+
await UniTask.Delay(100);
223+
224+
// Inspect the RemoteCall configuration on the server
225+
var identity = serverComponent.Identity;
226+
var rpc = System.Linq.Enumerable.First(identity.RemoteCallCollection.RemoteCalls, r => r != null && r.Name.Contains(nameof(RpcRateLimitBehaviour.RpcWithDefaultRateLimit)));
227+
228+
Assert.That(rpc.RateLimit.IsEnabled, Is.True);
229+
Assert.That(rpc.RateLimit.BucketConfig.MaxTokens, Is.EqualTo(RateLimitAttribute.DEFAULT_MAX_TOKENS));
230+
Assert.That(rpc.RateLimit.BucketConfig.Refill, Is.EqualTo(RateLimitAttribute.DEFAULT_REFILL));
231+
Assert.That(rpc.RateLimit.BucketConfig.Interval, Is.EqualTo(RateLimitAttribute.DEFAULT_INTERVAL));
232+
Assert.That(rpc.RateLimit.Penalty, Is.EqualTo(RateLimitAttribute.DEFAULT_PENALTY));
233+
});
234+
235+
[UnityTest]
236+
public IEnumerator RpcWithPartialSettingsUsesDefaultsForOthers() => UniTask.ToCoroutine(async () =>
237+
{
238+
// Trigger bucket creation
239+
clientComponent.RpcWithPartialRateLimit();
240+
await UniTask.Delay(100);
241+
242+
// Inspect the RemoteCall configuration on the server
243+
var identity = serverComponent.Identity;
244+
var rpc = System.Linq.Enumerable.First(identity.RemoteCallCollection.RemoteCalls, r => r != null && r.Name.Contains(nameof(RpcRateLimitBehaviour.RpcWithPartialRateLimit)));
245+
246+
Assert.That(rpc.RateLimit.IsEnabled, Is.True);
247+
// Explicitly set values
248+
Assert.That(rpc.RateLimit.BucketConfig.MaxTokens, Is.EqualTo(5));
249+
Assert.That(rpc.RateLimit.BucketConfig.Refill, Is.EqualTo(2));
250+
// Defaults
251+
Assert.That(rpc.RateLimit.BucketConfig.Interval, Is.EqualTo(RateLimitAttribute.DEFAULT_INTERVAL));
252+
Assert.That(rpc.RateLimit.Penalty, Is.EqualTo(RateLimitAttribute.DEFAULT_PENALTY));
253+
});
198254
}
199255

200256
public class RpcRateLimitHostTests : HostSetup<RpcRateLimitBehaviour>

0 commit comments

Comments
 (0)