Esempio n. 1
0
        public async Task TestQueriesDiskCache()
        {
            await _bufferedDiskCache.Put(_cacheKey, _encodedImage);

            EncodedImage image = await _bufferedDiskCache.Get(_cacheKey, _isCancelled);

            Assert.IsTrue(2 == image.GetByteBufferRef().GetUnderlyingReferenceTestOnly().GetRefCountTestOnly());
            byte[] buf1 = new byte[_pooledByteBuffer.Size];
            _pooledByteBuffer.Read(0, buf1, 0, _pooledByteBuffer.Size);
            byte[] buf2 = new byte[image.GetByteBufferRef().Get().Size];
            image.GetByteBufferRef().Get().Read(0, buf2, 0, image.GetByteBufferRef().Get().Size);
            CollectionAssert.AreEqual(buf1, buf2);
        }
        public void TestCloneOrNull()
        {
            EncodedImage encodedImage = new EncodedImage(_byteBufferRef);

            encodedImage.Format        = ImageFormat.JPEG;
            encodedImage.RotationAngle = 0;
            encodedImage.Width         = 1;
            encodedImage.Height        = 2;
            encodedImage.SampleSize    = 4;
            EncodedImage encodedImage2 = EncodedImage.CloneOrNull(encodedImage);

            Assert.AreEqual(3, _byteBufferRef.GetUnderlyingReferenceTestOnly().GetRefCountTestOnly());
            Assert.AreSame(
                encodedImage.GetByteBufferRef().GetUnderlyingReferenceTestOnly(),
                encodedImage2.GetByteBufferRef().GetUnderlyingReferenceTestOnly());
            Assert.AreEqual(encodedImage.Format, encodedImage2.Format);
            Assert.AreEqual(encodedImage.RotationAngle, encodedImage2.RotationAngle);
            Assert.AreEqual(encodedImage.Height, encodedImage2.Height);
            Assert.AreEqual(encodedImage.Width, encodedImage2.Width);
            Assert.AreEqual(encodedImage.SampleSize, encodedImage2.SampleSize);

            encodedImage               = new EncodedImage(_inputStreamSupplier, 100);
            encodedImage.Format        = ImageFormat.JPEG;
            encodedImage.RotationAngle = 0;
            encodedImage.Width         = 1;
            encodedImage.Height        = 2;
            encodedImage2              = EncodedImage.CloneOrNull(encodedImage);
            Assert.AreSame(encodedImage.GetInputStream(), encodedImage2.GetInputStream());
            Assert.AreEqual(encodedImage2.Size, encodedImage.Size);
        }
        public void TestByteBufferRef()
        {
            EncodedImage encodedImage = new EncodedImage(_byteBufferRef);

            Assert.AreEqual(2, _byteBufferRef.GetUnderlyingReferenceTestOnly().GetRefCountTestOnly());
            Assert.AreSame(
                encodedImage.GetByteBufferRef().GetUnderlyingReferenceTestOnly(),
                _byteBufferRef.GetUnderlyingReferenceTestOnly());
        }
Esempio n. 4
0
        public async Task TestFromStagingArea()
        {
            Task         putTask = _bufferedDiskCache.Put(_cacheKey, _encodedImage);
            EncodedImage image   = _stagingArea.Get(_cacheKey);

            Assert.AreSame(
                _closeableReference.GetUnderlyingReferenceTestOnly(),
                image.GetByteBufferRef().GetUnderlyingReferenceTestOnly());
            await putTask;
        }
Esempio n. 5
0
        public async Task TestFromStagingAreaLater()
        {
            await _bufferedDiskCache.Put(_cacheKey, _encodedImage);

            EncodedImage image = await _bufferedDiskCache.Get(_cacheKey, _isCancelled);

            Assert.IsTrue(_encodedImage.Size == image.Size);

            // Ref count should be equal to 3 (One for _closeableReference, one that is cloned when
            // _encodedImage is created and a third one that is cloned when the method GetByteBufferRef is
            // called in EncodedImage).
            Assert.IsTrue(3 == _encodedImage.GetByteBufferRef().GetUnderlyingReferenceTestOnly().GetRefCountTestOnly());
        }
Esempio n. 6
0
            protected override void OnNewResultImpl(EncodedImage newResult, bool isLast)
            {
                CloseableReference <IPooledByteBuffer> ret = null;

                try
                {
                    if (EncodedImage.IsValid(newResult))
                    {
                        ret = newResult.GetByteBufferRef();
                    }

                    Consumer.OnNewResult(ret, isLast);
                }
                finally
                {
                    CloseableReference <IPooledByteBuffer> .CloseSafely(ret);
                }
            }
Esempio n. 7
0
        /// <summary>
        /// Removes key-value from the StagingArea. Both key and value must match.
        /// </summary>
        /// <param name="key">The cache key.</param>
        /// <param name="encodedImage">Value corresponding to key.</param>
        /// <returns>true if item was removed.</returns>
        public bool Remove(ICacheKey key, EncodedImage encodedImage)
        {
            lock (_mapGate)
            {
                Preconditions.CheckNotNull(key);
                Preconditions.CheckNotNull(encodedImage);
                Preconditions.CheckArgument(EncodedImage.IsValid(encodedImage));

                EncodedImage oldValue = default(EncodedImage);
                if (!_map.TryGetValue(key, out oldValue))
                {
                    return(false);
                }

                CloseableReference <IPooledByteBuffer> oldReference = oldValue.GetByteBufferRef();
                CloseableReference <IPooledByteBuffer> reference    = encodedImage.GetByteBufferRef();

                try
                {
                    if (oldReference == null || reference == null || oldReference.Get() != reference.Get())
                    {
                        return(false);
                    }

                    _map.Remove(key);
                }
                finally
                {
                    CloseableReference <IPooledByteBuffer> .CloseSafely(reference);

                    CloseableReference <IPooledByteBuffer> .CloseSafely(oldReference);

                    EncodedImage.CloseSafely(oldValue);
                }

#if DEBUG_STAGING_AREA
                LogStats();
#endif // DEBUG_STAGING_AREA
                return(true);
            }
        }