public void ExpiringDictionary_FindAll_Returns_All_Matching_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 10)) {
                map.Add(1, "1a");
                map.Add(7, "1b");
                map.Add(3, "2a");

                var matches = map.FindAll(r => r.StartsWith("1"));
                Assert.AreEqual(2, matches.Count);
                Assert.IsTrue(matches.Contains("1a"));
                Assert.IsTrue(matches.Contains("1b"));
            }
        }
        public void ExpiringDictionary_RefreshAll_Refreshes_All_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 10)) {
                map.Add(22, "1");
                map.Add(23, "2");

                _Clock.AddMilliseconds(10);
                map.RefreshAll();

                HeartbeatTick();
                Assert.AreEqual(2, map.Count);
            }
        }
Exemple #3
0
 public static void EnqueueMigration(int id, MigrationData data)
 {
     lock (MigrationLock)
     {
         MigrationQueue.Add(id, data);
     }
 }
        public void ExpiringDictionary_SnapshotAndRefresh_Refreshes_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "A");
                map.Add(2, "B");

                _Clock.AddMilliseconds(10);
                var snapshot = map.SnapshotAndRefresh();
                HeartbeatTick();

                Assert.AreEqual(2, map.Count);

                Assert.AreEqual(2, snapshot.Length);
                Assert.IsTrue(snapshot.Contains(new KeyValuePair <int, string>(1, "A")));
                Assert.IsTrue(snapshot.Contains(new KeyValuePair <int, string>(2, "B")));
            }
        }
        public void ExpiringDictionary_FindAllAndRefresh_Refreshes_Matching_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 10)) {
                map.Add(8, "1a");
                map.Add(7, "1b");
                map.Add(6, "2a");

                _Clock.AddMilliseconds(10);
                var matches = map.FindAllAndRefresh(r => r.StartsWith("1"));
                Assert.AreEqual(2, matches.Count);
                Assert.IsTrue(matches.Contains("1a"));
                Assert.IsTrue(matches.Contains("1b"));

                HeartbeatTick();
                Assert.AreEqual(2, map.Count);
            }
        }
Exemple #6
0
 public static bool wincache_ucache_add(String key, object value, int ttl)
 {
     try
     {
         if (ttl == 0)
         {
             DUserTables.Add(key, value);
         }
         else
         {
             DUserTables.Add(key, value, new TimeSpan(0, 0, ttl));
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Exemple #7
0
 public static bool wincache_ucache_add(ExpiringDictionary <string, object> _DictionaryTable, String key, object value, int ttl)
 {
     try
     {
         if (ttl == 0)
         {
             _DictionaryTable.Add(key, value);
         }
         else
         {
             _DictionaryTable.Add(key, value, new TimeSpan(0, 0, ttl));
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        public void ExpiringDictionary_Will_Check_Items_For_Expiry_After_MillisecondsBetweenChecks_Has_Passed()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "Hello");
                HeartbeatTick();

                _Clock.AddMilliseconds(11);
                HeartbeatTick();
                Assert.AreEqual(0, map.Count);
            }
        }
        public void ExpiringDictionary_FindAndRefresh_Returns_First_Matching_Item_After_Refreshing_It()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 10)) {
                map.Add(8, "1");

                _Clock.AddMilliseconds(10);
                Assert.AreEqual("1", map.FindAndRefresh(r => r == "1"));

                HeartbeatTick();
                Assert.AreEqual(1, map.Count);
            }
        }
        public static void DropBelow(double level)
        {
            lock (entries)
            {
                ExpiringDictionary<int, LogEntry> afterward = new ExpiringDictionary<int, LogEntry>();
                foreach (KeyValuePair<int, LogEntry> kvp in entries)
                    if (kvp.Value.level >= level)
                        afterward.Add(kvp);

                entries = afterward;
            }
        }
        public void ExpiringDictionary_FindAll_Does_Not_Refresh_Matching_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 10)) {
                map.Add(8, "1");

                _Clock.AddMilliseconds(10);
                Assert.AreEqual(1, map.FindAll(r => r == "1").Count);

                HeartbeatTick();
                Assert.AreEqual(0, map.Count);
            }
        }
        public void ExpiringDictionary_Snapshot_Does_Not_Refresh_Items()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "1");

                _Clock.AddMilliseconds(10);
                map.Snapshot();
                HeartbeatTick();

                Assert.AreEqual(0, map.Count);
            }
        }
        public void ExpiringDictionary_UpsertRange_Does_Not_Refresh()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "B");
                HeartbeatTick();

                _Clock.AddMilliseconds(10);
                map.UpsertRange(new string[] { "N" }, (unused) => 1);
                HeartbeatTick();

                Assert.AreEqual(0, map.Count);
            }
        }
        public void ExpiringDictionary_GetAndRefreshOrCreate_Refreshes_If_Key_Exists()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(2, "kj");
                HeartbeatTick();

                _Clock.AddMilliseconds(10);
                Assert.AreEqual("kj", map.GetAndRefreshOrCreate(2, null));

                HeartbeatTick();
                Assert.AreEqual(1, map.Count);
            }
        }
        public void ExpiringDictionary_GetForKeyAndRefresh_Refreshes()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "X");
                HeartbeatTick();

                _Clock.AddMilliseconds(10);
                Assert.AreEqual("X", map.GetForKeyAndRefresh(1));

                HeartbeatTick();
                Assert.AreEqual(1, map.Count);
            }
        }
        public void ExpiringDictionary_Will_Not_Expire_Items_Before_ExpireMilliseconds_Has_Passed()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "Hello");
                HeartbeatTick();

                _Clock.AddMilliseconds(9);
                HeartbeatTick();
                Assert.AreEqual(1, map.Count);

                _Clock.AddMilliseconds(1);
                HeartbeatTick();
                Assert.AreEqual(0, map.Count);
            }
        }
        public void ExpiringDictionary_UpsertRangeAndRefresh_Does_Refresh()
        {
            using (var map = new ExpiringDictionary <int, string>(10, 1)) {
                map.Add(1, "B");
                HeartbeatTick();

                _Clock.AddMilliseconds(10);
                map.UpsertRangeAndRefresh(new string[] { "1", "2" }, (val) => int.Parse(val));
                HeartbeatTick();

                Assert.AreEqual(2, map.Count);
                Assert.AreEqual("1", map.GetForKey(1));
                Assert.AreEqual("2", map.GetForKey(2));
            }
        }
Exemple #18
0
        public static void DropBelow(double level)
        {
            lock (entries)
            {
                ExpiringDictionary <int, LogEntry> afterward = new ExpiringDictionary <int, LogEntry>();
                foreach (KeyValuePair <int, LogEntry> kvp in entries)
                {
                    if (kvp.Value.level >= level)
                    {
                        afterward.Add(kvp);
                    }
                }

                entries = afterward;
            }
        }
        private void AddOffence(OffenceType offence, OffenceValue value)
        {
            if (TotalOffenceValue() >= ServerConstants.MaxOffenceValue)
            {
                //Todo add ban/kick or whatever
                ServerConsole.Warning("Client found with high offence values: " + Enum.GetName(typeof(OffenceType), offence));
            }

            if (OffenceList.ContainsKey(offence))
            {
                OffenceList[offence] += (int)value;
            }
            else
            {
                OffenceList.Add(offence, (int)value);
            }
        }
Exemple #20
0
 public static bool AllowConnection(string Ip)
 {
     if (TempBanList.ContainsKey(Ip))
     {
         return(false);
     }
     if (ConnectionCount.ContainsKey(Ip))
     {
         ConnectionCount[Ip] = ConnectionCount[Ip] + 1;
         if (ConnectionCount[Ip] >= FLOOD_MAX)
         {
             ServerConsole.Warning("Detected a TCP flood on " + Ip);
             TempBanList.Add(Ip, true);
             return(false);
         }
     }
     else
     {
         ConnectionCount.Add(Ip, 1);
     }
     return(true);
 }
        public void ExpiringDictionary_Count_Returns_Count_Of_Items()
        {
            _Map.Add(1, "1");
            Assert.AreEqual(1, _Map.Count);

            _Map.Add(2, "1");
            Assert.AreEqual(2, _Map.Count);
        }