Пример #1
0
        public async Task Test_ReadOnlyMemory_With_RawBinaryTranscoder()
        {
            var collection = await _fixture.GetDefaultCollectionAsync().ConfigureAwait(false);

            var key = Guid.NewGuid().ToString();

            try
            {
                var data       = Enumerable.Range(1, 128).Select(p => (byte)p).ToArray();
                var transcoder = new RawBinaryTranscoder();

                await collection.InsertAsync <ReadOnlyMemory <byte> >(key, data.AsMemory(), options => options.Expiry(TimeSpan.FromSeconds(30)).Transcoder(transcoder))
                .ConfigureAwait(false);

                var result = await collection.GetAsync(key, options => options.Expiry().Transcoder(transcoder))
                             .ConfigureAwait(false);

                using var content = result.ContentAs <IMemoryOwner <byte> >() !;
                Assert.Equal(data, content.Memory.ToArray());
            }
            finally
            {
                await collection.RemoveAsync(key).ConfigureAwait(false);
            }
        }
Пример #2
0
        public async Task Test_ExpiryTime_With_RawBinaryTranscoder()
        {
            var collection = await _fixture.GetDefaultCollectionAsync().ConfigureAwait(false);

            var key = Guid.NewGuid().ToString();

            try
            {
                var data       = Enumerable.Range(1, 128).Select(p => (byte)p).ToArray();
                var transcoder = new RawBinaryTranscoder();

                await collection.InsertAsync(key, data, options => options.Expiry(TimeSpan.FromSeconds(30)).Transcoder(transcoder))
                .ConfigureAwait(false);

                var result = await collection.GetAsync(key, options => options.Expiry().Transcoder(transcoder))
                             .ConfigureAwait(false);

                var content = result.ExpiryTime;
                Assert.NotNull(content);
                Assert.Equal(data, result.ContentAs <byte[]>());
            }
            finally
            {
                await collection.RemoveAsync(key).ConfigureAwait(false);
            }
        }
Пример #3
0
        public void Test_GetFormat_Binary_Succeeds()
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = transcoder.GetFormat(new byte[] { 0x0 });

            Assert.Equal(DataFormat.Binary, flags.DataFormat);
        }
Пример #4
0
        public async Task TestScaleDown()
        {
            await using var fixture = new ClusterFixture(options =>
            {
                options.NumKvConnections = 1;
                options.MaxKvConnections = 2;
                options.Experiments.ChannelConnectionPools = false;
            });
            await fixture.InitializeAsync();

            var bucket = await fixture.GetDefaultBucket();

            var nodes = ((CouchbaseBucket)bucket).Nodes.OfType <ClusterNode>().ToArray();

            Assert.Single(nodes);

            var connectionPools = nodes.Select(p => p.ConnectionPool).ToArray();

            // Scale up to two connections
            await Task.WhenAll(connectionPools.Select(p => p.ScaleAsync(1)));

            Assert.All(connectionPools, p => Assert.Equal(2, p.Size));

            // We'll write 10KB for each operation so that we have some real load on the send side
            var transcoder = new RawBinaryTranscoder();
            var fakeBytes  = new byte[10 * 1024];

            var collection = bucket.DefaultCollection();

            using var limitedParallelization = new SemaphoreSlim(100);
            // Run a bunch of get operations, but make sure we don't start too many at once.
            // We don't want timeouts just because we flood the connections
            var operations = Enumerable.Range(0, 20000 * connectionPools.Length).Select(async _ =>
            {
                await limitedParallelization.WaitAsync();
                try
                {
                    var key = Guid.NewGuid().ToString();
                    await collection.UpsertAsync(key, fakeBytes, new KeyValue.UpsertOptions().Transcoder(transcoder));
                    await collection.RemoveAsync(key);
                }
                finally
                {
                    limitedParallelization.Release();
                }
            }).ToList();

            // Give some time for the tasks to get cranking
            await Task.Delay(500);

            // Scale back down to one connection
            await Task.WhenAll(connectionPools.Select(p => p.ScaleAsync(-1)));

            Assert.All(connectionPools, p => Assert.Equal(1, p.Size));

            // Wait for the operations to all complete. Some of them should fail with timeouts if they get dropped during scale down.
            await Task.WhenAll(operations);
        }
Пример #5
0
        public void Test_Encode_Object_Fails()
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = new Flags
            {
                DataFormat = DataFormat.Binary
            };

            using var stream = new MemoryStream();
            Assert.Throws <InvalidOperationException>(() => transcoder.Encode(stream, new object(), flags, OpCode.Add));
        }
Пример #6
0
        public void Test_Decode_Object_Fails()
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = new Flags
            {
                DataFormat = DataFormat.String
            };

            var memory = new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes("Hello, world!"));

            Assert.Throws <InvalidOperationException>(() => transcoder.Decode <object>(memory, flags, OpCode.NoOp));
        }
Пример #7
0
        public void Test_Decode_ByteArrays(DataFormat dataFormat)
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = new Flags
            {
                DataFormat = dataFormat //Note flags type is independent of T - everything is byte[]
            };

            var memory = new ReadOnlyMemory <byte>(new byte[] { 0x0, 0x1 });
            var bytes  = transcoder.Decode <byte[]>(memory, flags, OpCode.NoOp);

            Assert.True(bytes.Length == 2);
        }
Пример #8
0
        public void Test_Encode_ByteArrays()
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = new Flags
            {
                DataFormat = DataFormat.Binary
            };

            using var stream = new MemoryStream();
            transcoder.Encode(stream, new byte[] { 0x00, 0x01 }, flags, OpCode.Add);

            Assert.True(stream.Length == 2);
        }
Пример #9
0
        public void Test_Decode_ByteArrays()
        {
            var transcoder = new RawBinaryTranscoder();

            var flags = new Flags
            {
                DataFormat = DataFormat.Binary
            };

            var memory = new ReadOnlyMemory <byte>(new byte[] { 0x0, 0x1 });
            var bytes  = transcoder.Decode <byte[]>(memory, flags, OpCode.NoOp);

            Assert.True(bytes.Length == 2);
        }
Пример #10
0
        public void Test_GetFormat_Object_Fails()
        {
            var transcoder = new RawBinaryTranscoder();

            Assert.Throws <InvalidOperationException>(() => transcoder.GetFormat(new object()));
        }