Skip to content

Commit

Permalink
Merge pull request #2 from BastianBlokland/feature/additional-test-co…
Browse files Browse the repository at this point in the history
…verage

Feature/additional test coverage
  • Loading branch information
BastianBlokland authored Jul 23, 2019
2 parents d05564f + 7e5a240 commit 4eaeda6
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/ComponentTask/LocalTaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,24 @@ public void Execute()
if (this.isDisposed)
throw new ObjectDisposedException(nameof(LocalTaskRunner));

// Execute all the work that was scheduled on this runner.
using (var contextScope = ContextScope.WithContext(this.context))
try
{
this.context.Execute();
// Execute all the work that was scheduled on this runner.
using (var contextScope = ContextScope.WithContext(this.context))
{
this.context.Execute();
}
}

// Remove any tasks that are now finished.
lock (this.runningTasksLock)
finally
{
for (int i = this.runningTasks.Count - 1; i >= 0; i--)
// Remove any tasks that are now finished.
lock (this.runningTasksLock)
{
if (this.runningTasks[i].IsFinished)
this.runningTasks.RemoveAt(i);
for (int i = this.runningTasks.Count - 1; i >= 0; i--)
{
if (this.runningTasks[i].IsFinished)
this.runningTasks.RemoveAt(i);
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/editmode/LocalTaskRunner_MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ public void ResetsPreviousSynchronizationContextWhenTaskCreatorThrows()
Assert.True(SynchronizationContext.Current == syncContext);
}

[Test]
public void ThrowWhenTaskChangesSyncronizationContextInSyncronousPart()
{
var exHandler = new MockExceptionHandler();
using (var runner = new LocalTaskRunner(exHandler))
{
Assert.Throws<ContextChangedException>(() => runner.StartTask(TaskThatChangesSyncContext));
}

Task TaskThatChangesSyncContext()
{
SynchronizationContext.SetSynchronizationContext(new MockSynchronizationContext());
return Task.CompletedTask;
}
}

[Test]
public void StartingCompletedTaskIsReturnedDirectly()
{
Expand Down
134 changes: 133 additions & 1 deletion tests/playmode/ComponentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IEnumerator TaskStopsWhenComponentIsDestroyed()
var count = 0;
var go = new GameObject("TestGameObject");
var comp = go.AddComponent<MockComponent>();
var t = comp.GetTaskRunner().StartTask(IncrementCountAsync);
var t = comp.StartTask(IncrementCountAsync);

// Assert that count is increment to 1.
yield return null;
Expand Down Expand Up @@ -54,6 +54,116 @@ async Task IncrementCountAsync()
}
}

[UnityTest]
public IEnumerator MultipleTasksCanRunInParallelOnSameComponent()
{
var count = 0;
var go = new GameObject("TestGameObject");
var comp = go.AddComponent<MockComponent>();
comp.StartTask(IncrementCountAsync);
comp.StartTask(IncrementCountAsync);
comp.StartTask(IncrementCountAsync);

yield return null;
Assert.AreEqual(3, count);

yield return null;
Assert.AreEqual(6, count);

yield return null;
Assert.AreEqual(9, count);

// Cleanup.
Object.Destroy(go);

async Task IncrementCountAsync()
{
await Task.Yield();
count++;
await Task.Yield();
count++;
await Task.Yield();
count++;
}
}

[UnityTest]
public IEnumerator MultipleTasksCanRunInSequenceOnSameComponent()
{
var count = 0;
var go = new GameObject("TestGameObject");
var comp = go.AddComponent<MockComponent>();
comp.StartTask(IncrementCountAsync, 2);

// Assert count is increased every frame for 9 frames.
for (int i = 0; i < 9; i++)
{
Assert.AreEqual(i, count);
yield return null;
}

// Assert count stays at 9.
yield return null;
Assert.AreEqual(9, count);

// Cleanup.
Object.Destroy(go);

async Task IncrementCountAsync(int iters)
{
await Task.Yield();
count++;
await Task.Yield();
count++;
await Task.Yield();
count++;

if (iters > 0)
comp.StartTask(IncrementCountAsync, iters - 1).DontWait();
}
}

[UnityTest]
public IEnumerator MultipleTasksCanRunInSequenceWithPausesOnSameComponent()
{
var count = 0;
var go = new GameObject("TestGameObject");
var comp = go.AddComponent<MockComponent>();

// Run tasks.
comp.StartTask(IncrementCountAsync);
for (int i = 0; i < 3; i++)
{
Assert.AreEqual(i, count);
yield return null;
}

// Idle for couple of frames.
yield return null;
yield return null;

// Run another task.
comp.StartTask(IncrementCountAsync);
for (int i = 3; i < 6; i++)
{
Assert.AreEqual(i, count);
yield return null;
}

// Cleanup.
Object.Destroy(go);

async Task IncrementCountAsync()
{
await Task.Yield();
count++;
await Task.Yield();
count++;
await Task.Yield();
count++;
}
}

[UnityTest]
public IEnumerator SameRunnerIsReusedForTheSameComponent()
{
Expand Down Expand Up @@ -83,5 +193,27 @@ public IEnumerator ThrowsWhenCalledFromNonUnityThread()
yield return null;
Object.Destroy(go);
}

[UnityTest]
public IEnumerator NullComponentThrowsArgumentNullException()
{
Assert.Throws<System.ArgumentNullException>(() => ComponentExtensions.GetTaskRunner(null));
Assert.Throws<System.ArgumentNullException>(() => ComponentExtensions.StartTask(null, () => Task.CompletedTask));
yield break;
}

[UnityTest]
public IEnumerator DestroyedComponentThrowsMissingReferenceException()
{
var go = new GameObject("TestGameObject");
var comp = go.AddComponent<MockComponent>();
Object.DestroyImmediate(comp);

Assert.Throws<MissingReferenceException>(() => ComponentExtensions.GetTaskRunner(comp));
Assert.Throws<MissingReferenceException>(() => ComponentExtensions.StartTask(comp, () => Task.CompletedTask));

yield return null;
Object.Destroy(go);
}
}
}
17 changes: 17 additions & 0 deletions tests/playmode/GameObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,22 @@ public IEnumerator CreateThrowsWhenCalledFromNonUnityThread()
yield return null;
Object.Destroy(go);
}

[UnityTest]
public IEnumerator NullGameObjectThrowsArgumentNullException()
{
Assert.Throws<System.ArgumentNullException>(() => GameObjectExtensions.CreateTaskRunner(null));
yield break;
}

[UnityTest]
public IEnumerator DestroyedGameObjectThrowsMissingReferenceException()
{
var go = new GameObject("TestGameObject");
Object.DestroyImmediate(go);

Assert.Throws<MissingReferenceException>(() => GameObjectExtensions.CreateTaskRunner(go));
yield break;
}
}
}

0 comments on commit 4eaeda6

Please sign in to comment.