Repository navigation
Expand file tree
/
Copy pathPackagedRavenDBTests.cs
More file actions
201 lines (181 loc) · 8.65 KB
/
Copy pathPackagedRavenDBTests.cs
File metadata and controls
201 lines (181 loc) · 8.65 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
namespace ServiceControlInstaller.Engine.UnitTests.Validation
{
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FileSystem;
using NUnit.Framework;
using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Operations.Indexes;
using Raven.Client.ServerWide;
using Raven.Client.ServerWide.Operations;
using TestHelper;
[TestFixture]
[Platform("Win")]
[NonParallelizable]
class PackagedRavenDBTests
{
[Test]
public async Task CanPersistAndIndexDocumentsWithoutNativeKafkaDependencies()
{
var root = Path.Combine(Path.GetTempPath(), "PackagedRavenDB", Guid.NewGuid().ToString("N"));
var serverDirectory = Path.Combine(root, "Server");
var dataDirectory = Path.Combine(root, "Data");
var serverUrl = $"http://127.0.0.1:{PortUtility.FindAvailablePort(33400)}";
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CurrentContext.CancellationToken);
timeout.CancelAfter(TimeSpan.FromMinutes(3));
var cancellationToken = timeout.Token;
Process server = null;
Task<string> standardOutput = null;
Task<string> standardError = null;
try
{
FileUtils.UnzipToSubdirectory("RavenDBServer.zip", serverDirectory);
using (Assert.EnterMultipleScope())
{
Assert.That(Path.Combine(serverDirectory, "libcurl.dll"), Does.Not.Exist);
Assert.That(Path.Combine(serverDirectory, "librdkafka.dll"), Does.Not.Exist);
Assert.That(Path.Combine(serverDirectory, "librdkafkacpp.dll"), Does.Not.Exist);
Assert.That(Path.Combine(serverDirectory, "Raven.Server.exe"), Does.Exist);
}
await StartServer();
const string databaseName = "PackagingSmokeTest";
using var store = new DocumentStore
{
Urls = [serverUrl],
Database = databaseName,
Conventions = { DisableTopologyUpdates = true }
}.Initialize();
await store.Maintenance.Server.SendAsync(new CreateDatabaseOperation(new DatabaseRecord(databaseName)), cancellationToken);
await store.Maintenance.SendAsync(new PutIndexesOperation(new IndexDefinition
{
Name = indexName,
Maps = ["from document in docs select new { document.Name }"]
}), cancellationToken);
using (var session = store.OpenAsyncSession())
{
await session.StoreAsync(new PackagedDocument { Name = documentName }, documentId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
await AssertDocumentAndIndex(store, cancellationToken);
await StopServer();
await StartServer();
await AssertDocumentAndIndex(store, cancellationToken);
}
finally
{
await StopServer();
await TryDeleteDirectory(root, CancellationToken.None);
}
async Task StartServer()
{
var startInfo = new ProcessStartInfo(Path.Combine(serverDirectory, "Raven.Server.exe"))
{
WorkingDirectory = serverDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
startInfo.ArgumentList.Add("--non-interactive");
startInfo.ArgumentList.Add("--log-to-console");
startInfo.ArgumentList.Add($"--ServerUrl={serverUrl}");
startInfo.ArgumentList.Add($"--DataDir={dataDirectory}");
startInfo.ArgumentList.Add($"--Logs.Path={Path.Combine(root, "Logs")}");
startInfo.ArgumentList.Add("--Setup.Mode=None");
startInfo.ArgumentList.Add("--Security.UnsecuredAccessAllowed=Local");
startInfo.ArgumentList.Add("--License.Eula.Accepted=true");
startInfo.ArgumentList.Add($"--License.Path={Path.Combine(TestContext.CurrentContext.TestDirectory, "RavenLicense.json")}");
startInfo.ArgumentList.Add("--License.DisableAutoUpdate=true");
startInfo.ArgumentList.Add("--License.DisableLicenseSupportCheck=true");
server = Process.Start(startInfo);
standardOutput = server.StandardOutput.ReadToEndAsync(CancellationToken.None);
standardError = server.StandardError.ReadToEndAsync(CancellationToken.None);
using var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan };
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
Assert.That(server.HasExited, Is.False, "The packaged RavenDB server exited before accepting requests.");
try
{
using var response = await client.GetAsync($"{serverUrl}/build/version", cancellationToken);
if (response.IsSuccessStatusCode)
{
return;
}
}
catch (HttpRequestException)
{
}
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
}
}
async Task StopServer()
{
if (server == null)
{
return;
}
try
{
if (!server.HasExited)
{
server.Kill(entireProcessTree: true);
}
await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15), cancellationToken);
await TestContext.Out.WriteLineAsync(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken));
await TestContext.Out.WriteLineAsync(await standardError.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken));
}
finally
{
server.Dispose();
server = null;
}
}
}
// Cleanup of the temp folder must not fail the test; Directory.Delete keeps the path stable across retries.
static async Task TryDeleteDirectory(string root, CancellationToken cancellationToken, int attempts = 5, TimeSpan? delay = null)
{
var retryDelay = delay ?? TimeSpan.FromMilliseconds(500);
for (var attempt = 1; Directory.Exists(root); attempt++)
{
try
{
Directory.Delete(root, recursive: true);
return;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
if (attempt == attempts)
{
await TestContext.Out.WriteLineAsync($"WARNING: Could not delete '{root}' after {attempts} attempts: {ex}");
return;
}
await Task.Delay(retryDelay * attempt, cancellationToken);
}
}
}
static async Task AssertDocumentAndIndex(IDocumentStore store, CancellationToken cancellationToken)
{
using var session = store.OpenAsyncSession();
var document = await session.LoadAsync<PackagedDocument>(documentId, cancellationToken);
Assert.That(document?.Name, Is.EqualTo(documentName));
var indexedDocument = await session.Advanced.AsyncDocumentQuery<PackagedDocument>(indexName)
.WaitForNonStaleResults(TimeSpan.FromSeconds(30))
.WhereEquals(nameof(PackagedDocument.Name), documentName)
.SingleAsync(cancellationToken);
Assert.That(indexedDocument.Name, Is.EqualTo(documentName));
}
class PackagedDocument
{
public string Name { get; set; }
}
const string documentId = "documents/1";
const string documentName = "Packaged RavenDB";
const string indexName = "Packaging/ByName";
}
}