コード例 #1
0
ファイル: AsyncCacheTests.cs プロジェクト: AI4E/AI4E.Utils
        public async Task OldUpdateOperationCanceledOnUpdateTest()
        {
            var revision = 0;
            CancellationToken cancellation;

            async Task <int> update(CancellationToken c)
            {
                var r = Interlocked.Increment(ref revision);

                if (r == 1)
                {
                    cancellation = c;
                }

                await Task.Delay(50, c);

                return(r);
            }

            var asyncCache = new AsyncCache <int>(update);

            asyncCache.Update();
            asyncCache.Update();

            Assert.IsTrue(cancellation.IsCancellationRequested);
            Assert.AreEqual(revision, await asyncCache.Task);
        }
コード例 #2
0
ファイル: AsyncCacheTests.cs プロジェクト: AI4E/AI4E.Utils
        public void UpdateAfterDisposeThrowsTest()
        {
            Task <int> update(CancellationToken c)
            {
                return(Task.FromResult(0));
            }

            var asyncCache = new AsyncCache <int>(update);

            asyncCache.Dispose();

            Assert.ThrowsException <ObjectDisposedException>(() =>
            {
                asyncCache.Update();
            });
        }
コード例 #3
0
ファイル: AsyncCacheTests.cs プロジェクト: AI4E/AI4E.Utils
        public async Task UpdateHandedOutTaskTest()
        {
            var tcs      = new TaskCompletionSource <object>();
            var revision = 0;

            async Task <int> update()
            {
                var r = Interlocked.Increment(ref revision);
                await tcs.Task;

                return(r);
            }

            var asyncCache = new AsyncCache <int>(update);
            var task       = asyncCache.Task;

            asyncCache.Update();
            tcs.SetResult(null);

            var result = await task;

            Assert.AreEqual(revision, result);
        }
コード例 #4
0
        private void CreateCaches()
        {
            _servers = new AsyncCache <Server, API.Models.ServerReference>(
                (key, parentKey) =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Created server {key}.");
                }
                return(new Server(key, this));
            },
                (server, model) =>
            {
                server.Name = model.Name;
                _channels.Update(server.DefaultChannelId, server.Id, null);
                if (model is ExtendedServerInfo)
                {
                    var extendedModel   = model as ExtendedServerInfo;
                    server.AFKChannelId = extendedModel.AFKChannelId;
                    server.AFKTimeout   = extendedModel.AFKTimeout;
                    server.JoinedAt     = extendedModel.JoinedAt ?? DateTime.MinValue;
                    server.OwnerId      = extendedModel.OwnerId;
                    server.Region       = extendedModel.Region;

                    foreach (var role in extendedModel.Roles)
                    {
                        _roles.Update(role.Id, model.Id, role);
                    }
                    foreach (var channel in extendedModel.Channels)
                    {
                        _channels.Update(channel.Id, model.Id, channel);
                    }
                    foreach (var membership in extendedModel.Members)
                    {
                        _users.Update(membership.User.Id, membership.User);
                        server.UpdateMember(membership);
                    }
                    foreach (var membership in extendedModel.VoiceStates)
                    {
                        server.UpdateMember(membership);
                    }
                    foreach (var membership in extendedModel.Presences)
                    {
                        server.UpdateMember(membership);
                    }
                }
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated server {server.Name} ({server.Id}).");
                }
            },
                server =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed server {server.Name} ({server.Id}).");
                }
            }
                );

            _channels = new AsyncCache <Channel, API.Models.ChannelReference>(
                (key, parentKey) =>
            {
                if (_isDebugMode)
                {
                    if (parentKey != null)
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Created channel {key} in server {parentKey}.");
                    }
                    else
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Created private channel {key}.");
                    }
                }
                return(new Channel(key, parentKey, this));
            },
                (channel, model) =>
            {
                channel.Name = model.Name;
                channel.Type = model.Type;
                if (model is ChannelInfo)
                {
                    var extendedModel = model as ChannelInfo;
                    channel.Position  = extendedModel.Position;

                    if (extendedModel.IsPrivate)
                    {
                        var user              = _users.Update(extendedModel.Recipient.Id, extendedModel.Recipient);
                        channel.RecipientId   = user.Id;
                        user.PrivateChannelId = channel.Id;
                    }

                    if (extendedModel.PermissionOverwrites != null)
                    {
                        channel.PermissionOverwrites = extendedModel.PermissionOverwrites.Select(x => new Channel.PermissionOverwrite
                        {
                            Type  = x.Type,
                            Id    = x.Id,
                            Deny  = new PackedPermissions(x.Deny),
                            Allow = new PackedPermissions(x.Allow)
                        }).ToArray();
                    }
                    else
                    {
                        channel.PermissionOverwrites = null;
                    }
                }
                if (_isDebugMode)
                {
                    if (channel.IsPrivate)
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated private channel {channel.Name} ({channel.Id}).");
                    }
                    else
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated channel {channel.Name} ({channel.Id}) in server {channel.Server?.Name} ({channel.ServerId}).");
                    }
                }
            },
                channel =>
            {
                if (channel.IsPrivate)
                {
                    var user = channel.Recipient;
                    if (user.PrivateChannelId == channel.Id)
                    {
                        user.PrivateChannelId = null;
                    }
                    if (_isDebugMode)
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed private channel {channel.Name} ({channel.Id}).");
                    }
                }
                else
                {
                    if (_isDebugMode)
                    {
                        RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed channel {channel.Name} ({channel.Id}) in server {channel.Server?.Name} ({channel.ServerId}).");
                    }
                }
            });

            _messages = new AsyncCache <Message, API.Models.MessageReference>(
                (key, parentKey) =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Created message {key} in channel {parentKey}.");
                }
                return(new Message(key, parentKey, this));
            },
                (message, model) =>
            {
                if (model is API.Models.Message)
                {
                    var extendedModel = model as API.Models.Message;
                    if (extendedModel.Attachments != null)
                    {
                        message.Attachments = extendedModel.Attachments.Select(x => new Message.Attachment
                        {
                            Id       = x.Id,
                            Url      = x.Url,
                            ProxyUrl = x.ProxyUrl,
                            Size     = x.Size,
                            Filename = x.Filename,
                            Width    = x.Width,
                            Height   = x.Height
                        }).ToArray();
                    }
                    else
                    {
                        message.Attachments = new Message.Attachment[0];
                    }
                    if (extendedModel.Embeds != null)
                    {
                        message.Embeds = extendedModel.Embeds.Select(x =>
                        {
                            var embed = new Message.Embed
                            {
                                Url         = x.Url,
                                Type        = x.Type,
                                Description = x.Description,
                                Title       = x.Title
                            };
                            if (x.Provider != null)
                            {
                                embed.Provider = new Message.EmbedReference
                                {
                                    Url  = x.Provider.Url,
                                    Name = x.Provider.Name
                                };
                            }
                            if (x.Author != null)
                            {
                                embed.Author = new Message.EmbedReference
                                {
                                    Url  = x.Author.Url,
                                    Name = x.Author.Name
                                };
                            }
                            if (x.Thumbnail != null)
                            {
                                embed.Thumbnail = new Message.File
                                {
                                    Url      = x.Thumbnail.Url,
                                    ProxyUrl = x.Thumbnail.ProxyUrl,
                                    Width    = x.Thumbnail.Width,
                                    Height   = x.Thumbnail.Height
                                };
                            }
                            return(embed);
                        }).ToArray();
                    }
                    else
                    {
                        message.Embeds = new Message.Embed[0];
                    }
                    message.IsMentioningEveryone = extendedModel.IsMentioningEveryone;
                    message.IsTTS           = extendedModel.IsTextToSpeech;
                    message.MentionIds      = extendedModel.Mentions?.Select(x => x.Id)?.ToArray() ?? new string[0];
                    message.IsMentioningMe  = message.MentionIds.Contains(_myId);
                    message.RawText         = extendedModel.Content;
                    message.Timestamp       = extendedModel.Timestamp;
                    message.EditedTimestamp = extendedModel.EditedTimestamp;
                    if (extendedModel.Author != null)
                    {
                        message.UserId = extendedModel.Author.Id;
                    }
                }
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated message {message.Id} in channel {message.Channel?.Name} ({message.ChannelId}).");
                }
            },
                message =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed message {message.Id} in channel {message.Channel?.Name} ({message.ChannelId}).");
                }
            }
                );

            _roles = new AsyncCache <Role, API.Models.Role>(
                (key, parentKey) =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Created role {key} in server {parentKey}.");
                }
                return(new Role(key, parentKey, this));
            },
                (role, model) =>
            {
                role.Name = model.Name;
                role.Permissions.RawValue = (uint)model.Permissions;
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated role {role.Name} ({role.Id}) in server {role.Server?.Name} ({role.ServerId}).");
                }
            },
                role =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed role {role.Name} ({role.Id}) in server {role.Server?.Name} ({role.ServerId}).");
                }
            }
                );

            _users = new AsyncCache <User, API.Models.UserReference>(
                (key, parentKey) =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Created user {key}.");
                }
                return(new User(key, this));
            },
                (user, model) =>
            {
                user.AvatarId      = model.Avatar;
                user.Discriminator = model.Discriminator;
                user.Name          = model.Username;
                if (model is SelfUserInfo)
                {
                    var extendedModel = model as SelfUserInfo;
                    user.Email        = extendedModel.Email;
                    user.IsVerified   = extendedModel.IsVerified;
                }
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Updated user {user?.Name} ({user.Id}).");
                }
            },
                user =>
            {
                if (_isDebugMode)
                {
                    RaiseOnDebugMessage(DebugMessageType.Cache, $"Destroyed user {user?.Name} ({user.Id}).");
                }
            }
                );
        }
コード例 #5
0
 internal Membership UpdateMember(API.Models.MemberInfo membership)
 {
     return(_members.Update(membership.User?.Id ?? membership.UserId, Id, membership));
 }