Example #1
0
        public async Task GetStream_LargeData_VerifyContentMatches(long contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            // Since the size of memory being requested can be greater than 2GB, a 32 bit
            // integer type can't hold that. So we use an IntPtr to store the size, which
            // on a 64 bit platform would store the size in a 64 bit integer. Then we can pass
            // a pointer to that integer to the memory allocation method.
            IntPtr sizePtr = new IntPtr(contentSize);

            // Allocate a block of unmanaged memory
            IntPtr memIntPtr = Marshal.AllocHGlobal(sizePtr);

            // Generate content to put into the shared memory map
            Stream content = TestUtils.GetRandomContentInStream(contentSize, memIntPtr);

            // Put content into the shared memory map
            await sharedMemoryMap.PutStreamAsync(content);

            // Get content from the shared memory map
            Stream readContent = await sharedMemoryMap.GetStreamAsync();

            // Check if the read stream contains the same content that was written
            Assert.True(await TestUtils.StreamEqualsAsync(content, readContent));

            // Free the block of unmanaged memory
            Marshal.FreeHGlobal(memIntPtr);

            content.Close();
            content.Dispose();

            sharedMemoryMap.Dispose();
        }
        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();
            }
        }
Example #3
0
        public void Dispose_DeleteMemoryMappedFile_VerifyDeleted()
        {
            long            contentSize     = 1024;
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            sharedMemoryMap.Dispose(deleteFile: true);

            Assert.Null(Open(mapName));
        }
Example #4
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();
Example #5
0
        public void Create_VerifyCreated(long contentSize)
        {
            string mapName = Guid.NewGuid().ToString();
            long   size    = contentSize + SharedMemoryConstants.HeaderTotalBytes;

            Assert.True(_mapAccessor.TryCreate(mapName, size, out MemoryMappedFile mmf));

            SharedMemoryMap sharedMemoryMap = new SharedMemoryMap(_loggerFactory, _mapAccessor, mapName, mmf);

            Assert.NotNull(sharedMemoryMap);

            sharedMemoryMap.Dispose();
        }
Example #6
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();
        }
Example #7
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();
        }
Example #8
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();
        }
        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();
            }
        }
Example #10
0
        public async Task GetContentLength_LargeData_VerifyContentLengthMatches(long contentSize)
        {
            string          mapName         = Guid.NewGuid().ToString();
            SharedMemoryMap sharedMemoryMap = Create(mapName, contentSize);

            // Since the size of memory being requested can be greater than 2GB, a 32 bit
            // integer type can't hold that. So we use an IntPtr to store the size, which
            // on a 64 bit platform would store the size in a 64 bit integer. Then we can pass
            // a pointer to that integer to the memory allocation method.
            IntPtr sizePtr = new IntPtr(contentSize);

            // Allocate a block of unmanaged memory
            IntPtr memIntPtr = Marshal.AllocHGlobal(sizePtr);

            // Generate content to put into the shared memory map
            Stream content = TestUtils.GetRandomContentInStream(contentSize, memIntPtr);

            // Put content into the shared memory map
            await sharedMemoryMap.PutStreamAsync(content);

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

            sharedMemoryMap.Dispose();
        }