fix(creditcall): restore progress updates across transactions
This commit is contained in:
@@ -27,8 +27,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
|
|
||||||
internal readonly TransactionProgressTap Progress = new TransactionProgressTap();
|
internal readonly TransactionProgressTap Progress = new TransactionProgressTap();
|
||||||
|
|
||||||
public void SuppressUncertainProgress() { Progress.Suppress(); }
|
|
||||||
|
|
||||||
private volatile bool voiceReferralRequired;
|
private volatile bool voiceReferralRequired;
|
||||||
private volatile bool voiceReferralDone;
|
private volatile bool voiceReferralDone;
|
||||||
private readonly bool saveReceipt;
|
private readonly bool saveReceipt;
|
||||||
@@ -245,13 +243,11 @@ namespace Creditcall.ChipDna.Client
|
|||||||
|
|
||||||
parameters.Add(GetExtraParams("StartTransaction"));
|
parameters.Add(GetExtraParams("StartTransaction"));
|
||||||
Console.WriteLine(parameters.ToString());
|
Console.WriteLine(parameters.ToString());
|
||||||
Progress.Begin(reference);
|
|
||||||
Response response = chipDnaClientLib.StartTransaction(parameters);
|
Response response = chipDnaClientLib.StartTransaction(parameters);
|
||||||
|
|
||||||
string errors;
|
string errors;
|
||||||
if (response.GetValue(ParameterKeys.Errors, out errors) && !string.IsNullOrEmpty(errors))
|
if (response.GetValue(ParameterKeys.Errors, out errors) && !string.IsNullOrEmpty(errors))
|
||||||
{
|
{
|
||||||
Progress.StartFailed(errors);
|
|
||||||
var result = new Dictionary<string, string>
|
var result = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{ ParameterKeys.Errors, errors },
|
{ ParameterKeys.Errors, errors },
|
||||||
@@ -1307,7 +1303,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
|
|
||||||
private void ChipDnaClientLibOnTransactionFinished(object sender, EventParameters transactionFinishedEventArgs)
|
private void ChipDnaClientLibOnTransactionFinished(object sender, EventParameters transactionFinishedEventArgs)
|
||||||
{
|
{
|
||||||
Progress.End();
|
|
||||||
var result = new Dictionary<string, string>();
|
var result = new Dictionary<string, string>();
|
||||||
var transactionFinishedStringBuilder = new StringBuilder();
|
var transactionFinishedStringBuilder = new StringBuilder();
|
||||||
transactionFinishedStringBuilder.Append("Transaction Finished Event Parameters: ");
|
transactionFinishedStringBuilder.Append("Transaction Finished Event Parameters: ");
|
||||||
@@ -1518,7 +1513,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
|
|
||||||
private void ChipDnaClientLibOnErrorEvent(object sender, ClientHelperErrorEventArgs errorEventArgs)
|
private void ChipDnaClientLibOnErrorEvent(object sender, ClientHelperErrorEventArgs errorEventArgs)
|
||||||
{
|
{
|
||||||
Progress.SuppressIfActive();
|
|
||||||
var result = new Dictionary<string, string>
|
var result = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{ ParameterKeys.Errors, errorEventArgs.RaisedException.Message}
|
{ ParameterKeys.Errors, errorEventArgs.RaisedException.Message}
|
||||||
|
|||||||
@@ -288,7 +288,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
return TransactionExecution.Run(
|
return TransactionExecution.Run(
|
||||||
() => client.PerformStartTransaction(payload.Amount, payload.TransactionType),
|
() => client.PerformStartTransaction(payload.Amount, payload.TransactionType),
|
||||||
() => client.TransactionCompletionSource.Task,
|
() => client.TransactionCompletionSource.Task,
|
||||||
client.SuppressUncertainProgress,
|
|
||||||
TimeSpan.FromSeconds(transactionTimeoutSeconds));
|
TimeSpan.FromSeconds(transactionTimeoutSeconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+336
-209
@@ -4,6 +4,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
@@ -18,19 +19,20 @@ namespace Creditcall.ChipDna.Client
|
|||||||
TimeoutDoesNotCancelCompletion,
|
TimeoutDoesNotCancelCompletion,
|
||||||
StartAndTaskErrorsKeepExceptionBehavior,
|
StartAndTaskErrorsKeepExceptionBehavior,
|
||||||
SubscriptionIsRemovedOnSuccessAndException,
|
SubscriptionIsRemovedOnSuccessAndException,
|
||||||
OwnershipAcceptsReferenceLessProgressAndSuppressesPermanently,
|
ActiveStreamAcceptsProgressRegardlessOfReference,
|
||||||
RepeatedReferenceSuppressesProgress,
|
RepeatedReferencesKeepSequentialStreamsFresh,
|
||||||
NativeMiuraSequenceKeepsFinalAuthoritative,
|
NativeMiuraSequenceKeepsFinalAuthoritative,
|
||||||
NativeMiuraRecoveryCyclesKeepFinalAuthoritative,
|
NativeMiuraRecoveryCyclesKeepFinalAuthoritative,
|
||||||
NativeNotificationUsesNotificationKey,
|
NativeNotificationUsesNotificationKey,
|
||||||
NativeOwnershipAndRemovalBoundaries,
|
ActiveStreamOwnsRemovalUntilExecutionReturns,
|
||||||
PreDispatchFailureReleasesOnlyUnusedReference,
|
SequentialResultsKeepProgressAndFinancialResults,
|
||||||
UnprovenStartFailuresSuppressObservation,
|
SdkErrorResultsDoNotSilenceNextStream,
|
||||||
AsyncErrorSuppressesOnlyActiveObservation,
|
ExecutionFailuresDoNotSilenceNextStream,
|
||||||
TimeoutUnsubscribesAndKeepsFuturePaymentsEnabled,
|
TimeoutDoesNotSilenceNextStream,
|
||||||
QueueCapacityCannotDropFinal,
|
QueueCapacityCannotDropFinal,
|
||||||
ConcurrentCallbacksKeepValidLinesAndOneFinal,
|
ConcurrentCallbacksKeepValidLinesAndOneFinal,
|
||||||
DisconnectDoesNotInterruptExecution,
|
DisconnectDoesNotInterruptExecution,
|
||||||
|
SlowConsumerDoesNotBlockCallbacksOrCompletion,
|
||||||
PreauthFieldsAreOptionalProviderValues,
|
PreauthFieldsAreOptionalProviderValues,
|
||||||
FinalFieldsExcludeSensitiveParameters
|
FinalFieldsExcludeSensitiveParameters
|
||||||
};
|
};
|
||||||
@@ -71,10 +73,10 @@ namespace Creditcall.ChipDna.Client
|
|||||||
{
|
{
|
||||||
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", outcome }, { "REFERENCE", "existing-ref" } };
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", outcome }, { "REFERENCE", "existing-ref" } };
|
||||||
var source = new TaskCompletionSource<Dictionary<string, string>>();
|
var source = new TaskCompletionSource<Dictionary<string, string>>();
|
||||||
int starts = 0, suppressions = 0;
|
int starts = 0;
|
||||||
var result = TransactionExecution.Run(() => { starts++; source.SetResult(expected); },
|
var result = TransactionExecution.Run(() => { starts++; source.SetResult(expected); },
|
||||||
() => source.Task, () => suppressions++, TimeSpan.FromSeconds(1));
|
() => source.Task, TimeSpan.FromSeconds(1));
|
||||||
Check(ReferenceEquals(expected, result) && starts == 1 && suppressions == 0,
|
Check(ReferenceEquals(expected, result) && starts == 1,
|
||||||
"shared execution changed result or start count for " + outcome);
|
"shared execution changed result or start count for " + outcome);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,34 +84,33 @@ namespace Creditcall.ChipDna.Client
|
|||||||
private static void TimeoutDoesNotCancelCompletion()
|
private static void TimeoutDoesNotCancelCompletion()
|
||||||
{
|
{
|
||||||
var source = new TaskCompletionSource<Dictionary<string, string>>();
|
var source = new TaskCompletionSource<Dictionary<string, string>>();
|
||||||
int starts = 0, suppressions = 0;
|
int starts = 0;
|
||||||
var result = TransactionExecution.Run(() => starts++, () => source.Task, () => suppressions++, TimeSpan.Zero);
|
var result = TransactionExecution.Run(() => starts++, () => source.Task, TimeSpan.Zero);
|
||||||
Check(result["TRANSACTION_RESULT"] == "TIMEOUT" &&
|
Check(result["TRANSACTION_RESULT"] == "TIMEOUT" &&
|
||||||
result["ERRORS"] == "Transaction timed out waiting for device response", "timeout fields changed");
|
result["ERRORS"] == "Transaction timed out waiting for device response", "timeout fields changed");
|
||||||
Check(starts == 1 && suppressions == 1 && !source.Task.IsCompleted, "timeout cancelled or restarted the transaction");
|
Check(starts == 1 && !source.Task.IsCompleted, "timeout cancelled or restarted the transaction");
|
||||||
source.SetResult(new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } });
|
source.SetResult(new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } });
|
||||||
Check(source.Task.Result["TRANSACTION_RESULT"] == "Approved", "late SDK completion was disabled");
|
Check(source.Task.Result["TRANSACTION_RESULT"] == "Approved", "late SDK completion was disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void StartAndTaskErrorsKeepExceptionBehavior()
|
private static void StartAndTaskErrorsKeepExceptionBehavior()
|
||||||
{
|
{
|
||||||
int suppressions = 0;
|
var startError = new InvalidOperationException("start");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TransactionExecution.Run(() => { throw new InvalidOperationException("start"); },
|
TransactionExecution.Run(() => { throw startError; },
|
||||||
() => Task.FromResult(new Dictionary<string, string>()), () => suppressions++, TimeSpan.Zero);
|
() => Task.FromResult(new Dictionary<string, string>()), TimeSpan.Zero);
|
||||||
throw new Exception("expected original start exception");
|
throw new Exception("expected original start exception");
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException) { }
|
catch (InvalidOperationException ex) { Check(ReferenceEquals(ex, startError), "start exception was replaced"); }
|
||||||
var fault = new TaskCompletionSource<Dictionary<string, string>>();
|
var fault = new TaskCompletionSource<Dictionary<string, string>>();
|
||||||
fault.SetException(new IOException("SDK fault"));
|
fault.SetException(new IOException("SDK fault"));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TransactionExecution.Run(() => { }, () => fault.Task, () => suppressions++, TimeSpan.FromSeconds(1));
|
TransactionExecution.Run(() => { }, () => fault.Task, TimeSpan.FromSeconds(1));
|
||||||
throw new Exception("expected original Task.Wait AggregateException");
|
throw new Exception("expected original Task.Wait AggregateException");
|
||||||
}
|
}
|
||||||
catch (AggregateException) { }
|
catch (AggregateException ex) { Check(ex.InnerException is IOException, "Task.Wait exception behavior changed"); }
|
||||||
Check(suppressions == 2, "ambiguous exception did not suppress observation");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SubscriptionIsRemovedOnSuccessAndException()
|
private static void SubscriptionIsRemovedOnSuccessAndException()
|
||||||
@@ -117,19 +118,14 @@ namespace Creditcall.ChipDna.Client
|
|||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
int first = 0, second = 0;
|
int first = 0, second = 0;
|
||||||
tap.Observe((s,v) => first++, () => {
|
tap.Observe((s,v) => first++, () => {
|
||||||
tap.Begin("first");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "first"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "first"));
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
return new Dictionary<string, string>();
|
||||||
});
|
});
|
||||||
CheckUnsubscribed(tap);
|
CheckUnsubscribed(tap);
|
||||||
tap.Begin("after-first");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "after-first"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "after-first"));
|
||||||
tap.End();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
tap.Observe((s,v) => second++, () => {
|
tap.Observe((s,v) => second++, () => {
|
||||||
tap.Begin("second");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "second"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "second"));
|
||||||
throw new IOException("test");
|
throw new IOException("test");
|
||||||
});
|
});
|
||||||
@@ -140,42 +136,48 @@ namespace Creditcall.ChipDna.Client
|
|||||||
CheckUnsubscribed(tap);
|
CheckUnsubscribed(tap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OwnershipAcceptsReferenceLessProgressAndSuppressesPermanently()
|
private static void ActiveStreamAcceptsProgressRegardlessOfReference()
|
||||||
{
|
{
|
||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
int callbacks = 0, payments = 0;
|
var delivered = new List<string>();
|
||||||
tap.Observe((s,v) => callbacks++, () => {
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
||||||
payments++; tap.Begin("first");
|
tap.Observe((source, value) => delivered.Add(value), () => {
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", null));
|
foreach (var reference in new[] { null, "", "current", "previous" })
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "other"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", reference));
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "first"));
|
|
||||||
tap.Suppress();
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "first"));
|
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
return new Dictionary<string, string>();
|
||||||
});
|
});
|
||||||
tap.Observe((s,v) => callbacks++, () => {
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
||||||
payments++; tap.Begin("later");
|
Check(delivered.SequenceEqual(Enumerable.Repeat("CardRequested", 4)),
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "first"));
|
"active progress was reference-filtered or delivered outside Observe");
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "later"));
|
CheckUnsubscribed(tap);
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
|
||||||
});
|
|
||||||
Check(callbacks == 2 && payments == 2, "active reference-less progress was dropped, suppression leaked, or later payments were disabled");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RepeatedReferenceSuppressesProgress()
|
private static void RepeatedReferencesKeepSequentialStreamsFresh()
|
||||||
{
|
{
|
||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
int callbacks = 0;
|
int starts = 0;
|
||||||
tap.Observe((s,v) => callbacks++, () => {
|
for (int i = 0; i < 100; i++)
|
||||||
tap.Begin("same-second-reference");
|
using (var output = new MemoryStream())
|
||||||
tap.End();
|
{
|
||||||
tap.Begin("same-second-reference");
|
var writer = new TransactionStreamWriter(output);
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "same-second-reference"));
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" }, { "REFERENCE", "same-second-reference" } };
|
||||||
return new Dictionary<string, string>();
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
});
|
starts++;
|
||||||
Check(callbacks == 0, "reused reference attributed an ambiguous event");
|
var listener = (Delegate)typeof(TransactionProgressTap).GetField("listener", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tap);
|
||||||
|
Check(listener.GetInvocationList().Length == 1, "sequential transactions accumulated listeners");
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "same-second-reference"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
writer.Complete(new { type = "result", result });
|
||||||
|
writer.Deliver();
|
||||||
|
var frames = Frames(output);
|
||||||
|
Check(frames.Length == 3 && frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames[1]["value"].Value<string>() == "PinEntryStarted" && ReferenceEquals(result, expected),
|
||||||
|
"repeated reference silenced progress, reused a buffer or changed the result at transaction " + i);
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
||||||
|
}
|
||||||
|
Check(starts == 100, "sequential progress changed the transaction dispatch count");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void NativeMiuraSequenceKeepsFinalAuthoritative()
|
private static void NativeMiuraSequenceKeepsFinalAuthoritative()
|
||||||
@@ -189,16 +191,14 @@ namespace Creditcall.ChipDna.Client
|
|||||||
int starts = 0;
|
int starts = 0;
|
||||||
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
starts++;
|
starts++;
|
||||||
tap.Begin("miura-ref");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Inserted"));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Inserted"));
|
||||||
foreach (var update in new[] { "TransactionStarted", "PinEntryStarted", "OnlineAuthRequested", "OnlineAuthCompleted" })
|
foreach (var update in new[] { "TransactionStarted", "PinEntryStarted", "OnlineAuthRequested", "OnlineAuthCompleted" })
|
||||||
tap.Report("UPDATE", Native("UPDATE", update));
|
tap.Report("UPDATE", Native("UPDATE", update));
|
||||||
tap.End(); // TransactionFinished closes observation before final processing.
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
|
||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Removed"));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Removed"));
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
||||||
writer.Complete(new { type = "result", result });
|
writer.Complete(new { type = "result", result });
|
||||||
writer.Deliver();
|
writer.Deliver();
|
||||||
var frames = Frames(output);
|
var frames = Frames(output);
|
||||||
@@ -224,7 +224,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
int starts = 0;
|
int starts = 0;
|
||||||
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
starts++;
|
starts++;
|
||||||
tap.Begin("recovery");
|
|
||||||
for (int i = 0; i < cycles; i++)
|
for (int i = 0; i < cycles; i++)
|
||||||
{
|
{
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
@@ -236,9 +235,8 @@ namespace Creditcall.ChipDna.Client
|
|||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Inserted"));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Inserted"));
|
||||||
foreach (var update in new[] { "TransactionStarted", "PinEntryStarted", "OnlineAuthRequested", "OnlineAuthCompleted" })
|
foreach (var update in new[] { "TransactionStarted", "PinEntryStarted", "OnlineAuthRequested", "OnlineAuthCompleted" })
|
||||||
tap.Report("UPDATE", Native("UPDATE", update));
|
tap.Report("UPDATE", Native("UPDATE", update));
|
||||||
tap.End();
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
|
||||||
writer.Complete(new { type = "result", result });
|
writer.Complete(new { type = "result", result });
|
||||||
writer.Deliver();
|
writer.Deliver();
|
||||||
var frames = Frames(output);
|
var frames = Frames(output);
|
||||||
@@ -262,7 +260,6 @@ namespace Creditcall.ChipDna.Client
|
|||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
var delivered = new List<string>();
|
var delivered = new List<string>();
|
||||||
tap.Observe((source, value) => delivered.Add(source + ":" + value), () => {
|
tap.Observe((source, value) => delivered.Add(source + ":" + value), () => {
|
||||||
tap.Begin("native-key");
|
|
||||||
tap.Report("CARD_STATUS", Native("CARD_STATUS", "Inserted"));
|
tap.Report("CARD_STATUS", Native("CARD_STATUS", "Inserted"));
|
||||||
foreach (var value in new[] { "Inserted", "Tapped", "Swiped" })
|
foreach (var value in new[] { "Inserted", "Tapped", "Swiped" })
|
||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", value));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", value));
|
||||||
@@ -270,173 +267,219 @@ namespace Creditcall.ChipDna.Client
|
|||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Unknown"));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Unknown"));
|
||||||
tap.Report("UPDATE", Native("PAN", "4761730000001133"));
|
tap.Report("UPDATE", Native("PAN", "4761730000001133"));
|
||||||
tap.Report("UPDATE", null);
|
tap.Report("UPDATE", null);
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
return new Dictionary<string, string>();
|
||||||
});
|
});
|
||||||
Check(delivered.SequenceEqual(new[] { "CARD_STATUS:Inserted", "CARD_STATUS:Tapped", "CARD_STATUS:Swiped" }),
|
Check(delivered.SequenceEqual(new[] { "CARD_STATUS:Inserted", "CARD_STATUS:Tapped", "CARD_STATUS:Swiped" }),
|
||||||
"native NOTIFICATION extraction used CARD_STATUS, exposed card data, or changed the wire source");
|
"native NOTIFICATION extraction used CARD_STATUS, exposed card data, or changed the wire source");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void NativeOwnershipAndRemovalBoundaries()
|
|
||||||
|
private static void ActiveStreamOwnsRemovalUntilExecutionReturns()
|
||||||
{
|
{
|
||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
var delivered = new List<string>();
|
var delivered = new List<string>();
|
||||||
var removals = new[] { "CardRemovalRequested", "CardRemovalEnforced" };
|
var removals = new[] { "CardRemovalRequested", "CardRemovalEnforced" };
|
||||||
|
foreach (var removal in removals)
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", removal));
|
||||||
tap.Observe((source, value) => delivered.Add(source + ":" + value), () => {
|
tap.Observe((source, value) => delivered.Add(source + ":" + value), () => {
|
||||||
foreach (var removal in removals)
|
|
||||||
foreach (var reference in new[] { null, "", "first", "other" })
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, reference));
|
|
||||||
tap.Begin("first");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", ""));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested", ""));
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "other"));
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "previous"));
|
||||||
tap.Report("SIGNATURE", Native(null, null));
|
tap.Report("SIGNATURE", Native(null, null));
|
||||||
tap.Report("PAUSE", Native("PAUSE_STATE", "PostCardDetails"));
|
tap.Report("PAUSE", Native("PAUSE_STATE", "PostCardDetails"));
|
||||||
foreach (var removal in removals)
|
foreach (var removal in removals)
|
||||||
foreach (var reference in new[] { null, "", "first", "other" })
|
foreach (var reference in new[] { null, "", "current", "previous" })
|
||||||
{
|
{
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, reference));
|
tap.Report("UPDATE", Native("UPDATE", removal, reference));
|
||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Removed", reference));
|
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Removed", reference));
|
||||||
}
|
}
|
||||||
tap.End();
|
|
||||||
foreach (var removal in removals)
|
|
||||||
foreach (var reference in new[] { null, "", "first", "other" })
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, reference));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
tap.Begin("next");
|
|
||||||
// Accepted best-effort limitation: unidentified progress, including removal,
|
|
||||||
// is allowed in this new window even if it came from an older queue.
|
|
||||||
foreach (var removal in removals)
|
|
||||||
{
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, "first"));
|
|
||||||
}
|
|
||||||
tap.Report("CARD_STATUS", Native("NOTIFICATION", "Removed"));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "first"));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "Approved"));
|
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
return new Dictionary<string, string>();
|
||||||
});
|
});
|
||||||
Check(delivered.SequenceEqual(new[] { "UPDATE:CardRequested", "SIGNATURE:Requested", "PAUSE:Paused",
|
foreach (var removal in removals)
|
||||||
"UPDATE:CardRemovalRequested", "UPDATE:CardRemovalRequested", "UPDATE:CardRemovalRequested",
|
tap.Report("UPDATE", Native("UPDATE", removal));
|
||||||
"UPDATE:CardRemovalEnforced", "UPDATE:CardRemovalEnforced", "UPDATE:CardRemovalEnforced",
|
tap.Observe((source, value) => delivered.Add(source + ":" + value), () => {
|
||||||
"UPDATE:CardRemovalRequested", "UPDATE:CardRemovalEnforced", "UPDATE:CardRequested" }),
|
// Late display-only updates are accepted by the current stream.
|
||||||
"active-window, explicit-reference, or removal ownership changed");
|
foreach (var removal in removals)
|
||||||
}
|
|
||||||
|
|
||||||
private static void PreDispatchFailureReleasesOnlyUnusedReference()
|
|
||||||
{
|
|
||||||
var tap = new TransactionProgressTap();
|
|
||||||
int starts = 0, callbacks = 0;
|
|
||||||
foreach (var rejected in new[] { true, false })
|
|
||||||
{
|
|
||||||
var expected = rejected
|
|
||||||
? new Dictionary<string, string> { { "ERRORS", "ClientNotConnectedToServer" }, { "REFERENCE", "same-second" } }
|
|
||||||
: new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" }, { "REFERENCE", "same-second" } };
|
|
||||||
var result = tap.Observe((source, value) => callbacks++, () => TransactionExecution.Run(() => {
|
|
||||||
starts++;
|
|
||||||
tap.Begin("same-second");
|
|
||||||
if (rejected) tap.StartFailed("ClientNotConnectedToServer");
|
|
||||||
else tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
if (!rejected) tap.End();
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
|
||||||
Check(ReferenceEquals(result, expected), "pre-dispatch handling changed the financial result");
|
|
||||||
CheckUnsubscribed(tap);
|
|
||||||
}
|
|
||||||
Check(starts == 2 && callbacks == 1, "proven pre-dispatch rejection prevented a safe same-reference retry");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void UnprovenStartFailuresSuppressObservation()
|
|
||||||
{
|
|
||||||
foreach (var error in new[] { "AmountTooSmall", "MissingParameter", "NoPinPadsAvailable", "TransactionInProgress",
|
|
||||||
"ClientNotConnectedToServer,AmountTooSmall", " ClientNotConnectedToServer", "500005", "Unknown" })
|
|
||||||
{
|
|
||||||
var tap = new TransactionProgressTap();
|
|
||||||
int starts = 0, callbacks = 0;
|
|
||||||
foreach (var first in new[] { true, false })
|
|
||||||
{
|
|
||||||
var expected = first ? new Dictionary<string, string> { { "ERRORS", error } }
|
|
||||||
: new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } };
|
|
||||||
var result = tap.Observe((source, value) => callbacks++, () => TransactionExecution.Run(() => {
|
|
||||||
starts++;
|
|
||||||
tap.Begin(first ? "failed" : "later");
|
|
||||||
if (first) tap.StartFailed(error);
|
|
||||||
// A subsequent safe rejection must never clear an existing latch.
|
|
||||||
tap.StartFailed("ClientNotConnectedToServer");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
foreach (var removal in new[] { "CardRemovalRequested", "CardRemovalEnforced" })
|
|
||||||
{
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, "later"));
|
|
||||||
}
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested", "later"));
|
|
||||||
tap.End();
|
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
|
||||||
Check(ReferenceEquals(result, expected), "observational handling changed result for " + error);
|
|
||||||
}
|
|
||||||
Check(starts == 2 && callbacks == 0, "unproven error recovered progress or disabled payment: " + error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void AsyncErrorSuppressesOnlyActiveObservation()
|
|
||||||
{
|
|
||||||
var tap = new TransactionProgressTap();
|
|
||||||
int callbacks = 0, starts = 0;
|
|
||||||
tap.SuppressIfActive(); // Idle SDK errors have no active transaction to make ambiguous.
|
|
||||||
foreach (var reference in new[] { "first", "later" })
|
|
||||||
{
|
|
||||||
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } };
|
|
||||||
var result = tap.Observe((source, value) => callbacks++, () => TransactionExecution.Run(() => {
|
|
||||||
starts++;
|
|
||||||
tap.Begin(reference);
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
tap.SuppressIfActive();
|
|
||||||
foreach (var removal in new[] { "CardRemovalRequested", "CardRemovalEnforced" })
|
|
||||||
{
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal));
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, reference));
|
|
||||||
}
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
|
||||||
tap.End();
|
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
|
||||||
Check(ReferenceEquals(result, expected), "asynchronous SDK error altered financial execution");
|
|
||||||
}
|
|
||||||
Check(starts == 2 && callbacks == 1, "asynchronous error failed to latch suppression or suppressed while idle");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TimeoutUnsubscribesAndKeepsFuturePaymentsEnabled()
|
|
||||||
{
|
|
||||||
var tap = new TransactionProgressTap();
|
|
||||||
var completion = new TaskCompletionSource<Dictionary<string, string>>();
|
|
||||||
int starts = 0, callbacks = 0;
|
|
||||||
var timeout = tap.Observe((source, value) => callbacks++, () => TransactionExecution.Run(() => {
|
|
||||||
starts++;
|
|
||||||
tap.Begin("timeout");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
}, () => completion.Task, tap.Suppress, TimeSpan.Zero));
|
|
||||||
Check(timeout["TRANSACTION_RESULT"] == "TIMEOUT" && !completion.Task.IsCompleted, "timeout changed financial completion");
|
|
||||||
CheckUnsubscribed(tap);
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
|
||||||
completion.SetResult(new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } });
|
|
||||||
tap.End();
|
|
||||||
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Declined" } };
|
|
||||||
var later = tap.Observe((source, value) => callbacks++, () => TransactionExecution.Run(() => {
|
|
||||||
starts++;
|
|
||||||
tap.Begin("later");
|
|
||||||
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
|
||||||
foreach (var removal in new[] { "CardRemovalRequested", "CardRemovalEnforced" })
|
|
||||||
{
|
{
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal));
|
tap.Report("UPDATE", Native("UPDATE", removal));
|
||||||
tap.Report("UPDATE", Native("UPDATE", removal, "later"));
|
tap.Report("UPDATE", Native("UPDATE", removal, "previous"));
|
||||||
}
|
}
|
||||||
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "later"));
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "previous"));
|
||||||
tap.End();
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
}, () => Task.FromResult(expected), tap.Suppress, TimeSpan.FromSeconds(1)));
|
tap.Report("UPDATE", Native("UPDATE", "Approved"));
|
||||||
Check(starts == 2 && callbacks == 1 && ReferenceEquals(later, expected), "timeout suppression leaked progress or blocked future payments");
|
return new Dictionary<string, string>();
|
||||||
|
});
|
||||||
|
var expected = new[] { "UPDATE:CardRequested", "UPDATE:PinEntryStarted", "SIGNATURE:Requested", "PAUSE:Paused" }
|
||||||
|
.Concat(removals.SelectMany(value => Enumerable.Repeat("UPDATE:" + value, 4)))
|
||||||
|
.Concat(removals.SelectMany(value => Enumerable.Repeat("UPDATE:" + value, 2)))
|
||||||
|
.Concat(new[] { "UPDATE:PinEntryStarted", "UPDATE:CardRequested" });
|
||||||
|
Check(delivered.SequenceEqual(expected), "active-stream removal prompts were filtered, reordered or delivered after cleanup");
|
||||||
CheckUnsubscribed(tap);
|
CheckUnsubscribed(tap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void SequentialResultsKeepProgressAndFinancialResults()
|
||||||
|
{
|
||||||
|
foreach (var outcome in new[] { "Approved", "Declined", "Cancelled" })
|
||||||
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
int starts = 0;
|
||||||
|
foreach (var resultCode in new[] { outcome, "Approved" })
|
||||||
|
using (var output = new MemoryStream())
|
||||||
|
{
|
||||||
|
var writer = new TransactionStreamWriter(output);
|
||||||
|
var expected = new Dictionary<string, string> {
|
||||||
|
{ "TRANSACTION_RESULT", resultCode }, { "REFERENCE", "unchanged-reference" },
|
||||||
|
{ "TOTAL_AMOUNT", "3100" }, { "RECEIPT_DATA_CARDHOLDER", "synthetic-receipt" }
|
||||||
|
};
|
||||||
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "unchanged-reference"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
writer.Complete(new { type = "result", result });
|
||||||
|
writer.Deliver();
|
||||||
|
var frames = Frames(output);
|
||||||
|
Check(ReferenceEquals(result, expected) && frames.Length == 3 &&
|
||||||
|
frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames[1]["value"].Value<string>() == "PinEntryStarted" &&
|
||||||
|
JToken.DeepEquals(frames[2]["result"], JObject.FromObject(expected)),
|
||||||
|
"sequential " + outcome + " result lost progress or changed authoritative fields");
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
Check(starts == 2, "sequential " + outcome + " result changed dispatch count");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void SdkErrorResultsDoNotSilenceNextStream()
|
||||||
|
{
|
||||||
|
foreach (var error in new[] { "ClientNotConnectedToServer", "AmountTooSmall", "MissingParameter", "NoPinPadsAvailable",
|
||||||
|
"TransactionInProgress", "ClientNotConnectedToServer,AmountTooSmall", "500005", "Unknown" })
|
||||||
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
int starts = 0;
|
||||||
|
foreach (var failed in new[] { true, false })
|
||||||
|
using (var output = new MemoryStream())
|
||||||
|
{
|
||||||
|
var writer = new TransactionStreamWriter(output);
|
||||||
|
var expected = failed ? new Dictionary<string, string> { { "ERRORS", error }, { "REFERENCE", "same-reference" } }
|
||||||
|
: new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" }, { "REFERENCE", "same-reference" } };
|
||||||
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "same-reference"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
writer.Complete(new { type = "result", result });
|
||||||
|
writer.Deliver();
|
||||||
|
var frames = Frames(output);
|
||||||
|
Check(ReferenceEquals(result, expected) && frames.Length == 3 &&
|
||||||
|
frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames[1]["value"].Value<string>() == "PinEntryStarted" &&
|
||||||
|
JToken.DeepEquals(frames[2]["result"], JObject.FromObject(expected)),
|
||||||
|
"SDK error silenced subsequent progress or changed final fields: " + error);
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
Check(starts == 2, "SDK error changed transaction dispatch count: " + error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void ExecutionFailuresDoNotSilenceNextStream()
|
||||||
|
{
|
||||||
|
foreach (var failure in new[] { "start exception", "cancelled completion", "faulted completion" })
|
||||||
|
using (var firstOutput = new MemoryStream())
|
||||||
|
using (var nextOutput = new MemoryStream())
|
||||||
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
var first = new TransactionStreamWriter(firstOutput);
|
||||||
|
var completion = new TaskCompletionSource<Dictionary<string, string>>();
|
||||||
|
var startError = new InvalidOperationException("synthetic start failure");
|
||||||
|
int starts = 0;
|
||||||
|
bool caught = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
tap.Observe(first.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
if (failure == "start exception") throw startError;
|
||||||
|
Task.Run(() => {
|
||||||
|
if (failure == "cancelled completion") completion.SetCanceled();
|
||||||
|
else completion.SetException(new IOException("synthetic SDK fault"));
|
||||||
|
});
|
||||||
|
}, () => completion.Task, TimeSpan.FromSeconds(5)));
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex)
|
||||||
|
{
|
||||||
|
caught = failure == "start exception" && ReferenceEquals(ex, startError);
|
||||||
|
}
|
||||||
|
catch (AggregateException ex)
|
||||||
|
{
|
||||||
|
caught = failure == "cancelled completion" ? ex.InnerException is TaskCanceledException
|
||||||
|
: failure == "faulted completion" && ex.InnerException is IOException;
|
||||||
|
}
|
||||||
|
Check(caught, "execution exception/cancellation behavior changed for " + failure);
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
first.Complete(new { type = "error", error = "Transaction stream failed" });
|
||||||
|
first.Deliver();
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
||||||
|
|
||||||
|
var next = new TransactionStreamWriter(nextOutput);
|
||||||
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Declined" }, { "REFERENCE", "next" } };
|
||||||
|
var result = tap.Observe(next.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "next"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
next.Complete(new { type = "result", result });
|
||||||
|
next.Deliver();
|
||||||
|
var frames = Frames(nextOutput);
|
||||||
|
Check(starts == 2 && ReferenceEquals(result, expected) && frames.Length == 3 &&
|
||||||
|
frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames[1]["value"].Value<string>() == "PinEntryStarted" &&
|
||||||
|
JToken.DeepEquals(frames[2]["result"], JObject.FromObject(expected)),
|
||||||
|
"next transaction lost progress or changed result after " + failure);
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TimeoutDoesNotSilenceNextStream()
|
||||||
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
var pending = new TaskCompletionSource<Dictionary<string, string>>();
|
||||||
|
int starts = 0;
|
||||||
|
using (var firstOutput = new MemoryStream())
|
||||||
|
using (var nextOutput = new MemoryStream())
|
||||||
|
{
|
||||||
|
var first = new TransactionStreamWriter(firstOutput);
|
||||||
|
var timeout = tap.Observe(first.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
}, () => pending.Task, TimeSpan.Zero));
|
||||||
|
first.Complete(new { type = "result", result = timeout });
|
||||||
|
first.Deliver();
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
|
||||||
|
var next = new TransactionStreamWriter(nextOutput);
|
||||||
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" }, { "REFERENCE", "next" } };
|
||||||
|
var result = tap.Observe(next.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted", "next"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
next.Complete(new { type = "result", result });
|
||||||
|
next.Deliver();
|
||||||
|
|
||||||
|
var frames = Frames(nextOutput);
|
||||||
|
Check(frames.Where(f => f["type"].Value<string>() == "status")
|
||||||
|
.Select(f => f["value"].Value<string>()).SequenceEqual(new[] { "CardRequested", "PinEntryStarted" }),
|
||||||
|
"next transaction lost fresh progress after the previous timeout");
|
||||||
|
Check(starts == 2 && !pending.Task.IsCompleted && ReferenceEquals(result, expected) &&
|
||||||
|
frames.Last()["result"]["TRANSACTION_RESULT"].Value<string>() == "Approved",
|
||||||
|
"progress recovery changed dispatch count, pending completion or the next financial result");
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static JObject[] Frames(MemoryStream stream)
|
private static JObject[] Frames(MemoryStream stream)
|
||||||
{
|
{
|
||||||
var text = Encoding.UTF8.GetString(stream.ToArray());
|
var text = Encoding.UTF8.GetString(stream.ToArray());
|
||||||
@@ -469,9 +512,7 @@ namespace Creditcall.ChipDna.Client
|
|||||||
var delivery = Task.Run(() => writer.Deliver());
|
var delivery = Task.Run(() => writer.Deliver());
|
||||||
var tap = new TransactionProgressTap();
|
var tap = new TransactionProgressTap();
|
||||||
tap.Observe(writer.Progress, () => {
|
tap.Observe(writer.Progress, () => {
|
||||||
tap.Begin("concurrent");
|
|
||||||
Parallel.For(0, 5000, i => tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted")));
|
Parallel.For(0, 5000, i => tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted")));
|
||||||
tap.End();
|
|
||||||
return new Dictionary<string, string>();
|
return new Dictionary<string, string>();
|
||||||
});
|
});
|
||||||
writer.Complete(new { type = "result", result = new { TRANSACTION_RESULT = "Declined" } });
|
writer.Complete(new { type = "result", result = new { TRANSACTION_RESULT = "Declined" } });
|
||||||
@@ -492,18 +533,104 @@ namespace Creditcall.ChipDna.Client
|
|||||||
|
|
||||||
private static void DisconnectDoesNotInterruptExecution()
|
private static void DisconnectDoesNotInterruptExecution()
|
||||||
{
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
int starts = 0;
|
||||||
using (var output = new DisconnectedStream())
|
using (var output = new DisconnectedStream())
|
||||||
{
|
{
|
||||||
var writer = new TransactionStreamWriter(output);
|
var writer = new TransactionStreamWriter(output);
|
||||||
writer.Progress("UPDATE", "CardRequested");
|
|
||||||
writer.Deliver();
|
|
||||||
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } };
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } };
|
||||||
int starts = 0;
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
var result = TransactionExecution.Run(() => starts++, () => Task.FromResult(expected),
|
starts++;
|
||||||
() => { throw new Exception("disconnect suppressed financial execution"); }, TimeSpan.FromSeconds(1));
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
writer.Deliver(); // The disconnected consumer cannot abort execution.
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
writer.Complete(new { type = "result", result });
|
writer.Complete(new { type = "result", result });
|
||||||
writer.Progress("UPDATE", "CardRequested");
|
writer.Progress("UPDATE", "CardRequested");
|
||||||
Check(starts == 1 && ReferenceEquals(result, expected), "disconnect changed final transaction result");
|
Check(starts == 1 && ReferenceEquals(result, expected), "disconnect changed final transaction result");
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
using (var output = new MemoryStream())
|
||||||
|
{
|
||||||
|
var writer = new TransactionStreamWriter(output);
|
||||||
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Declined" } };
|
||||||
|
var result = tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted"));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1)));
|
||||||
|
writer.Complete(new { type = "result", result });
|
||||||
|
writer.Deliver();
|
||||||
|
var frames = Frames(output);
|
||||||
|
Check(starts == 2 && ReferenceEquals(result, expected) && frames.Length == 3 &&
|
||||||
|
frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames[1]["value"].Value<string>() == "PinEntryStarted" &&
|
||||||
|
JToken.DeepEquals(frames[2]["result"], JObject.FromObject(expected)),
|
||||||
|
"disconnected previous stream suppressed progress, reused its buffer or changed the next result");
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class BlockingStream : MemoryStream
|
||||||
|
{
|
||||||
|
internal readonly ManualResetEventSlim WriteEntered = new ManualResetEventSlim();
|
||||||
|
internal readonly ManualResetEventSlim Release = new ManualResetEventSlim();
|
||||||
|
|
||||||
|
public override void Write(byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
WriteEntered.Set();
|
||||||
|
if (!Release.Wait(TimeSpan.FromSeconds(10))) throw new IOException("test consumer stayed blocked");
|
||||||
|
base.Write(buffer, offset, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
Release.Set();
|
||||||
|
WriteEntered.Dispose();
|
||||||
|
Release.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SlowConsumerDoesNotBlockCallbacksOrCompletion()
|
||||||
|
{
|
||||||
|
using (var output = new BlockingStream())
|
||||||
|
{
|
||||||
|
var tap = new TransactionProgressTap();
|
||||||
|
var writer = new TransactionStreamWriter(output);
|
||||||
|
var delivery = Task.Run(() => writer.Deliver());
|
||||||
|
var expected = new Dictionary<string, string> { { "TRANSACTION_RESULT", "Approved" } };
|
||||||
|
int starts = 0;
|
||||||
|
var execution = Task.Run(() => tap.Observe(writer.Progress, () => TransactionExecution.Run(() => {
|
||||||
|
starts++;
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRequested"));
|
||||||
|
Check(output.WriteEntered.Wait(TimeSpan.FromSeconds(5)), "consumer never reached the blocked write");
|
||||||
|
Parallel.For(0, 5000, i => tap.Report("UPDATE", Native("UPDATE", "PinEntryStarted")));
|
||||||
|
}, () => Task.FromResult(expected), TimeSpan.FromSeconds(1))));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Check(execution.Wait(TimeSpan.FromSeconds(5)), "slow consumer blocked SDK callbacks or execution");
|
||||||
|
Check(starts == 1 && ReferenceEquals(execution.Result, expected), "slow consumer changed financial completion");
|
||||||
|
CheckUnsubscribed(tap);
|
||||||
|
writer.Complete(new { type = "result", result = execution.Result });
|
||||||
|
tap.Report("UPDATE", Native("UPDATE", "CardRemovalRequested"));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
output.Release.Set();
|
||||||
|
writer.Complete(new { type = "error", error = "test cleanup" });
|
||||||
|
Check(execution.Wait(TimeSpan.FromSeconds(5)), "execution did not exit after consumer release");
|
||||||
|
Check(delivery.Wait(TimeSpan.FromSeconds(5)), "delivery did not exit after consumer release");
|
||||||
|
}
|
||||||
|
var frames = Frames(output);
|
||||||
|
Check(frames[0]["value"].Value<string>() == "CardRequested" &&
|
||||||
|
frames.Count(f => f["type"].Value<string>() == "result") == 1 &&
|
||||||
|
frames.Last()["type"].Value<string>() == "result" &&
|
||||||
|
JToken.DeepEquals(frames.Last()["result"], JObject.FromObject(expected)),
|
||||||
|
"slow consumer reordered progress, lost the final result or delivered progress after final");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-80
@@ -12,37 +12,28 @@ namespace Creditcall.ChipDna.Client
|
|||||||
internal static class TransactionExecution
|
internal static class TransactionExecution
|
||||||
{
|
{
|
||||||
internal static Dictionary<string, string> Run(Action start,
|
internal static Dictionary<string, string> Run(Action start,
|
||||||
Func<Task<Dictionary<string, string>>> completion, Action suppress, TimeSpan timeout)
|
Func<Task<Dictionary<string, string>>> completion, TimeSpan timeout)
|
||||||
{
|
{
|
||||||
try
|
start();
|
||||||
|
var task = completion();
|
||||||
|
// Task.Wait intentionally retains its existing exception behavior.
|
||||||
|
if (!task.Wait(timeout))
|
||||||
{
|
{
|
||||||
start();
|
return new Dictionary<string, string>
|
||||||
var task = completion();
|
|
||||||
// Task.Wait intentionally retains its existing exception behavior.
|
|
||||||
if (!task.Wait(timeout))
|
|
||||||
{
|
{
|
||||||
suppress();
|
{ ParameterKeys.TransactionResult, "TIMEOUT" },
|
||||||
return new Dictionary<string, string>
|
{ ParameterKeys.Errors, "Transaction timed out waiting for device response" }
|
||||||
{
|
};
|
||||||
{ ParameterKeys.TransactionResult, "TIMEOUT" },
|
|
||||||
{ ParameterKeys.Errors, "Transaction timed out waiting for device response" }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
try { return task.Result; }
|
|
||||||
catch (AggregateException agg)
|
|
||||||
{
|
|
||||||
var first = agg.Flatten().InnerExceptions[0];
|
|
||||||
return new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{ ParameterKeys.TransactionResult, "ERROR" },
|
|
||||||
{ ParameterKeys.Errors, $"Transaction task faulted: {first.Message}" }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch
|
try { return task.Result; }
|
||||||
|
catch (AggregateException agg)
|
||||||
{
|
{
|
||||||
suppress();
|
var first = agg.Flatten().InnerExceptions[0];
|
||||||
throw;
|
return new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ ParameterKeys.TransactionResult, "ERROR" },
|
||||||
|
{ ParameterKeys.Errors, $"Transaction task faulted: {first.Message}" }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,10 +41,7 @@ namespace Creditcall.ChipDna.Client
|
|||||||
internal sealed class TransactionProgressTap
|
internal sealed class TransactionProgressTap
|
||||||
{
|
{
|
||||||
private readonly object gate = new object();
|
private readonly object gate = new object();
|
||||||
private readonly HashSet<string> references = new HashSet<string>(StringComparer.Ordinal);
|
|
||||||
private Action<string, string> listener;
|
private Action<string, string> listener;
|
||||||
private string reference;
|
|
||||||
private bool suppressed;
|
|
||||||
|
|
||||||
internal Dictionary<string, string> Observe(Action<string, string> callback, Func<Dictionary<string, string>> execute)
|
internal Dictionary<string, string> Observe(Action<string, string> callback, Func<Dictionary<string, string>> execute)
|
||||||
{
|
{
|
||||||
@@ -61,57 +49,11 @@ namespace Creditcall.ChipDna.Client
|
|||||||
try { return execute(); }
|
try { return execute(); }
|
||||||
finally { lock (gate) { listener -= callback; } }
|
finally { lock (gate) { listener -= callback; } }
|
||||||
}
|
}
|
||||||
internal void Begin(string value)
|
|
||||||
{
|
|
||||||
lock (gate)
|
|
||||||
{
|
|
||||||
if (suppressed) return;
|
|
||||||
// The existing reference generation can repeat within one second.
|
|
||||||
// A repeated reference cannot prove callback ownership.
|
|
||||||
if (reference != null || string.IsNullOrEmpty(value) || !references.Add(value))
|
|
||||||
suppressed = true;
|
|
||||||
reference = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
internal void End() { lock (gate) { reference = null; } }
|
|
||||||
internal void Suppress() { lock (gate) { suppressed = true; reference = null; } }
|
|
||||||
internal void SuppressIfActive()
|
|
||||||
{
|
|
||||||
lock (gate)
|
|
||||||
{
|
|
||||||
if (reference == null) return;
|
|
||||||
suppressed = true;
|
|
||||||
reference = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void StartFailed(string errors)
|
|
||||||
{
|
|
||||||
lock (gate)
|
|
||||||
{
|
|
||||||
if (suppressed) return;
|
|
||||||
// SDK 3.17 StartCommand returns this exact error before SendRequest.
|
|
||||||
// No other error category proves that dispatch never happened.
|
|
||||||
if (errors == ClientErrorCodes.ClientNotConnectedToServer.ToString())
|
|
||||||
{
|
|
||||||
if (reference != null) references.Remove(reference);
|
|
||||||
reference = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
suppressed = true;
|
|
||||||
reference = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Report(string source, EventParameters parameters)
|
internal void Report(string source, EventParameters parameters)
|
||||||
{
|
{
|
||||||
lock (gate)
|
lock (gate)
|
||||||
{
|
{
|
||||||
if (suppressed || reference == null || parameters == null) return;
|
if (listener == null || parameters == null) return;
|
||||||
string eventReference;
|
|
||||||
parameters.GetValue(ParameterKeys.Reference, out eventReference);
|
|
||||||
bool hasReference = !string.IsNullOrEmpty(eventReference);
|
|
||||||
if (hasReference && eventReference != reference) return;
|
|
||||||
|
|
||||||
string value;
|
string value;
|
||||||
switch (source)
|
switch (source)
|
||||||
@@ -128,10 +70,8 @@ namespace Creditcall.ChipDna.Client
|
|||||||
}
|
}
|
||||||
if (!TransactionStreamFields.IsProgress(source, value)) return;
|
if (!TransactionStreamFields.IsProgress(source, value)) return;
|
||||||
|
|
||||||
// Normal SDK 3.17 progress has no reference. The serial active window
|
// Progress is display-only, including late SDK callbacks. The active
|
||||||
// provides best-effort UI ownership, not a financial decision: a queued
|
// request's listener only attempts a bounded enqueue, never socket I/O.
|
||||||
// callback, including removal, may briefly show stale progress in a later window.
|
|
||||||
// The listener only attempts a bounded, nonblocking enqueue.
|
|
||||||
listener?.Invoke(source, value);
|
listener?.Invoke(source, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user