public OrganizationRepositoryTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory)
 {
     Log.SetLogLevel <OrganizationRepository>(LogLevel.Trace);
     _cache      = GetService <ICacheClient>() as InMemoryCacheClient;
     _repository = GetService <IOrganizationRepository>();
     _plans      = GetService <BillingPlans>();
 }
Example #2
0
        static async Task Main()
        {
            // Sets up Miki.Logging for internal library logging. Can be removed if you do not want to
            // see internal logs.
            ExampleHelper.InitLog(LogLevel.Information);

            // Fetches your token from environment values.
            var token = ExampleHelper.GetTokenFromEnv();

            var memCache = new InMemoryCacheClient(new ProtobufSerializer());

            var apiClient = new DiscordApiClient(token, memCache);

            // Discord direct gateway implementation.
            var gateway = new GatewayCluster(
                new GatewayProperties
            {
                ShardCount = 1,
                ShardId    = 0,
                Token      = token.ToString(),
            });

            var discordClient = new DiscordClient(apiClient, gateway, memCache);

            // Subscribe to ready event.
            discordClient.Events.MessageCreate.SubscribeTask(OnMessageReceived);

            // Start the connection to the gateway.
            await gateway.StartAsync();

            // Wait, else the application will close.
            await Task.Delay(-1);
        }
        public OrganizationRepositoryTests(ITestOutputHelper output) : base(output)
        {
            _cache      = GetService <ICacheClient>() as InMemoryCacheClient;
            _repository = GetService <IOrganizationRepository>();

            Log.SetLogLevel <OrganizationRepository>(LogLevel.Trace);
        }
 public UserAgentParser(ILoggerFactory loggerFactory)
 {
     _localCache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
         LoggerFactory = loggerFactory, MaxItems = 250, CloneValues = true
     });
     _logger = loggerFactory.CreateLogger <UserAgentParser>();
 }
        public override async Task InitializeAsync()
        {
            await base.InitializeAsync();

            _migrationIndex = new MigrationIndex(_configuration);
            await _migrationIndex.DeleteAsync();

            await _migrationIndex.ConfigureAsync();

            var cacheClient = new InMemoryCacheClient();
            var messageBus  = new InMemoryMessageBus();

            _lockProvider             = new CacheLockProvider(cacheClient, messageBus, Log);
            _migrationStateRepository = new MigrationStateRepository(_migrationIndex);

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <VersionedWithoutVersionMigration>();
            serviceCollection.AddSingleton <Version1Migration>();
            serviceCollection.AddSingleton <Version2Migration>();
            serviceCollection.AddSingleton <Version3Migration>();
            serviceCollection.AddSingleton <FailingMigration>();
            serviceCollection.AddSingleton <FailingResumableMigration>();
            serviceCollection.AddSingleton <RepeatableMigration>();
            _serviceProvider  = serviceCollection.BuildServiceProvider();
            _migrationManager = new MigrationManager(_serviceProvider, _migrationStateRepository, _lockProvider, Log);
        }
        public void Register(ref ContainerBuilder container)
        {
            //Debugger.Launch();
            container.RegisterType <DummyPermissionChecker>()
            .As <IPermissionChecker>()
            .InstancePerLifetimeScope()
            .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
            .EnableClassInterceptors();

            container.RegisterType <A.Core.ActionContext>()
            .As <A.Core.Interface.IActionContext>()
            .InstancePerLifetimeScope()
            .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
            .EnableClassInterceptors();

            InMemoryCacheClient client = new InMemoryCacheClient()
            {
                MaxItems = 250
            };

            container.RegisterInstance <ICacheClient>(client);

            container.Register(c => new LogInterceptorProxy()).PropertiesAutowired();
            container.Register(c => new TransactionInterceptorProxy()).PropertiesAutowired();
            container.Register(c => new CacheInterceptorProxy(c.Resolve <ICacheClient>(), c.Resolve <IActionContext>()));
        }
Example #7
0
        private async Task <CacheValue <string> > GetCache(string cachekey, string cachevalue)
        {
            ICacheClient cache = new InMemoryCacheClient();
            await cache.SetAsync(cachekey, cachevalue);

            return(await cache.GetAsync <string>(cachekey));
        }
        private async Task StartInternalAsync(
            string lockName,
            LockLostBehavior lockLostBehavior,
            CancellationToken cancellationToken)
        {
            _log.TraceMethodEntry();

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var lockTimeout = TimeSpan.FromSeconds(5);

            using (var inMemoryLockCacheClient = new InMemoryCacheClient())
                using (var distributedLockCacheClient = new HybridCacheClient(inMemoryLockCacheClient, _messageBus))
                {
                    var lockProvider = new CacheLockProvider(distributedLockCacheClient, _messageBus);

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        var lockHandle = await lockProvider.AcquireAsync(
                            lockName,
                            lockTimeout,
                            cancellationToken)
                                         .ConfigureAwait(false);

                        if (lockHandle != null)
                        {
                            try
                            {
                                // we have the lock
                                // keep renewing it
                                await HoldAndRenewAsync(
                                    lockHandle,
                                    cancellationToken)
                                .ConfigureAwait(false);
                            }
                            catch (Exception e)
                            {
                                switch (lockLostBehavior)
                                {
                                case LockLostBehavior.Complete:
                                    return;

                                case LockLostBehavior.Error:
                                    throw new LockLostException(lockName, e);

                                case LockLostBehavior.Retry:
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
        }
 public MaxMindGeoIpService(IFileStorage storage, ILoggerFactory loggerFactory)
 {
     _storage    = storage;
     _localCache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
         LoggerFactory = loggerFactory, MaxItems = 250, CloneValues = true
     });
     _logger = loggerFactory.CreateLogger <MaxMindGeoIpService>();
 }
Example #10
0
        static async Task MainAsync(string[] args)
        {
            // Enables library wide logging for all Miki libraries. Consider using this logging for debugging or general information.
            new LogBuilder().SetLogHeader(x => $"[{DateTime.UtcNow.ToLongTimeString()}]")
            .AddLogEvent((msg, level) =>
            {
                if (level >= logLevel)
                {
                    Console.WriteLine($"{msg}");
                }
            }).Apply();

            // A cache client is needed to store ratelimits and entities.
            // This is an in-memory cache, and will be used as a local storage repository.
            IExtendedCacheClient cache = new InMemoryCacheClient(
                new ProtobufSerializer()
                );

            // Discord REST API implementation gets set up.
            IApiClient api = new DiscordApiClient(Token, cache);

            // Discord direct gateway implementation.
            IGateway gateway = new GatewayCluster(new GatewayProperties
            {
                ShardCount             = 1,
                ShardId                = 0,
                Token                  = Token,
                Compressed             = true,
                WebSocketClientFactory = () => new BasicWebSocketClient()
            });

            // This function adds additional utility, caching systems and more.
            DiscordClient bot = new DiscordClient(new DiscordClientConfigurations
            {
                ApiClient   = api,
                Gateway     = gateway,
                CacheClient = cache
            });

            // Add caching events for the gateway.
            new BasicCacheStage().Initialize(gateway, cache);

            // Hook up on the MessageCreate event. This will send every message through this flow.
            bot.MessageCreate += async(msg) => {
                if (msg.Content == "ping")
                {
                    IDiscordTextChannel channel = await msg.GetChannelAsync();

                    await channel.SendMessageAsync("pong!");
                }
            };

            // Start the connection to the gateway.
            await gateway.StartAsync();

            // Wait, else the application will close.
            await Task.Delay(-1);
        }
        public void CanSetAndGetValue()
        {
            var cache = new InMemoryCacheClient();

            cache.Set("test", 1);
            var value = cache.Get <int>("test");

            Assert.Equal(1, value);
        }
Example #12
0
 public virtual async Task CanHaveMultipleQueueInstancesWithLockingAsync()
 {
     using (var cache = new InMemoryCacheClient(Log)) {
         using (var messageBus = new InMemoryMessageBus(Log)) {
             var distributedLock = new CacheLockProvider(cache, messageBus, Log);
             await CanHaveMultipleQueueInstancesWithLockingImplAsync(distributedLock);
         }
     }
 }
Example #13
0
        public BlackjackServiceTests()
            : base(x => new MikiDbContext(x))
        {
            var cache = new InMemoryCacheClient(
                new ProtobufSerializer());
            var mock = Mock.Of <ITransactionService>();

            Service = new BlackjackService(cache, mock);
        }
 public SemanticVersionParser(ILoggerFactory loggerFactory)
 {
     _localCache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
         LoggerFactory = loggerFactory
     })
     {
         MaxItems = 250
     };
     _logger = loggerFactory.CreateLogger <SemanticVersionParser>();
 }
Example #15
0
 public UserAgentParser(ILoggerFactory loggerFactory)
 {
     _localCache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
         LoggerFactory = loggerFactory
     })
     {
         MaxItems = 250
     };
     _logger = loggerFactory.CreateLogger <UserAgentParser>();
 }
        public ElasticRepositoryTestBase(ITestOutputHelper output) : base(output) {
            SystemClock.Reset();
            Log.MinimumLevel = LogLevel.Trace;
            Log.SetLogLevel<ScheduledTimer>(LogLevel.Warning);

            _cache = new InMemoryCacheClient(Log);
            _messgeBus = new InMemoryMessageBus(Log);
            _workItemQueue = new InMemoryQueue<WorkItemData>(loggerFactory: Log);
            _configuration = new MyAppElasticConfiguration(_workItemQueue, _cache, _messgeBus, Log);
            _client = _configuration.Client;
        }
        public void Register(ref UnityContainer container)
        {
            //Debugger.Launch();
            container.RegisterType <IPermissionChecker, DummyPermissionChecker>(new HierarchicalLifetimeManager());
            InMemoryCacheClient client = new InMemoryCacheClient()
            {
                MaxItems = 250
            };

            container.RegisterInstance <ICacheClient>(client);
        }
Example #18
0
        public async Task CanRunThrottledJobs() {
            using (var client = new InMemoryCacheClient(new InMemoryCacheClientOptions { LoggerFactory = Log })) {
                var jobs = new List<ThrottledJob>(new[] { new ThrottledJob(client, Log), new ThrottledJob(client, Log), new ThrottledJob(client, Log) });

                var sw = Stopwatch.StartNew();
                await Task.WhenAll(jobs.Select(async job => await job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: TimeSpan.FromSeconds(1).ToCancellationToken())));
                sw.Stop();
                Assert.InRange(jobs.Sum(j => j.RunCount), 4, 14);
                _logger.Info(jobs.Sum(j => j.RunCount).ToString());
                Assert.InRange(sw.ElapsedMilliseconds, 20, 1500);
            }
        }
        public ElasticRepositoryTestBase(ITestOutputHelper output) : base(output)
        {
            SystemClock.Reset();
            Log.MinimumLevel = LogLevel.Trace;
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Warning);

            _cache         = new InMemoryCacheClient(Log);
            _messgeBus     = new InMemoryMessageBus(Log);
            _workItemQueue = new InMemoryQueue <WorkItemData>(loggerFactory: Log);
            _configuration = new MyAppElasticConfiguration(_workItemQueue, _cache, _messgeBus, Log);
            _client        = _configuration.Client;
        }
Example #20
0
        public async Task CanRunThrottledJobs()
        {
            var client = new InMemoryCacheClient();
            var jobs   = new List <ThrottledJob>(new[] { new ThrottledJob(client), new ThrottledJob(client), new ThrottledJob(client) });

            var sw = Stopwatch.StartNew();
            await Task.WhenAll(jobs.Select(async job => await TaskExtensions.AnyContext(job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: TimeSpan.FromSeconds(1).ToCancellationToken()))));

            sw.Stop();
            Assert.InRange(jobs.Sum(j => j.RunCount), 6, 14);
            Assert.InRange(sw.ElapsedMilliseconds, 20, 1500);
        }
Example #21
0
 public virtual async Task CanDequeueWithLockingAsync()
 {
     using (var cache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
         LoggerFactory = Log
     })) {
         using (var messageBus = new InMemoryMessageBus(new InMemoryMessageBusOptions {
             LoggerFactory = Log
         })) {
             var distributedLock = new CacheLockProvider(cache, messageBus, Log);
             await CanDequeueWithLockingImpAsync(distributedLock);
         }
     }
 }
Example #22
0
        public async Task AcquireLockAsyncTest()
        {
            var cacheClient = new InMemoryCacheClient(new ProtobufSerializer());
            var tokenSource = new CancellationTokenSource();

            var testLock = await cacheClient.AcquireLockAsync("test:InMemory:acquire", tokenSource.Token);

            Assert.Equal(1, await cacheClient.GetAsync <int>("test:InMemory:acquire"));

            await testLock.ReleaseAsync();

            Assert.Null(await cacheClient.GetAsync <string>("test:InMemory:acquire"));
        }
Example #23
0
        public static IServiceCollection AddCacheClient(this IServiceCollection services)
        {
            services.AddSingleton(provider =>
            {
                var options = provider.GetRequiredService <IOptions <CacheOptions> >().Value;
                var type    = options.Type.ToLower();

                ICacheClient client;

                if (type == "auto")
                {
                    var redisOptions = provider.GetRequiredService <IOptions <RedisOptions> >().Value;
                    type             = !string.IsNullOrEmpty(redisOptions.ConnectionString) ? "redis" : "memory";
                }

                switch (type)
                {
                case "inmemory":
                case "memory":
                    client = new InMemoryCacheClient(new InMemoryCacheClientOptions
                    {
                        LoggerFactory = provider.GetRequiredService <ILoggerFactory>(),
                        Serializer    = provider.GetRequiredService <ISerializer>()
                    });
                    break;

                case "redis":
                    client = new RedisCacheClient(new RedisCacheClientOptions
                    {
                        ConnectionMultiplexer = provider.GetRequiredService <IConnectionMultiplexer>(),
                        LoggerFactory         = provider.GetRequiredService <ILoggerFactory>(),
                        Serializer            = provider.GetRequiredService <ISerializer>()
                    });
                    break;

                default:
                    throw new NotSupportedException($"The cache type {options.Type} is not supported.");
                }

                if (!string.IsNullOrEmpty(options.Prefix))
                {
                    client = new ScopedCacheClient(client, options.Prefix);
                }

                return(client);
            });

            return(services);
        }
Example #24
0
        public async Task Setup(string Token)
        {
            if (await cacheManger.ExistAsync("DBHost"))
            {
                mysqlHandler = new MysqlHandler(new MysqlConfig()
                {
                    DBHost     = await cacheManger.GetAsync("DBHost"),
                    DBUser     = await cacheManger.GetAsync("DBUser"),
                    DBPassword = await cacheManger.GetAsync("DBPassword"),
                    DBName     = await cacheManger.GetAsync("DBName")
                });
            }


            Log.OnLog += (string msg, LogLevel level) =>
            {
                if (level >= LogLevel.Information)
                {
                    Console.WriteLine(msg);
                }
            };

            IExtendedCacheClient cache = new InMemoryCacheClient(
                new ProtobufSerializer()
                );

            DiscordApiClient api = new DiscordApiClient(Token, cache);

            _gateway = new CentralizedGatewayShard(new GatewayConfiguration
            {
                ShardCount      = 1,
                ShardId         = 0,
                Token           = Token,
                WebSocketClient = new BasicWebSocketClient()
            });

            DiscordClient bot = new DiscordClient(new DiscordClientConfigurations
            {
                ApiClient   = api,
                Gateway     = _gateway,
                CacheClient = cache
            });

            new BasicCacheStage().Initialize(_gateway, cache);
            this.bot            = bot;
            DiscordBot.Instance = this;
        }
        public ElasticRepositoryTestBase(ITestOutputHelper output) : base(output)
        {
            Log.MinimumLevel = LogLevel.Information;
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Warning);

            _cache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
                LoggerFactory = Log
            });
            _messageBus = new InMemoryMessageBus(new InMemoryMessageBusOptions {
                LoggerFactory = Log
            });
            _workItemQueue = new InMemoryQueue <WorkItemData>(new InMemoryQueueOptions <WorkItemData> {
                LoggerFactory = Log
            });
            _configuration = new MyAppElasticConfiguration(_workItemQueue, _cache, _messageBus, Log);
            _client        = _configuration.Client;
        }
Example #26
0
        public ElasticRepositoryTestBase(ITestOutputHelper output) : base(output)
        {
            Log.MinimumLevel = LogLevel.Trace;
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Warning);

            _cache = new InMemoryCacheClient(new InMemoryCacheClientOptions {
                LoggerFactory = Log
            });
            _messageBus = new InMemoryMessageBus(new InMemoryMessageBusOptions {
                LoggerFactory = Log
            });
            _workItemQueue = new InMemoryQueue <WorkItemData>(new InMemoryQueueOptions <WorkItemData> {
                LoggerFactory = Log
            });
            _configuration = new MyAppElasticConfiguration(_workItemQueue, _cache, _messageBus, Log);
            _client        = _configuration.Client;
            _configuration.WaitForReady(new CancellationTokenSource(TimeSpan.FromMinutes(1)).Token);
        }
Example #27
0
        public async Task AcquireLockAsyncTwiceThrowsTimeoutTest()
        {
            var cacheClient = new InMemoryCacheClient(new ProtobufSerializer());
            var tokenSource = new CancellationTokenSource();

            var testLock = await cacheClient.AcquireLockAsync(
                "test:acquire:multiple", tokenSource.Token);

            tokenSource.CancelAfter(1000);
            await Assert.ThrowsAnyAsync <OperationCanceledException>(
                () => cacheClient.AcquireLockAsync(
                    "test:acquire:multiple", tokenSource.Token)
                .AsTask());

            await testLock.ReleaseAsync();

            Assert.Null(await cacheClient.GetAsync <string>("test:acquire:multiple"));
        }
Example #28
0
        public async Task CanSetMaxItems()
        {
            Log.MinimumLevel = LogLevel.Trace;

            // run in tight loop so that the code is warmed up and we can catch timing issues
            for (int x = 0; x < 5; x++)
            {
                var cache = new InMemoryCacheClient(o => o.MaxItems(10).CloneValues(true));

                using (cache) {
                    await cache.RemoveAllAsync();

                    for (int i = 0; i < cache.MaxItems; i++)
                    {
                        await cache.SetAsync("test" + i, i);
                    }

                    _logger.LogTrace(String.Join(",", cache.Keys));
                    Assert.Equal(10, cache.Count);
                    await cache.SetAsync("next", 1);

                    _logger.LogTrace(String.Join(",", cache.Keys));
                    Assert.Equal(10, cache.Count);
                    Assert.False((await cache.GetAsync <int>("test0")).HasValue);
                    Assert.Equal(1, cache.Misses);
                    await SystemClock.SleepAsync(50); // keep the last access ticks from being the same for all items

                    Assert.NotNull(await cache.GetAsync <int?>("test1"));
                    Assert.Equal(1, cache.Hits);
                    await cache.SetAsync("next2", 2);

                    _logger.LogTrace(String.Join(",", cache.Keys));
                    Assert.False((await cache.GetAsync <int>("test2")).HasValue);
                    Assert.Equal(2, cache.Misses);
                    Assert.True((await cache.GetAsync <int>("test1")).HasValue);
                    Assert.Equal(2, cache.Misses);
                }
            }
        }
Example #29
0
 public StackRepositoryTests(ITestOutputHelper output) : base(output)
 {
     _cache      = _configuration.Cache as InMemoryCacheClient;
     _repository = GetService <IStackRepository>();
 }
 public void CanSetAndGetValue() {
     var cache = new InMemoryCacheClient();
     cache.Set("test", 1);
     var value = cache.Get<int>("test");
     Assert.Equal(1, value);
 }
 public ProjectRepositoryTests(ITestOutputHelper output) : base(output)
 {
     _cache           = _configuration.Cache as InMemoryCacheClient;
     _repository      = GetService <IProjectRepository>();
     Log.MinimumLevel = LogLevel.Trace;
 }
Example #32
0
        public async void CanRunThrottledJobs() {
            var client = new InMemoryCacheClient();
            var jobs = new List<ThrottledJob>(new[] {
                new ThrottledJob(client),
                new ThrottledJob(client),
                new ThrottledJob(client)
            });

            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await Task.WhenAll(jobs.Select(
                async job => await job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: tokenSource.Token))
            );

            Assert.InRange(jobs.Sum(j => j.RunCount), 6, 14);
        }
 public StackRepositoryTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory)
 {
     _cache      = GetService <ICacheClient>() as InMemoryCacheClient;
     _repository = GetService <IStackRepository>();
 }
Example #34
0
 public async Task CanRunThrottledJobs() {
     var client = new InMemoryCacheClient();
     var jobs = new List<ThrottledJob>(new[] { new ThrottledJob(client), new ThrottledJob(client), new ThrottledJob(client) });
     
     var sw = Stopwatch.StartNew();
     await Task.WhenAll(jobs.Select(async job => await TaskExtensions.AnyContext(job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: TimeSpan.FromSeconds(1).ToCancellationToken()))));
     sw.Stop();
     Assert.InRange(jobs.Sum(j => j.RunCount), 6, 14);
     Assert.InRange(sw.ElapsedMilliseconds, 20, 1500);
 }