1

I am working on WebAPI application using RavenDB. I have a couple of XUnit tests which have kind of a similar outline:

var checkQuery = session.Query<Resource>().Where(x => x.AliasIds.Any(a => a == alias.Id));
PAssert.Throws<InvalidOperationException>(() => checkQuery.Single());

var testString = Guid.NewGuid().ToString();
Controller.Post(testString);

var res = checkQuery.Single();
PAssert.IsTrue(() => res != null);  

What happens is that when I have multiple tests run at the same time they fail at the line

var res = checkQuery.Single();

With exception:

Result Message: System.InvalidOperationException : Sequence contains no elements

What I have found:

  • It works fine if I got first call to checkQuery.Single() commented.
  • It works fine if I add Thread.Sleep(1000) before problematic line.

I tried to add

store.DatabaseCommands.DisableAllCaching();
store.Conventions.ShouldCacheRequest = _ => false;

but it didn't help.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Alex Lapa
  • 1,149
  • 11
  • 21

1 Answers1

2

Assuming that Controller.Post(testString) is adding a new entry, you probably just have a stale index. In the real world, some natural amount of time would pass between post and query. In unit tests, you don't have that delay, so it's common to provide the following on your index:

.Customize(x => x.WaitForNonStaleResults())

This is not something you should do in production. You can read more in the documentation here.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Didn't check if it is an answer yet, but is there a way to set it up this way in store configuration somewhere? – Alex Lapa Aug 19 '13 at 04:19