Skip to content

fix: backport TickSystemTests.VerifyTickSystem() test #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions com.unity.multiplayer.mlapi/Tests/Runtime/TickSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace MLAPI.RuntimeTests
{
public class TickSystemTests: IDisposable
public class TickSystemTests : IDisposable
{
private NetworkTickSystem m_TickSystem = null;
private float m_TestDuration = 5.0f;
private float m_TestDuration = 3.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but I would probably make the test duration be expressed as some multiple of the tick interval (e.g. 100 * m_TickInterval), and by the same token make the sleep interval expressed as some fraction of the tick interval vs. having purely magic number constants

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed — @jeffreyrainy for more visibility on your feedback :)

private float m_SleepInterval = 0.001f;
private float m_TickInterval = 0.010f;

Expand All @@ -25,27 +25,27 @@ public IEnumerator VerifyTickSystem()
{
m_TickSystem = new NetworkTickSystem(m_TickInterval);

ushort tick0 = m_TickSystem.GetTick();
ushort lastTick = tick0;
float t0 = Time.unscaledTime;
float t1;
var startTick = m_TickSystem.GetTick();
var startTime = Time.unscaledTime;

var lastTick = startTick;
do
{
t1 = Time.unscaledTime;
ushort tick = m_TickSystem.GetTick();
var currentTick = m_TickSystem.GetTick();
Assert.IsTrue(currentTick >= lastTick); // check monotonicity of ticks
lastTick = currentTick;

Assert.IsTrue(tick >= lastTick); // check monotonicity of ticks

lastTick = tick;
yield return new WaitForSeconds(m_SleepInterval);
} while (t1 - t0 <= m_TestDuration);
} while (Time.unscaledTime - startTime <= m_TestDuration);

var endTick = m_TickSystem.GetTick();
var endTime = Time.unscaledTime;

int ticks = lastTick - tick0;
int expectedTicks = (int)(m_TestDuration / m_TickInterval);
var elapsedTicks = endTick - startTick;
var elapsedTime = endTime - startTime;

// check overall number of ticks is within one tick of the expected value
Assert.IsTrue(Math.Abs(expectedTicks - ticks) < 2);
var elapsedTicksExpected = (int)(elapsedTime / m_TickInterval);
Assert.Less(Math.Abs(elapsedTicksExpected - elapsedTicks), 2); // +/- 1 is OK
}
}
}