Ejemplo n.º 1
0
        public static WebSocketDisposable ReserveConnection(string uri)
        {
            Tuple<WebSocket, RefCountDisposable> connection;
            lock(openConnectionsLock)
            {
                if(!openConnections.TryGetValue(uri, out connection))
                {
                    Debug.WriteLine("Creating new websocket");

                    WebSocket ws = new WebSocket(uri);
                    // Set callbacks?
                    ws.Open();

                    var dispose = Disposable.Create(() =>
                    {
                        Debug.WriteLine("Disposing of websocket");
                        ws.Close();
                        openConnections.Remove(uri);
                    });

                    RefCountDisposable refCount = new RefCountDisposable(dispose);
                    connection = Tuple.Create(ws, refCount);
                    openConnections.Add(uri, connection);

                    return new WebSocketDisposable(connection.Item1, connection.Item2.GetDisposable());
                }                
            }

            Debug.WriteLine("Returning old websocket");
            return new WebSocketDisposable(connection.Item1, connection.Item2.GetDisposable());
        }
        public void GetDisposableDisposeFirst()
        {
            int i = 0;
            var src = Disposable.Create (() => i++);
            var d = new RefCountDisposable (src);
            var des = d.GetDisposable ();
            Assert.AreNotSame (src, des, "#1");
            d.Dispose (); // triggers final disposable, up to refcount.
            Assert.AreEqual (0, i, "#2");
            des.Dispose ();
            Assert.AreEqual (1, i, "#3");
            Assert.IsTrue (d.IsDisposed);

            des = d.GetDisposable ();
            des.Dispose ();
            Assert.AreEqual (1, i, "#4");
        }
        public void GetDisposableDisposeLater()
        {
            int i = 0;
            var src = Disposable.Create (() => i++);
            var d = new RefCountDisposable (src);
            var des = d.GetDisposable ();
            Assert.AreNotSame (src, des, "#1");
            Assert.AreEqual (0, i, "#2");
            des.Dispose ();
            Assert.AreEqual (0, i, "#3");
            Assert.IsFalse (d.IsDisposed);

            des = d.GetDisposable ();
            des.Dispose ();
            Assert.AreEqual (0, i, "#4");
            d.Dispose (); // finally, it calls src dispose
            Assert.AreEqual (1, i, "#5");
        }
Ejemplo n.º 4
0
        private static void RefCountDisposable()
        {
            Demo.DisplayHeader("The RefCountDisposable - dispose the underlying disposable when all referencing disposables are dispsosed");

            var inner = Disposable.Create(() => Console.WriteLine("Disposing inner-disposable"));
            var refCountDisposable = new RefCountDisposable(inner);
            var d1 = refCountDisposable.GetDisposable();
            var d2 = refCountDisposable.GetDisposable();
            var d3 = refCountDisposable.GetDisposable();

            refCountDisposable.Dispose();
            Console.WriteLine("Disposing 1st");
            d1.Dispose();
            Console.WriteLine("Disposing 2nd");
            d2.Dispose();
            Console.WriteLine("Disposing 3rd");
            d3.Dispose();
        }
Ejemplo n.º 5
0
 public InnerDisposable(RefCountDisposable parent)
 {
     _parent = parent;
 }
Ejemplo n.º 6
0
        public void RefCountDisposable_PrimaryDisposesFirst()
        {
            var d = new BooleanDisposable();
            var r = new RefCountDisposable(d);
            Assert.IsFalse(d.IsDisposed);

            var d1 = r.GetDisposable();
            var d2 = r.GetDisposable();
            Assert.IsFalse(d.IsDisposed);

            d1.Dispose();
            Assert.IsFalse(d.IsDisposed);

            r.Dispose();
            Assert.IsFalse(d.IsDisposed);

            d2.Dispose();
            Assert.IsTrue(d.IsDisposed);
        }
Ejemplo n.º 7
0
        public void RefCountDisposable_RefCounting()
        {
            var d = new BooleanDisposable();
            var r = new RefCountDisposable(d);
            Assert.IsFalse(d.IsDisposed);

            var d1 = r.GetDisposable();
            var d2 = r.GetDisposable();
            Assert.IsFalse(d.IsDisposed);

            d1.Dispose();
            Assert.IsFalse(d.IsDisposed);

            d2.Dispose();
            Assert.IsFalse(d.IsDisposed); // CHECK

            r.Dispose();
            Assert.IsTrue(d.IsDisposed);
            Assert.IsTrue(r.IsDisposed);

            var d3 = r.GetDisposable(); // CHECK
            d3.Dispose();
        }
Ejemplo n.º 8
0
 public void RefCountDisposable_SingleReference()
 {
     var d = new BooleanDisposable();
     var r = new RefCountDisposable(d);
     Assert.IsFalse(d.IsDisposed);
     r.Dispose();
     Assert.IsTrue(d.IsDisposed);
     r.Dispose();
     Assert.IsTrue(d.IsDisposed);
 }
Ejemplo n.º 9
0
 public InnerDisposable(RefCountDisposable parent)
 {
     _parent = parent;
 }
Ejemplo n.º 10
0
        private static void Impl(bool primaryFirst, bool primaryRandom, IEnumerable<int> nDependents)
        {
            var rand = new Random();

            foreach (var n in nDependents)
            {
                var e = new ManualResetEvent(false);
                var hasDependent = new ManualResetEvent(false);
                var r = new RefCountDisposable(Disposable.Create(() => { e.Set(); }));

                var d = default(IDisposable);
                if (primaryFirst)
                {
                    d = r.GetDisposable();
                    r.Dispose();
                }
                else if (primaryRandom)
                {
                    var sleep = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        hasDependent.WaitOne();
                        Helpers.SleepOrSpin(sleep);
                        r.Dispose();
                    });

                    if (n == 0)
                        hasDependent.Set();
                }

                Console.Write(n + " - ");

                var cd = new CountdownEvent(n * 2);
                for (int i = 0; i < n; i++)
                {
                    var j = i;

                    var sleep1 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
                    var sleep2 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
                    var sleep3 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Helpers.SleepOrSpin(sleep1);

                        Console.Write("+");

                        var f = r.GetDisposable();

                        if (j == 0)
                            hasDependent.Set();

                        Helpers.SleepOrSpin(sleep2);

                        ThreadPool.QueueUserWorkItem(__ =>
                        {
                            Helpers.SleepOrSpin(sleep3);

                            f.Dispose();

                            Console.Write("-");

                            cd.Signal();
                        });

                        cd.Signal();
                    });
                }

                cd.Wait();

                if (primaryFirst)
                    d.Dispose();
                else if (!primaryRandom)
                    r.Dispose();

                e.WaitOne();

                Console.WriteLine(".");
            }
        }