forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpReplicatorTest.cs
More file actions
186 lines (160 loc) · 6.51 KB
/
Copy pathHttpReplicatorTest.cs
File metadata and controls
186 lines (160 loc) · 6.51 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
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Support;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Directory = Lucene.Net.Store.Directory;
#if FEATURE_ASPNETCORE_TESTHOST
using Microsoft.AspNetCore.TestHost;
#else
using Lucene.Net.Replicator.Net;
#endif
namespace Lucene.Net.Replicator.Http
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Technically, the ConfigOption is only supported by ASP.NET Core
// so we just ignore the other option when running on HttpListener.
[TestFixture(IOOption.Synchronous, ConfigOption.StartupClass)]
[TestFixture(IOOption.Asynchronous, ConfigOption.StartupClass)]
#if FEATURE_ASPNETCORE_TESTHOST
[TestFixture(IOOption.Synchronous, ConfigOption.Middleware)]
[TestFixture(IOOption.Asynchronous, ConfigOption.Middleware)]
#endif
public class HttpReplicatorTest : ReplicatorTestCase
{
private DirectoryInfo clientWorkDir;
private IReplicator serverReplicator;
private IndexWriter writer;
private DirectoryReader reader;
private int port;
private string host;
private TestServer server;
private Directory serverIndexDir;
private Directory handlerIndexDir;
private MockErrorConfig mockErrorConfig;
private readonly bool useSynchronousIO;
private readonly bool useStartupClass;
public enum IOOption
{
Synchronous,
Asynchronous,
}
public enum ConfigOption
{
StartupClass,
Middleware
}
public HttpReplicatorTest(IOOption ioOption, ConfigOption configOption)
{
this.useSynchronousIO = ioOption == IOOption.Synchronous;
this.useStartupClass = configOption == ConfigOption.StartupClass;
}
private void StartServer()
{
ReplicationService service = new ReplicationService(new Dictionary<string, IReplicator> { { "s1", serverReplicator } });
server = NewHttpServer(service, mockErrorConfig, useSynchronousIO, useStartupClass);
port = ServerPort(server);
host = ServerHost(server);
}
public override void SetUp()
{
base.SetUp();
clientWorkDir = CreateTempDir("httpReplicatorTest");
handlerIndexDir = NewDirectory();
serverIndexDir = NewDirectory();
mockErrorConfig = new MockErrorConfig(); // LUCENENET specific
serverReplicator = new LocalReplicator();
StartServer();
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, null);
conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
writer = new IndexWriter(serverIndexDir, conf);
reader = DirectoryReader.Open(writer, false);
}
public override void TearDown()
{
StopHttpServer(server);
IOUtils.Dispose(reader, writer, handlerIndexDir, serverIndexDir);
base.TearDown();
}
private void PublishRevision(int id)
{
Document doc = new Document();
writer.AddDocument(doc);
writer.SetCommitData(Collections.SingletonMap("ID", id.ToString("X")));
writer.Commit();
serverReplicator.Publish(new IndexRevision(writer));
}
private void ReopenReader()
{
DirectoryReader newReader = DirectoryReader.OpenIfChanged(reader);
assertNotNull(newReader);
reader.Dispose();
reader = newReader;
}
[Test]
public void TestBasic()
{
IReplicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", server.CreateHandler());
ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null),
new PerSessionDirectoryFactory(clientWorkDir.FullName));
PublishRevision(1);
client.UpdateNow();
ReopenReader();
assertEquals(1, int.Parse(reader.IndexCommit.UserData["ID"], NumberStyles.HexNumber));
PublishRevision(2);
client.UpdateNow();
ReopenReader();
assertEquals(2, int.Parse(reader.IndexCommit.UserData["ID"], NumberStyles.HexNumber));
}
[Test]
public void TestServerErrors()
{
// tests the behaviour of the client when the server sends an error
IReplicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", server.CreateHandler());
using ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null),
new PerSessionDirectoryFactory(clientWorkDir.FullName));
try
{
PublishRevision(5);
try
{
mockErrorConfig.RespondWithError = true;
client.UpdateNow();
fail("expected exception");
}
catch (Exception t) when (t.IsThrowable())
{
// expected
}
mockErrorConfig.RespondWithError = false;
client.UpdateNow(); // now it should work
ReopenReader();
assertEquals(5, int.Parse(reader.IndexCommit.UserData["ID"], NumberStyles.HexNumber));
client.Dispose();
}
finally
{
mockErrorConfig.RespondWithError = false;
}
}
}
}