-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathTestContextExtensions.cs
More file actions
77 lines (67 loc) · 2.25 KB
/
TestContextExtensions.cs
File metadata and controls
77 lines (67 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DurableTask.Client.Entities;
using Microsoft.DurableTask.Entities;
using Microsoft.Extensions.Logging;
namespace DtsPortableSdkEntityTests;
internal static class TestContextExtensions
{
public static async Task<T> WaitForEntityStateAsync<T>(
this TestContext context,
EntityInstanceId entityInstanceId,
TimeSpan? timeout = null,
Func<T, string?>? describeWhatWeAreWaitingFor = null)
{
if (timeout == null)
{
timeout = Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(30);
}
Stopwatch sw = Stopwatch.StartNew();
EntityMetadata? response;
do
{
response = await context.Client.Entities.GetEntityAsync(entityInstanceId, includeState: true);
if (response != null)
{
if (describeWhatWeAreWaitingFor == null)
{
break;
}
else
{
var waitForResult = describeWhatWeAreWaitingFor(response.State.ReadAs<T>());
if (string.IsNullOrEmpty(waitForResult))
{
break;
}
else
{
context.Logger.LogInformation($"Waiting for {entityInstanceId} : {waitForResult}");
}
}
}
else
{
context.Logger.LogInformation($"Waiting for {entityInstanceId} to have state.");
}
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
while (sw.Elapsed < timeout);
if (response != null)
{
string serializedState = response.State.Value;
context.Logger.LogInformation($"Found state: {serializedState}");
return response.State.ReadAs<T>();
}
else
{
throw new TimeoutException($"Durable entity '{entityInstanceId}' still doesn't have any state!");
}
}
}