nice checks for timings of successful contract

This commit is contained in:
benbierens 2024-05-24 14:33:59 +02:00
parent 6c956c1a64
commit 684a99027b
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
2 changed files with 21 additions and 16 deletions

View File

@ -105,13 +105,13 @@ namespace CodexPlugin
public TimeSpan? PendingToSubmitted => contractSubmittedUtc - contractPendingUtc;
public TimeSpan? SubmittedToStarted => contractStartedUtc - contractSubmittedUtc;
public TimeSpan? SubmittedToFinished => contractFinishedUtc - contractSubmittedUtc;
public TimeSpan? StartedToFinished => contractFinishedUtc - contractStartedUtc;
public void WaitForStorageContractSubmitted()
{
WaitForStorageContractState(gracePeriod, "submitted", sleep: 200);
contractSubmittedUtc = DateTime.UtcNow;
LogSubmittedDuration();
AssertDuration(PendingToSubmitted, gracePeriod, nameof(PendingToSubmitted));
}
public void WaitForStorageContractStarted()
@ -121,6 +121,7 @@ namespace CodexPlugin
WaitForStorageContractState(timeout, "started");
contractStartedUtc = DateTime.UtcNow;
LogStartedDuration();
AssertDuration(SubmittedToStarted, timeout, nameof(SubmittedToStarted));
}
public void WaitForStorageContractFinished()
@ -129,11 +130,12 @@ namespace CodexPlugin
{
WaitForStorageContractStarted();
}
var currentContractTime = DateTime.UtcNow - contractStartedUtc!.Value;
var currentContractTime = DateTime.UtcNow - contractSubmittedUtc!.Value;
var timeout = (Purchase.Duration - currentContractTime) + gracePeriod;
WaitForStorageContractState(timeout, "finished");
contractFinishedUtc = DateTime.UtcNow;
LogFinishedDuration();
AssertDuration(SubmittedToFinished, timeout, nameof(SubmittedToFinished));
}
public StoragePurchase GetPurchaseStatus(string purchaseId)
@ -173,18 +175,31 @@ namespace CodexPlugin
private void LogSubmittedDuration()
{
Log($"Pending to Submitted in {Time.FormatDuration(PendingToSubmitted)}");
Log($"Pending to Submitted in {Time.FormatDuration(PendingToSubmitted)} " +
$"( < {Time.FormatDuration(gracePeriod)})");
}
private void LogStartedDuration()
{
Log($"Submitted to Started in {Time.FormatDuration(SubmittedToStarted)}");
Log($"Submitted to Started in {Time.FormatDuration(SubmittedToStarted)} " +
$"( < {Time.FormatDuration(Purchase.Expiry + gracePeriod)})");
}
private void LogFinishedDuration()
{
Log($"Submitted to Finished in {Time.FormatDuration(SubmittedToFinished)}");
Log($"Started to Finished in {Time.FormatDuration(StartedToFinished)}");
Log($"Submitted to Finished in {Time.FormatDuration(SubmittedToFinished)} " +
$"( < {Time.FormatDuration(Purchase.Duration + gracePeriod)})");
}
private void AssertDuration(TimeSpan? span, TimeSpan max, string message)
{
if (span == null) throw new ArgumentNullException(nameof(MarketplaceAccess) + ": " + message + " (IsNull)");
if (span.Value.TotalDays >= max.TotalSeconds)
{
throw new Exception(nameof(MarketplaceAccess) +
$": Duration out of range. Max: {Time.FormatDuration(max)} but was: {Time.FormatDuration(span.Value)} " +
message);
}
}
private void Log(string msg)

View File

@ -83,10 +83,6 @@ namespace CodexTests.BasicTests
AssertBalance(contracts, client, Is.LessThan(clientInitialBalance), "Buyer was not charged for storage.");
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished));
AssertDuration(purchaseContract.PendingToSubmitted, 30, 1, nameof(purchaseContract.PendingToSubmitted));
AssertDuration(purchaseContract.SubmittedToStarted, purchase.Expiry.TotalSeconds, 30, nameof(purchaseContract.SubmittedToStarted));
AssertDuration(purchaseContract.SubmittedToFinished, purchase.Duration.TotalSeconds, 30, nameof(purchaseContract.SubmittedToFinished));
}
private void WaitForAllSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, IGethNode geth)
@ -121,11 +117,5 @@ namespace CodexTests.BasicTests
var slotHost = contracts.GetSlotHost(request, contractSlotIndex);
Assert.That(slotHost?.Address, Is.Not.Null);
}
private void AssertDuration(TimeSpan? span, double expectedSeconds, int tolerance, string message)
{
Assert.That(span.HasValue, "IsNull: " + message);
Assert.That(span!.Value.TotalSeconds, Is.EqualTo(expectedSeconds).Within(tolerance), message);
}
}
}