Esempio n. 1
0
        public void getNextBeaconChunkRetrievesNextChunk()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddActionData(42, 2000L, "z");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when retrieving the first chunk
            var obtained = target.GetNextBeaconChunk(1, "prefix", 10, '&');

            // then
            Assert.That(obtained, Is.EqualTo("prefix&b&jjj"));

            // then
            Assert.That(target.GetActionsBeingSent(1), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a"), new BeaconCacheRecord(1001L, "iii") }));
            var expectedEventRecords = new[] { new BeaconCacheRecord(1000L, "b"), new BeaconCacheRecord(1001L, "jjj") };

            foreach (var record in expectedEventRecords)
            {
                record.MarkForSending();
            }
            Assert.That(target.GetEventsBeingSent(1), Is.EqualTo(expectedEventRecords));
        }
Esempio n. 2
0
        public void RemoveChunkedDataDoesNothingIfCalledWithNonExistingBeaconID()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddActionData(42, 2000L, "z");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when retrieving the first chunk and removing the wrong beacon chunk
            target.GetNextBeaconChunk(1, "prefix", 10, '&');
            target.RemoveChunkedData(2);

            // then
            Assert.That(target.GetActionsBeingSent(1), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a"), new BeaconCacheRecord(1001L, "iii") }));
            var expectedEventRecords = new[] { new BeaconCacheRecord(1000L, "b"), new BeaconCacheRecord(1001L, "jjj") };

            foreach (BeaconCacheRecord record in expectedEventRecords)
            {
                record.MarkForSending();
            }
            Assert.That(target.GetEventsBeingSent(1), Is.EqualTo(expectedEventRecords));
        }
Esempio n. 3
0
        public void RemoveChunkedDataClearsAlreadyRetrievedChunks()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddActionData(42, 2000L, "z");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when retrieving the first chunk and removing retrieved chunks
            var obtained = target.GetNextBeaconChunk(1, "prefix", 10, '&');

            target.RemoveChunkedData(1);

            // then
            Assert.That(obtained, Is.EqualTo("prefix&b&jjj"));

            Assert.That(target.GetActionsBeingSent(1), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a"), new BeaconCacheRecord(1001L, "iii") }));
            Assert.That(target.GetEventsBeingSent(1), Is.Empty);

            // when retrieving the second chunk and removing retrieved chunks
            obtained = target.GetNextBeaconChunk(1, "prefix", 10, '&');
            target.RemoveChunkedData(1);

            // then
            Assert.That(obtained, Is.EqualTo("prefix&a&iii"));

            Assert.That(target.GetActionsBeingSent(1), Is.Empty);
            Assert.That(target.GetEventsBeingSent(1), Is.Empty);
        }
Esempio n. 4
0
        public void ResetChunkedRaisesEvent()
        {
            // given
            var key = new BeaconKey(1, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(key, 1000L, "a");
            target.AddActionData(key, 1001L, "iii");
            target.AddEventData(key, 1000L, "b");
            target.AddEventData(key, 1001L, "jjj");

            // do same step we'd do when we send the
            target.GetNextBeaconChunk(key, "prefix", 10, '&');

            // data has been copied, but still add some new event & action data
            target.AddActionData(key, 6666L, "123");
            target.AddEventData(key, 6666L, "987");

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            // and when resetting the previously copied data
            target.ResetChunkedData(key);

            // then
            Assert.That(notifyCount, Is.EqualTo(1));
        }
Esempio n. 5
0
        public void ResetChunkedRestoresData()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // do same step we'd do when we send the
            target.GetNextBeaconChunk(1, "prefix", 10, '&');

            // data has been copied, but still add some new event & action data
            target.AddActionData(1, 6666L, "123");
            target.AddEventData(1, 6666L, "987");

            // and when resetting the previously copied data
            target.ResetChunkedData(1);

            // then
            Assert.That(target.GetActionsBeingSent(1), Is.Null);
            Assert.That(target.GetEventsBeingSent(1), Is.Null);
            Assert.That(target.GetActions(1), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a"), new BeaconCacheRecord(1001L, "iii"), new BeaconCacheRecord(6666L, "123") }));
            Assert.That(target.GetEvents(1), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "b"), new BeaconCacheRecord(1001L, "jjj"), new BeaconCacheRecord(6666L, "987") }));
        }
Esempio n. 6
0
        public void AddEventDataAddsBeaconIdToCache()
        {
            // given
            var target = new BeaconCache(logger);
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(2, 0);

            // when adding beacon with id 1
            target.AddEventData(keyOne, 1000L, "a");

            // then
            Assert.That(target.BeaconKeys, Is.EqualTo(new HashSet <BeaconKey> {
                keyOne
            }));
            Assert.That(target.GetEvents(keyOne), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a") }));

            // and when adding beacon with id 2
            target.AddEventData(keyTwo, 1100L, "b");

            // then
            Assert.That(target.BeaconKeys, Is.EqualTo(new HashSet <BeaconKey> {
                keyOne, keyTwo
            }));
            Assert.That(target.GetEvents(keyOne), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a") }));
            Assert.That(target.GetEvents(keyTwo), Is.EqualTo(new[] { new BeaconCacheRecord(1100L, "b") }));
        }
Esempio n. 7
0
        public void ResetChunkedRestoresCacheSize()
        {
            // given
            var key = new BeaconKey(1, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(key, 1000L, "a");
            target.AddActionData(key, 1001L, "iii");
            target.AddEventData(key, 1000L, "b");
            target.AddEventData(key, 1001L, "jjj");

            // do same step we'd do when we send the
            target.GetNextBeaconChunk(key, "prefix", 10, '&');

            // data has been copied, but still add some new event & action data
            target.AddActionData(key, 6666L, "123");
            target.AddEventData(key, 6666L, "987");

            // and when resetting the previously copied data
            target.ResetChunkedData(key);

            // then
            Assert.That(target.NumBytesInCache, Is.EqualTo(28L));
        }
Esempio n. 8
0
        public void GetNextBeaconChunkDoesNotCopyDataForSending()
        {
            // given
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(42, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyOne, 1001L, "iii");
            target.AddActionData(keyTwo, 2000L, "z");
            target.AddEventData(keyOne, 1000L, "b");
            target.AddEventData(keyOne, 1001L, "jjj");

            // when
            var obtained = target.GetNextBeaconChunk(keyOne, "prefix", 0, '&');

            // then
            Assert.That(obtained, Is.Empty);

            Assert.That(target.GetActions(keyOne), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "a"), new BeaconCacheRecord(1001L, "iii") }));
            Assert.That(target.GetEvents(keyOne), Is.EqualTo(new[] { new BeaconCacheRecord(1000L, "b"), new BeaconCacheRecord(1001L, "jjj") }));
            Assert.That(target.GetActionsBeingSent(keyOne), Is.Null);
            Assert.That(target.GetEventsBeingSent(keyOne), Is.Null);
        }
Esempio n. 9
0
        public void ResetChunkedDoesNothingIfEntryDoesNotExist()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // do same step we'd do when we send the
            target.GetNextBeaconChunk(1, "prefix", 10, '&');

            // data has been copied, but still add some new event & action data
            target.AddActionData(1, 6666L, "123");
            target.AddEventData(1, 6666L, "987");

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            // and when resetting the previously copied data
            target.ResetChunkedData(666);

            // then
            Assert.That(target.NumBytesInCache, Is.EqualTo(12L));
            Assert.That(notifyCount, Is.EqualTo(0));
        }
Esempio n. 10
0
        public void DeleteCacheEntriesDoesNothingIfGivenBeaconIdIsNotInCache()
        {
            // given
            var keyOne   = new BeaconKey(1, 0);
            var keyTwo   = new BeaconKey(42, 0);
            var keyThree = new BeaconKey(666, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyTwo, 1000L, "z");
            target.AddEventData(keyOne, 1000L, "iii");

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            var cachedSize = target.NumBytesInCache;

            // when
            target.DeleteCacheEntry(keyThree);

            // then
            Assert.That(target.BeaconKeys, Is.EqualTo(new HashSet <BeaconKey> {
                keyOne, keyTwo
            }));
            Assert.That(target.NumBytesInCache, Is.EqualTo(cachedSize));
            Assert.That(notifyCount, Is.EqualTo(0));
        }
Esempio n. 11
0
        public void DeleteCacheEntryRemovesTheGivenBeacon()
        {
            // given
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(42, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyTwo, 1000L, "z");
            target.AddEventData(keyOne, 1000L, "iii");

            // when removing beacon with id 1
            target.DeleteCacheEntry(keyOne);

            // then
            Assert.That(target.BeaconKeys, Is.EqualTo(new HashSet <BeaconKey> {
                keyTwo
            }));

            // and when removing beacon with id 42
            target.DeleteCacheEntry(keyTwo);

            // then
            Assert.That(target.BeaconKeys, Is.Empty);
        }
Esempio n. 12
0
        public void DeleteCacheEntriesDoesNothingIfGivenBeaconIDIsNotInCache()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(42, 1000L, "z");
            target.AddEventData(1, 1000L, "iii");

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            var cachedSize = target.NumBytesInCache;

            // when
            target.DeleteCacheEntry(666);

            // then
            Assert.That(target.BeaconIDs, Is.EqualTo(new HashSet <int> {
                1, 42
            }));
            Assert.That(target.NumBytesInCache, Is.EqualTo(cachedSize));
            Assert.That(notifyCount, Is.EqualTo(0));
        }
Esempio n. 13
0
        public void ADefaultConstructedCacheDoesNotContainBeacons()
        {
            // given
            var target = new BeaconCache();

            // then
            Assert.That(target.BeaconIDs, Is.Empty);
            Assert.That(target.NumBytesInCache, Is.EqualTo(0L));
        }
Esempio n. 14
0
        public void IsEmptyGivesFalseIfBeaconDataSizeIsNotEqualToZero()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddEventData(1, 1000L, "b");

            // then
            Assert.That(target.IsEmpty(1), Is.False);
        }
Esempio n. 15
0
        public void IsEmptyGivesTrueIfBeaconDoesNotContainActiveData()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddEventData(1, 1000L, "b");

            target.GetNextBeaconChunk(1, "prefix", 0, '&');

            // then
            Assert.That(target.IsEmpty(1), Is.True);
        }
Esempio n. 16
0
        public void IsEmptyGivesTrueIfBeaconDoesNotExistInCache()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // then
            Assert.That(target.IsEmpty(666), Is.True);
        }
Esempio n. 17
0
        public void AddEventDataIncreasesCacheSize()
        {
            // given
            var target = new BeaconCache();

            // when adding some data
            target.AddEventData(1, 1000L, "a");
            target.AddEventData(42, 1000L, "z");
            target.AddEventData(1, 1000L, "iii");

            // then
            Assert.That(target.NumBytesInCache, Is.EqualTo(new BeaconCacheRecord(1000L, "a").DataSizeInBytes
                                                           + new BeaconCacheRecord(1000L, "z").DataSizeInBytes
                                                           + new BeaconCacheRecord(1000L, "iii").DataSizeInBytes));
        }
Esempio n. 18
0
        public void GetNextBeaconChunkReturnsNullIfGivenBeaconIDDoesNotExist()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(42, 1000L, "z");
            target.AddEventData(1, 1000L, "iii");

            // when
            var obtained = target.GetNextBeaconChunk(666, "", 1024, '&');

            // then
            Assert.That(obtained, Is.Null);
        }
Esempio n. 19
0
        public void EvictRecordsByNumberDoesNothingAndReturnsZeroIfBeaconIDDoesNotExist()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when
            var obtained = target.EvictRecordsByNumber(666, 100);

            // then
            Assert.That(obtained, Is.EqualTo(0));
        }
Esempio n. 20
0
        public void IsEmptyGivesTrueIfBeaconDoesNotExistInCache()
        {
            // given
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(666, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyOne, 1001L, "iii");
            target.AddEventData(keyOne, 1000L, "b");
            target.AddEventData(keyOne, 1001L, "jjj");

            // then
            Assert.That(target.IsEmpty(keyTwo), Is.True);
        }
Esempio n. 21
0
        public void EvictRecordsByNumber()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when
            int obtained = target.EvictRecordsByNumber(1, 2);

            // then
            Assert.That(obtained, Is.EqualTo(2));
        }
Esempio n. 22
0
        public void DeleteCacheEntryDecrementsCacheSize()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(42, 1000L, "z");
            target.AddEventData(1, 1000L, "iii");

            // when deleting entry with beacon id 42
            target.DeleteCacheEntry(42);

            // then
            Assert.That(target.NumBytesInCache, Is.EqualTo(new BeaconCacheRecord(1000L, "a").DataSizeInBytes
                                                           + new BeaconCacheRecord(1000L, "iii").DataSizeInBytes));
        }
Esempio n. 23
0
        public void IsEmptyGivesTrueIfBeaconDoesNotContainActiveData()
        {
            // given
            var key = new BeaconKey(1, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(key, 1000L, "a");
            target.AddEventData(key, 1000L, "b");

            target.PrepareDataForSending(key);

            target.GetNextBeaconChunk(key, "prefix", 0, '&');

            // then
            Assert.That(target.IsEmpty(key), Is.True);
        }
Esempio n. 24
0
        public void GetNextBeaconChunkDecreasesBeaconCacheSize()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(1, 1001L, "iii");
            target.AddActionData(42, 2000L, "z");
            target.AddEventData(1, 1000L, "b");
            target.AddEventData(1, 1001L, "jjj");

            // when
            target.GetNextBeaconChunk(1, "prefix", 0, '&');

            // cache stats are also adjusted
            Assert.That(target.NumBytesInCache, Is.EqualTo(new BeaconCacheRecord(2000L, "z").DataSizeInBytes));
        }
Esempio n. 25
0
        public void AddActionDataIncreasesCacheSize()
        {
            // given
            var target = new BeaconCache(logger);
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(42, 0);

            // when adding some data
            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyTwo, 1000L, "z");
            target.AddActionData(keyOne, 1000L, "iii");

            // then
            Assert.That(target.NumBytesInCache, Is.EqualTo(new BeaconCacheRecord(1000L, "a").DataSizeInBytes
                                                           + new BeaconCacheRecord(1000L, "z").DataSizeInBytes
                                                           + new BeaconCacheRecord(1000L, "iii").DataSizeInBytes));
        }
Esempio n. 26
0
        public void EvictRecordsByNumber()
        {
            // given
            var key = new BeaconKey(1, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(key, 1000L, "a");
            target.AddActionData(key, 1001L, "iii");
            target.AddEventData(key, 1000L, "b");
            target.AddEventData(key, 1001L, "jjj");

            // when
            var obtained = target.EvictRecordsByNumber(key, 2);

            // then
            Assert.That(obtained, Is.EqualTo(2));
        }
Esempio n. 27
0
        public void GetNextBeaconChunkReturnsNullIfGivenBeaconIdDoesNotExist()
        {
            // given
            var keyOne   = new BeaconKey(1, 0);
            var keyTwo   = new BeaconKey(42, 0);
            var keyThree = new BeaconKey(666, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyTwo, 1000L, "z");
            target.AddEventData(keyOne, 1000L, "iii");

            // when
            var obtained = target.GetNextBeaconChunk(keyThree, "", 1024, '&');

            // then
            Assert.That(obtained, Is.Null);
        }
Esempio n. 28
0
        public void EvictRecordsByAgeDoesNothingAndReturnsZeroIfBeaconIdDoesNotExist()
        {
            // given
            var keyOne = new BeaconKey(1, 0);
            var keyTwo = new BeaconKey(666, 0);

            var target = new BeaconCache(logger);

            target.AddActionData(keyOne, 1000L, "a");
            target.AddActionData(keyOne, 1001L, "iii");
            target.AddEventData(keyOne, 1000L, "b");
            target.AddEventData(keyOne, 1001L, "jjj");

            // when
            var obtained = target.EvictRecordsByAge(keyTwo, 0);

            // then
            Assert.That(obtained, Is.EqualTo(0));
        }
Esempio n. 29
0
        public void DeleteCacheEntryDoesNotRaiseEvent()
        {
            // given
            var target = new BeaconCache();

            target.AddActionData(1, 1000L, "a");
            target.AddActionData(42, 1000L, "z");
            target.AddEventData(1, 1000L, "iii");

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            // when deleting both entries
            target.DeleteCacheEntry(1);
            target.DeleteCacheEntry(42);

            // then
            Assert.That(notifyCount, Is.EqualTo(0));
        }
Esempio n. 30
0
        public void AddEventDataRaisesEvent()
        {
            // given
            var target = new BeaconCache();

            var notifyCount = 0;

            target.RecordAdded += (s, a) => { notifyCount += 1; };

            // when adding an element
            target.AddEventData(1, 1000L, "a");

            // then verify event got raised
            Assert.That(notifyCount, Is.EqualTo(1));

            // when adding some more data
            target.AddEventData(1, 1100L, "b");
            target.AddEventData(666, 1200L, "xyz");

            // then verify event got raised another two times
            Assert.That(notifyCount, Is.EqualTo(3));
        }