-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPersistenceTestsConfiguration.cs
More file actions
127 lines (100 loc) · 4.73 KB
/
PersistenceTestsConfiguration.cs
File metadata and controls
127 lines (100 loc) · 4.73 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
117
118
119
120
121
122
123
124
125
126
127
namespace ServiceControl.Audit.Persistence.Tests
{
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NServiceBus.CustomChecks;
using NUnit.Framework;
using Raven.Client.Documents;
using Raven.Client.Documents.BulkInsert;
using Raven.Client.ServerWide.Operations;
using ServiceControl.Audit.Auditing.BodyStorage;
using ServiceControl.Audit.Persistence.RavenDB;
using UnitOfWork;
class PersistenceTestsConfiguration
{
public IAuditDataStore AuditDataStore { get; private set; }
public IFailedAuditStorage FailedAuditStorage { get; private set; }
public IBodyStorage BodyStorage { get; private set; }
public IAuditIngestionUnitOfWorkFactory AuditIngestionUnitOfWorkFactory { get; private set; }
public IDocumentStore DocumentStore { get; private set; }
public IServiceProvider ServiceProvider => host.Services;
public string Name => "RavenDB";
public async Task Configure(Action<PersistenceSettings> setSettings)
{
var config = new RavenPersistenceConfiguration();
var hostBuilder = Host.CreateApplicationBuilder();
var persistenceSettings = new PersistenceSettings(TimeSpan.FromHours(1), true, 100000);
setSettings(persistenceSettings);
if (!persistenceSettings.PersisterSpecificSettings.ContainsKey(RavenPersistenceConfiguration.DatabasePathKey))
{
var instance = await SharedEmbeddedServer.GetInstance();
persistenceSettings.PersisterSpecificSettings[RavenPersistenceConfiguration.ConnectionStringKey] = instance.ServerUrl;
}
if (!persistenceSettings.PersisterSpecificSettings.ContainsKey(RavenPersistenceConfiguration.LogPathKey))
{
persistenceSettings.PersisterSpecificSettings[RavenPersistenceConfiguration.LogPathKey] = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Logs");
}
if (persistenceSettings.PersisterSpecificSettings.TryGetValue(RavenPersistenceConfiguration.DatabaseNameKey, out var configuredDatabaseName))
{
databaseName = configuredDatabaseName;
}
else
{
databaseName = Guid.NewGuid().ToString();
persistenceSettings.PersisterSpecificSettings[RavenPersistenceConfiguration.DatabaseNameKey] = databaseName;
}
var persistence = config.Create(persistenceSettings);
persistence.AddPersistence(hostBuilder.Services);
persistence.AddInstaller(hostBuilder.Services);
var assembly = typeof(RavenPersistenceConfiguration).Assembly;
foreach (var type in assembly.DefinedTypes)
{
if (type.IsAssignableTo(typeof(ICustomCheck)))
{
hostBuilder.Services.AddTransient(typeof(ICustomCheck), type);
}
}
host = hostBuilder.Build();
await host.StartAsync();
AuditDataStore = host.Services.GetRequiredService<IAuditDataStore>();
FailedAuditStorage = host.Services.GetRequiredService<IFailedAuditStorage>();
var documentStoreProvider = host.Services.GetRequiredService<IRavenDocumentStoreProvider>();
DocumentStore = await documentStoreProvider.GetDocumentStore();
var bulkInsert = DocumentStore.BulkInsert(
options: new BulkInsertOptions
{
SkipOverwriteIfUnchanged = true,
});
var sessionProvider = host.Services.GetRequiredService<IRavenSessionProvider>();
BodyStorage = new RavenAttachmentsBodyStorage(sessionProvider, bulkInsert, persistenceSettings.MaxBodySizeToStore);
AuditIngestionUnitOfWorkFactory = host.Services.GetRequiredService<IAuditIngestionUnitOfWorkFactory>();
}
public Task CompleteDBOperation()
{
DocumentStore.WaitForIndexing();
return Task.CompletedTask;
}
public async Task Cleanup()
{
if (DocumentStore != null)
{
await DocumentStore.Maintenance.Server.SendAsync(new DeleteDatabasesOperation(
new DeleteDatabasesOperation.Parameters
{
DatabaseNames = [databaseName],
HardDelete = true
}));
}
if (host != null)
{
await host.StopAsync();
host.Dispose();
}
}
string databaseName;
IHost host;
}
}