public void ShouldPublishAndGetOver1000Shardlets()
        {
            // Windows Azure Tables returns up to a maximum of 1000 entities in a single request and returns a continuation token
            // when more results are available.  Make sure our implementation returns more than 1000 rows.

            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardSetConfig = GetShardSetConfig();

            InsertTestShardlets(driver, 1200);

            // act
            var keys =
                driver
                .GetShardlets(shardSetConfig.ShardSetName)
                .Select(s => s.DistributionKey)
                .OrderBy(k => k)
                .ToArray();

            //assert
            Assert.AreEqual(1200, keys.Count());
            Assert.AreEqual(-9223304498103175194, keys.First());
            Assert.AreEqual(9218225541702210765, keys.Last());
        }
        public void ShouldPublishAndTerminate100ShardletConnectionsBySpid()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardingKey = 1.ToString(CultureInfo.InvariantCulture);
            var shardlet    = GetShardlet(shardingKey);

            // act

            // publish connections - avoid connections under 500 for testing
            // don't want to KILL core processes in SQL Server
            const int   numberOfConnections = 210;
            const short startSpid           = 500;
            const short endSpid             = 500 + numberOfConnections - 1;

            for (var i = startSpid; i <= endSpid; i++)
            {
                driver.PublishShardletConnection(shardlet, i);
            }

            var spids = driver.GetConnectedSpids(shardlet).OrderBy(spid => spid).ToArray();

            Assert.AreEqual(numberOfConnections, spids.Count());
            Assert.AreEqual(startSpid, spids.First());
            Assert.AreEqual(endSpid, spids.Last());

            driver.TerminateConnections(shardlet);

            spids = driver.GetConnectedSpids(shardlet).ToArray();

            Assert.AreEqual(0, spids.Count());
        }
        public void ShouldGetIndividualShardlet()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardSetConfig = GetShardSetConfig();

            InsertTestShardlets(driver, 10);

            // act (should be in cache)
            const string shardingKey     = "1";
            var          distributionKey = GetDistributionKey(shardingKey);
            var          shardlet        = driver.GetShardletByShardingKey(shardSetConfig.ShardSetName, shardingKey, distributionKey);

            //assert
            Assert.IsNotNull(shardlet);
            Assert.AreEqual(shardingKey, shardlet.ShardingKey);
            Assert.AreEqual(distributionKey, shardlet.DistributionKey);

            // act (no caching)
            AzureCache.Clear();
            shardlet = driver.GetShardletByShardingKey(shardSetConfig.ShardSetName, shardingKey, distributionKey);

            //assert
            Assert.IsNotNull(shardlet);
            Assert.AreEqual(shardingKey, shardlet.ShardingKey);
            Assert.AreEqual(distributionKey, shardlet.DistributionKey);
        }
        public void ShouldPublishAndRemoveShardletConnection()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardingKey = 1.ToString(CultureInfo.InvariantCulture);
            var shardlet    = GetShardlet(shardingKey);

            // act
            driver.PublishShardletConnection(shardlet, 1);
            driver.PublishShardletConnection(shardlet, 2);

            var spids = driver.GetConnectedSpids(shardlet).ToArray();

            Assert.AreEqual(2, spids.Count());
            Assert.AreEqual(1, spids.First());
            Assert.AreEqual(2, spids.Last());

            driver.RemoveShardletConnection(shardlet, 1);

            spids = driver.GetConnectedSpids(shardlet).ToArray();

            Assert.AreEqual(1, spids.Count());
            Assert.AreEqual(2, spids.First());
        }
        public void ShouldPublishAndRemoveRangeShardDistributionKeys()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardSetConfig = GetShardSetConfig();

            const int numberOfRows = 20;

            InsertTestRangeShards(driver, shardSetConfig.ShardSetName, numberOfRows);

            // note - we are not putting in a valid shardmap here ... just testing the IO

            // act
            var rangeShard = driver.GetAzureRangeShard(shardSetConfig.ShardSetName, 0);

            //assert
            Assert.IsNotNull(rangeShard);

            // remove
            RemoveTestRangeShards(driver, shardSetConfig.ShardSetName, numberOfRows);

            //assert
            rangeShard = driver.GetAzureRangeShard(shardSetConfig.ShardSetName, 0);
            Assert.IsNull(rangeShard);
        }
        public void ShouldPublishAndRemoveOver100ShardletConnectionsByUtcDateTime()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardingKey = 1.ToString(CultureInfo.InvariantCulture);
            var shardlet    = GetShardlet(shardingKey);

            // act
            const int   numberOfConnections = 210;
            const short startSpid           = 500;
            const short endSpid             = 500 + numberOfConnections - 1;

            for (var i = startSpid; i <= endSpid; i++)
            {
                driver.PublishShardletConnection(shardlet, i);
            }

            var spids = driver.GetConnectedSpids(shardlet).OrderBy(spid => spid).ToArray();

            Assert.AreEqual(numberOfConnections, spids.Count());
            Assert.AreEqual(startSpid, spids.First());
            Assert.AreEqual(endSpid, spids.Last());

            Thread.Sleep(10000);

            driver.RemoveConnections(shardlet.ShardSetName, DateTime.UtcNow);

            spids = driver.GetConnectedSpids(shardlet).ToArray();

            Assert.AreEqual(0, spids.Count());
        }
 private static void RemoveTestShardlets(AzureShardSetConnectionDriver driver, int numberToInsert)
 {
     for (var i = 1; i <= numberToInsert; i++)
     {
         var shardlet = GetShardlet(i.ToString(CultureInfo.InvariantCulture));
         driver.RemoveShardlet(shardlet);
     }
 }
 private static void RemoveTestRangeShards(AzureShardSetConnectionDriver driver, string shardSetName,
                                           int numberToInsert)
 {
     for (var i = 1; i <= numberToInsert; i++)
     {
         var rangeShard = GetRangeShard(i);
         driver.RemoveShard(shardSetName, rangeShard);
     }
 }
        public void ShouldPublishAndRemoveShardlets()
        {
            //assemble
            var driver = new AzureShardSetConnectionDriver();

            var shardSetConfig = GetShardSetConfig();

            const int numberOfRows = 20;

            InsertTestShardlets(driver, numberOfRows);

            // act
            var keys =
                driver
                .GetShardlets(shardSetConfig.ShardSetName)
                .Select(s => s.DistributionKey)
                .OrderBy(k => k)
                .ToArray();

            //assert
            Assert.AreEqual(numberOfRows, keys.Count());
            Assert.AreEqual(-9142586270102516767, keys.First());
            Assert.AreEqual(8943927636039079085, keys.Last());

            // remove
            RemoveTestShardlets(driver, numberOfRows);

            keys =
                driver
                .GetShardlets(shardSetConfig.ShardSetName)
                .Select(s => s.DistributionKey)
                .OrderBy(k => k)
                .ToArray();

            //assert
            Assert.AreEqual(0, keys.Count());
        }
 public void TestInitialize()
 {
     AzureShardSetConnectionDriver.InitializeAzureTables(_testShardSetName, true);
     AzureShardSetConnectionDriver.InitializeAzureTables();
     AzureCache.Clear();
 }