Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Dec 28, 2024
1 parent 5198aeb commit 5930503
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 70 deletions.
20 changes: 16 additions & 4 deletions Package/Tests/CoreTests/APIs/NewAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public void PromiseNewIsInvokedAndCompletedProperly_void(

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();

TestHelper.GetTryCompleterVoid(completeType, "Reject").Invoke(deferred);
if (!throwInAction)
{
TestHelper.GetCompleterVoid(completeType, "Reject").Invoke(deferred);
}

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();
Assert.True(invoked);
Expand Down Expand Up @@ -195,7 +198,10 @@ public void PromiseNewIsInvokedAndCompletedProperly_capture_void(

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();

TestHelper.GetTryCompleterVoid(completeType, "Reject").Invoke(deferred);
if (!throwInAction)
{
TestHelper.GetCompleterVoid(completeType, "Reject").Invoke(deferred);
}

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();
Assert.True(invoked);
Expand Down Expand Up @@ -262,7 +268,10 @@ public void PromiseNewIsInvokedAndCompletedProperly_T(

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();

TestHelper.GetTryCompleterT(completeType, expectedResolveValue, "Reject").Invoke(deferred);
if (!throwInAction)
{
TestHelper.GetCompleterT(completeType, expectedResolveValue, "Reject").Invoke(deferred);
}

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();
Assert.True(invoked);
Expand Down Expand Up @@ -331,7 +340,10 @@ public void PromiseNewIsInvokedAndCompletedProperly_capture_T(

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();

TestHelper.GetTryCompleterT(completeType, expectedResolveValue, "Reject").Invoke(deferred);
if (!throwInAction)
{
TestHelper.GetCompleterT(completeType, expectedResolveValue, "Reject").Invoke(deferred);
}

TestHelper.ExecuteForegroundCallbacksAndWaitForThreadsToComplete();
Assert.True(invoked);
Expand Down
12 changes: 6 additions & 6 deletions Package/Tests/CoreTests/Concurrency/AwaitConcurrencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async Promise Await()
);

promiseRetainer.Dispose();
TestHelper.GetTryCompleterVoid(completeType, rejectValue).Invoke(deferred);
TestHelper.GetCompleterVoid(completeType, rejectValue).Invoke(deferred);
Assert.AreEqual(ThreadHelper.multiExecutionCount, invokedCount);
}

Expand Down Expand Up @@ -141,7 +141,7 @@ async void Await()
);

promiseRetainer.Dispose();
TestHelper.GetTryCompleterT(completeType, 1, rejectValue).Invoke(deferred);
TestHelper.GetCompleterT(completeType, 1, rejectValue).Invoke(deferred);
Assert.AreEqual(ThreadHelper.multiExecutionCount, invokedCount);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ public void PromiseMayBeCompletedAndAwaitedConcurrently_void(
{
var deferred = default(Promise.Deferred);
var promise = default(Promise);
var tryCompleter = TestHelper.GetTryCompleterVoid(completeType, rejectValue);
var completer = TestHelper.GetCompleterVoid(completeType, rejectValue);

Promise.State result = Promise.State.Pending;

Expand Down Expand Up @@ -230,7 +230,7 @@ async Promise Await()
},
parallelActions: new Action[]
{
() => tryCompleter(deferred)
() => completer(deferred)
},
teardown: () =>
{
Expand All @@ -257,7 +257,7 @@ public void PromiseMayBeCompletedAndAwaitedConcurrently_T(
{
var deferred = default(Promise<int>.Deferred);
var promise = default(Promise<int>);
var tryCompleter = TestHelper.GetTryCompleterT(completeType, 1, rejectValue);
var completer = TestHelper.GetCompleterT(completeType, 1, rejectValue);

Promise.State result = Promise.State.Pending;

Expand Down Expand Up @@ -296,7 +296,7 @@ async void Await()
},
parallelActions: new Action[]
{
() => tryCompleter(deferred)
() => completer(deferred)
},
teardown: () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void PromiseReturnedInCallbackMayBeCompletedConcurrently_void(
var returnDeferred = default(Promise.Deferred);
var returnPromise = default(Promise);
Action threadBarrier = null;
var tryCompleter = TestHelper.GetTryCompleterVoid(completeType, rejectValue);
var completer = TestHelper.GetCompleterVoid(completeType, rejectValue);

Promise.State result = Promise.State.Pending;

Expand Down Expand Up @@ -310,7 +310,7 @@ public void PromiseReturnedInCallbackMayBeCompletedConcurrently_void(
},
parallelActions: new Action[]
{
() => tryCompleter(returnDeferred)
() => completer(returnDeferred)
},
teardown: () =>
{
Expand Down Expand Up @@ -339,7 +339,7 @@ public void PromiseReturnedInCallbackMayBeCompletedConcurrently_T(
var returnDeferred = default(Promise<int>.Deferred);
var returnPromise = default(Promise<int>);
Action threadBarrier = null;
var tryCompleter = TestHelper.GetTryCompleterT(completeType, 1, rejectValue);
var completer = TestHelper.GetCompleterT(completeType, 1, rejectValue);

Promise.State result = Promise.State.Pending;

Expand Down Expand Up @@ -370,7 +370,7 @@ public void PromiseReturnedInCallbackMayBeCompletedConcurrently_T(
},
parallelActions: new Action[]
{
() => tryCompleter(returnDeferred)
() => completer(returnDeferred)
},
teardown: () =>
{
Expand Down
96 changes: 40 additions & 56 deletions Package/Tests/Helpers/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ public static float Lerp(float a, float b, float t)
throw new Exception();
}

public static Action<Promise<T>.Deferred> GetCompleterT<T, TReject>(CompleteType completeType, T resolveValue, TReject rejectValue)
public static Action<Promise<T>.Deferred> GetCompleterT<T, TReject>(CompleteType completeType, T value, TReject rejectValue)
{
switch (completeType)
{
case CompleteType.Resolve:
return deferred => deferred.Resolve(resolveValue);
return deferred => deferred.Resolve(value);
case CompleteType.Reject:
return deferred => deferred.Reject(rejectValue);
case CompleteType.Cancel:
Expand All @@ -262,36 +262,6 @@ public static float Lerp(float a, float b, float t)
throw new Exception();
}

public static Action<Promise.Deferred> GetTryCompleterVoid<TReject>(CompleteType completeType, TReject rejectValue)
{
// TODO: Remove try/catch, update calling tests.
switch (completeType)
{
case CompleteType.Resolve:
return deferred => { try { deferred.Resolve(); } catch { } };
case CompleteType.Reject:
return deferred => { try { deferred.Reject(rejectValue); } catch { } };
case CompleteType.Cancel:
return deferred => { try { deferred.Cancel(); } catch { } };
}
throw new Exception();
}

public static Action<Promise<T>.Deferred> GetTryCompleterT<T, TReject>(CompleteType completeType, T resolveValue, TReject rejectValue)
{
// TODO: Remove try/catch, update calling tests.
switch (completeType)
{
case CompleteType.Resolve:
return deferred => { try { deferred.Resolve(resolveValue); } catch { } };
case CompleteType.Reject:
return deferred => { try { deferred.Reject(rejectValue); } catch { } };
case CompleteType.Cancel:
return deferred => { try { deferred.Cancel(); } catch { } };
}
throw new Exception();
}

public static Promise BuildPromise<TReject>(CompleteType completeType, bool isAlreadyComplete, TReject reason, out Action tryCompleter)
{
if (isAlreadyComplete)
Expand All @@ -310,21 +280,22 @@ public static Promise BuildPromise<TReject>(CompleteType completeType, bool isAl
}

var deferred = Promise.NewDeferred();
// TODO: Remove try/catch, update calling tests.
Action completer;
switch (completeType)
{
case CompleteType.Resolve:
tryCompleter = () => { try { deferred.Resolve(); } catch { } };
completer = () => deferred.Resolve();
break;
case CompleteType.Reject:
tryCompleter = () => { try { deferred.Reject(reason); } catch { } };
completer = () => deferred.Reject(reason);
break;
case CompleteType.Cancel:
tryCompleter = () => { try { deferred.Cancel(); } catch { } };
completer = () => deferred.Cancel();
break;
default:
throw new Exception();
}
tryCompleter = () => Interlocked.Exchange(ref completer, null)?.Invoke();
return deferred.Promise;
}

Expand All @@ -346,21 +317,22 @@ public static Promise<T> BuildPromise<T, TReject>(CompleteType completeType, boo
}

var deferred = Promise<T>.NewDeferred();
// TODO: Remove try/catch, update calling tests.
Action completer;
switch (completeType)
{
case CompleteType.Resolve:
tryCompleter = () => { try { deferred.Resolve(value); } catch { } };
completer = () => deferred.Resolve(value);
break;
case CompleteType.Reject:
tryCompleter = () => { try { deferred.Reject(reason); } catch { } };
completer = () => deferred.Reject(reason);
break;
case CompleteType.Cancel:
tryCompleter = () => { try { deferred.Cancel(); } catch { } };
completer = () => deferred.Cancel();
break;
default:
throw new Exception();
}
tryCompleter = () => Interlocked.Exchange(ref completer, null)?.Invoke();
return deferred.Promise;
}

Expand Down Expand Up @@ -388,25 +360,31 @@ public static Promise BuildPromise<TReject>(CompleteType completeType, bool isAl
}

var deferred = Promise.NewDeferred();
var registration = cancelationToken.Register(deferred);
// TODO: Remove try/catch, update calling tests.
Action completer;
switch (completeType)
{
case CompleteType.Resolve:
tryCompleter = () => { try { deferred.Resolve(); } catch { } };
completer = () => deferred.Resolve();
break;
case CompleteType.Reject:
tryCompleter = () => { try { deferred.Reject(reason); } catch { } };
completer = () => deferred.Reject(reason);
break;
case CompleteType.Cancel:
tryCompleter = () => { try { deferred.Cancel(); } catch { } };
completer = () => deferred.Cancel();
break;
default:
throw new Exception();
}
return registration.IsRegistered
? deferred.Promise.Finally(registration.Dispose)
: deferred.Promise;
var registration = cancelationToken.Register(() =>
{
if (Interlocked.Exchange(ref completer, null) != null)
{
deferred.Cancel();
}
});
tryCompleter = () => Interlocked.Exchange(ref completer, null)?.Invoke();
return deferred.Promise
.Finally(registration.Dispose);
}

public static Promise<T> BuildPromise<T, TReject>(CompleteType completeType, bool isAlreadyComplete, T value, TReject reason, CancelationToken cancelationToken, out Action tryCompleter)
Expand All @@ -433,25 +411,31 @@ public static Promise<T> BuildPromise<T, TReject>(CompleteType completeType, boo
}

var deferred = Promise<T>.NewDeferred();
var registration = cancelationToken.Register(deferred);
// TODO: Remove try/catch, update calling tests.
Action completer;
switch (completeType)
{
case CompleteType.Resolve:
tryCompleter = () => { try { deferred.Resolve(value); } catch { } };
completer = () => deferred.Resolve(value);
break;
case CompleteType.Reject:
tryCompleter = () => { try { deferred.Reject(reason); } catch { } };
completer = () => deferred.Reject(reason);
break;
case CompleteType.Cancel:
tryCompleter = () => { try { deferred.Cancel(); } catch { } };
completer = () => deferred.Cancel();
break;
default:
throw new Exception();
}
return registration.IsRegistered
? deferred.Promise.Finally(registration.Dispose)
: deferred.Promise;
var registration = cancelationToken.Register(() =>
{
if (Interlocked.Exchange(ref completer, null) != null)
{
deferred.Cancel();
}
});
tryCompleter = () => Interlocked.Exchange(ref completer, null)?.Invoke();
return deferred.Promise
.Finally(registration.Dispose);
}

public static Promise ThenDuplicate(this Promise promise, CancelationToken cancelationToken = default(CancelationToken))
Expand Down

0 comments on commit 5930503

Please sign in to comment.