private void VerifyCommandFailedEvent(CommandFailedEvent actual, BsonDocument expected, string databaseName, string collectionName)
 {
     actual.CommandName.Should().Be(expected["command_name"].ToString());
 }
        public void ErrorSending(IEnumerable<RequestMessage> messages, ConnectionId connectionId, Exception exception)
        {
            foreach (var message in messages)
            {
                CommandState state;
                if (_state.TryRemove(message.RequestId, out state))
                {
                    state.Stopwatch.Stop();
                    if (_failedEvent != null)
                    {
                        var @event = new CommandFailedEvent(
                            state.CommandName,
                            exception,
                            state.OperationId,
                            message.RequestId,
                            connectionId,
                            state.Stopwatch.Elapsed);

                        _failedEvent(@event);
                    }
                }
            }
        }
        public void ConnectionFailed(ConnectionId connectionId, Exception exception)
        {
            if (_failedEvent == null)
            {
                return;
            }

            var pairs = _state.ToList();
            _state.Clear();
            foreach (var pair in pairs)
            {
                pair.Value.Stopwatch.Stop();
                var @event = new CommandFailedEvent(
                    pair.Value.CommandName,
                    exception,
                    pair.Value.OperationId,
                    pair.Key,
                    connectionId,
                    pair.Value.Stopwatch.Elapsed);

                _failedEvent(@event);
            }
        }
        public void ConnectionFailed(ConnectionId connectionId, Exception exception)
        {
            if (_failedEvent == null)
            {
                return;
            }

            var requestIds = _state.Keys;
            foreach (var requestId in requestIds)
            {
                CommandState state;
                if (_state.TryRemove(requestId, out state))
                {
                    state.Stopwatch.Stop();
                    var @event = new CommandFailedEvent(
                        state.CommandName,
                        exception,
                        state.OperationId,
                        requestId,
                        connectionId,
                        state.Stopwatch.Elapsed);

                    _failedEvent(@event);
                }
            }
        }