public void Allows_DefaultBucketFor2PerMinuteCallOnce_DoesNotLimit()
        {
            var  store  = new RedisLimitStore(redis);
            bool result = store.Allows(appKey, bucket, Guid.NewGuid().ToString());

            Assert.IsTrue(result);
        }
 public void TestRedisFreeApp()
 {
     int[] xxx   = { 1, 2 };
     var   store = new RedisLimitStore(redis);
     // TODO
     //Assert.IsTrue(store.IsActiveAppKey("free"), "AppKey not found");
     // Assert.IsTrue(store.IsActiveBucket("free","default"), "bucket not found");
 }
Пример #3
0
        public void Allows_For2PerMinuteCallOnce_DoesNotLimit()
        {
            var db = redis.Database;

            var  sut    = new RedisLimitStore(redis);
            bool result = sut.Allows(appKey, bucket, Guid.NewGuid().ToString());

            Assert.IsTrue(result);
        }
        public void test1()
        {
            var sut    = new RedisLimitStore(redis);
            var result = sut.LoadBuckets("free");

            foreach (Bucket b in result)
            {
                Console.WriteLine(b.Name);
            }
        }
Пример #5
0
        public void LoadBucket_ForMadeUpAppKey_ThrowsException()
        {
            string    testAppKey = Guid.NewGuid().ToString();
            IDatabase db         = redis.Database;
            var       sut        = new RedisLimitStore(redis);

            //act
            sut.LoadBucket(testAppKey);

            //assert - throws
        }
Пример #6
0
        public void Setup_ForNewAppKey_DefaultBucketExists()
        {
            string    testAppKey = Guid.NewGuid().ToString();
            IDatabase db         = redis.Database;
            var       sut        = new RedisLimitStore(redis);

            //act
            sut.Setup(new Bucket(testAppKey));

            //assert (throws if not exist)
            sut.LoadBucket(testAppKey);
        }
Пример #7
0
        public void Allows_DefaultBucketFor2PerMinuteCallThrice_Limits()
        {
            var    sut       = new RedisLimitStore(redis);
            string testOpKey = Guid.NewGuid().ToString();

            sut.Allows(appKey, bucket, testOpKey);
            sut.Allows(appKey, bucket, testOpKey);

            bool result = sut.Allows(appKey, bucket, testOpKey);

            Assert.IsFalse(result);
        }
        public void Allows_DefaultBucketFor2PerSecondCallFiveTimesWithGapAfter2_AllowsFirstFour()
        {
            var    store     = new RedisLimitStore(redis);
            string testOpKey = Guid.NewGuid().ToString();

            Assert.IsTrue(store.Allows(appKey, quickBucket, testOpKey), "First call");
            Assert.IsTrue(store.Allows(appKey, quickBucket, testOpKey), "Second call");
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(store.Allows(appKey, quickBucket, testOpKey), "Third call");
            Assert.IsTrue(store.Allows(appKey, quickBucket, testOpKey), "Fourth call");
            Assert.IsFalse(store.Allows(appKey, quickBucket, testOpKey));
        }
Пример #9
0
        public void LoadBucket_ForMadeUpAppKeyAndBucket_ReturnsFalse()
        {
            string    testAppKey     = Guid.NewGuid().ToString();
            string    testBucketName = "testBucketName1";
            IDatabase db             = redis.Database;
            var       sut            = new RedisLimitStore(redis);

            //act
            sut.LoadBucket(testAppKey, testBucketName);

            //assert (throws)
        }
Пример #10
0
        public void AllowsPerformanceTest()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Ping {0}", redis.Database.Ping().TotalMilliseconds);

                var  store  = new RedisLimitStore(redis);
                var  sw     = Stopwatch.StartNew();
                bool result = store.Allows(appKey, bucket, Guid.NewGuid().ToString());
                sw.Stop();
                Console.WriteLine("Allows: {0}", sw.Elapsed.TotalMilliseconds);
            }
        }
Пример #11
0
        public void Setup_ForExistingAppKeyAndBucket_BucketExists()
        {
            string    testAppKey     = Guid.NewGuid().ToString();
            string    testBucketName = "testBucketName1";
            IDatabase db             = redis.Database;
            var       sut            = new RedisLimitStore(redis);

            sut.Setup(new Bucket(testAppKey, testBucketName));

            //act
            sut.Setup(new Bucket(testAppKey, testBucketName));

            //assert (throws if not exist)
            sut.LoadBucket(testAppKey, testBucketName);
        }
Пример #12
0
        public static void InitializeClass(TestContext context)
        {
            var redisOptions = new ConfigurationOptions()
            {
                EndPoints = { { "limtr.redis.cache.windows.net", 6379 } }, Password = "******"
            };

            redis = new Redis(ConnectionMultiplexer.Connect(redisOptions));

            var store = new RedisLimitStore(redis);

            store.Setup(new Bucket(appKey, bucket, 2, TimeSpan.FromMinutes(1)));
            store.Setup(new Bucket(appKey, quickBucket, 2, TimeSpan.FromSeconds(1)));
            //store.Setup(new Bucket("free", null, 10, TimeSpan.FromSeconds(10), 7, TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(200)));
        }
Пример #13
0
        public void Allows_DefaultBucketFor2PerSecondCallFiveTimesWithGap_TrimsListTo2()
        {
            IDatabase db        = redis.Database;
            var       store     = new RedisLimitStore(redis);
            string    testOpKey = Guid.NewGuid().ToString();

            store.Allows(appKey, quickBucket, testOpKey);
            store.Allows(appKey, quickBucket, testOpKey);
            System.Threading.Thread.Sleep(1000);
            store.Allows(appKey, quickBucket, testOpKey);
            store.Allows(appKey, quickBucket, testOpKey);
            store.Allows(appKey, quickBucket, testOpKey);

            RedisValue[] items = db.ListRange(RedisLimitStore.MakeHitKey(appKey, quickBucket, testOpKey));
            Assert.AreEqual(2, items.Length);
        }
Пример #14
0
        public void Allows_ForSetupThrottledBucketWithLimitAndThrottle_Limits()
        {
            IDatabase db = redis.Database;
            var       sut = new RedisLimitStore(redis);
            string    appKey = Guid.NewGuid().ToString(), bucket = "test", operation = "op1";

            sut.Setup(new Bucket(appKey, bucket, 3, TimeSpan.FromSeconds(60), 2, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(1)));
            sut.Allows(appKey, bucket, operation);
            sut.Allows(appKey, bucket, operation);
            sut.Allows(appKey, bucket, operation);

            //act
            bool result = sut.Allows(appKey, bucket, operation);

            //assert
            Assert.IsFalse(result);
        }
Пример #15
0
        public void Allows_DefaultBucketFor2PerSecondCallFiveTimesWithGap_TrimsListTo2()
        {
            //This test sometimes fails--I believe because the async op to trim the list can take a few seconds.

            IDatabase db        = redis.Database;
            var       sut       = new RedisLimitStore(redis);
            string    testOpKey = Guid.NewGuid().ToString();

            sut.Allows(appKey, quickBucket, testOpKey);
            sut.Allows(appKey, quickBucket, testOpKey);
            System.Threading.Thread.Sleep(3000);
            sut.Allows(appKey, quickBucket, testOpKey);
            sut.Allows(appKey, quickBucket, testOpKey);
            sut.Allows(appKey, quickBucket, testOpKey);

            RedisValue[] items = db.ListRange(RedisLimitStore.MakeHitKey(appKey, quickBucket, testOpKey));
            Assert.AreEqual(2, items.Length);
        }
Пример #16
0
        public static void InitializeClass(TestContext context)
        {
            var redisOptions = new ConfigurationOptions()
            {
                EndPoints = { { "localhost", 6379 } }, AbortOnConnectFail = false, AllowAdmin = true
            };

            redis = new Redis(ConnectionMultiplexer.Connect(redisOptions));
            var sut = new RedisLimitStore(redis);

            foreach (IServer server in redis.Servers)
            {
                server.FlushAllDatabases();
            }

            sut.Setup(new Bucket(appKey, bucket, 2, TimeSpan.FromMinutes(1)));
            sut.Setup(new Bucket(appKey, quickBucket, 2, TimeSpan.FromSeconds(1)));
        }
Пример #17
0
        public void Allows_ForSetupThrottledBucketWithLimitAndWithoutThrottle_LimitsWithoutThrottle()
        {
            IDatabase db = redis.Database;
            var       sut = new RedisLimitStore(redis);
            string    appKey = Guid.NewGuid().ToString(), bucket = "test", operation = "op1";

            sut.Setup(new Bucket(appKey, bucket, 2, TimeSpan.FromSeconds(60)));
            sut.Allows(appKey, bucket, operation);
            sut.Allows(appKey, bucket, operation);

            //act
            var  sw     = Stopwatch.StartNew();
            bool result = sut.Allows(appKey, bucket, operation);

            sw.Stop();

            //assert
            Assert.IsTrue(sw.ElapsedMilliseconds < 1000);
            Assert.IsFalse(result);
        }
Пример #18
0
        public void Allows_ForSetupThrottledBucketWithLimitAndThrottle_Throttles()
        {
            IDatabase db = redis.Database;
            var       sut = new RedisLimitStore(redis);
            string    appKey = Guid.NewGuid().ToString(), bucket = "test", operation = "op1";

            sut.Setup(new Bucket(appKey, bucket, 3, TimeSpan.FromSeconds(60), 2, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(1)));
            sut.Allows(appKey, bucket, operation);
            sut.Allows(appKey, bucket, operation);

            //act
            var  sw     = Stopwatch.StartNew();
            bool result = sut.Allows(appKey, bucket, operation);

            sw.Stop();

            //assert
            Console.WriteLine(sw.Elapsed);
            Assert.IsTrue(sw.Elapsed > TimeSpan.FromSeconds(1), "elapsed was too quick; throttle seems to be ineffective");
            Assert.IsTrue(result);
        }