public void When_disposing_one_of_the_hubs()
        {
            var hub1 = new MessageHub();
            var hub2 = new MessageHub();

            var totalMessages = new List <string>();

            hub1.RegisterGlobalHandler((type, msg) => totalMessages.Add((string)msg));
            hub2.RegisterGlobalHandler((type, msg) => totalMessages.Add((string)msg));

            var hub1Messages = new List <string>();
            var hub2Messages = new List <string>();

            hub1.Subscribe <string>(x => hub1Messages.Add(x));
            hub2.Subscribe <string>(x => hub2Messages.Add(x));

            hub1.Publish("A");

            hub2.Publish("B");
            hub2.Publish("C");

            hub1.Dispose();

            Should.Throw <ObjectDisposedException>(() => hub1.Publish("C"))
            .Message.ShouldBe("Cannot access a disposed object.\r\nObject name: 'The ThreadLocal object has been disposed.'.");

            hub2.Publish("D");

            totalMessages.ShouldBe(new[] { "A", "B", "C", "D" });

            hub1Messages.ShouldBe(new[] { "A" });
            hub2Messages.ShouldBe(new[] { "B", "C", "D" });

            hub2.Dispose();
        }
        public void When_using_multiple_hubs()
        {
            var hub1 = new MessageHub();
            var hub2 = new MessageHub();

            var totalMessages = new List <string>();

            hub1.RegisterGlobalHandler((type, msg) => totalMessages.Add((string)msg));
            hub2.RegisterGlobalHandler((type, msg) => totalMessages.Add((string)msg));

            var hub1Messages = new List <string>();
            var hub2Messages = new List <string>();

            hub1.Subscribe <string>(x => hub1Messages.Add(x));
            hub2.Subscribe <string>(x => hub2Messages.Add(x));

            hub1.Publish("A");

            hub2.Publish("B");
            hub2.Publish("C");

            totalMessages.ShouldBe(new [] { "A", "B", "C" });

            hub1Messages.ShouldBe(new [] { "A" });
            hub2Messages.ShouldBe(new [] { "B", "C" });

            hub1.Dispose();
            hub2.Dispose();
        }
        public void When_operating_on_a_disposed_hub()
        {
            var totalMessages = 0;
            var hub           = new MessageHub();

            hub.RegisterGlobalHandler((type, msg) =>
            {
                type.ShouldBe(typeof(string));
                msg.ShouldBeOfType <string>();
                Interlocked.Increment(ref totalMessages);
            });

            var queue = new ConcurrentQueue <string>();

            Action <string> handler = msg => queue.Enqueue(msg);

            var token = hub.Subscribe(handler);

            hub.Dispose();

            Should.NotThrow(() => hub.Subscribe(handler));
            Should.NotThrow(() => hub.Unsubscribe(token));
            Should.NotThrow(() => hub.IsSubscribed(token));
            Should.NotThrow(() => hub.ClearSubscriptions());

            totalMessages.ShouldBe(0);
        }
Esempio n. 4
0
 public void CloseServer()
 {
     _commState?.Dispose();
     _ardServ?.Dispose();
     _msgHubLogger?.Dispose();
     _msgHub.Dispose();
 }
Esempio n. 5
0
        protected override void OnClosing(CancelEventArgs eArgs)
        {
            _discoverSvc.Dispose();
            _schedulerSvc.Dispose();
            _vlcControl.Dispose();
            _hub.Dispose();

            base.OnClosing(eArgs);
        }
Esempio n. 6
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    _messageHub.Dispose();
                }

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null
                _disposed = true;
            }
        }