Example #1
0
        public async Task Dispose_VerifyAllSharedMemoryResourcesFreed()
        {
            // Prepare content
            string invocationId1 = Guid.NewGuid().ToString();
            string invocationId2 = Guid.NewGuid().ToString();
            string content       = "foobar";

            SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor);

            // Put content into shared memory and add mapping to invocations
            SharedMemoryMetadata metadata1 = await manager.PutObjectAsync(content);

            string mapName1 = metadata1.MemoryMapName;

            manager.AddSharedMemoryMapForInvocation(invocationId1, mapName1);
            SharedMemoryMetadata metadata2 = await manager.PutObjectAsync(content);

            string mapName2 = metadata2.MemoryMapName;

            manager.AddSharedMemoryMapForInvocation(invocationId2, mapName2);

            // Open the shared memory map; should open
            Assert.True(_mapAccessor.TryOpen(mapName1, out MemoryMappedFile mmf1));
            Assert.True(_mapAccessor.TryOpen(mapName1, out MemoryMappedFile mmf2));
            mmf1.Dispose();
            mmf2.Dispose();

            // Dispose the shared memory manager; all shared memory maps it was tracking should be freed
            manager.Dispose();

            // Open the shared memory map; should not open
            Assert.False(_mapAccessor.TryOpen(mapName1, out _));
            Assert.False(_mapAccessor.TryOpen(mapName1, out _));
        }
Example #2
0
        public async Task FreeSharedMemoryMapsForInvocation_VerifySuccess()
        {
            // Prepare content
            string invocationId1 = Guid.NewGuid().ToString();
            string invocationId2 = Guid.NewGuid().ToString();
            string content       = "foobar";

            using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor))
            {
                // Put content into shared memory and add mapping to invocations
                SharedMemoryMetadata metadata1 = await manager.PutObjectAsync(content);

                string mapName1 = metadata1.MemoryMapName;
                manager.AddSharedMemoryMapForInvocation(invocationId1, mapName1);
                SharedMemoryMetadata metadata2 = await manager.PutObjectAsync(content);

                string mapName2 = metadata2.MemoryMapName;
                manager.AddSharedMemoryMapForInvocation(invocationId2, mapName2);

                // Free the shared memory maps for invocation1 and try top open it after freeing; should not open
                Assert.True(manager.TryFreeSharedMemoryMapsForInvocation(invocationId1));
                Assert.False(_mapAccessor.TryOpen(mapName1, out _));

                // Shared memory maps for invocation2 should still be available to open
                Assert.True(_mapAccessor.TryOpen(mapName2, out MemoryMappedFile mmf));
                mmf.Dispose();
            }
        }
Example #3
0
        public void AddSharedMemoryMapsForInvocation_VerifySuccess()
        {
            // Prepare two invocation IDs and shared memory map names corresponding to each
            string        invocationId1 = Guid.NewGuid().ToString();
            string        invocationId2 = Guid.NewGuid().ToString();
            List <string> mapNames1     = new List <string>
            {
                Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString()
            };
            List <string> mapNames2 = new List <string>
            {
                Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString(),
                          Guid.NewGuid().ToString()
            };

            using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor))
            {
                // Create mappings of invocation ID with shared memory map names
                mapNames1.ForEach(mapName => manager.AddSharedMemoryMapForInvocation(invocationId1, mapName));
                mapNames2.ForEach(mapName => manager.AddSharedMemoryMapForInvocation(invocationId2, mapName));

                // Verify the mappings
                Assert.True(manager.InvocationSharedMemoryMaps.TryGetValue(invocationId1, out HashSet <string> invocationMapNames1));
                Assert.True(manager.InvocationSharedMemoryMaps.TryGetValue(invocationId2, out HashSet <string> invocationMapNames2));
                Assert.Equal(mapNames1, invocationMapNames1);
                Assert.Equal(mapNames2, invocationMapNames2);
            }
        }
Example #4
0
        public void AddDuplicateSharedMemoryMapsForInvocation_VerifyOnlyOneIsAdded()
        {
            // Prepare invocation ID and shared memory map names
            string invocationId = Guid.NewGuid().ToString();
            string mapName      = Guid.NewGuid().ToString();

            using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor))
            {
                // Create mapping of invocation ID with same shared memory map name, twice
                manager.AddSharedMemoryMapForInvocation(invocationId, mapName);
                manager.AddSharedMemoryMapForInvocation(invocationId, mapName);

                // Verify only one mapping exists
                Assert.True(manager.InvocationSharedMemoryMaps.TryGetValue(invocationId, out HashSet <string> mapNames));
                Assert.Single(mapNames);
            }
        }