जब यूनिट परीक्षण RavenDb के साथ परीक्षण करते हैं, तो अक्सर यह मामला होता है कि नया जोड़ा गया डेटा पुनर्प्राप्त या अन्यथा संसाधित किया जाता है। इससे 'स्टेल इंडेक्स' अपवाद हो सकते हैं उदा।रावेनडीबी: यूनिट परीक्षण
Bulk operation cancelled because the index is stale and allowStale is false
अपनी अनुक्रमणिका तक इंतजार के लिए जवाब
- How should stale indexes be handled during testing?
- WaitForNonStaleResults per DocumentStore
- RavenDb : Update a Denormalized Reference property value
तरह से डेटाबेस के लिए मजबूर करने (IDocumentStore
उदाहरण) के एक नंबर के अनुसार प्रसंस्करण से पहले पुरानी नहीं हैं एक प्रश्न या बैच आपरेशन IDocumentStore
initialisation के दौरान DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites
उपयोग करने के लिए, इस तरह है:
public class InMemoryRavenSessionProvider : IRavenSessionProvider
{
private static IDocumentStore documentStore;
public static IDocumentStore DocumentStore
{
get { return (documentStore ?? (documentStore = CreateDocumentStore())); }
}
private static IDocumentStore CreateDocumentStore()
{
var store = new EmbeddableDocumentStore
{
RunInMemory = true,
Conventions = new DocumentConvention
{
DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites,
IdentityPartsSeparator = "-"
}
};
store.Initialize();
IndexCreation.CreateIndexes(typeof (RavenIndexes).Assembly, store);
return store;
}
public IDocumentSession GetSession()
{
return DocumentStore.OpenSession();
}
}
दुर्भाग्य से, कोड से काम नहीं होता। मुझे अभी भी स्टेल इंडेक्स के बारे में अपवाद मिल रहे हैं। इन्हें डमी प्रश्नों में डालकर हल किया जा सकता है जिसमें .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
शामिल है।
यह ठीक है, जब तक कि इन्हें यूनिट टेस्ट में शामिल किया जा सके, लेकिन यदि वे नहीं कर सकते हैं तो क्या होगा? मुझे लगता है कि ये WaitForNonStaleResults*
कॉल उत्पादन कोड में रेंग रहे हैं इसलिए मैं पास करने के लिए यूनिट-टेस्ट प्राप्त कर सकता हूं।
तो, क्या रेवेनडीबी के नवीनतम संस्करण का उपयोग करके, निश्चित रूप से यूनिट परीक्षण के उद्देश्यों के लिए - इंडेक्स को संसाधित करने की अनुमति देने से पहले ताज़ा करने के लिए मजबूर करने के लिए एक निश्चित अग्नि तरीका है?
संपादित करें 1
यहाँ एक समाधान उत्तर के आधार पर देने के लिए नीचे दिए गए है कि एक प्रतीक्षा मजबूर करता जब तक सूचकांक बासी नहीं है। मैंने यूनिट-परीक्षण सुविधा के लिए इसे एक विस्तार विधि के रूप में लिखा है;
public static class IDocumentSessionExt
{
public static void ClearStaleIndexes(this IDocumentSession db)
{
while (db.Advanced.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0)
{
Thread.Sleep(10);
}
}
}
और यहाँ एक यूनिट टेस्ट कि वर्बोज़ WaitForNonStaleResultsAsOfLastWrite
तकनीक का उपयोग किया गया था, लेकिन अब neater विस्तार विधि का उपयोग करता।
[Fact]
public void Should_return_list_of_Relationships_for_given_mentor()
{
using (var db = Fake.Db())
{
var mentorId = Fake.Mentor(db).Id;
Fake.Relationship(db, mentorId, Fake.Mentee(db).Id);
Fake.Relationship(db, mentorId, Fake.Mentee(db).Id);
Fake.Relationship(db, Fake.Mentor(db).Id, Fake.Mentee(db).Id);
//db.Query<Relationship>()
// .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
// .Count()
// .ShouldBe(3);
db.ClearStaleIndexes();
db.Query<Relationship>().Count().ShouldBe(3);
MentorService.GetRelationships(db, mentorId).Count.ShouldBe(2);
}
}
यह अब DocumentStore बजाय DocumentSession के खिलाफ आयोजित है, इसलिए विस्तार विधि db.Advanced.DocumentStore.DatabaseCommands की तरह कुछ का उपयोग करने के बदल जाएगा। GetStatistics()। StaleIndexes.Any(), या अगर आप – adrian