Ejemplo n.º 1
0
        public static ConnectionMultiplexer CreateDefault(
            TextWriter output,
            string clientName                = null,
            int?syncTimeout                  = null,
            bool?allowAdmin                  = null,
            int?keepAlive                    = null,
            int?connectTimeout               = null,
            string password                  = null,
            string tieBreaker                = null,
            TextWriter log                   = null,
            bool fail                        = true,
            string[] disabledCommands        = null,
            string[] enabledCommands         = null,
            bool checkConnect                = true,
            string failMessage               = null,
            string channelPrefix             = null,
            Proxy?proxy                      = null,
            string configuration             = null,
            bool logTransactionData          = true,
            int?defaultDatabase              = null,
            BacklogPolicy backlogPolicy      = null,
            [CallerMemberName] string caller = null)
        {
            StringWriter localLog = null;

            if (log == null)
            {
                log = localLog = new StringWriter();
            }
            try
            {
                var config = ConfigurationOptions.Parse(configuration);
                if (disabledCommands != null && disabledCommands.Length != 0)
                {
                    config.CommandMap = CommandMap.Create(new HashSet <string>(disabledCommands), false);
                }
                else if (enabledCommands != null && enabledCommands.Length != 0)
                {
                    config.CommandMap = CommandMap.Create(new HashSet <string>(enabledCommands), true);
                }

                if (Debugger.IsAttached)
                {
                    syncTimeout = int.MaxValue;
                }

                if (channelPrefix != null)
                {
                    config.ChannelPrefix = channelPrefix;
                }
                if (tieBreaker != null)
                {
                    config.TieBreaker = tieBreaker;
                }
                if (password != null)
                {
                    config.Password = string.IsNullOrEmpty(password) ? null : password;
                }
                if (clientName != null)
                {
                    config.ClientName = clientName;
                }
                else if (caller != null)
                {
                    config.ClientName = caller;
                }
                if (syncTimeout != null)
                {
                    config.SyncTimeout = syncTimeout.Value;
                }
                if (allowAdmin != null)
                {
                    config.AllowAdmin = allowAdmin.Value;
                }
                if (keepAlive != null)
                {
                    config.KeepAlive = keepAlive.Value;
                }
                if (connectTimeout != null)
                {
                    config.ConnectTimeout = connectTimeout.Value;
                }
                if (proxy != null)
                {
                    config.Proxy = proxy.Value;
                }
                if (defaultDatabase != null)
                {
                    config.DefaultDatabase = defaultDatabase.Value;
                }
                if (backlogPolicy != null)
                {
                    config.BacklogPolicy = backlogPolicy;
                }
                var watch = Stopwatch.StartNew();
                var task  = ConnectionMultiplexer.ConnectAsync(config, log);
                if (!task.Wait(config.ConnectTimeout >= (int.MaxValue / 2) ? int.MaxValue : config.ConnectTimeout * 2))
                {
                    task.ContinueWith(x =>
                    {
                        try
                        {
                            GC.KeepAlive(x.Exception);
                        }
                        catch { /* No boom */ }
                    }, TaskContinuationOptions.OnlyOnFaulted);
                    throw new TimeoutException("Connect timeout");
                }
                watch.Stop();
                if (output != null)
                {
                    Log(output, "Connect took: " + watch.ElapsedMilliseconds + "ms");
                }
                var muxer = task.Result;
                if (checkConnect && (muxer == null || !muxer.IsConnected))
                {
                    // If fail is true, we throw.
                    Assert.False(fail, failMessage + "Server is not available");
                    Skip.Inconclusive(failMessage + "Server is not available");
                }
                if (output != null)
                {
                    muxer.MessageFaulted += (msg, ex, origin) =>
                    {
                        output?.WriteLine($"Faulted from '{origin}': '{msg}' - '{(ex == null ? "(null)" : ex.Message)}'");
                        if (ex != null && ex.Data.Contains("got"))
                        {
                            output?.WriteLine($"Got: '{ex.Data["got"]}'");
                        }
                    };
                    muxer.Connecting += (e, t) => output?.WriteLine($"Connecting to {Format.ToString(e)} as {t}");
                    if (logTransactionData)
                    {
                        muxer.TransactionLog += msg => output?.WriteLine("tran: " + msg);
                    }
                    muxer.InfoMessage  += msg => output?.WriteLine(msg);
                    muxer.Resurrecting += (e, t) => output?.WriteLine($"Resurrecting {Format.ToString(e)} as {t}");
                    muxer.Closing      += complete => output?.WriteLine(complete ? "Closed" : "Closing...");
                }
                return(muxer);
            }
            catch
            {
                if (localLog != null)
                {
                    output?.WriteLine(localLog.ToString());
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        internal virtual IInternalConnectionMultiplexer Create(
            string clientName                = null,
            int?syncTimeout                  = null,
            bool?allowAdmin                  = null,
            int?keepAlive                    = null,
            int?connectTimeout               = null,
            string password                  = null,
            string tieBreaker                = null,
            TextWriter log                   = null,
            bool fail                        = true,
            string[] disabledCommands        = null,
            string[] enabledCommands         = null,
            bool checkConnect                = true,
            string failMessage               = null,
            string channelPrefix             = null,
            Proxy?proxy                      = null,
            string configuration             = null,
            bool logTransactionData          = true,
            bool shared                      = true,
            int?defaultDatabase              = null,
            BacklogPolicy backlogPolicy      = null,
            [CallerMemberName] string caller = null)
        {
            if (Output == null)
            {
                Assert.True(false, "Failure: Be sure to call the TestBase constuctor like this: BasicOpsTests(ITestOutputHelper output) : base(output) { }");
            }

            // Share a connection if instructed to and we can - many specifics mean no sharing
            if (shared &&
                _fixture != null && _fixture.IsEnabled &&
                enabledCommands == null &&
                disabledCommands == null &&
                fail &&
                channelPrefix == null &&
                proxy == null &&
                configuration == null &&
                password == null &&
                tieBreaker == null &&
                defaultDatabase == null &&
                (allowAdmin == null || allowAdmin == true) &&
                expectedFailCount == 0 &&
                backlogPolicy == null)
            {
                configuration = GetConfiguration();
                if (configuration == _fixture.Configuration)
                {   // only if the
                    return(_fixture.Connection);
                }
            }

            var muxer = CreateDefault(
                Writer,
                clientName, syncTimeout, allowAdmin, keepAlive,
                connectTimeout, password, tieBreaker, log,
                fail, disabledCommands, enabledCommands,
                checkConnect, failMessage,
                channelPrefix, proxy,
                configuration ?? GetConfiguration(),
                logTransactionData, defaultDatabase,
                backlogPolicy,
                caller);

            muxer.InternalError      += OnInternalError;
            muxer.ConnectionFailed   += OnConnectionFailed;
            muxer.ConnectionRestored += (s, e) =>
            {
                Log($"Connection Restored ({e.ConnectionType},{e.FailureType}): {e.Exception}");
            };
            return(muxer);
        }