Example #1
0
        private void ReduceSubscriptions(string pTopics)
        {
            if (string.IsNullOrEmpty(pTopics))
            {
                return;
            }

            TheBaseAssets.MySYSLOG.WriteToLog(296, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("QueuedSender", $"Unsubscribe request of Topics={pTopics} from {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l4_Message));

            lock (CombineSubscriptionLock)
            {
                string[] subs = pTopics.Split(';');
                for (int i = 0; i < subs.Length; i++)
                {
                    string tRealScope = TheBaseAssets.MyScopeManager.GetRealScopeIDFromTopic(subs[i], out string tTopicName);  //Low Frequency
                    if (string.IsNullOrEmpty(tTopicName))
                    {
                        continue;
                    }
                    foreach (string t in MySubscriptions.TheKeys)
                    {
                        if (!string.IsNullOrEmpty(tRealScope))
                        {
                            if (tRealScope.Equals(MySubscriptions.MyRecords[t]?.RScopeID) && tTopicName.Equals(MySubscriptions.MyRecords[t]?.Topic))
                            {
                                MySubscriptions.RemoveAnItemByKey(t, null);
                                break;
                            }
                        }
                        else
                        {
                            if (subs[i].Equals(t))
                            {
                                MySubscriptions.RemoveAnItemByKey(t, null);
                                break;
                            }
                        }
                    }
                }
            }
        }
        public void RemoveAnItemByKeyFromTheMirrorCacheTest()
        {
            #region ASSEMBLE

            int totalCandidates            = 5000;
            int indexMiddle                = totalCandidates / 2;
            int indexCurrent               = 0;
            var random                     = new Random();
            var data                       = Enumerable.Range(1, totalCandidates).OrderBy(i => random.Next(1, totalCandidates));
            ManualResetEventSlim gate      = new ManualResetEventSlim();
            CountdownEvent       countdown = new CountdownEvent(1);
            TheMirrorCache <TheStorageEngineTSM> mirror;
            TheStorageEngineTSM        tsmCurrent = null;
            TheStorageEngineTSM        tsmMiddle  = null;
            TheStorageEngineTSM        tsmRemoved = null;
            TheStorageEngineTSM        tsmMatch   = null;
            List <TheStorageEngineTSM> TSMs       = new List <TheStorageEngineTSM>();
            List <TheStorageEngineTSM> myRecords  = new List <TheStorageEngineTSM>();

            // Build the collection of TSMs and cache the middle one
            foreach (var payload in data)
            {
                tsmCurrent = new TheStorageEngineTSM()
                {
                    cdeMID     = Guid.NewGuid(),
                    TXTPattern = payload.ToString()
                };
                TSMs.Add(tsmCurrent);
                if ((indexCurrent++ >= indexMiddle) && (tsmMiddle == null))
                {
                    tsmMiddle = tsmCurrent;
                }
            }
            if (tsmMiddle == null)
            {
                Assert.Fail("Unable to cache the middle TSM!");
            }

            // Spin up your mirror
            mirror = new TheMirrorCache <TheStorageEngineTSM>(10)
            {
                CacheStoreInterval       = 1,
                IsStoreIntervalInSeconds = true,
                IsCachePersistent        = true,
                UseSafeSave      = true,
                AllowFireUpdates = true,
            };

            // Add your items...
            mirror.AddItems(TSMs, response =>
            {
                myRecords = response;
                countdown.Signal();
            });

            countdown.Wait();
            if (TSMs.Count != myRecords.Count)
            {
                Assert.Fail("Not all test records were not added successfully.");
            }

            #endregion

            #region ACT

            // Attempt to remove your middle item
            Task.Factory.StartNew(() =>
            {
                mirror.RemoveAnItemByKey(tsmMiddle.cdeMID.ToString(), payload =>
                {
                    tsmRemoved = payload;
                    gate.Set();
                });
            });

            // Wait for response
            gate.Reset();
            gate.Wait(30000);
            if (tsmRemoved == null)
            {
                Assert.Fail("Unable to remove item by ID!");
            }

            // Attempt to retrieve your middle item
            tsmMatch = mirror.GetEntryByID(tsmMiddle.cdeMID);

            mirror?.Dispose();

            #endregion

            #region ASSERT

            Assert.IsTrue(tsmMatch == null);

            #endregion
        }