-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGatewayTestSuiteConstraints.cs
More file actions
116 lines (100 loc) · 4.09 KB
/
GatewayTestSuiteConstraints.cs
File metadata and controls
116 lines (100 loc) · 4.09 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
namespace NServiceBus.Gateway.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Configuration.AdvancedExtensibility;
using Raven.Client.Documents;
using Raven.Client.ServerWide;
using Raven.Client.ServerWide.Operations;
public partial class GatewayTestSuiteConstraints
{
public Task<GatewayDeduplicationConfiguration> ConfigureDeduplicationStorage(string endpointName, EndpointConfiguration configuration, RunSettings settings)
{
var ravenGatewayDeduplicationConfiguration = new RavenGatewayDeduplicationConfiguration((builder, _) =>
{
var databaseName = Guid.NewGuid().ToString();
var documentStore = GetInitializedDocumentStore(databaseName);
for (var i = 0; i < 3; i++)
{
try
{
documentStore.Maintenance.Server.Send(new CreateDatabaseOperation(new DatabaseRecord(databaseName)));
break;
}
catch (AggregateException)
{
// Usually, Failed to retrieve cluster topology from all known nodes
}
}
try
{
semaphoreSlim.Wait();
databases.Add(databaseName);
}
finally
{
semaphoreSlim.Release();
}
return documentStore;
});
var gatewaySettings = configuration.Gateway(ravenGatewayDeduplicationConfiguration);
configuration.GetSettings().Set(gatewaySettings);
return Task.FromResult<GatewayDeduplicationConfiguration>(ravenGatewayDeduplicationConfiguration);
}
public async Task Cleanup()
{
await semaphoreSlim.WaitAsync();
try
{
foreach (var databaseName in databases)
{
// Periodically the delete will throw an exception because Raven has the database locked
// To solve this we have a retry loop with a delay
var triesLeft = 3;
while (triesLeft-- > 0)
{
try
{
// We are using a new store because the global one is disposed of before cleanup
using (var storeForDeletion = GetInitializedDocumentStore(databaseName))
{
storeForDeletion.Maintenance.Server.Send(new DeleteDatabasesOperation(storeForDeletion.Database, hardDelete: true));
break;
}
}
catch
{
if (triesLeft == 0)
{
throw;
}
await Task.Delay(250);
}
}
Console.WriteLine("Deleted '{0}' database", databaseName);
}
databases.Clear();
}
finally
{
semaphoreSlim.Release();
}
}
static DocumentStore GetInitializedDocumentStore(string defaultDatabase)
{
var url = Environment.GetEnvironmentVariable("RavenSingleNodeUrl") ?? throw new Exception("RavenDB URL must be specified in an environment variable named RavenSingleNodeUrl.");
var documentStore = new DocumentStore
{
Urls = new[] { url },
Database = defaultDatabase
};
documentStore.Initialize();
return documentStore;
}
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
List<string> databases = [];
}
}