Skip to content

Commit 926296e

Browse files
committed
.
1 parent 694e2f4 commit 926296e

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

readme.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
Extends [Verify](https://github.com/VerifyTests/Verify) to enable snapshotting of emails via [EmailPreviewServices](https://emailpreviewservices.com).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
88

9+
The purpose of this project is provide faster feedback when using code to generate html emails.
10+
11+
[EmailPreviewServices is a paid service](https://emailpreviewservices.com/en/pricing), and a account is required to get an API key.
12+
913
**See [Milestones](../../milestones?state=closed) for release notes.**
1014

1115

@@ -38,9 +42,9 @@ public static void Init() =>
3842
<sup><a href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-Initialize' title='Start of snippet'>anchor</a></sup>
3943
<!-- endSnippet -->
4044

41-
The default behavior is to use an environment variable named `EmailPreviewServicesApiKey` as the api key.
45+
The default behavior is to use an environment variable named `EmailPreviewServicesApiKey` as the API key.
4246

43-
An explicit api key can be used:
47+
An explicit API key can be used:
4448

4549
<!-- snippet: InitializeWithKey -->
4650
<a id='snippet-InitializeWithKey'></a>
@@ -53,6 +57,13 @@ public static void Init() =>
5357
<!-- endSnippet -->
5458

5559

60+
## Sample html
61+
62+
Assume the code under test produces the following html email.
63+
64+
snippet: html
65+
66+
5667
## Generating previews
5768

5869
<!-- snippet: sample -->
@@ -67,7 +78,7 @@ public async Task GeneratePreview()
6778
Html = html,
6879
Devices =
6980
[
70-
Device.Outlook2016,
81+
Device.Outlook2019,
7182
]
7283
};
7384
await Verify(preview);
@@ -85,11 +96,20 @@ Result:
8596

8697
## Performance
8798

88-
Generating previews takes in the range of tens of seconds. The time take can vary based on the number and type of email devices selected. For example generating previews for 5 devices (OutlookWebChrome, Outlook2019, Outlook2016, iPhone13, and GmailFirefox) takes ~30sec.
99+
Generating previews takes in the range of tens of seconds. The time take can vary based on the number and type of email devices selected. For example:
100+
101+
* Generating a a single device (GmailFirefox) takes ~20sec.
102+
* Generating previews for 5 devices (OutlookWebChrome, Outlook2019, Outlook2016, iPhone13, and GmailFirefox) takes ~25sec.
103+
104+
Execution will timeout after 6min and throw an exception.
105+
106+
Splitting individual devices, or groups of devices, over multiple tests can be used to have more control of when previews are generated and achieve faster feedback.
107+
108+
109+
## When to run tests
89110

90-
Execution will timeout after ~6.5min and throw an exception.
111+
Tests that execute in the 10s of seconds range can significantly slow down a test run. As such it is recomended that email preview tests are configured to be explicit in `Debug` mode.
91112

92-
Splitting individual or groups of devices over multiple tests can be used to have more control of when previews are generated and getting faster feedback.
93113

94114

95115
## Icon

src/Tests/Samples.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[TestFixture]
22
public class Samples
33
{
4+
#region html
5+
46
static string html =
57
"""
68
<!DOCTYPE html>
@@ -26,13 +28,15 @@ Get Started
2628
</html>
2729
""";
2830

31+
#endregion
32+
2933
[Test]
3034
[Explicit]
3135
public async Task BuildDeviceEnum()
3236
{
3337
var devices = await Builder.Service.GetDeviceListAsync();
3438
var stringBuilder = new StringBuilder();
35-
foreach (var device in devices.OrderBy(_=>_.Name))
39+
foreach (var device in devices.OrderBy(_ => _.Name))
3640
{
3741
var name = device.Name
3842
.Replace("Seznam.cz", "Seznam")
@@ -75,6 +79,8 @@ public async Task GenerateAll([Values] Device device)
7579
await Verify(preview);
7680
}
7781

82+
#region sample
83+
7884
[Test]
7985
[Explicit]
8086
public async Task GeneratePreview()
@@ -90,4 +96,6 @@ public async Task GeneratePreview()
9096
};
9197
await Verify(preview);
9298
}
99+
100+
#endregion
93101
}

src/Verify.EmailPreviewServices/Builder.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ static void ThrowIfDuplictes(ICollection<Device> devices)
6868

6969
static async Task GetDevicePreviews(EmailPreviewData preview)
7070
{
71-
const int maxAttempts = 50;
71+
const int maxAttempts = 360;
7272

73-
await Task.Delay(1000);
7473
for (var i = 0; i < maxAttempts; i++)
7574
{
75+
await Task.Delay(1000);
7676
var previews = await Service.GetPreviewAsync(preview.Id);
7777

7878
var failed = previews.Previews.SingleOrDefault(_ => _.Status == DevicePreviewDataStatus.FAILED);
@@ -85,21 +85,19 @@ static async Task GetDevicePreviews(EmailPreviewData preview)
8585
{
8686
return;
8787
}
88-
89-
await BackoffDelay(i);
9088
}
9189

9290
throw new("Timed out");
9391
}
9492

95-
static Task BackoffDelay(int attempt)
96-
{
97-
const double multiplier = 1.5;
98-
const int maxDelaySeconds = 8;
99-
100-
var exponentialDelay = Math.Pow(multiplier, attempt);
101-
var cappedDelay = Math.Min(exponentialDelay, maxDelaySeconds);
102-
103-
return Task.Delay(TimeSpan.FromSeconds(cappedDelay));
104-
}
93+
// static Task BackoffDelay(int attempt)
94+
// {
95+
// const double multiplier = 1.5;
96+
// const int maxDelaySeconds = 8;
97+
//
98+
// var exponentialDelay = Math.Pow(multiplier, attempt);
99+
// var cappedDelay = Math.Min(exponentialDelay, maxDelaySeconds);
100+
//
101+
// return Task.Delay(TimeSpan.FromSeconds(cappedDelay));
102+
// }
105103
}

0 commit comments

Comments
 (0)