Last active
September 28, 2015 09:07
-
-
Save DominicFinn/1416018 to your computer and use it in GitHub Desktop.
Lucene test. Concurrency pain fix idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using Cms51.Search.Lucene; | |
using Lucene.Net.Analysis.Standard; | |
using Lucene.Net.Documents; | |
using Lucene.Net.Index; | |
using Lucene.Net.Store; | |
using North51.Commons.Testing; | |
using North51.Commons.Web; | |
using Rhino.Mocks; | |
using Version = Lucene.Net.Util.Version; | |
namespace Cms51.Search.Tests.Lucene | |
{ | |
public class ConcurrencyTest : Specification | |
{ | |
RAMDirectory directory; | |
protected override void Given() | |
{ | |
var luceneConfiguration = Dependency<ILuceneConfiguration>(); | |
var analyzer = new StandardAnalyzer(Version.LUCENE_29); | |
luceneConfiguration | |
.Stub(x => x.GetAnalyzer()) | |
.Return(analyzer); | |
this.Inject<IEncoder>(new DefaultEncoder()); | |
// creates the search index | |
directory = new RAMDirectory(); | |
var indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); | |
indexWriter.Close(); | |
} | |
protected override void When() | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
new Thread(delegate() | |
{ | |
try | |
{ | |
var document = new Document(); | |
document.Add(new Field("testId", Guid.NewGuid().ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); | |
document.Add(new Field("resourceId", Guid.NewGuid().ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); | |
document.Add(new Field("Testfield", "A larger amount of test data for lucence to have a think about", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); | |
var indexWriter = new LockedIndexWriter(directory); | |
indexWriter.Add(document); | |
} | |
finally | |
{ | |
} | |
}).Start(); | |
} | |
Thread.Sleep(1000); | |
} | |
[Then] | |
public void Test() | |
{ | |
//Should pass without considerable amount of complaining from visual studio unable to obtain locks and clicking msgboxs | |
} | |
internal sealed class LockedIndexWriter | |
{ | |
readonly RAMDirectory directory; | |
static object mutex; | |
public LockedIndexWriter(RAMDirectory directory) | |
{ | |
this.directory = directory; | |
} | |
static LockedIndexWriter() | |
{ | |
mutex = new object(); | |
} | |
public void Add(Document document) | |
{ | |
lock (mutex) | |
{ | |
var indexWriter = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_29), false, IndexWriter.MaxFieldLength.UNLIMITED); | |
try | |
{ | |
indexWriter.AddDocument(document, new StandardAnalyzer(Version.LUCENE_29)); | |
indexWriter.Commit(); | |
} | |
finally | |
{ | |
indexWriter.Close(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment