private void WaitForEvent(EventCapturer eventCapturer, BsonDocument operation) { var eventType = MapExpectedEventNameToDriverTypeName(operation.GetValue("event").ToString()); if (eventType == null) { return; } var expectedCount = operation.GetValue("count").ToInt32(); var notifyTask = eventCapturer.NotifyWhen(coll => coll.Count(c => c.GetType().Name == eventType) >= expectedCount); var timeout = TimeSpan.FromMinutes(1); if (operation.TryGetValue("timeout", out var timeoutValue)) { timeout = TimeSpan.FromMilliseconds(timeoutValue.AsInt32); } var index = Task.WaitAny(new[] { notifyTask }, timeout); if (index != 0) { throw new Exception($"{nameof(WaitForEvent)} for {eventType}({expectedCount}) executing exceeded timeout: {timeout}. \n\nTriggered events:\n{eventCapturer}"); } }
public async Task Ensure_server_session_are_allocated_only_on_connection_checkout() { var eventCapturer = new EventCapturer() .Capture <ConnectionPoolCheckedOutConnectionEvent>() .Capture <CommandStartedEvent>(); using var client = DriverTestConfiguration.CreateDisposableClient( (MongoClientSettings settings) => { settings.MaxConnectionPoolSize = 1; settings.ClusterConfigurator = c => c.Subscribe(eventCapturer); }, logger: null); var database = client.GetDatabase("test"); var collection = database.GetCollection <BsonDocument>("inventory"); database.DropCollection("inventory"); collection.InsertOne(new BsonDocument("x", 0)); var serverSessionPool = (CoreServerSessionPool)Reflector.GetFieldValue(client.Cluster, "_serverSessionPool"); var serverSessionsList = (List <ICoreServerSession>)Reflector.GetFieldValue(serverSessionPool, "_pool"); var serverSession = serverSessionsList.Single(); eventCapturer.Clear(); var eventsTask = eventCapturer.NotifyWhen(events => { var connectionCheckedOutEvent = events.OfType <ConnectionPoolCheckedOutConnectionEvent>().FirstOrDefault(); var commandStartedEvent = events.OfType <CommandStartedEvent>().FirstOrDefault(); if (commandStartedEvent.ConnectionId != null) { serverSessionsList.Count.Should().Be(0); commandStartedEvent.Command["lsid"].Should().Be(serverSession.Id); return(true); } else if (connectionCheckedOutEvent.ConnectionId != null) { serverSessionsList.Single().Should().Be(serverSession); } return(false); }); collection.InsertOne(new BsonDocument("x", 1)); await TasksUtils.WithTimeout(eventsTask, 1000); }
private void WaitForEvent(EventCapturer eventCapturer, BsonDocument operation) { var eventType = MapExpectedEventNameToDriverTypeName(operation.GetValue("event").ToString()); if (eventType == null) { return; } var expectedCount = operation.GetValue("count").ToInt32(); var notifyTask = eventCapturer.NotifyWhen(coll => coll.Count(c => c.GetType().Name == eventType) >= expectedCount); var testFailedTimeout = Task.Delay(TimeSpan.FromMinutes(1), CancellationToken.None); var index = Task.WaitAny(notifyTask, testFailedTimeout); if (index != 0) { throw new Exception($"{nameof(WaitForEvent)} executing is too long."); } }