Ejemplo n.º 1
0
 /// <summary>
 ///     Verifies that the specified action throws an exception of type <typeparam name="T"/>,
 ///     indicating whether to retry.
 /// </summary>
 public static T Throws <T>(RetryMode retry, Action action)
     where T : Exception
 {
     return(Throws <T>(retry, action, (Action <T, int>)null));
 }
Ejemplo n.º 2
0
        public static Task <IUserMessage> EmbedAsync(this IMessageChannel channel, string message, EmbedBuilder embedBuilder, IGuild guild = null, ContextMessageOption options = ContextMessageOption.None, IUser replyUser = null, RetryMode retryMode = RetryMode.AlwaysRetry)
        {
            if (false == embedBuilder?.Color.HasValue)
            {
                embedBuilder = embedBuilder.WithOkColour(guild);
            }

            var error       = options.Has(ContextMessageOption.Error);
            var replyToUser = options.Has(ContextMessageOption.ReplyUser);

            return(channel.EmbedAsync
                   (
                       embedBuilder ?? new EmbedBuilder()
            {
                Description = string.Format("{0}{1}{2}",
                                            error ? "💢 " : "",
                                            replyToUser ? $"{replyUser?.Mention} " : "",
                                            message
                                            ),
                Color = (error ? Bot.Ditto.Cache.Db.EmbedErrorColour(guild)
                        : Bot.Ditto.Cache.Db.EmbedColour(guild)
                         )
            },
                       embedBuilder == null ? string.Empty : message,
                       new RequestOptions()
            {
                RetryMode = retryMode
            }
                   ));
        }
Ejemplo n.º 3
0
 public static Task <IUserMessage> EmbedAsync(this IMessageChannel channel, EmbedBuilder embedBuilder, IGuild guild, ContextMessageOption options = ContextMessageOption.None, RetryMode retryMode = RetryMode.AlwaysRetry)
 => EmbedAsync(channel, "", embedBuilder, guild, options, retryMode: retryMode);
Ejemplo n.º 4
0
 public Task <IUserMessage> EmbedAsync(EmbedBuilder embedBuilder, ContextMessageOption options = ContextMessageOption.None, RetryMode retryMode = RetryMode.AlwaysRetry)
 => EmbedAsync("", embedBuilder, options, retryMode: retryMode);
Ejemplo n.º 5
0
 public static Task <IUserMessage> EmbedAsync(this IMessageChannel channel, string message, IGuild guild = null, ContextMessageOption options = ContextMessageOption.None, RetryMode retryMode = RetryMode.AlwaysRetry)
 => EmbedAsync(channel, message, null, guild, options, retryMode: retryMode);
 public RetryPolicyMock(RetryMode mode, int maxRetries = 3, TimeSpan delay = default, TimeSpan maxDelay = default) : base(mode, delay, maxDelay, maxRetries)
 {
 }
Ejemplo n.º 7
0
 public static void ThrowsPart(RetryMode retry, Action action)
 {
     ThrowsPart(new CompositionErrorExpectation {
         Id = (ErrorId)CompositionErrorId.Unknown
     }, retry, action);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Checks if retry can be done by requeuing the process to the scheduler.
 /// </summary>
 public static bool CanBeRetriedByReschedule(this RetryMode @this) => @this == RetryMode.Reschedule || @this == RetryMode.Both;
Ejemplo n.º 9
0
 /// <summary>
 /// Constructor.
 /// </summary>
 private RetryInfo(RetryReason retryReason, RetryMode retryMode)
 {
     RetryReason = retryReason;
     RetryMode   = retryMode;
 }
Ejemplo n.º 10
0
 public static void ThrowsErrors(ErrorId id1, ErrorId id2, RetryMode retry, Action action)
 {
     ThrowsErrors(new ErrorId[] { id1, id2 }, retry, action);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Checks if retry can be done inline by the pip executor.
 /// </summary>
 public static bool CanBeRetriedInline(this RetryMode @this) => @this == RetryMode.Inline || @this == RetryMode.Both;
Ejemplo n.º 12
0
 private static void ThrowsError(CompositionErrorExpectation expectation, RetryMode retry, Action action)
 {
     ThrowsErrors(new CompositionErrorExpectation[] { expectation }, retry, action);
 }
Ejemplo n.º 13
0
 public static void ThrowsError(ErrorId id, RetryMode retry, Action action)
 {
     ThrowsError(new CompositionErrorExpectation {
         Id = id,
     }, retry, action);
 }
Ejemplo n.º 14
0
 public static void ThrowsPart(ICompositionElement element, RetryMode retry, Action action)
 {
     ThrowsPart(new CompositionErrorExpectation {
         Id = (ErrorId)CompositionErrorId.Unknown, Element = element
     }, retry, action);
 }
Ejemplo n.º 15
0
 /// <summary>
 ///     Verifies that the specified action throws the specified exception,
 ///     indicating whether to retry.
 /// </summary>
 public static void Throws(Exception expected, RetryMode retry, Action action)
 {
     Throws(expected, retry, action, (Action <Exception, int>)null);
 }
Ejemplo n.º 16
0
 public Task <IUserMessage> EmbedAsync(string message, EmbedBuilder embedBuilder, ContextMessageOption options = ContextMessageOption.None, RetryMode retryMode = RetryMode.AlwaysRetry)
 => Channel.EmbedAsync(message, embedBuilder, Guild, options, User, retryMode: retryMode);
 protected RetryPolicyTestBase(RetryMode mode, bool isAsync) : base(isAsync)
 {
     _mode = mode;
 }
Ejemplo n.º 18
0
 public Task <IUserMessage> EmbedAsync(string message, ContextMessageOption options = ContextMessageOption.None, RetryMode retryMode = RetryMode.AlwaysRetry)
 => EmbedAsync(message, null, options, retryMode: retryMode);
Ejemplo n.º 19
0
        public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, string userAgent,
                                      string url = null, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null,
                                      RateLimitPrecision rateLimitPrecision = RateLimitPrecision.Second,
                                      bool useSystemClock = true)
            : base(restClientProvider, userAgent, defaultRetryMode, serializer, rateLimitPrecision, useSystemClock)
        {
            _gatewayUrl = url;
            if (url != null)
            {
                _isExplicitUrl = true;
            }
            WebSocketClient = webSocketProvider();
            //WebSocketClient.SetHeader("user-agent", DiscordConfig.UserAgent); (Causes issues in .NET Framework 4.6+)

            WebSocketClient.BinaryMessage += async(data, index, count) =>
            {
                using (var decompressed = new MemoryStream())
                {
                    if (data[0] == 0x78)
                    {
                        //Strip the zlib header
                        _compressed.Write(data, index + 2, count - 2);
                        _compressed.SetLength(count - 2);
                    }
                    else
                    {
                        _compressed.Write(data, index, count);
                        _compressed.SetLength(count);
                    }

                    //Reset positions so we don't run out of memory
                    _compressed.Position = 0;
                    _decompressor.CopyTo(decompressed);
                    _compressed.Position  = 0;
                    decompressed.Position = 0;

                    using (var reader = new StreamReader(decompressed))
                        using (var jsonReader = new JsonTextReader(reader))
                        {
                            var msg = _serializer.Deserialize <SocketFrame>(jsonReader);
                            if (msg != null)
                            {
                                await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                            }
                        }
                }
            };
            WebSocketClient.TextMessage += async text =>
            {
                using (var reader = new StringReader(text))
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        var msg = _serializer.Deserialize <SocketFrame>(jsonReader);
                        if (msg != null)
                        {
                            await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                        }
                    }
            };
            WebSocketClient.Closed += async ex =>
            {
                await DisconnectAsync().ConfigureAwait(false);

                await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
            };
        }
Ejemplo n.º 20
0
 private static void ThrowsChangeRejectedErrors(CompositionErrorExpectation[] expectations, RetryMode retry, Action action)
 {
     ExceptionAssert.Throws <ChangeRejectedException>(retry, action, (thrownException, retryCount) =>
     {
         AssertCore(retryCount, "CompositionException", thrownException, expectations);
     });
 }