Exemple #1
0
        public void TryGetValue_ReferenceNotFound()
        {
            var dictionary = new WeakDictionary <object, object>();

            object v;
            bool   result = dictionary.TryGetValue(new Object(), out v);

            Assert.Equal(false, result);
            Assert.Equal(null, v);
            Assert.Equal(false, dictionary.Contains(new Object()));
        }
        public void Indexer_ReferenceFound()
        {
            object k1 = new Object();
            object v1 = new Object();

            var dictionary = new WeakDictionary<object, object>();
            dictionary[k1] = v1;

            // Now look for the same key we inserted
            object v2 = dictionary[k1];

            Assert.Equal(true, Object.ReferenceEquals(v1, v2));
            Assert.Equal(true, dictionary.Contains(k1));
        }
Exemple #3
0
        public void Indexer_ReferenceFound()
        {
            object k1 = new Object();
            object v1 = new Object();

            var dictionary = new WeakDictionary <object, object>();

            dictionary[k1] = v1;

            // Now look for the same key we inserted
            object v2 = dictionary[k1];

            Assert.Equal(true, Object.ReferenceEquals(v1, v2));
            Assert.Equal(true, dictionary.Contains(k1));
        }
Exemple #4
0
        public void ContainsRemovesDeadValue()
        {
            Console.WriteLine("Fixed contains test ..");

            Object k = new Object();
            object v = new Object();

            var dictionary = new WeakDictionary <object, object>();

            dictionary[k] = v;

            // Do not put an assert here! It will cause the test to mysteriously fail
            // as somehow an NUnit Assert can hold onto the value!
            v = null;
            GC.Collect();

            Assert.Equal(false, dictionary.Contains(k));
        }
        public void TryGetValue_ReferenceNotFound()
        {
            var dictionary = new WeakDictionary<object, object>();

            object v;
            bool result = dictionary.TryGetValue(new Object(), out v);

            Assert.Equal(false, result);
            Assert.Equal(null, v);
            Assert.Equal(false, dictionary.Contains(new Object()));
        }
        public void ContainsRemovesDeadValue()
        {
            Console.WriteLine("Fixed contains test ..");

            Object k = new Object();
            object v = new Object();

            var dictionary = new WeakDictionary<object, object>();
            dictionary[k] = v;

            // Do not put an assert here! It will cause the test to mysteriously fail
            // as somehow an NUnit Assert can hold onto the value!
            v = null;
            GC.Collect();

            Assert.Equal(false, dictionary.Contains(k));
        }
Exemple #7
0
        internal async Task <Stream> OpenStreamAsync(bool synchronous, bool skipCache = false, bool linger = true)
        {
            var url = new LazyUri(Url);
            await Utils.CheckLocalFileAccessAsync(url);

            var mgr = this.manager;
            Func <long, Task <HttpResponseMessage> > createStream = null;

#if !STANDALONE && DESKTOP
            if (Caching.AzureApi != null && (mgr == null || !mgr.IsAlive) && !skipCache)
            {
                var container          = Caching.GetAzureContainer(url);
                HashSet <string> files = null;
                if (synchronous)
                {
                    ObjectManager.SynchronizationContext.Send(async() =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });
                }
                else
                {
                    await ObjectManager.SynchronizationContext.SendAsync(async() =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });
                }
                var name = Caching.GetFileCachePath(url);
                if (files.Contains(name))
                {
                    createStream = offset => Caching.GetAzureResponseAsync(container, name, offset, this);
                }
            }
            else
#endif
            if (
#if !STANDALONE && DESKTOP
                Caching.AzureApi == null &&
#endif
                !skipCache)
            {
#if DESKTOP
                var cache = Caching.GetFileCachePath(url);

                if (File.Exists(cache))
                {
                    var str = new FileStream(cache, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
                    if (str.Length == 0)
                    {
                        var errfile = Path.ChangeExtension(cache, ".err");
                        if (File.Exists(errfile))
                        {
                            str.Dispose();
                            var errlines = File.ReadAllText(errfile);
                            return(new MediaStream(MediaStream.ExceptionFromCachedResponse(errlines), this));
                        }
                    }
                    Sanity.AssertFastReadByte(str);
                    return(str);
                }
#endif
            }

            lock (this)
            {
                if (manager == null)
                {
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);
                }

                var stream = manager.TryCreateStream(this, 0, linger);
                if (stream == null)
                {
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);
                    stream  = manager.TryCreateStream(this, 0, linger);
                    Sanity.Assert(stream != null);
                }
                Sanity.AssertFastReadByte(stream);
                return(stream);
            }
        }