Can't run projections after restarting embedded event store

I’m trying to use an embedded event store to run my tests, stopping it and starting a new one for every test. I’m using ProjectionsManager to create and enable a projection on every embedded event store I start. Here is the code in my [SetUp]:
var tcp = new IPEndPoint(IPAddress.Loopback, 9721);
var http = new IPEndPoint(IPAddress.Loopback, 9722);
_vnode = EmbeddedVNodeBuilder
.AsSingleNode()
.RunInMemory()
.WithInternalTcpOn(tcp)
.WithInternalHttpOn(http)
.WithExternalTcpOn(tcp)
.WithExternalHttpOn(http)
.AddExternalHttpPrefix(“http://+:9722/”)
.RunProjections(ProjectionsMode.All)
.StartStandardProjections()
.Build();
_vnode.StartAndWaitUntilReady().Wait();
_connection = EmbeddedEventStoreConnection.Create(_vnode,
ConnectionSettings.Create()
.EnableVerboseLogging()
.UseConsoleLogger()
.SetDefaultUserCredentials(new UserCredentials(“admin”, “changeit”))
.Build());
var projectionsManager = new ProjectionsManager(_logger, http, TimeSpan.FromSeconds(60));
projectionsManager.CreateContinuousAsync("$by_event_name", @"
fromAll().whenAny(function(s,e) {
if (e.metadata === null) return;
var eventName = e.metadata.eventName;
if (!eventName) return;
var lastDot = e.streamId.lastIndexOf(’.’);
if (lastDot == -1) return;
var namespace = e.streamId.slice(0, lastDot);
linkTo(’$en-’ + namespace + ‘.’ + eventName, e);
})", new UserCredentials(“admin”, “changeit”)).Wait();
projectionsManager.EnableAsync("$by_event_name", new UserCredentials(“admin”, “changeit”)).Wait();

``

and my [TearDown] is:

        _vnode.Stop();
        _connection.Close();

``

I’ve also tried using _vnode.Stop(TimeSpan.FromSeconds(60), true, true) in place of _vnode.Stop() to make sure it shuts down good and proper.

The first test I run always seems to work fine, but subsequent tests in the same session intermittently time out with a 408 when the ProjectionsManager tries to hit the event store over HTTP. Accessing it with the EmbeddedEventStoreConnection always seems to work if I try it; it’s only the ProjectionsManager that ever fails. As an aside, when I stopped trying to create my own projection and tried to just use the standard projections, I think I also encountered a situation where they weren’t running after a few test cases, but I haven’t managed to reproduce that consistently. I’m currently running version 3.5.2 of the NuGet packages, but I encountered the same behavior after upgrading to 3.6.2. The error I get is:

SetUp : System.AggregateException : One or more errors occurred.
----> EventStore.ClientAPI.Exceptions.ProjectionCommandFailedException : Server returned 408 (Server was unable to handle request in time) for POST on http://127.0.0.1:9722/projections/continuous?name=$by_event_name&type=JS&emit=1

``

Calling StartAndWait doesn’t ensure that the projections subsystem has completely been initialised and i’m curious to know what happens if you add a sleep of 2 - 3 seconds after calling StartAndWait. Does all your tests pass then?

Yeah, it looks like that solves this issue, thanks! I ran into my other issue where $by_category appears to not be running, but I guess that must be a separate issue. Is there any more direct way of waiting for the projection subsystem to come online?

Unfortunately there isn’t one currently. Opened an issue here to investigate the possibility of improving the StartAndWaitUntilReady method to ensure that the subsystems have been initialised before completing.

Awesome, thanks so much!

@pieter I forget if there is currently a message for projections being
ready, is there one? If so the work around for now could be just to
wait on that message from the bus.

@greg we have discussed it before but that work hasn’t been done yet.
The first part was to make sure the subsystems uses the SystemReady message to know that the Event Store Core is ready. The subsystems now need to indicate that they have been initialised. As you said, just another message.