Exemplo n.º 1
0
        public void Test_TRexSpatialMemoryCacheStorageTests_TimeCheckReusingElements(int numElements, int overflowBy)
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(numElements, numElements / 2);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            var startTime = DateTime.Now;

            var item = new TRexSpatialMemoryCacheContextTests_Element
            {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            // Fill all available slots
            for (int i = 0; i < numElements; i++)
            {
                storage.Add(item, null);
            }

            var midTime = DateTime.Now;

            for (int i = 0; i < overflowBy; i++)
            {
                storage.Add(item, dummyCache);
            }

            Assert.False(true, $"Time for adding {numElements} elements is {midTime - startTime} and adding {overflowBy} overflows is {DateTime.Now - midTime}");
        }
Exemplo n.º 2
0
        public void Test_TRexSpatialMemoryCacheContext_Creation_Default_Sensitivity()
        {
            ITRexSpatialMemoryCacheContext context = new TRexSpatialMemoryCacheContext(null, null);

            Assert.True(context.Sensitivity == TRexSpatialMemoryCacheInvalidationSensitivity.ProductionDataIngest,
                        "Default cache invalidation sensitivity is not production data ingest.");
        }
Exemplo n.º 3
0
        public void Test_TRexSpatialMemoryCacheStorageTests_FillWithElementsThenOverflowBy(int numElements, int overflowBy)
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(numElements, numElements / 2);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            // Fill all available slots
            for (int i = 0; i < numElements; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element
                {
                    SizeInBytes  = 0,
                    CacheOriginX = (int)(2000 + i * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = (int)(3000 + i * SubGridTreeConsts.SubGridTreeDimension)
                }, dummyCache);
            }

            Assert.False(storage.HasFreeSpace(), "Storage has free space when filled");

            for (int i = 0; i < overflowBy; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element
                {
                    SizeInBytes  = 0,
                    CacheOriginX = (int)(2000 + (numElements + i) * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = (int)(3000 + (numElements + i) * SubGridTreeConsts.SubGridTreeDimension)
                }, dummyCache);
            }

            Assert.True(storage.TokenCount == numElements, $"Element count incorrect (= {storage.TokenCount})");
        }
Exemplo n.º 4
0
        public void Test_TRexSpatialMemoryCacheStorageTests_TimeCheckCreatingElements(int numElements, int overflowBy)
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(numElements, numElements / 2);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            var startTime = DateTime.Now;

            // Fill all available slots
            for (int i = 0; i < numElements; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element
                {
                    SizeInBytes  = 1000,
                    CacheOriginX = (int)(2000 + i * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = (int)(3000 + i * SubGridTreeConsts.SubGridTreeDimension)
                }, dummyCache);
            }

            var midTime = DateTime.Now;

            for (int i = 0; i < overflowBy; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element
                {
                    SizeInBytes  = 1000,
                    CacheOriginX = (int)(1000 + (numElements + i) * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = (int)(1000 + (numElements + i) * SubGridTreeConsts.SubGridTreeDimension)
                }, dummyCache);
            }

            Assert.False(true, $"Time for adding {numElements} elements is {midTime - startTime} and adding {overflowBy} overflows is {DateTime.Now - midTime}");
        }
Exemplo n.º 5
0
        public void Test_TRexSpatialMemoryCacheContext_Creation_NonDefault_Project()
        {
            Guid _projectUID = Guid.NewGuid();

            ITRexSpatialMemoryCacheContext context = new TRexSpatialMemoryCacheContext(null, null, TimeSpan.Zero, "", GridDataType.All, _projectUID);

            Assert.True(context.ProjectUID == _projectUID, "Default projectUID not set.");
        }
Exemplo n.º 6
0
        public void Test_TRexSpatialMemoryCacheContext_Creation_Default()
        {
            ITRexSpatialMemoryCacheContext context = new TRexSpatialMemoryCacheContext(null, null);

            Assert.True(context.ContextTokens != null, "No index sub grid tree created");
            Assert.True(context.MRUList == null);
            Assert.True(context.OwnerMemoryCache == null);
            Assert.False(context.MarkedForRemoval);
            Assert.True(context.MarkedForRemovalAtUtc == TRex.Common.Consts.MIN_DATETIME_AS_UTC);
        }
Exemplo n.º 7
0
        public void Test_TRexSpatialMemoryCacheContext_Creation_WithOwnerAndMRU()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            Assert.True(context.ContextTokens != null, "No index subgrid tree created");
            Assert.True(context.MRUList != null, "No MRU list available");
            Assert.True(context.OwnerMemoryCache != null, "No owning memory cache available");
        }
Exemplo n.º 8
0
        public void MarkForRemoval_FailOnNonUTCDate()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            Action act = () => context.MarkForRemoval(DateTime.Now);

            act.Should().Throw <TRexException>().WithMessage("MarkForRemoval is not a UTC date");
        }
Exemplo n.º 9
0
        public void Test_TRexSpatialMemoryCacheContext_AddOneElement_FailWithInvalidationVersionMismatch()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            var element = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion + 1).Should().Be(CacheContextAdditionResult.RejectedDueToInvlidationVersionMismatch);
        }
Exemplo n.º 10
0
        public void Test_TRexSpatialMemoryCacheContext_FailOverwriteOfExistingElement()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            var element = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            Assert.True(context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion) == CacheContextAdditionResult.Added, "Result is false on addition of first element");
            Assert.True(context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion) == CacheContextAdditionResult.AlreadyExisting, "Result is true on second addition of same element");
        }
Exemplo n.º 11
0
        public void Test_TRexCacheItem_Creation_Specific()
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(10, 5);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            var theObject = new TRexSpatialMemoryCacheContextTests_Element();
            TRexCacheItem <TRexSpatialMemoryCacheContextTests_Element> item = new TRexCacheItem <TRexSpatialMemoryCacheContextTests_Element>(theObject, dummyCache, 100, 1, 2);

            Assert.True(ReferenceEquals(item.Item, theObject));
            Assert.True(item.Prev == 1);
            Assert.True(item.Next == 2);
            Assert.True(item.MRUEpochToken == 100);
        }
Exemplo n.º 12
0
        public void Test_TRexSpatialMemoryCacheContext_MarkForRemovalDirect()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            Assert.False(context.MarkedForRemoval);

            var currentDate = DateTime.UtcNow;

            context.MarkForRemoval(DateTime.UtcNow);
            Assert.True(context.MarkedForRemoval, "Marking context for removal did not set state");
            Assert.True(context.MarkedForRemovalAtUtc >= currentDate && context.MarkedForRemovalAtUtc <= DateTime.UtcNow, "Marking context for removal did not set state");
        }
Exemplo n.º 13
0
        public void Test_TRexSpatialMemoryCacheStorageTests_AddOneElement()
        {
            ITRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem> storage = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            storage.Add(new TRexSpatialMemoryCacheContextTests_Element
            {
                SizeInBytes  = 0,
                CacheOriginX = 2000,
                CacheOriginY = 3000
            }, dummyCache);

            Assert.True(storage.HasFreeSpace(), "Storage has no free space when filled with only one element");

            Assert.True(storage.TokenCount == 1, $"Element count incorrect (= {storage.TokenCount})");
        }
Exemplo n.º 14
0
        public void Test_TRexSpatialMemoryCacheStorageTests_MaxEpochTokenAgeOnGet()
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(1000, 500);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            // Fill half slots
            for (int i = 0; i < 500; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element()
                {
                    CacheOriginX = (int)(1000 + i * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = 1000,
                    SizeInBytes  = 1000
                }, dummyCache);
            }

            var currentMRUHead = storage.MRUHead;

            // Get each item in the same order and verify they are not touched and moved to the MRUHead
            for (int i = 0; i < 500; i++)
            {
                var item = storage.Get(i);

                Assert.True(storage.MRUHead == currentMRUHead, $"MRUHead changed unexpectedly after getting item {item}: storage.MRUHead = {storage.MRUHead}, currentMRUHead = {currentMRUHead}");
            }

            // Fill remaining slots
            for (int i = 500; i < 1000; i++)
            {
                storage.Add(new TRexSpatialMemoryCacheContextTests_Element()
                {
                    CacheOriginX = (int)(1000 + i * SubGridTreeConsts.SubGridTreeDimension),
                    CacheOriginY = 1000,
                    SizeInBytes  = 1000
                }, dummyCache);
            }

            // Get each item in the same order and verify they are touched and moved to the MRUHead
            for (int i = 0; i < 1000; i++)
            {
                var item = storage.Get(i);

                Assert.True(storage.MRUHead == i, $"Referenced item not promoted to MRU head after getting item {item} at index {i}, MRUHead = {storage.MRUHead}");
            }
        }
Exemplo n.º 15
0
        public void Test_TRexSpatialMemoryCacheStorageTests_RemoveOneElement()
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            var index = storage.Add(new TRexSpatialMemoryCacheContextTests_Element
            {
                SizeInBytes  = 0,
                CacheOriginX = 2000,
                CacheOriginY = 3000
            }, dummyCache);

            Assert.True(storage.TokenCount == 1, $"Element count incorrect (= {storage.TokenCount})");

            storage.Remove(index);

            Assert.True(storage.TokenCount == 0, $"Element count incorrect (= {storage.TokenCount})");
        }
Exemplo n.º 16
0
        public void Test_TRexSpatialMemoryCacheContext_ReanimateViaItemAddition()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            var element = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            context.MarkForRemoval(DateTime.UtcNow);
            Assert.True(context.MarkedForRemoval, "Marking context for removal did not set state");

            // Reanimate by adding an element
            Assert.True(context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion) == CacheContextAdditionResult.Added, "Result is false on addition of first element");

            Assert.False(context.MarkedForRemoval, "Marking context for removal did not set state");
            Assert.True(context.MarkedForRemovalAtUtc == TRex.Common.Consts.MIN_DATETIME_AS_UTC, "Marking context for removal did not set date");
        }
Exemplo n.º 17
0
        public void InvalidateSubGrid()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            // context.InvalidateSubGrid facades context.InvalidateSubGrid
            context.InvalidateSubGrid(0, 0, out var present);
            present.Should().BeFalse();

            var element = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion).Should().Be(CacheContextAdditionResult.Added);

            context.InvalidateSubGrid(2000, 3000, out present);
            present.Should().BeTrue();
        }
Exemplo n.º 18
0
        public void Test_TRexSpatialMemoryCacheContext_AddOneElement()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            var element = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion).Should().Be(CacheContextAdditionResult.Added);

            Assert.True(context.TokenCount == 1, $"Element count incorrect (= {context.TokenCount})");
            Assert.True(context.MRUList.TokenCount == 1, $"MRU list count incorrect (= {context.MRUList.TokenCount})");

            // Check the newly added element in the context is present in the context map with a 1-based index
            int token = context.ContextTokens[element.CacheOriginX >> SubGridTreeConsts.SubGridIndexBitsPerLevel, element.CacheOriginY >> SubGridTreeConsts.SubGridIndexBitsPerLevel];

            Assert.True(token == 1, "Single newly added element does not have index of 1 present in ContextTokens");
        }
Exemplo n.º 19
0
        public void Test_TRexSpatialMemoryCacheStorageTests_GetElement()
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);

            var item = new TRexSpatialMemoryCacheContextTests_Element
            {
                SizeInBytes  = 0,
                CacheOriginX = 2000,
                CacheOriginY = 3000
            };

            var index = storage.Add(item, dummyCache);

            Assert.True(storage.TokenCount == 1, $"Element count incorrect (= {storage.TokenCount})");

            var getItem = storage.Get(index);

            Assert.True(ReferenceEquals(item, getItem), "Item retrieved from storage not same as item placed in storage");
        }
Exemplo n.º 20
0
        public void Test_TRexSpatialMemoryCacheContext_MarkForRemovalViaLastElementRemoved()
        {
            ITRexSpatialMemoryCacheContext context =
                new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(100, 1000000, 0.5),
                                                  new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50));

            Assert.False(context.MarkedForRemoval);

            var currentDate = DateTime.UtcNow;
            var element     = new TRexSpatialMemoryCacheContextTests_Element {
                SizeInBytes = 1000, CacheOriginX = 2000, CacheOriginY = 3000
            };

            Assert.True(context.OwnerMemoryCache.Add(context, element, context.InvalidationVersion) == CacheContextAdditionResult.Added, "Result is false on addition of first element");
            Assert.True(!context.MarkedForRemoval && context.TokenCount == 1);

            context.Remove(element);

            Assert.True(context.MarkedForRemoval, "Removing last element in context for removal did not mark for removal");
            Assert.True(context.MarkedForRemovalAtUtc >= currentDate, "Removal date not expected");
        }
Exemplo n.º 21
0
        public void Test_TRexSpatialMemoryCacheStorageTests_GetAfterRemove()
        {
            var storage    = new TRexSpatialMemoryCacheStorage <ITRexMemoryCacheItem>(100, 50);
            var dummyCache = new TRexSpatialMemoryCacheContext(new TRexSpatialMemoryCache(1000, 1000, 0), storage);
            var item       = new TRexSpatialMemoryCacheContextTests_Element
            {
                SizeInBytes  = 0,
                CacheOriginX = 2000,
                CacheOriginY = 3000
            };

            var index = storage.Add(item, dummyCache);

            Assert.True(storage.TokenCount == 1, $"Element count incorrect after add (= {storage.TokenCount})");
            Assert.True(storage.Get(index) != null);

            // Remove the item
            storage.Remove(index);

            Assert.True(storage.Get(index) == null, "Was able to extract item after invalidation");
            Assert.True(storage.TokenCount == 0, $"Element count incorrect after get after invalidation(= {storage.TokenCount})");
        }
Exemplo n.º 22
0
        public void Test_TRexSpatialMemoryCacheContext_Creation_Default_Project()
        {
            ITRexSpatialMemoryCacheContext context = new TRexSpatialMemoryCacheContext(null, null);

            Assert.True(context.ProjectUID == Guid.Empty, "Default projectUID not set.");
        }