public void GetValue_CallGetMultipleTimesFirstGetterReturnsNull_CacheFirstNonNullValue() { // Arrange int i = 0; string value = null; Func <string, string> valueGetter = x => { i++; var val = value; value = "foo"; return(val); }; var keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place null in the cache keyValueCache.GetValue("bar"); // Place a non null value in the cahce // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.AreEqual("foo", result); Assert.AreEqual(i, 2); }
public void TestFetch__KeyNotPresent() { object key = new object(); object value = new object(); KeyValueCache <object, object> cache = new KeyValueCache <object, object>(); object fetchedValue = cache.Fetch(key, () => value); Assert.Same(value, fetchedValue); }
public void TestFetch__CreateValueNull() { KeyValueCache <object, object> cache = new KeyValueCache <object, object>(); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { cache.Fetch(new object(), null); }); Assert.Equal("createValue", ex.ParamName); }
public void GetValue_KeyNotInCache_CallValueGetter() { // Arrange Func <string, string> valueGetter = x => "foo"; var keyValueCache = new KeyValueCache <string, string>(valueGetter); // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.AreEqual("foo", result); }
public void GetAndContainsKey() { var i = 0; var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }); Assert.IsFalse(mtc.ContainsKey(1)); Assert.AreEqual(0, i); Assert.IsTrue(mtc.TryGetByKey(1, out string val)); Assert.AreEqual(1, i); Assert.AreEqual("1", val); Assert.IsTrue(mtc.ContainsKey(1)); }
public void GetValue_KeyNotInCache_CallValueGetter() { // Arrange Func <string, string> valueGetter = MockRepository.GenerateStrictMock <Func <string, string> >(); valueGetter.Stub(x => x("bar")).Return("foo"); KeyValueCache <string, string> keyValueCache = new KeyValueCache <string, string>(valueGetter); // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.Equal("foo", result); }
public void Concurrency() { Policy.CachePolicyManagerTest.TestSetUp(); var l = new object(); int i = 0; var mtc = new KeyValueCache <int, string>((key) => { lock (l) { i++; } TestContext.WriteLine($"GetValue {key} [{System.Threading.Thread.CurrentThread.ManagedThreadId}]"); System.Threading.Thread.Sleep(20); return(key.ToString()); }); // Set an execution context. var tasks = new Task[13]; tasks[0] = Task.Run(() => Timer(0, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[1] = Task.Run(() => Timer(1, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[2] = Task.Run(() => Timer(2, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[3] = Task.Run(() => Timer(3, 3, () => { Assert.AreEqual(mtc[3], "3"); })); tasks[4] = Task.Run(() => Timer(4, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[5] = Task.Run(() => Timer(5, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[6] = Task.Run(() => Timer(6, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[7] = Task.Run(() => Timer(7, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[8] = Task.Run(() => Timer(8, 3, () => { Assert.AreEqual(mtc[3], "3"); })); tasks[9] = Task.Run(() => Timer(9, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[10] = Task.Run(() => Timer(10, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[11] = Task.Run(() => Timer(11, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[12] = Task.Run(() => Timer(12, 3, () => { Assert.AreEqual(mtc[3], "3"); })); Task.WaitAll(tasks); Assert.AreEqual(3, i); TestContext.WriteLine("Round 2"); tasks = new Task[13]; tasks[0] = Task.Run(() => Timer(0, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[1] = Task.Run(() => Timer(1, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[2] = Task.Run(() => Timer(2, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[3] = Task.Run(() => Timer(3, 3, () => { Assert.AreEqual(mtc[3], "3"); })); tasks[4] = Task.Run(() => Timer(4, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[5] = Task.Run(() => Timer(5, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[6] = Task.Run(() => Timer(6, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[7] = Task.Run(() => Timer(7, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[8] = Task.Run(() => Timer(8, 3, () => { Assert.AreEqual(mtc[3], "3"); })); tasks[9] = Task.Run(() => Timer(9, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[10] = Task.Run(() => Timer(10, 1, () => { Assert.AreEqual(mtc[1], "1"); })); tasks[11] = Task.Run(() => Timer(11, 2, () => { Assert.AreEqual(mtc[2], "2"); })); tasks[12] = Task.Run(() => Timer(12, 3, () => { Assert.AreEqual(mtc[3], "3"); })); Task.WaitAll(tasks); Assert.AreEqual(3, i); }
public void GetValue_CallGetTwiceValueGetterReturnsNull_CallGetterTwice() { // Arrange Func <string, string> valueGetter = MockRepository.GenerateStrictMock <Func <string, string> >(); valueGetter.Stub(x => x("bar")).Return(null).Repeat.Twice(); KeyValueCache <string, string> keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place null in the cache // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.Equal(null, result); valueGetter.VerifyAllExpectations(); }
public void GetValue_CallGetTwice_OnlyCallValueGetterOnce() { // Arrange Func <string, string> valueGetter = MockRepository.GenerateStrictMock <Func <string, string> >(); valueGetter.Stub(x => x("bar")).Return("foo").Repeat.Once(); KeyValueCache <string, string> keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place the value in the cache // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.AreEqual("foo", result); valueGetter.VerifyAllExpectations(); }
public void TestFetch__KeyPresent() { object key = new object(); object value = new object(); KeyValueCache <object, object> cache = new KeyValueCache <object, object>(); cache.Fetch(key, () => value); bool called2ndTime = false; object fetchedValue = cache.Fetch(key, delegate { called2ndTime = true; return(null); }); Assert.Same(value, fetchedValue); Assert.False(called2ndTime); }
public void GetValue_CallGetMultipleTimesFirstGetterReturnsNull_CacheFirstNonNullValue() { // Arrange Func <string, string> valueGetter = MockRepository.GenerateStrictMock <Func <string, string> >(); valueGetter.Stub(x => x("bar")).Return(null).Repeat.Once(); KeyValueCache <string, string> keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place null in the cache valueGetter.Stub(x => x("bar")).Return("foo").Repeat.Once(); keyValueCache.GetValue("bar"); // Place a non null value in the cahce // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.Equal("foo", result); valueGetter.VerifyAllExpectations(); }
public void GetValue_CallGetTwiceValueGetterReturnsNull_CallGetterTwice() { // Arrange int i = 0; Func <string, string> valueGetter = x => { i++; return(null); }; var keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place null in the cache // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.AreEqual(null, result); Assert.AreEqual(i, 2); }
public void GetValue_CallGetTwice_OnlyCallValueGetterOnce() { // Arrange int i = 0; Func <string, string> valueGetter = x => { i++; return("foo"); }; var keyValueCache = new KeyValueCache <string, string>(valueGetter); keyValueCache.GetValue("bar"); // Place the value in the cache // Act string result = keyValueCache.GetValue("bar"); // Assert Assert.AreEqual("foo", result); Assert.AreEqual(i, 1); }
private string CalcAtomStr(string Function, List <string> Prev, List <string> Curr, List <string> Next) { string ResultStr = Function; if (RegPattern.IsMatch(Function)) { MatchCollection mcs = RegPattern.Matches(Function); for (int i = 0; i < mcs.Count; i++) { Match m = mcs[i]; string key = m.Groups[1].Value.ToLower(); string val = m.Groups[2].Value.Trim(); List <string> CurMap = new List <string>(); string KeyIn = Curr[0] + "[]"; switch (key) { case "currentnote": CurMap = Curr; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("currentnote", CurMap[0]); break; case "prevnote": CurMap = Prev; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("prevnote", CurMap[0]); break; case "nextnote": CurMap = Next; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("nextnote", CurMap[0]); break; default: continue; } string KeyAtom = ""; // if (KeyIn == "") return ""; if (KeyIn != "") { if (KeyValueCache.ContainsKey(KeyIn)) { KeyAtom = KeyValueCache[KeyIn]; } else { int idx = 0; if (val != "") { val = val.Replace("n", (CurMap.Count - 1).ToString()); try { idx = (int)Calcer.Compute(val, ""); } catch {; } } if (idx < CurMap.Count) { KeyAtom = CurMap[idx]; try { KeyValueCache.Add(KeyIn, KeyAtom); } catch {; } } } } ResultStr = ResultStr.Replace(m.Groups[0].Value, KeyAtom); } return(ResultStr); } else { return(""); } }
public void PolicyManager() { Policy.CachePolicyManagerTest.TestSetUp(); var i = 0; var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }, "KeyValueCacheTest"); Assert.IsNotNull(mtc.PolicyKey); var policy = new DailyCachePolicy(); CachePolicyManager.Current.Set(mtc.PolicyKey, policy); var policy2 = mtc.GetPolicy(); Assert.IsNotNull(policy2); Assert.AreSame(policy, policy2); var pa = CachePolicyManager.Current.GetPolicies(); Assert.AreEqual(2, pa.Length); // Check the internal nocachepolicy. var p0 = pa.Where(x => x.Key.StartsWith("KeyValueCacheTest_")).SingleOrDefault(); Assert.IsNotNull(p0); Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value); // Check the default policy for type. var p1 = pa.Where(x => x.Key == "KeyValueCacheTest").SingleOrDefault(); Assert.IsNotNull(p1); Assert.IsInstanceOf(typeof(DailyCachePolicy), p1.Value); // Get (add) new item to cache. var s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(1, i); // No new globally managed policies should have been created. pa = CachePolicyManager.Current.GetPolicies(); Assert.AreEqual(2, pa.Length); // Check policy for item is DailyCachePolicy but has its own instance. policy2 = mtc.GetPolicyByKey(1); Assert.IsNotNull(policy2); Assert.IsInstanceOf(typeof(DailyCachePolicy), policy2); Assert.AreNotSame(policy, policy2); // There should be no policy where item not found. Assert.IsNull(mtc.GetPolicyByKey(2)); // Flush cache where not expired. mtc.Flush(); s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(1, i); // Force flush; should reload cache after. CachePolicyManager.Current.ForceFlush(); s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(2, i); }
public override async void Run(DiscordClient discord) { var channel = await discord.GetChannelAsync(BotDetails.BotFeedChannel); var lastMessage = (await channel.GetMessagesAsync(1)).FirstOrDefault(); var matchId = Regex.Match(lastMessage.Content, $"[0-9]+$").Value; if (matchId == null || matchId == string.Empty) { do { var lastNMessage = (await channel.GetMessagesAsync(1, before: lastMessage.Id)).FirstOrDefault(); matchId = Regex.Match(lastNMessage.Content, $"[0-9]+$").Value; lastMessage = lastNMessage; } while (matchId == null || matchId == string.Empty); } var matchDetailsString = KeyValueCache.Get(matchId); if (matchDetailsString == null) { matchDetailsString = await NetComm.GetResponseOfURL($"matches/{matchId}", _httpClient); KeyValueCache.Put(matchId, matchDetailsString); } dynamic matchDetails = JsonToFrom.Deserialize <dynamic>(matchDetailsString); long matchEndTime = (long)matchDetails.start_time + (long)matchDetails.duration; double diffInTime = GetDiffInDays(matchEndTime); foreach (var player in PlayerConfiguration.Players) { var recentMatches = JsonToFrom.Deserialize <dynamic>(await NetComm.GetResponseOfURL($"players/{player.SteamId}/matches?date={diffInTime}", _httpClient)); foreach (var recentMatch in recentMatches) { string recentMatchId = recentMatch.match_id; if (!_matchMap.ContainsKey(recentMatchId)) { _matchMap[recentMatchId] = new List <Player>(); } _matchMap[recentMatchId].Add(player); } } var _matchMapOrdered = _matchMap.OrderBy(x => x.Key); foreach (var matchMap in _matchMapOrdered) { if (KeyValueCache.Get(matchMap.Key) == null) { KeyValueCache.Put(matchMap.Key, await NetComm.GetResponseOfURL($"matches/{matchMap.Key}", _httpClient)); } dynamic parsedMatch = JsonToFrom.Deserialize <dynamic>(KeyValueCache.Get(matchMap.Key)); string matchString = string.Empty; foreach (var player in matchMap.Value) { matchString += GenerateMatchString(parsedMatch, GetNormalOrRankedMatch(parsedMatch), player); } matchString += GenerateDotaBuffLink(matchMap.Key); channel.SendMessageAsync(matchString); Program.Logger.Log($"Offline Tracker logged match {matchMap.Key}."); } }