コード例 #1
0
        public async Task ToObject_CollectionBytes_VerifyFailure(bool isFunctionDataCacheEnabled)
        {
            ILogger <RpcSharedMemory> logger = NullLogger <RpcSharedMemory> .Instance;
            string invocationId = Guid.NewGuid().ToString();
            long   contentSize  = 16 * 1024; // 16KB, enough to try out the functionality we need to test

            using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor))
            {
                // Create a SharedMemoryMap
                string          mapName         = Guid.NewGuid().ToString();
                SharedMemoryMap sharedMemoryMap = CreateSharedMemoryMap(mapName, contentSize);

                // Wite content into it
                byte[] content      = TestUtils.GetRandomBytesInArray((int)contentSize);
                long   bytesWritten = await sharedMemoryMap.PutBytesAsync(content);

                Assert.Equal(contentSize, bytesWritten);

                // Create a RpcSharedMemory object pointing to the shared memory region we created earlier
                // Although the type is not correct, instead of Bytes it is CollectionBytes (unsupported)
                RpcSharedMemory rpcSharedMemory = new RpcSharedMemory
                {
                    Name   = mapName,
                    Count  = contentSize,
                    Offset = 0,
                    Type   = RpcDataType.CollectionBytes
                };

                // Try to convert the object but this should fail since the type we are requested is unsupported
                await Assert.ThrowsAsync <InvalidDataException>(async() => await rpcSharedMemory.ToObjectAsync(logger, invocationId, manager, isFunctionDataCacheEnabled));

                // Dispose off the created resources
                sharedMemoryMap.Dispose();
            }
        }
コード例 #2
0
        public async Task GetStream_VerifyContentMatches(int contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            byte[] content = TestUtils.GetRandomBytesInArray(contentSize);

            long bytesWritten = await sharedMemoryMap.PutBytesAsync(content);

            Assert.Equal(contentSize, bytesWritten);

            Stream readContent = await sharedMemoryMap.GetStreamAsync();
コード例 #3
0
        public async Task GetContentLength_VerifyContentLengthMatches(int contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            byte[] content = TestUtils.GetRandomBytesInArray(contentSize);

            await sharedMemoryMap.PutBytesAsync(content);

            Assert.Equal(contentSize, await sharedMemoryMap.GetContentLengthAsync());

            sharedMemoryMap.Dispose();
        }
コード例 #4
0
        public async Task PutBytes_VerifySuccess(int contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            byte[] content = TestUtils.GetRandomBytesInArray(contentSize);

            long bytesWritten = await sharedMemoryMap.PutBytesAsync(content);

            Assert.Equal(contentSize, bytesWritten);

            sharedMemoryMap.Dispose();
        }
コード例 #5
0
        public async Task GetBytes_VerifyContentMatches(int contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            byte[] content = TestUtils.GetRandomBytesInArray(contentSize);

            await sharedMemoryMap.PutBytesAsync(content);

            byte[] readContent = await sharedMemoryMap.GetBytesAsync();

            Assert.True(TestUtils.UnsafeCompare(content, readContent));

            sharedMemoryMap.Dispose();
        }
コード例 #6
0
        public async Task ToObject_Bytes_FunctionDataCacheEnabled_VerifySuccess()
        {
            ILogger <RpcSharedMemory> logger = NullLogger <RpcSharedMemory> .Instance;
            string invocationId = Guid.NewGuid().ToString();
            long   contentSize  = 16 * 1024; // 16KB, enough to try out the functionality we need to test

            using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor))
            {
                // Create a SharedMemoryMap
                string          mapName         = Guid.NewGuid().ToString();
                SharedMemoryMap sharedMemoryMap = CreateSharedMemoryMap(mapName, contentSize);

                // Wite content into it
                byte[] content      = TestUtils.GetRandomBytesInArray((int)contentSize);
                long   bytesWritten = await sharedMemoryMap.PutBytesAsync(content);

                Assert.Equal(contentSize, bytesWritten);

                // Create a RpcSharedMemory object pointing to the shared memory region we created earlier
                RpcSharedMemory rpcSharedMemory = new RpcSharedMemory
                {
                    Name   = mapName,
                    Count  = contentSize,
                    Offset = 0,
                    Type   = RpcDataType.Bytes
                };

                // Convert RpcSharedMemory object into byte[]
                object rpcObj = await rpcSharedMemory.ToObjectAsync(logger, invocationId, manager, isFunctionDataCacheEnabled : true);

                Assert.NotNull(rpcObj);

                // Since the FunctionDataCache is enabled, the object should be a SharedMemoryObject
                Assert.IsType <SharedMemoryObject>(rpcObj);
                SharedMemoryObject sharedMemoryObject = rpcObj as SharedMemoryObject;

                // Verify that the read object is correct
                Assert.Equal(mapName, sharedMemoryObject.MemoryMapName);
                Assert.Equal(contentSize, sharedMemoryObject.Count);

                // Since the FunctionDataCache is enabled, ensure that the SharedMemoryManager is tracking the object that was read
                Assert.Equal(1, manager.AllocatedSharedMemoryMaps.Count);
                Assert.True(manager.AllocatedSharedMemoryMaps.TryGetValue(mapName, out _));

                // Dispose off the created resources
                sharedMemoryMap.Dispose();
            }
        }