public CouchbaseCluster(MemcacheClientConfiguration configuration, string bucket, IPEndPoint[] configurationHosts)
        {
            if (configurationHosts.Length == 0)
                throw new ArgumentException("There should be at least one value in the list", "configurationHosts");

            _isInitialized = false;

            _linesStreamReader = null;
            _webResponse = null;

            _configuration = configuration;
            if (_configuration.Authenticator == null)
                _configuration.Authenticator = MemcacheClientConfiguration.SaslPlainAuthenticatorFactory(string.Empty, bucket, string.Empty);

            _bucket = bucket;

            _currentConfigurationHost = 0;
            _configurationHosts = configurationHosts;

            _memcacheNodes = new Dictionary<string, IMemcacheNode>();

            _locator = null;

            _connectionTimer = new Timer(_ => ConnectToConfigurationStream(), null, Timeout.Infinite, Timeout.Infinite);
            _receivedInitialConfigurationBarrier = new ManualResetEventSlim();
        }
        public void TearDown()
        {
            _reader.Dispose();
            _reader = null;

            _pipe.Close();
            _pipe = null;
        }
        public void SetUp()
        {
            var localCounters = new TestCounters();
            _counters = localCounters;

            _pipe = new Aqueduct();
            _reader = new AsyncLinesStreamReader(_pipe.Out);
            _reader.OnError += e => { localCounters.IncrementErrors(); Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); };
            _reader.OnChunk += _ => { localCounters.IncrementChunks(); };

            _reader.StartReading();
        }
        private void KillCurrentConnection()
        {
            if (_linesStreamReader != null)
            {
                _linesStreamReader.Dispose();
                _linesStreamReader = null;
            }

            if (_webResponse != null)
            {
                _webResponse.Dispose();
                _webResponse = null;
            }
        }
        private void ConfigurationStreamRequestEndGetResponse(IAsyncResult ar)
        {
            if (_linesStreamReader != null)
            {
                OnError(new MemcacheException("Sanity check: The previous AsyncLinesStreamReader was not disposed"));
                return;
            }
            if (_webResponse != null)
            {
                OnError(new MemcacheException("Sanity check: The previous WebResponse was not disposed"));
                return;
            }

            WebResponse response = null;
            AsyncLinesStreamReader linesStreamReader = null;

            try
            {
                var request = (WebRequest)ar.AsyncState;
                response = request.EndGetResponse(ar);

                // Allocate new string reader and start reading
                linesStreamReader = new AsyncLinesStreamReader(response.GetResponseStream());
                linesStreamReader.OnError += HandleLinesStreamError;
                linesStreamReader.OnChunk += HandleConfigurationUpdate;

                lock (this)
                    if (!_isDisposed)
                    {
                        _webResponse = response;
                        _linesStreamReader = linesStreamReader;
                    }
                    else
                    {
                        response.Dispose();
                        linesStreamReader.Dispose();
                        return;
                    }

                _linesStreamReader.StartReading();
            }
            catch (Exception e)
            {
                if (OnError != null)
                    OnError(e);

                if (response != null)
                    response.Dispose();
                _webResponse = null;

                if (linesStreamReader != null)
                    linesStreamReader.Dispose();
                _linesStreamReader = null;

                // Handle HTTP query errors by trying another host
                RetryConnectingToConfigurationStream(delaySeconds: 1.0);
            }
        }