public async Task GetObject_SharedMemoryObject_VerifyMatches(string content) { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Put content into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Get object from shared memory object readObject = await manager.GetObjectAsync(metadata.MemoryMapName, 0, content.Length, typeof(SharedMemoryObject)); SharedMemoryObject readContent = readObject as SharedMemoryObject; Stream readContentStream = readContent.Content; Assert.NotNull(readContentStream); Assert.Equal(metadata.MemoryMapName, readContent.MemoryMapName); Assert.Equal(metadata.Count, readContent.Count); using (StreamReader reader = new StreamReader(readContentStream)) { string readContentString = await reader.ReadToEndAsync(); // Verify read content matches the content that was written Assert.Equal(content, readContentString); } } }
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); } }
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 _)); }
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(); } }
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(); } }
public async Task PutObject_NoActiveReferences_ForceOneEviction_VerifyCorrectEviction() { int contentSize = 2 * 1024 * 1024; // 2MB int cacheSize = 6 * 1024 * 1024; // 6MB string cacheSizeVal = cacheSize.ToString(); IEnvironment environment = new TestEnvironment(); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheMaximumSizeBytesSettingName, cacheSizeVal); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, "1"); using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, environment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory as three distinct objects SharedMemoryMetadata metadata1 = await manager.PutObjectAsync(content); SharedMemoryMetadata metadata2 = await manager.PutObjectAsync(content); SharedMemoryMetadata metadata3 = await manager.PutObjectAsync(content); // Put the three objects into the cache FunctionDataCacheKey key1 = new FunctionDataCacheKey("foo1", "bar1"); FunctionDataCacheKey key2 = new FunctionDataCacheKey("foo2", "bar2"); FunctionDataCacheKey key3 = new FunctionDataCacheKey("foo3", "bar3"); Assert.True(cache.TryPut(key1, metadata1, isIncrementActiveReference: false, isDeleteOnFailure: false)); Assert.True(cache.TryPut(key2, metadata2, isIncrementActiveReference: false, isDeleteOnFailure: false)); Assert.True(cache.TryPut(key3, metadata3, isIncrementActiveReference: false, isDeleteOnFailure: false)); // Verify that the cache is full Assert.Equal(0, cache.RemainingCapacityBytes); // At this point, the cache is full. // We will create another object and try to insert it. // This should be inserted (as another object will be evicted to make room for this). SharedMemoryMetadata metadata4 = await manager.PutObjectAsync(content); FunctionDataCacheKey key4 = new FunctionDataCacheKey("foo4", "bar4"); Assert.True(cache.TryPut(key4, metadata4, isIncrementActiveReference: false, isDeleteOnFailure: false)); // The first object should be evicted (least recently used) by now Assert.False(cache.TryGet(key1, isIncrementActiveReference: false, out var _)); // Try to open the shared memory map of the first object and ensure it is removed and cannot be opened Assert.False(_mapAccessor.TryOpen(metadata1.MemoryMapName, out var _)); // The last three objects (the first two added before eviction and the one resulting in eviction) should be present Assert.True(cache.TryGet(key2, isIncrementActiveReference: false, out var _)); Assert.True(cache.TryGet(key3, isIncrementActiveReference: false, out var _)); Assert.True(cache.TryGet(key4, isIncrementActiveReference: false, out var _)); // Verify that the cache is full Assert.Equal(0, cache.RemainingCapacityBytes); } }
public void FreeNonExistentSharedMemoryMap_VerifyFailure() { string mapName = Guid.NewGuid().ToString(); using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { Assert.False(manager.TryFreeSharedMemoryMap(mapName)); } }
public void FreeNonExistentInvocationIdSharedMemoryMaps_VerifySuccess() { string invocationId = Guid.NewGuid().ToString(); using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { Assert.True(manager.TryFreeSharedMemoryMapsForInvocation(invocationId)); } }
public async Task PutThreeObjects_GetOne_NoActiveReferences_VerifyEvictionOrder() { int contentSize = 2 * 1024 * 1024; // 2MB using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, _testEnvironment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory as three distinct objects List <SharedMemoryMetadata> metadatas = new List <SharedMemoryMetadata>() { await manager.PutObjectAsync(content), await manager.PutObjectAsync(content), await manager.PutObjectAsync(content), }; // Put the three objects into the cache List <FunctionDataCacheKey> keys = new List <FunctionDataCacheKey>() { new FunctionDataCacheKey("foo_1", "bar_1"), new FunctionDataCacheKey("foo_2", "bar_2"), new FunctionDataCacheKey("foo_3", "bar_3"), }; for (int i = 0; i < 3; i++) { Assert.True(cache.TryPut(keys[i], metadatas[i], isIncrementActiveReference: false, isDeleteOnFailure: false)); } // Access the middle object so that now it is the most recently used Assert.True(cache.TryGet(keys[1], isIncrementActiveReference: false, out var _)); // The order of objects in LRU should be 1->3->2. // Since 2 is the most recently used, it is at the end of the list. // Evict an object and check that the right object was evicted (1) Assert.True(cache.EvictOne()); Assert.False(cache.TryGet(keys[0], isIncrementActiveReference: false, out var _)); // Check if the other objects are still present; // We cannot check using TryGet as that will impact the LRU ordering Assert.Contains(keys[1], cache.LRUList); Assert.Contains(keys[2], cache.LRUList); // Evict an object and check that the right object was evicted (3) Assert.True(cache.EvictOne()); Assert.False(cache.TryGet(keys[2], isIncrementActiveReference: false, out var _)); // Check if the other object are still present; // We cannot check using TryGet as that will impact the LRU ordering Assert.Contains(keys[1], cache.LRUList); // Evict an object and check that the right object was evicted (2) Assert.True(cache.EvictOne()); Assert.Empty(cache.LRUList); } }
public void CreateTwoNewSharedMemoryMapsWithSameName_VerifyFailure(long contentSize) { string mapName = Guid.NewGuid().ToString(); using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { Assert.NotNull(manager.Create(mapName, contentSize)); Assert.Null(manager.Create(mapName, contentSize)); } }
public async Task PutObject_EmptyByteArray_VerifyFailure() { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Put into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(new byte[0]); // Verify expected results Assert.Null(metadata); } }
public async Task GetObject_EmptyContent_SharedMemoryObject_VerifyException() { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (Stream content = new MemoryStream()) { // Put content into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Get object from shared memory await Assert.ThrowsAnyAsync <Exception>(() => manager.GetObjectAsync(metadata.MemoryMapName, 0, (int)content.Length, typeof(SharedMemoryObject))); } }
public void ToggleDisabled_VerifyIsEnabled(string envVal) { IEnvironment testEnvironment = new TestEnvironment(); testEnvironment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, envVal); using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, testEnvironment)) { Assert.False(cache.IsEnabled); } }
public void SetValidCacheSizeInEnvironment_VerifyMaximumCapacityBytes(long cacheSize) { IEnvironment testEnvironment = new TestEnvironment(); testEnvironment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, "1"); testEnvironment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheMaximumSizeBytesSettingName, $"{cacheSize}"); using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, testEnvironment)) { Assert.Equal(cacheSize, cache.RemainingCapacityBytes); } }
public async Task PutObject_String_VerifySuccess(string content) { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Put into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Verify expected results Assert.NotNull(metadata); Assert.NotNull(metadata.MemoryMapName); Assert.True(Guid.TryParse(metadata.MemoryMapName, out _)); Assert.Equal(content.Length, metadata.Count); } }
public void TestSupportedObjects() { // 5MB byte[] object objectA = TestUtils.GetRandomBytesInArray(5 * 1024 * 1024); // string containing 5 * 1024 * 1024 chars (total size = 5 * 1024 * 1024 * sizeof(char)) object objectB = new StringBuilder().Append('a', 5 * 1024 * 1024).ToString(); using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { Assert.True(manager.IsSupported(objectA)); Assert.True(manager.IsSupported(objectB)); } }
/// <summary> /// Init XRef info. /// </summary> /// <param name="projectPath">Project path(is not project var file)</param> /// <returns>Warning message string. Not used now.</returns> public string InitXRef(string projectPath) { if (string.IsNullOrEmpty(projectPath)) { return(null); } // remove ending path seperated char if has projectPath = projectPath.TrimEnd(new char[] { '\\' }); BlockListMgr = new BlockListManager(projectPath); IOAddressMgr = new IOAddressManager(projectPath); SharedMemoryMgr = new SharedMemoryManager(projectPath); CompoundBlockMgr = new CompoundBlockManager(projectPath); //KHo 20111226 BuildListMgr = new BuildListManager(projectPath); XRefListPath = Path.Combine(projectPath, "xref.data"); XShmListPath = Path.Combine(projectPath, "xshm.data"); // if upgrade from xref.data to xref.data + xshm.data, we keep new files' // time is same as old xref.data's. bool keepTime = false; DateTime dtLastWriteTime = DateTime.Now; if (File.Exists(XRefListPath) && !File.Exists(XShmListPath)) { dtLastWriteTime = File.GetLastWriteTime(XRefListPath); keepTime = true; } // load xref & xshm list info string msg = ""; LoadXRefList(ref msg); // auto-save file if current is dirty if (this.XRefDirty) { SaveXRefList(false); // keep two files' time if needed if (keepTime) { File.SetLastWriteTime(XRefListPath, dtLastWriteTime); File.SetLastWriteTime(XShmListPath, dtLastWriteTime); } } return(msg); }
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(); } }
public void TestUnsupportedObjects() { // Objects that don't meet the minimum size requirements object objectA = new byte[5]; object objectB = new string("abc"); // Objects that don't meet the type requirements object objectC = new int[5 * 1024 * 1024]; using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { Assert.False(manager.IsSupported(objectA)); Assert.False(manager.IsSupported(objectB)); Assert.False(manager.IsSupported(objectC)); } }
static void Main(string[] args) { var fixedmap = new FixedAddressTypesMap(0x0032e2000); fixedmap.GetOrAddType <SharedType>(); using (var reciever = new SharedMemoryManager <SharedType>(typeof(SharedType).FullName, 1024)) using (var sender = new SharedMemoryManager <string>(typeof(string).FullName, 1024)) { var obj = reciever.ReceiveObject(); var str = string.Format("Recieved: {0}", obj); Console.WriteLine(str); sender.ShareObject(str); } Console.ReadKey(); }
public async Task GetObject_String_VerifyMatches(string content) { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Put content into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Get object from shared memory object readObject = await manager.GetObjectAsync(metadata.MemoryMapName, 0, content.Length, typeof(string)); string readContent = readObject as string; // Verify read content matches the content that was written Assert.Equal(content, readContent); } }
public async Task GetObject_ByteArray_VerifyMatches(int contentSize) { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Prepare content and put into shared memory byte[] content = TestUtils.GetRandomBytesInArray(contentSize); SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Get object from shared memory object readObject = await manager.GetObjectAsync(metadata.MemoryMapName, 0, contentSize, typeof(byte[])); byte[] readContent = readObject as byte[]; // Verify read content matches the content that was written Assert.True(TestUtils.UnsafeCompare(content, readContent)); } }
public async Task PutObject_ByteArray_VerifySuccess(int contentSize) { using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Verify expected results Assert.NotNull(metadata); Assert.NotNull(metadata.MemoryMapName); Assert.True(Guid.TryParse(metadata.MemoryMapName, out _)); Assert.Equal(contentSize, metadata.Count); } }
public async Task FreeSharedMemoryMap_VerifySuccess() { // Prepare content string content = "foobar"; using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) { // Put content into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); string mapName = metadata.MemoryMapName; // Free the shared memory map and try top open it after freeing; should not open Assert.True(manager.TryFreeSharedMemoryMap(mapName)); Assert.False(_mapAccessor.TryOpen(mapName, out _)); } }
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); } }
public async Task PutObject_ActiveReference_VerifyNotEvicted() { int contentSize = 2 * 1024 * 1024; // 2MB int cacheSize = 3 * 1024 * 1024; // 3MB string cacheSizeVal = cacheSize.ToString(); IEnvironment environment = new TestEnvironment(); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheMaximumSizeBytesSettingName, cacheSizeVal); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, "1"); using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, environment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory as two objects SharedMemoryMetadata metadata1 = await manager.PutObjectAsync(content); SharedMemoryMetadata metadata2 = await manager.PutObjectAsync(content); // Put one object into the cache and keep an active reference FunctionDataCacheKey key1 = new FunctionDataCacheKey("foo1", "bar1"); Assert.True(cache.TryPut(key1, metadata1, isIncrementActiveReference: true, isDeleteOnFailure: false)); // The first object has used up the cache space. // When trying to insert the second object into the cache, it should fail // since the first has an active reference and cannot be evicted. FunctionDataCacheKey key2 = new FunctionDataCacheKey("foo2", "bar2"); Assert.False(cache.TryPut(key2, metadata2, isIncrementActiveReference: false, isDeleteOnFailure: false)); // Ensure that the first object was not evicted Assert.True(cache.TryGet(key1, isIncrementActiveReference: false, out var _)); // Drop the active reference on the first object cache.DecrementActiveReference(key1); // Now, when trying to insert the second object into the cache, it should succeed // since the first object can be evicted (since its active reference was dropped). Assert.True(cache.TryPut(key2, metadata2, isIncrementActiveReference: false, isDeleteOnFailure: false)); // Ensure that the first object was evicted Assert.False(cache.TryGet(key1, isIncrementActiveReference: false, out var _)); } }
public ScriptInvocationContextExtensionsTests() { ILogger <MemoryMappedFileAccessor> logger = NullLogger <MemoryMappedFileAccessor> .Instance; _testEnvironment = new TestEnvironment(); _testEnvironment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, "1"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { _mapAccessor = new MemoryMappedFileAccessorWindows(logger); } else { _mapAccessor = new MemoryMappedFileAccessorUnix(logger, _testEnvironment); } _sharedMemoryManager = new SharedMemoryManager(_loggerFactory, _mapAccessor); _functionDataCache = new FunctionDataCache(_sharedMemoryManager, _loggerFactory, _testEnvironment); }
public async Task PutThreeObjects_VerifyLRUOrder() { int contentSize = 2 * 1024 * 1024; // 2MB using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, _testEnvironment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory as three distinct objects List <SharedMemoryMetadata> metadatas = new List <SharedMemoryMetadata>() { await manager.PutObjectAsync(content), await manager.PutObjectAsync(content), await manager.PutObjectAsync(content), }; // Put the three objects into the cache List <FunctionDataCacheKey> keys = new List <FunctionDataCacheKey>() { new FunctionDataCacheKey("foo_1", "bar_1"), new FunctionDataCacheKey("foo_2", "bar_2"), new FunctionDataCacheKey("foo_3", "bar_3"), }; for (int i = 0; i < 3; i++) { Assert.True(cache.TryPut(keys[i], metadatas[i], isIncrementActiveReference: false, isDeleteOnFailure: false)); } // The order of the LRU list should be the same as that in which objects were inserted above. // i.e. the first element of the list should be the least recently used (oldest inserted). int pos = 1; foreach (FunctionDataCacheKey key in cache.LRUList) { string keyId = key.Id; int num = int.Parse(keyId.Split("_")[1]); Assert.Equal(pos, num); pos++; } } }
public async Task PutObject_NoEvictions_VerifyGet(int contentSize) { using (ISharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, _testEnvironment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Put into cache FunctionDataCacheKey key = new FunctionDataCacheKey("foo", "bar"); Assert.True(cache.TryPut(key, metadata, isIncrementActiveReference: false, isDeleteOnFailure: false)); // Get from cache Assert.True(cache.TryGet(key, isIncrementActiveReference: false, out SharedMemoryMetadata getMetadata)); // Compare if the obtained values are equal Assert.Equal(metadata, getMetadata); } }
public async Task PutObject_FailToPut_DoNotDeleteOnFailure() { int contentSize = 4 * 1024 * 1024; // 4MB int cacheSize = 3 * 1024 * 1024; // 3MB string cacheSizeVal = cacheSize.ToString(); IEnvironment environment = new TestEnvironment(); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheMaximumSizeBytesSettingName, cacheSizeVal); environment.SetEnvironmentVariable(FunctionDataCacheConstants.FunctionDataCacheEnabledSettingName, "1"); using (SharedMemoryManager manager = new SharedMemoryManager(_loggerFactory, _mapAccessor)) using (FunctionDataCache cache = new FunctionDataCache(manager, _loggerFactory, environment)) { // Prepare content byte[] content = TestUtils.GetRandomBytesInArray(contentSize); // Put into shared memory SharedMemoryMetadata metadata = await manager.PutObjectAsync(content); // Try to put the object into the cache; this will fail because the cache is smaller than the object size. // Since isDeleteOnFailure is false, the object will not be deleted from shared memory. FunctionDataCacheKey key = new FunctionDataCacheKey("foo", "bar"); Assert.False(cache.TryPut(key, metadata, isIncrementActiveReference: true, isDeleteOnFailure: false)); // Ensure that nothing was cached and no references are held Assert.Empty(cache.LRUList); Assert.Empty(cache.ActiveReferences); Assert.Equal(cacheSize, cache.RemainingCapacityBytes); // Ensure that the SharedMemoryManager has the allocated memory map and it was not deleted Assert.Equal(1, manager.AllocatedSharedMemoryMaps.Count); // Try to open the shared memory map of the first object and ensure it exists and can be opened Assert.True(_mapAccessor.TryOpen(metadata.MemoryMapName, out var _)); } }
private static void FindWindowForTest() { sharedMemoryManager = new SharedMemoryManager(IoftpdMessagewindow); bool isWindowThere = sharedMemoryManager.IsWindowThere; Assert.IsTrue(isWindowThere, string.Format("'{0}' was not found!", IoftpdMessagewindow)); }