public void ResourceExpiresOnTime()
        {
            const int timeoutSec = 1;
            int       port       = Utils.NewPortNumber.Calculate();
            var       network1   = new MemoryPeerDiscoveryTransport(port);
            var       network2   = new MemoryPeerDiscoveryTransport(port);

            using (var cts = new CancellationTokenSource(Utils.TestTimeoutMs))
                using (var svc1 = new PeerDiscoveryAgent(network1))
                {
                    const string category   = "ResourceExpiresOnTime";
                    var          resources1 = svc1.Subscribe(category);
                    Assert.Empty(resources1.Resources);

                    using (var svc2 = new PeerDiscoveryAgent(network2, new PeerDiscoveryAgent.Options {
                        ResourceExpirySec = timeoutSec
                    }))
                    {
                        // Create resources from svc2
                        var resource1 = svc2.PublishAsync(category, "conn1", null, cts.Token).Result;

                        // It should show up in svc1 even after the timeout.
                        {
                            Task.Delay(timeoutSec * 1200).Wait();
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Any(), cts.Token);
                            Assert.Single(res1);
                            Assert.Equal(resource1.UniqueId, res1.First().UniqueId);
                        }

                        // Stop the transport.
                        network1.Stop();
                        {
                            // The resource eventually expires.
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Count() == 0, cts.Token);
                            Assert.Empty(res1);
                        }

                        // Restart the transport.
                        network1.Start();
                        {
                            // The resource appears again.
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Any(), cts.Token);
                            Assert.Single(res1);
                            Assert.Equal(resource1.UniqueId, res1.First().UniqueId);
                        }

                        // Stop the transport.
                        network1.Stop();
                        {
                            // The resource expires again.
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Count() == 0, cts.Token);
                            Assert.Empty(res1);
                        }

                        // Restart the transport so that the agent disposal can stop it without exceptions.
                        network1.Start();
                    }
                }
        }
Пример #2
0
        public void ResourceExpiresOnTime()
        {
            const int timeoutSec = 1;
            var       network1   = new MemoryPeerDiscoveryTransport(1);
            var       network2   = new MemoryPeerDiscoveryTransport(2);

            using (var cts = new CancellationTokenSource(Utils.TestTimeoutMs))
                using (var svc1 = new PeerDiscoveryAgent(network1))
                {
                    const string category   = "ResourceExpiresOnTime";
                    var          resources1 = svc1.Subscribe(category);
                    Assert.Empty(resources1.Resources);

                    using (var svc2 = new PeerDiscoveryAgent(network2, new PeerDiscoveryAgent.Options {
                        ResourceExpirySec = timeoutSec
                    }))
                    {
                        // Create resources from svc2
                        var resource1 = svc2.PublishAsync(category, "conn1", null, cts.Token).Result;

                        // It should show up in svc1 even after the timeout.
                        {
                            Task.Delay(timeoutSec * 1200).Wait();
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Any(), cts.Token);
                            Assert.Single(res1);
                            Assert.Equal(resource1.UniqueId, res1.First().UniqueId);
                        }

                        // Stop the network.
                        network1.Stop();

                        // Wait a bit after the timeout.
                        Task.Delay(timeoutSec * 1200).Wait();
                        {
                            var res1 = Utils.QueryAndWaitForResourcesPredicate(svc1, category, rl => rl.Count() == 0, cts.Token);
                            Assert.Empty(res1);
                        }
                    }
                }
        }