private void TestShardKeyGeneric <TKey>(ShardKeyType keyType, TKey inputValue, Type realType)
        {
            // Excercise DetectType
            //
            ShardKey k1 = new ShardKey(inputValue);

            Assert.AreEqual(realType, k1.DataType);

            // Go to/from raw value
            ShardKey k2 = new ShardKey(keyType, inputValue);

            byte[] k2raw = k2.RawValue;

            ShardKey k3 = ShardKey.FromRawValue(keyType, k2raw);

            Assert.AreEqual(inputValue, k2.Value);
            Assert.AreEqual(inputValue, k3.Value);
            Assert.AreEqual(k2, k3);

            // verify comparisons
            Assert.AreEqual(0, k2.CompareTo(k3));

            try
            {
                k3 = k2.GetNextKey();
                Assert.IsTrue(k3 > k2);
            }
            catch (InvalidOperationException)
            {
            }
        }
示例#2
0
        private void Verify(ShardKeyType kind)
        {
            byte[]   bytes  = null;
            ShardKey key    = null;
            ShardKey result = null;

            switch (kind)
            {
            case ShardKeyType.Int32:
                bytes = new byte[sizeof(int)];
                rValGen.NextBytes(bytes);
                Int32 int32 = BitConverter.ToInt32(bytes, 0);
                key    = new ShardKey(int32);
                result = key.GetNextKey();
                Debug.Assert(result.IsMax || result == new ShardKey(int32 + 1));
                break;

            case ShardKeyType.Int64:
                bytes = new byte[sizeof(long)];
                rValGen.NextBytes(bytes);
                Int64 int64 = BitConverter.ToInt64(bytes, 0);
                key    = new ShardKey(int64);
                result = key.GetNextKey();
                Debug.Assert(result.IsMax || result == new ShardKey(int64 + 1));
                break;

            case ShardKeyType.Guid:
                Guid guid = Guid.NewGuid();
                key    = new ShardKey(guid);
                result = key.GetNextKey();
                // verify only the API call
                break;

            case ShardKeyType.Binary:
                bytes = new byte[128];
                rValGen.NextBytes(bytes);
                key    = new ShardKey(bytes);
                result = key.GetNextKey();
                // verify only the API call
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          "kind",
                          kind,
                          Errors._ShardKey_UnsupportedShardKeyType);
            }
        }
示例#3
0
        public void ShardKeyTests()
        {
            ShardKey key    = null;
            ShardKey result = null;

            byte[] array    = null;
            byte[] arraymax = null;

            // Verify boundary conditions
            result = maxNonNullKey32.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxInt32);

            result = maxNonNullKey64.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxInt64);

            array  = Enumerable.Repeat((byte)0xff, 16).ToArray();
            key    = ShardKey.FromRawValue(ShardKeyType.Guid, array); // can not use other ctor because normalized representation differ
            result = key.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxGuid);

            array  = Enumerable.Repeat((byte)0xff, 128).ToArray();
            key    = new ShardKey(array);
            result = key.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxBinary);

            key    = new ShardKey(max32 - 1);
            result = key.GetNextKey();
            Debug.Assert(result == maxNonNullKey32);

            key    = new ShardKey(max64 - 1);
            result = key.GetNextKey();
            Debug.Assert(result == maxNonNullKey64);

            arraymax  = Enumerable.Repeat((byte)0xff, 16).ToArray();
            array     = Enumerable.Repeat((byte)0xff, 16).ToArray();
            array[15] = 0xfe;
            key       = ShardKey.FromRawValue(ShardKeyType.Guid, array); // can not use other ctor because normalized representation differ
            result    = key.GetNextKey();
            Debug.Assert(result == ShardKey.FromRawValue(ShardKeyType.Guid, arraymax));

            arraymax   = Enumerable.Repeat((byte)0xff, 128).ToArray();
            array      = Enumerable.Repeat((byte)0xff, 128).ToArray();
            array[127] = 0xfe;
            key        = new ShardKey(array);
            result     = key.GetNextKey();
            Debug.Assert(result == ShardKey.FromRawValue(ShardKeyType.Binary, arraymax));

            key = new ShardKey(ShardKeyType.Int32, null);
            AssertExtensions.AssertThrows <InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Int64, null);
            AssertExtensions.AssertThrows <InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Guid, null);
            AssertExtensions.AssertThrows <InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Binary, null);
            AssertExtensions.AssertThrows <InvalidOperationException>(() => key.GetNextKey());

            result = ShardKey.MinInt32.GetNextKey();
            Debug.Assert(result == new ShardKey(Int32.MinValue + 1));

            result = ShardKey.MinInt64.GetNextKey();
            Debug.Assert(result == new ShardKey(Int64.MinValue + 1));

            result    = ShardKey.MinGuid.GetNextKey();
            array     = new byte[16];
            array[15] = 0x01;
            key       = ShardKey.FromRawValue(ShardKeyType.Guid, array);
            Debug.Assert(result == key);

            result     = ShardKey.MinBinary.GetNextKey();
            array      = new byte[128];
            array[127] = 0x01;
            key        = ShardKey.FromRawValue(ShardKeyType.Binary, array);
            Debug.Assert(result == key);

            for (int i = 0; i < 10; i++)
            {
                Verify(ShardKeyType.Int32);
                Verify(ShardKeyType.Int64);
                Verify(ShardKeyType.Guid);
                Verify(ShardKeyType.Binary);
            }
        }
        public void ShardKeyTests()
        {
            ShardKey key = null;
            ShardKey result = null;
            byte[] array = null;
            byte[] arraymax = null;

            // Verify boundary conditions
            result = maxNonNullKey32.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxInt32);

            result = maxNonNullKey64.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxInt64);

            array = Enumerable.Repeat((byte)0xff, 16).ToArray();
            key = ShardKey.FromRawValue(ShardKeyType.Guid, array); // can not use other ctor because normalized representation differ
            result = key.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxGuid);

            array = Enumerable.Repeat((byte)0xff, 128).ToArray();
            key = new ShardKey(array);
            result = key.GetNextKey();
            Debug.Assert(result.IsMax);
            Debug.Assert(result == ShardKey.MaxBinary);

            key = new ShardKey(max32 - 1);
            result = key.GetNextKey();
            Debug.Assert(result == maxNonNullKey32);

            key = new ShardKey(max64 - 1);
            result = key.GetNextKey();
            Debug.Assert(result == maxNonNullKey64);

            arraymax = Enumerable.Repeat((byte)0xff, 16).ToArray();
            array = Enumerable.Repeat((byte)0xff, 16).ToArray();
            array[15] = 0xfe;
            key = ShardKey.FromRawValue(ShardKeyType.Guid, array); // can not use other ctor because normalized representation differ
            result = key.GetNextKey();
            Debug.Assert(result == ShardKey.FromRawValue(ShardKeyType.Guid, arraymax));

            arraymax = Enumerable.Repeat((byte)0xff, 128).ToArray();
            array = Enumerable.Repeat((byte)0xff, 128).ToArray();
            array[127] = 0xfe;
            key = new ShardKey(array);
            result = key.GetNextKey();
            Debug.Assert(result == ShardKey.FromRawValue(ShardKeyType.Binary, arraymax));

            key = new ShardKey(ShardKeyType.Int32, null);
            AssertExtensions.AssertThrows<InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Int64, null);
            AssertExtensions.AssertThrows<InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Guid, null);
            AssertExtensions.AssertThrows<InvalidOperationException>(() => key.GetNextKey());

            key = new ShardKey(ShardKeyType.Binary, null);
            AssertExtensions.AssertThrows<InvalidOperationException>(() => key.GetNextKey());

            result = ShardKey.MinInt32.GetNextKey();
            Debug.Assert(result == new ShardKey(Int32.MinValue + 1));

            result = ShardKey.MinInt64.GetNextKey();
            Debug.Assert(result == new ShardKey(Int64.MinValue + 1));

            result = ShardKey.MinGuid.GetNextKey();
            array = new byte[16];
            array[15] = 0x01;
            key = ShardKey.FromRawValue(ShardKeyType.Guid, array);
            Debug.Assert(result == key);

            result = ShardKey.MinBinary.GetNextKey();
            array = new byte[128];
            array[127] = 0x01;
            key = ShardKey.FromRawValue(ShardKeyType.Binary, array);
            Debug.Assert(result == key);

            for (int i = 0; i < 10; i++)
            {
                Verify(ShardKeyType.Int32);
                Verify(ShardKeyType.Int64);
                Verify(ShardKeyType.Guid);
                Verify(ShardKeyType.Binary);
            }
        }
        private void Verify(ShardKeyType kind)
        {
            byte[] bytes = null;
            ShardKey key = null;
            ShardKey result = null;

            switch (kind)
            {
                case ShardKeyType.Int32:
                    bytes = new byte[sizeof(int)];
                    rValGen.NextBytes(bytes);
                    Int32 int32 = BitConverter.ToInt32(bytes, 0);
                    key = new ShardKey(int32);
                    result = key.GetNextKey();
                    Debug.Assert(result.IsMax || result == new ShardKey(int32 + 1));
                    break;

                case ShardKeyType.Int64:
                    bytes = new byte[sizeof(long)];
                    rValGen.NextBytes(bytes);
                    Int64 int64 = BitConverter.ToInt64(bytes, 0);
                    key = new ShardKey(int64);
                    result = key.GetNextKey();
                    Debug.Assert(result.IsMax || result == new ShardKey(int64 + 1));
                    break;

                case ShardKeyType.Guid:
                    Guid guid = Guid.NewGuid();
                    key = new ShardKey(guid);
                    result = key.GetNextKey();
                    // verify only the API call
                    break;

                case ShardKeyType.Binary:
                    bytes = new byte[128];
                    rValGen.NextBytes(bytes);
                    key = new ShardKey(bytes);
                    result = key.GetNextKey();
                    // verify only the API call
                    break;

                default:
                    throw new ArgumentOutOfRangeException(
                        "kind",
                        kind,
                        Errors._ShardKey_UnsupportedShardKeyType);
            }
        }