public void WhenTimeoutIsReachedNoDataIsReturned()
		{
			_loadDataCalled = 0;
			object result = null;
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));

			var waiter = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadDataWithWait(20, 5),
				ExpireAfter = TimeSpan.FromMilliseconds(3)
			}, (r) => result = r);
			target.Execute().Count();	//Initialize 
			var task = Task.Run(() =>
			{
				for (int i = 0; i < 8; i++)
				{
					_runner.RunCycle();//Fun the getter function
				}
			});
			Task.WaitAll(task);
			target.Execute().Count();	//Initialize 
			target.Execute().Count();	//Initialize 


			Assert.IsNull(result);
			Assert.AreEqual(1, _loadDataCalled);
		}
		public void ShouldExistTheDefaultEmptyGroup()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));

			Assert.AreEqual(1, target.Groups.Count);
			var group = target.Groups[string.Empty];
			Assert.IsNotNull(group);
		}
Пример #3
0
		public CachingModule()
		{
			Log = ServiceLocator.Locator.Resolve<ILogger>();
			_coroutinesManager = ServiceLocator.Locator.Resolve<ICoroutinesManager>();
			var main = ServiceLocator.Locator.Resolve<IMain>();
			var timeout = TimeSpan.FromHours(1);
			_nodeCache = new NodeCache(_coroutinesManager, timeout);
			_nodeCacheCoroutineThread = new NodeCacheCoroutine(_nodeCache);
			_coroutinesManager.StartCoroutine(_nodeCacheCoroutineThread);
			SetParameter(HttpParameters.CacheInstance, _nodeCache);
			main.RegisterCommand("nodecache.groups", new CommandDefinition((Action)ShowGroups));
			main.RegisterCommand("nodecache.items", new CommandDefinition((Action<string>)ShowItems,typeof(string)));
		}
		public void ItShouldBePossibleToAddAndGetNotExistingItem()
		{
			_loadDataCalled = 0;
			object result = null;
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			var cr = new NodeCacheCoroutine(target);
			_runner.StartCoroutine(cr);
			_runner.RunCycle();

			var waiter = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadDataWithWait(10, 5)
			}, (r) =>
			{
				result = r;
			});
			_runner.StartCoroutine(waiter.AsCoroutine());
			_runner.RunCycleFor(200);

			Assert.AreEqual(1, _loadDataCalled);
			Assert.AreEqual("RESULT", result);
		}
		public void AddAndGetNonExistingItemWithTimeoutWillNotSetTheValue()
		{
			_loadDataCalled = 0;
			object result = null;
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));

			var waiter = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadData(20)
			}, (r) => result = r);
			target.Execute().Count();	//Initialize 
			//Fun the getter function
			_runner.RunCycle(6);
			target.Execute().Count();	//Initialize 
			target.Execute().Count();	//Initialize 

			//ExecuteEnumerator(waiter.NestedEnumerator);
			Assert.IsNull(result);
			Assert.AreEqual(1, _loadDataCalled);
		}
		public void ItShouldBePossibleToAddAndGetAnAlreadyExistingItem()
		{
			_loadDataCalled = 0;
			object result = null;
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddItem("test", "RESULT");
			_runner.StartCoroutine(new NodeCacheCoroutine(target));
			_runner.RunCycle();		//Initialize node cache coroutine

			var waiter = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadData(5),
				ExpireAfter = TimeSpan.FromMinutes(1)
			}, (r) =>
			{
				result = r;
			});
			_runner.StartCoroutine(waiter.AsCoroutine());
			//target.Execute().Count();	//Initialize 
			_runner.RunCycle();

			Assert.AreEqual("RESULT", result);
			Assert.AreEqual(0, _loadDataCalled);
		}
		public void OverlappingRequestWillNotInvokeTwiceTheLoadDataButWillHaveTheSameResult()
		{
			_loadDataCalled = 0;
			object result1 = null;
			object result2 = null;
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			_runner.StartCoroutine(new NodeCacheCoroutine(target));
			_runner.RunCycle();		//Initialize node cache coroutine

			var waiter1 = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadData(5)
			}, (r) =>
			{
				result1 = r;
			});
			_runner.StartCoroutine(waiter1.AsCoroutine());
			var waiter2 = (FluentResultBuilder)target.AddAndGet(new CacheDefinition
			{
				Id = "test",
				LoadData = () => LoadData(5, "ANOTHER THING"),
				ExpireAfter = TimeSpan.FromMinutes(5)
			}, (r) =>
			{
				result2 = r;
			});
			_runner.StartCoroutine(waiter2.AsCoroutine());
			_runner.RunCycle();	//Load the coroutines
			_runner.RunCycle();	//Start the coroutines methods
			_runner.RunCycle(5);	//Wait for completion
			_runner.RunCycle();	//Copy the data

			Assert.AreEqual(1, _loadDataCalled);
			Assert.AreEqual("RESULT", result1, "Result 1 is " + result1);
			Assert.AreEqual("RESULT", result2, "Result 2 is " + result1);
		}
		public void ItShouldBePossibleToInvalidateItemNoItemShouldDoNothing()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));

			target.InvalidateItem("testId");
			target.Execute().Count();

			Assert.IsFalse(target.GetItems("").ContainsKey("testId"));
		}
		public void ItShouldBePossibleToAddUpdateItemDuplicateShouldUpdateCd()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddItem(new CacheDefinition
			{
				Value = "testValue",
				Id = "testId"
			});
			target.Execute().Count();


			target.AddOrUpdateItem(new CacheDefinition
			{
				Value = "testValueDifferent",
				Id = "testId"
			});
			target.Execute().Count();

			var item = target.GetItems("")["testId"];
			Assert.AreEqual("testValueDifferent", item.Value);
		}
		public void ItShouldBePossibleToAddUpdateItemWithCd()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddOrUpdateItem(new CacheDefinition
			{
				Value = "testValue",
				Id = "testId"
			});
			target.Execute().Count();

			var group = target.Groups[string.Empty];
			Assert.IsTrue(target.GetItems("").ContainsKey("testId"));
			var item = target.GetItems("")["testId"];
			Assert.AreEqual(group.ExpireAfter, item.ExpireAfter);
			Assert.AreEqual("testValue", item.Value);
			Assert.IsFalse(target.Groups.ContainsKey("test"));
		}
		public void ItShouldBePossibleToAddUpdateItemDuplicateShouldUpdate()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddItem("testId", "testValue");
			target.Execute().Count();

			target.AddOrUpdateItem("testId", "testValueChange");
			target.Execute().Count();

			var item = target.GetItems("")["testId"];
			Assert.AreEqual("testValueChange", item.Value);
		}
		public void ItShouldBePossibleToAddUpdateItemToSpecificGroup()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddGroup(new CacheGroupDefinition
			{
				Id = "testGroup",
				Capped = 1000,
				ExpireAfter = TimeSpan.FromMilliseconds(500),
				RollingExpiration = true
			});
			target.Execute().Count();
			target.AddOrUpdateItem("testId", "testValue", "testGroup");
			target.Execute().Count();

			var group = target.Groups["testGroup"];
			Assert.IsTrue(target.GetItems("testGroup").ContainsKey("testId"));
			var item = target.GetItems("testGroup")["testId"];
			Assert.AreEqual(group.ExpireAfter, item.ExpireAfter);
			Assert.AreEqual("testValue", item.Value);
		}
		public void ItShouldBePossibleToAddItemToDefaultGroup()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddItem("testId", "testValue");
			target.Execute().Count();

			var group = target.Groups[string.Empty];
			Assert.IsTrue(target.GetItems("").ContainsKey("testId"));
			var item = target.GetItems("")["testId"];
			Assert.AreEqual(group.ExpireAfter, item.ExpireAfter);
			Assert.AreEqual("testValue", item.Value);
		}
		public void ItShouldBePossibleToRemoveGroupNoGroupShouldDoNothing()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));

			target.RemoveGroup("notExisting");
			target.Execute().Count();

			Assert.AreEqual(1, target.Groups.Count);
			Assert.IsFalse(target.Groups.ContainsKey("notExisting"));
		}
		public void ItShouldBePossibleToRemoveGroup()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddGroup(new CacheGroupDefinition
			{
				Id = "test",
				Capped = 1000,
				ExpireAfter = TimeSpan.FromMilliseconds(500),
				RollingExpiration = true
			});
			target.Execute().Count();
			Assert.AreEqual(2, target.Groups.Count);

			target.RemoveGroup("test");
			target.Execute().Count();

			Assert.AreEqual(1, target.Groups.Count);
			Assert.IsFalse(target.Groups.ContainsKey("test"));
		}
		public void ItShouldBePossibleToAddGroupUpdatingShouldThrow()
		{
			var target = new NodeCache(_runner,TimeSpan.FromSeconds(10));
			target.AddGroup(new CacheGroupDefinition
				{
					Id = "test",
					Capped = 1000,
					ExpireAfter = TimeSpan.FromMilliseconds(500),
					RollingExpiration = true
				});
			target.Execute().Count();

			Exception resultEx = null;
			try
			{
				target.AddGroup(new CacheGroupDefinition
				{
					Id = "test",
					Capped = 100,
					ExpireAfter = TimeSpan.FromMilliseconds(500),
					RollingExpiration = true
				});
			}
			catch (Exception ex)
			{
				resultEx = ex;
			}
			Assert.IsNotNull(resultEx);
		}