Waiting for embedded ES node to start completely

I am trying to create persistent subscription for my tests, and can’t find a way to make it work without a sleep after node.Start(). If I wait for NodeStatusChanged than I am getting the “Authentication error” on CreatePersistentSubscriptionAsync.

class Program

{

static void Main(string[] args)

{

var node = EmbeddedVNodeBuilder

.AsSingleNode()

.OnDefaultEndpoints()

.RunInMemory()

.Build();

//var wait = new AutoResetEvent(false);

//node.NodeStatusChanged += (sender, arg) =>

//{

// if (arg.NewVNodeState == VNodeState.Master) wait.Set();

//};

node.Start();

Thread.Sleep(3000);

//wait.WaitOne();

var settings = ConnectionSettings.Create()

.SetDefaultUserCredentials(new UserCredentials(“admin”, “changeit”));

var connection = EmbeddedEventStoreConnection.Create(node, settings);

connection.ConnectAsync().Wait();

connection.CreatePersistentSubscriptionAsync(“test”, “group”, PersistentSubscriptionSettings.Create(), null).Wait();

node.Stop();

}

}

With the latest embedded client (3.5.0) you can use the StartAndWaitUntilReady method from the ClusterVNode that returns a Task. This task will only complete once the node is ready (user management service has been initialised and the subsystems asked to start.

If you are using clients < 3.5.0 you can do the same as above and subscribe to UserManagementMessage.UserManagementServiceInitialized
e.g.

mainBus.Subscribe(new AdHocHandler<UserManagementMessage.UserManagementServiceInitialized>(

_ => //dostuff
));

``

Thanks for the quick answer! Yes, I have the latest version and StartAndWaitUntilReady did what it’s name states :slight_smile:

2016 m. kovas 16 d., trečiadienis 09:46:23 UTC+2, Pieter Germishuys rašė:

A little offtopic question. Is there a reason that ClusterVNode does not implement IDisposable? At first glance it feels like a good candidate to implement it…