Exemple #1
0
        public void AddEventData(int beaconID, long timestamp, string data)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(GetType().Name + " AddEventData(sn=" + beaconID + ", timestamp=" + timestamp + ", data='" + data + "')");
            }
            // get a reference to the cache entry
            var entry = GetCachedEntryOrInsert(beaconID);

            // add event data for that beacon
            var record = new BeaconCacheRecord(timestamp, data);

            try
            {
                entry.Lock();
                entry.AddEventData(record);
            }
            finally
            {
                entry.Unlock();
            }

            // update cache stats
            Interlocked.Add(ref cacheSizeInBytes, record.DataSizeInBytes);

            // handle event listeners
            OnDataAdded();
        }
Exemple #2
0
        public void AddEventData(BeaconKey beaconKey, long timestamp, string data)
        {
            if (logger.IsDebugEnabled)
            {
                var logString = new StringBuilder(GetType().Name)
                                .Append(" AddEventData(sn=").Append(beaconKey.BeaconId.ToInvariantString())
                                .Append(", seq=").Append(beaconKey.BeaconSeqNo.ToInvariantString())
                                .Append(", timestamp=").Append(timestamp.ToInvariantString())
                                .Append(", data='").Append(data).Append("')").ToString();

                logger.Debug(logString);
            }
            // get a reference to the cache entry
            var entry = GetCachedEntryOrInsert(beaconKey);

            // add event data for that beacon
            var record = new BeaconCacheRecord(timestamp, data);

            try
            {
                entry.Lock();
                entry.AddEventData(record);
            }
            finally
            {
                entry.Unlock();
            }

            // update cache stats
            Interlocked.Add(ref cacheSizeInBytes, record.DataSizeInBytes);

            // handle event listeners
            OnDataAdded();
        }
Exemple #3
0
        public void AddingEventData()
        {
            // given
            var dataOne = new BeaconCacheRecord(0L, "foo");
            var dataTwo = new BeaconCacheRecord(1L, "bar");

            var target = new BeaconCacheEntry();

            // when adding first record
            target.AddEventData(dataOne);

            // then
            Assert.That(target.ActionData, Is.Empty);
            Assert.That(target.EventData, Is.EqualTo(new object[] { dataOne }));
            Assert.That(target.EventDataBeingSent, Is.Null);
            Assert.That(target.ActionDataBeingSent, Is.Null);

            // and when adding second record
            target.AddEventData(dataTwo);

            // then
            Assert.That(target.ActionData, Is.Empty);
            Assert.That(target.EventData, Is.EqualTo(new object[] { dataOne, dataTwo }));
            Assert.That(target.EventDataBeingSent, Is.Null);
            Assert.That(target.ActionDataBeingSent, Is.Null);
        }
Exemple #4
0
        public void GetChunkMarksRetrievedData()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when retrieving data
            var obtained = target.GetChunk("prefix", 1024, '&');

            // then
            Assert.That(obtained, Is.EqualTo("prefix&" + dataOne.Data + "&" + dataFour.Data + "&" + dataTwo.Data + "&" + dataThree.Data));
            // and all of them are marked
            Assert.That(dataOne.IsMarkedForSending, Is.True);
            Assert.That(dataTwo.IsMarkedForSending, Is.True);
            Assert.That(dataThree.IsMarkedForSending, Is.True);
            Assert.That(dataFour.IsMarkedForSending, Is.True);
        }
Exemple #5
0
        public void ResetDataMarkedForSendingMovesPreviouslyCopiedDataBack()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when data is reset
            target.ResetDataMarkedForSending();

            // then
            Assert.That(target.EventData, Is.EqualTo(new[] { dataOne, dataFour }));
            Assert.That(target.ActionData, Is.EqualTo(new[] { dataTwo, dataThree }));
            Assert.That(target.EventDataBeingSent, Is.Null);
            Assert.That(target.ActionDataBeingSent, Is.Null);
        }
Exemple #6
0
        public void RemoveOldestRecordsComparesTopActionAndEventDataAndRemovesOldest()
        {
            // given
            var dataOne   = new BeaconCacheRecord(1000, "One");
            var dataTwo   = new BeaconCacheRecord(1100L, "Two");
            var dataThree = new BeaconCacheRecord(950L, "Three");
            var dataFour  = new BeaconCacheRecord(1200L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);
            target.AddEventData(dataFour);

            // when
            var obtained = target.RemoveOldestRecords(1);

            // then
            Assert.That(obtained, Is.EqualTo(1));
            Assert.That(target.ActionData, Is.EqualTo(new[] { dataTwo, dataThree }));
            Assert.That(target.EventData, Is.EqualTo(new[] { dataFour }));

            // when removing the next two
            obtained = target.RemoveOldestRecords(2);

            // then
            Assert.That(obtained, Is.EqualTo(2));
            Assert.That(target.ActionData, Is.Empty);
            Assert.That(target.EventData, Is.EqualTo(new[] { dataFour }));
        }
Exemple #7
0
        public void ResetDataMarkedForSendingResetsMarkedForSendingFlag()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when data is retrieved
            target.GetChunk("", 1024, '&');

            // then all records are marked for sending
            Assert.That(dataOne.IsMarkedForSending, Is.True);
            Assert.That(dataTwo.IsMarkedForSending, Is.True);
            Assert.That(dataThree.IsMarkedForSending, Is.True);
            Assert.That(dataFour.IsMarkedForSending, Is.True);

            // and when
            target.ResetDataMarkedForSending();

            // then
            Assert.That(dataOne.IsMarkedForSending, Is.False);
            Assert.That(dataTwo.IsMarkedForSending, Is.False);
            Assert.That(dataThree.IsMarkedForSending, Is.False);
            Assert.That(dataFour.IsMarkedForSending, Is.False);
        }
Exemple #8
0
        public void GetChunksTakesSizeIntoAccount()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when requesting first chunk
            var obtained = target.GetChunk("prefix", 1, '&');

            // then only prefix is returned, since "prefix".length > maxSize (=1)
            Assert.That(obtained, Is.EqualTo("prefix"));

            // and when retrieving something which is one character longer than "prefix"
            obtained = target.GetChunk("prefix", "prefix".Length, '&');

            // then based on the algorithm prefix and first element are retrieved
            Assert.That(obtained, Is.EqualTo("prefix&One"));

            // and when retrieving another chunk
            obtained = target.GetChunk("prefix", "prefix&One".Length, '&');

            // then
            Assert.That(obtained, Is.EqualTo("prefix&One&Four"));
        }
Exemple #9
0
        public void RemoveOldestRecordsDoesNotRemoveAnythingFromEventAndActionsBeingSent()
        {
            // given
            var dataOne   = new BeaconCacheRecord(1000L, "One");
            var dataTwo   = new BeaconCacheRecord(1500L, "Two");
            var dataThree = new BeaconCacheRecord(2000L, "Three");
            var dataFour  = new BeaconCacheRecord(2500L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when
            var obtained = target.RemoveOldestRecords(10000);

            // then
            Assert.That(obtained, Is.EqualTo(0));
            Assert.That(target.EventDataBeingSent, Is.EqualTo(new[] { dataOne, dataFour }));
            Assert.That(target.ActionDataBeingSent, Is.EqualTo(new[] { dataTwo, dataThree }));
        }
        public void SameInstancesAreEqual()
        {
            // given
            var target = new BeaconCacheRecord(0L, "abc");

            // then
            Assert.That(target.Equals(target), Is.True);
        }
        public void NullIsNotEqual()
        {
            // given
            var target = new BeaconCacheRecord(0L, "abc");

            // then
            Assert.That(target.Equals(null), Is.False);
        }
        public void ADifferentTypeIsNotConsideredEqual()
        {
            // given
            var target = new BeaconCacheRecord(0L, "abc");

            // then
            Assert.That(target.Equals("abc"), Is.False);
        }
        public void SameInstancesHaveSameHashCode()
        {
            // given
            var target = new BeaconCacheRecord(1234L, "abc");
            var other  = target;

            // then
            Assert.That(target.GetHashCode(), Is.EqualTo(other.GetHashCode()));
        }
        public void TwoInstancesAreNotEqualIfDataDiffers()
        {
            // given
            var target = new BeaconCacheRecord(1234L, "abc");
            var other  = new BeaconCacheRecord(1234L, "abcd");

            // then
            Assert.That(target.Equals(other), Is.False);
        }
        public void TwoInstancesAreNotEqualIfMarkedForSendingDiffers()
        {
            // given
            var target = new BeaconCacheRecord(1234L, "abc");
            var other  = new BeaconCacheRecord(1234L, "abc");

            other.MarkForSending();

            // then
            Assert.That(target.Equals(other), Is.False);
        }
Exemple #16
0
        public void GetChunkGetsChunksFromEventDataBeforeActionData()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when getting data to send
            var obtained = target.GetChunk("a", 2, '&');

            // then it's the first event data
            Assert.That(obtained, Is.EqualTo("a&" + dataOne.Data));

            // and when removing already sent data and getting next chunk
            target.RemoveDataMarkedForSending();
            obtained = target.GetChunk("a", 2, '&');

            // then it's second event data
            Assert.That(obtained, Is.EqualTo("a&" + dataFour.Data));

            // and when removing already sent data and getting next chunk
            target.RemoveDataMarkedForSending();
            obtained = target.GetChunk("a", 2, '&');

            // then it's the first action data
            Assert.That(obtained, Is.EqualTo("a&" + dataTwo.Data));

            // and when removing already sent data and getting next chunk
            target.RemoveDataMarkedForSending();
            obtained = target.GetChunk("a", 2, '&');

            // then it's the second action data
            Assert.That(obtained, Is.EqualTo("a&" + dataThree.Data));

            // and when removing already sent data and getting next chunk
            target.RemoveDataMarkedForSending();
            obtained = target.GetChunk("a", 2, '&');

            // then we get an empty string, since all chunks were sent & deleted
            Assert.That(obtained, Is.Empty);
        }
        public void NotEqualInstancesHaveDifferentHashCode()
        {
            // given
            var target     = new BeaconCacheRecord(1234L, "abc");
            var otherOne   = new BeaconCacheRecord(4321L, "abc");
            var otherTwo   = new BeaconCacheRecord(1234L, "abcd");
            var otherThree = new BeaconCacheRecord(1234L, "abcd");

            otherThree.MarkForSending();

            // then
            Assert.That(target.GetHashCode(), Is.Not.EqualTo(otherOne.GetHashCode()));
            Assert.That(target.GetHashCode(), Is.Not.EqualTo(otherTwo.GetHashCode()));
            Assert.That(target.GetHashCode(), Is.Not.EqualTo(otherThree.GetHashCode()));
        }
        public void TwoInstancesAreEqualIfFieldsAreEqual()
        {
            // given
            var target = new BeaconCacheRecord(1234L, "abc");
            var other  = new BeaconCacheRecord(1234L, "abc");

            // then
            Assert.That(target.Equals(other), Is.True);

            // and when setting send flag
            target.MarkForSending();
            other.MarkForSending();

            // then
            Assert.That(target.Equals(other), Is.True);
        }
        public void TwoEqualInstancesHaveSameHashCode()
        {
            // given
            var target = new BeaconCacheRecord(1234L, "abc");
            var other  = new BeaconCacheRecord(1234L, "abc");

            // then
            Assert.That(target.GetHashCode(), Is.EqualTo(other.GetHashCode()));

            // and when marking both for sending
            target.MarkForSending();
            other.MarkForSending();

            // then
            Assert.That(target.GetHashCode(), Is.EqualTo(other.GetHashCode()));
        }
Exemple #20
0
        public void HasDataForSendingReturnsFalseIfDataWasNotCopied()
        {
            // given
            var dataOne = new BeaconCacheRecord(1000L, "One");
            var dataTwo = new BeaconCacheRecord(1500L, "Two");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataTwo);

            // when
            var obtained = target.HasDataToSend;

            // then
            Assert.That(obtained, Is.False);
        }
Exemple #21
0
        public void HasDataForSendingReturnsTrueIfActionDataWasAddedBeforeCopying()
        {
            // given
            var record = new BeaconCacheRecord(1000L, "One");

            var target = new BeaconCacheEntry();

            target.AddActionData(record);

            target.CopyDataForSending();

            // when
            var obtained = target.HasDataToSend;

            // then
            Assert.That(obtained, Is.True);
        }
Exemple #22
0
        public void NeedsDataCopyBeforeSendingGivesTrueBeforeDataIsCopied()
        {
            // given
            var target = new BeaconCacheEntry();

            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");


            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            // when, then
            Assert.That(target.NeedsDataCopyBeforeSending, Is.True);
        }
        public void MarkForSending()
        {
            // given
            var target = new BeaconCacheRecord(0L, "abc");

            // then a newly created record is not marked for sending
            Assert.That(target.IsMarkedForSending, Is.False);

            // and when explicitly marked for sending
            target.MarkForSending();

            // then
            Assert.That(target.IsMarkedForSending, Is.True);

            // and when the sending mark is removed
            target.UnsetSending();

            // then
            Assert.That(target.IsMarkedForSending, Is.False);
        }
        public void NeedsDataCopyBeforeChunkingGivesFalseAfterDataHasBeenCopied()
        {
            // given
            var target = new BeaconCacheEntry();

            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");


            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForChunking();

            // when, then
            Assert.That(target.NeedsDataCopyBeforeChunking, Is.False);
        }
Exemple #25
0
        public void RemoveRecordsOlderThanRemovesRecordsFromEventData()
        {
            // given
            var dataOne   = new BeaconCacheRecord(4000L, "One");
            var dataTwo   = new BeaconCacheRecord(3000L, "Two");
            var dataThree = new BeaconCacheRecord(2000L, "Three");
            var dataFour  = new BeaconCacheRecord(1000L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataTwo);
            target.AddEventData(dataThree);
            target.AddEventData(dataFour);

            // when removing everything older than 3000
            var obtained = target.RemoveRecordsOlderThan(dataTwo.Timestamp);

            // then
            Assert.That(obtained, Is.EqualTo(2)); // two were removed
            Assert.That(target.EventData, Is.EqualTo(new[] { dataOne, dataTwo }));
        }
Exemple #26
0
        public void RemoveOldestRecordsRemovesEventDataIfActionDataIsEmpty()
        {
            // given
            var dataOne   = new BeaconCacheRecord(4000L, "One");
            var dataTwo   = new BeaconCacheRecord(3000L, "Two");
            var dataThree = new BeaconCacheRecord(2000L, "Three");
            var dataFour  = new BeaconCacheRecord(1000L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataTwo);
            target.AddEventData(dataThree);
            target.AddEventData(dataFour);

            // when
            var obtained = target.RemoveOldestRecords(2);

            // then
            Assert.That(obtained, Is.EqualTo(2)); // two were removed
            Assert.That(target.EventData, Is.EqualTo(new[] { dataThree, dataFour }));
        }
Exemple #27
0
        public void GetTotalNumberOfBytesCountsAddedRecordBytes()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            // when getting total number of bytes on an empty entry, then
            Assert.That(target.TotalNumBytes, Is.EqualTo(0L));

            // and when adding first entry
            target.AddActionData(dataOne);

            // then
            Assert.That(target.TotalNumBytes, Is.EqualTo(dataOne.DataSizeInBytes));

            // and when adding next entry
            target.AddEventData(dataTwo);

            // then
            Assert.That(target.TotalNumBytes, Is.EqualTo(dataOne.DataSizeInBytes + dataTwo.DataSizeInBytes));

            // and when adding next entry
            target.AddEventData(dataThree);

            // then
            Assert.That(target.TotalNumBytes, Is.EqualTo(dataOne.DataSizeInBytes + dataTwo.DataSizeInBytes + dataThree.DataSizeInBytes));

            // and when adding next entry
            target.AddActionData(dataFour);

            // Assert.That
            Assert.That(target.TotalNumBytes, Is.EqualTo(dataOne.DataSizeInBytes + dataTwo.DataSizeInBytes
                                                         + dataThree.DataSizeInBytes + dataFour.DataSizeInBytes));
        }
Exemple #28
0
        public void RemoveOldestRecordsStopsIfListsAreEmpty()
        {
            // given
            var dataOne   = new BeaconCacheRecord(4000L, "One");
            var dataTwo   = new BeaconCacheRecord(3000L, "Two");
            var dataThree = new BeaconCacheRecord(2000L, "Three");
            var dataFour  = new BeaconCacheRecord(1000L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataTwo);
            target.AddEventData(dataThree);
            target.AddEventData(dataFour);

            // when
            var obtained = target.RemoveOldestRecords(100);

            // then
            Assert.That(obtained, Is.EqualTo(4));
            Assert.That(target.EventData, Is.Empty);
            Assert.That(target.ActionData, Is.Empty);
        }
Exemple #29
0
        public void RemoveOldestRecordsRemovesEventDataIfTopEventDataAndActionDataHaveSameTimestamp()
        {
            // given
            var dataOne   = new BeaconCacheRecord(1000, "One");
            var dataTwo   = new BeaconCacheRecord(1100L, "Two");
            var dataThree = new BeaconCacheRecord(dataOne.Timestamp, "Three");
            var dataFour  = new BeaconCacheRecord(dataTwo.Timestamp, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataTwo);
            target.AddActionData(dataThree);
            target.AddActionData(dataFour);

            // when
            var obtained = target.RemoveOldestRecords(1);

            // then
            Assert.That(obtained, Is.EqualTo(1));
            Assert.That(target.ActionData, Is.EqualTo(new[] { dataThree, dataFour }));
            Assert.That(target.EventData, Is.EqualTo(new[] { dataTwo }));
        }
Exemple #30
0
        public void GetChunkGetsAlreadyMarkedData()
        {
            // given
            var dataOne   = new BeaconCacheRecord(0L, "One");
            var dataTwo   = new BeaconCacheRecord(0L, "Two");
            var dataThree = new BeaconCacheRecord(1L, "Three");
            var dataFour  = new BeaconCacheRecord(1L, "Four");

            var target = new BeaconCacheEntry();

            target.AddEventData(dataOne);
            target.AddEventData(dataFour);
            target.AddActionData(dataTwo);
            target.AddActionData(dataThree);

            target.CopyDataForSending();

            // when getting data to send
            var obtained = target.GetChunk("a", 100, '&');

            // then
            Assert.That(obtained, Is.EqualTo("a&One&Four&Two&Three"));
            Assert.That(dataOne.IsMarkedForSending, Is.True);
            Assert.That(dataTwo.IsMarkedForSending, Is.True);
            Assert.That(dataThree.IsMarkedForSending, Is.True);
            Assert.That(dataFour.IsMarkedForSending, Is.True);

            // when getting data to send once more
            obtained = target.GetChunk("a", 100, '&');

            // then
            Assert.That(obtained, Is.EqualTo("a&One&Four&Two&Three"));
            Assert.That(dataOne.IsMarkedForSending, Is.True);
            Assert.That(dataTwo.IsMarkedForSending, Is.True);
            Assert.That(dataThree.IsMarkedForSending, Is.True);
            Assert.That(dataFour.IsMarkedForSending, Is.True);
        }