From a493adde70f52716703503e244a8b4891d51e8ef Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 20 Aug 2019 15:57:50 +1200 Subject: [PATCH] feat: make IPolicy synchronous --- src/BlackList.cs | 10 ++------ src/IPolicy.cs | 24 +++--------------- src/MultiAddressBlackList.cs | 9 ++----- src/MultiAddressWhiteList.cs | 11 +++------ src/Policy.cs | 15 ++++++------ src/Swarm.cs | 18 +++++--------- src/WhiteList.cs | 11 ++------- test/BlackListTest.cs | 27 ++++++-------------- test/MultiAdressBlackListTest.cs | 42 +++++++++++--------------------- test/MultiAdressWhiteListTest.cs | 42 +++++++++++--------------------- test/PeerManagerTest.cs | 6 ++--- test/PolicyTest.cs | 10 +++----- test/WhiteList.cs | 27 ++++++-------------- 13 files changed, 74 insertions(+), 178 deletions(-) diff --git a/src/BlackList.cs b/src/BlackList.cs index 7d6b8d1..51543d4 100644 --- a/src/BlackList.cs +++ b/src/BlackList.cs @@ -21,16 +21,10 @@ public class BlackList : ConcurrentBag, IPolicy where T : IEquatable { /// - public Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) + public bool IsAllowed(T target) { - return Task.FromResult(!this.Contains(target)); + return !this.Contains(target); } - /// - public async Task IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) - { - var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false); - return !q; - } } } diff --git a/src/IPolicy.cs b/src/IPolicy.cs index a661a49..0fda7a7 100644 --- a/src/IPolicy.cs +++ b/src/IPolicy.cs @@ -21,28 +21,10 @@ interface IPolicy /// /// An object to test against the rule. /// - /// - /// Is used to stop the task. When cancelled, the is raised. - /// - /// - /// A task that represents the asynchronous operation. The task's result is - /// true if the passes the rule. - /// - Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)); - - /// - /// Determines if the target fails the rule. - /// - /// - /// An object to test against the rule. - /// - /// - /// Is used to stop the task. When cancelled, the is raised. - /// /// - /// A task that represents the asynchronous operation. The task's result is - /// true if the fails the rule. + /// true if the passes the rule; + /// otherwise false. /// - Task IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)); + bool IsAllowed(T target); } } diff --git a/src/MultiAddressBlackList.cs b/src/MultiAddressBlackList.cs index 6bfa65d..663a777 100644 --- a/src/MultiAddressBlackList.cs +++ b/src/MultiAddressBlackList.cs @@ -18,9 +18,9 @@ namespace PeerTalk public class MultiAddressBlackList : List, IPolicy { /// - public Task IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) + public bool IsAllowed(MultiAddress target) { - return Task.FromResult(!this.Any(filter => Matches(filter, target))); + return !this.Any(filter => Matches(filter, target)); } bool Matches(MultiAddress filter, MultiAddress target) @@ -30,10 +30,5 @@ bool Matches(MultiAddress filter, MultiAddress target) .All(fp => target.Protocols.Any(tp => tp.Code == fp.Code && tp.Value == fp.Value)); } - /// - public async Task IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) - { - return !await IsAllowedAsync(target, cancel).ConfigureAwait(false); - } } } diff --git a/src/MultiAddressWhiteList.cs b/src/MultiAddressWhiteList.cs index e37c61f..1f7c5d5 100644 --- a/src/MultiAddressWhiteList.cs +++ b/src/MultiAddressWhiteList.cs @@ -19,12 +19,12 @@ namespace PeerTalk public class MultiAddressWhiteList : ConcurrentBag, IPolicy { /// - public Task IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) + public bool IsAllowed(MultiAddress target) { if (IsEmpty) - return Task.FromResult(true); + return true; - return Task.FromResult(this.Any(filter => Matches(filter, target))); + return this.Any(filter => Matches(filter, target)); } bool Matches(MultiAddress filter, MultiAddress target) @@ -34,10 +34,5 @@ bool Matches(MultiAddress filter, MultiAddress target) .All(fp => target.Protocols.Any(tp => tp.Code == fp.Code && tp.Value == fp.Value)); } - /// - public async Task IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) - { - return !await IsAllowedAsync(target, cancel).ConfigureAwait(false); - } } } diff --git a/src/Policy.cs b/src/Policy.cs index a218c6d..d5bad1e 100644 --- a/src/Policy.cs +++ b/src/Policy.cs @@ -16,13 +16,12 @@ namespace PeerTalk public abstract class Policy : IPolicy { /// - public abstract Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)); + public abstract bool IsAllowed(T target); /// - public async Task IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) + public bool IsNotAllowed(T target) { - var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false); - return !q; + return !IsAllowed(target); } } @@ -35,9 +34,9 @@ public abstract class Policy : IPolicy public class PolicyAlways : Policy { /// - public override Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) + public override bool IsAllowed(T target) { - return Task.FromResult(true); + return true; } } @@ -50,9 +49,9 @@ public class PolicyAlways : Policy public class PolicyNever : Policy { /// - public override Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) + public override bool IsAllowed(T target) { - return Task.FromResult(false); + return false; } } } diff --git a/src/Swarm.cs b/src/Swarm.cs index 618478f..570bc38 100644 --- a/src/Swarm.cs +++ b/src/Swarm.cs @@ -231,7 +231,7 @@ public IEnumerable KnownPeers /// added to the . /// /// - public async Task RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) + public Task RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) { var peerId = address.PeerId; if (peerId == LocalPeer.Id) @@ -239,7 +239,7 @@ public IEnumerable KnownPeers throw new Exception("Cannot register to self."); } - if (!await IsAllowedAsync(address, cancel).ConfigureAwait(false)) + if (!IsAllowed(address)) { throw new Exception($"Communication with '{address}' is not allowed."); } @@ -250,7 +250,7 @@ public IEnumerable KnownPeers Addresses = new List { address } }; - return RegisterPeer(peer); + return Task.FromResult(RegisterPeer(peer)); } /// @@ -1047,17 +1047,11 @@ public async Task StopListeningAsync(MultiAddress address) } /// - public async Task IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) + public bool IsAllowed(MultiAddress target) { - return await BlackList.IsAllowedAsync(target, cancel).ConfigureAwait(false) - && await WhiteList.IsAllowedAsync(target, cancel).ConfigureAwait(false); + return BlackList.IsAllowed(target) + && WhiteList.IsAllowed(target); } - /// - public async Task IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken)) - { - var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false); - return !q; - } } } diff --git a/src/WhiteList.cs b/src/WhiteList.cs index 672beb6..24c395e 100644 --- a/src/WhiteList.cs +++ b/src/WhiteList.cs @@ -22,16 +22,9 @@ public class WhiteList : ConcurrentBag, IPolicy where T : IEquatable { /// - public Task IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) + public bool IsAllowed(T target) { - return Task.FromResult(this.IsEmpty || this.Contains(target)); - } - - /// - public async Task IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken)) - { - var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false); - return !q; + return this.IsEmpty || this.Contains(target); } } } diff --git a/test/BlackListTest.cs b/test/BlackListTest.cs index c1220f5..565a874 100644 --- a/test/BlackListTest.cs +++ b/test/BlackListTest.cs @@ -10,35 +10,22 @@ namespace PeerTalk public class BlackListTest { [TestMethod] - public async Task Allowed() + public void Allowed() { var policy = new BlackList(); policy.Add("c"); policy.Add("d"); - Assert.IsTrue(await policy.IsAllowedAsync("a")); - Assert.IsTrue(await policy.IsAllowedAsync("b")); - Assert.IsFalse(await policy.IsAllowedAsync("c")); - Assert.IsFalse(await policy.IsAllowedAsync("d")); + Assert.IsTrue(policy.IsAllowed("a")); + Assert.IsTrue(policy.IsAllowed("b")); + Assert.IsFalse(policy.IsAllowed("c")); + Assert.IsFalse(policy.IsAllowed("d")); } [TestMethod] - public async Task NotAllowed() + public void Empty() { var policy = new BlackList(); - policy.Add("c"); - policy.Add("d"); - Assert.IsFalse(await policy.IsNotAllowedAsync("a")); - Assert.IsFalse(await policy.IsNotAllowedAsync("b")); - Assert.IsTrue(await policy.IsNotAllowedAsync("c")); - Assert.IsTrue(await policy.IsNotAllowedAsync("d")); - } - - [TestMethod] - public async Task Empty() - { - var policy = new BlackList(); - Assert.IsTrue(await policy.IsAllowedAsync("a")); - Assert.IsFalse(await policy.IsNotAllowedAsync("a")); + Assert.IsTrue(policy.IsAllowed("a")); } } } diff --git a/test/MultiAdressBlackListTest.cs b/test/MultiAdressBlackListTest.cs index 165677f..9d49a7f 100644 --- a/test/MultiAdressBlackListTest.cs +++ b/test/MultiAdressBlackListTest.cs @@ -17,49 +17,35 @@ public class MultiAddressBlackListTest MultiAddress d = "/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64"; [TestMethod] - public async Task Allowed() + public void Allowed() { var policy = new MultiAddressBlackList(); policy.Add(a); policy.Add(b); - Assert.IsFalse(await policy.IsAllowedAsync(a)); - Assert.IsFalse(await policy.IsAllowedAsync(a1)); - Assert.IsFalse(await policy.IsAllowedAsync(b)); - Assert.IsTrue(await policy.IsAllowedAsync(c)); - Assert.IsTrue(await policy.IsAllowedAsync(d)); + Assert.IsFalse(policy.IsAllowed(a)); + Assert.IsFalse(policy.IsAllowed(a1)); + Assert.IsFalse(policy.IsAllowed(b)); + Assert.IsTrue(policy.IsAllowed(c)); + Assert.IsTrue(policy.IsAllowed(d)); } [TestMethod] - public async Task Allowed_Alias() + public void Allowed_Alias() { var policy = new MultiAddressBlackList(); policy.Add(a); - Assert.IsFalse(await policy.IsAllowedAsync(a)); - Assert.IsFalse(await policy.IsAllowedAsync(a1)); - Assert.IsFalse(await policy.IsAllowedAsync(b)); - Assert.IsTrue(await policy.IsAllowedAsync(c)); - Assert.IsTrue(await policy.IsAllowedAsync(d)); + Assert.IsFalse(policy.IsAllowed(a)); + Assert.IsFalse(policy.IsAllowed(a1)); + Assert.IsFalse(policy.IsAllowed(b)); + Assert.IsTrue(policy.IsAllowed(c)); + Assert.IsTrue(policy.IsAllowed(d)); } [TestMethod] - public async Task NotAllowed() + public void Empty() { var policy = new MultiAddressBlackList(); - policy.Add(a); - policy.Add(b); - Assert.IsTrue(await policy.IsNotAllowedAsync(a)); - Assert.IsTrue(await policy.IsNotAllowedAsync(a1)); - Assert.IsTrue(await policy.IsNotAllowedAsync(b)); - Assert.IsFalse(await policy.IsNotAllowedAsync(c)); - Assert.IsFalse(await policy.IsNotAllowedAsync(d)); - } - - [TestMethod] - public async Task Empty() - { - var policy = new MultiAddressBlackList(); - Assert.IsTrue(await policy.IsAllowedAsync(a)); - Assert.IsFalse(await policy.IsNotAllowedAsync(a)); + Assert.IsTrue( policy.IsAllowed(a)); } } } diff --git a/test/MultiAdressWhiteListTest.cs b/test/MultiAdressWhiteListTest.cs index e832617..3fd81b9 100644 --- a/test/MultiAdressWhiteListTest.cs +++ b/test/MultiAdressWhiteListTest.cs @@ -17,49 +17,35 @@ public class MultiAddressWhiteListTest MultiAddress d = "/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64"; [TestMethod] - public async Task Allowed() + public void Allowed() { var policy = new MultiAddressWhiteList(); policy.Add(a); policy.Add(b); - Assert.IsTrue(await policy.IsAllowedAsync(a)); - Assert.IsTrue(await policy.IsAllowedAsync(a1)); - Assert.IsTrue(await policy.IsAllowedAsync(b)); - Assert.IsFalse(await policy.IsAllowedAsync(c)); - Assert.IsFalse(await policy.IsAllowedAsync(d)); + Assert.IsTrue(policy.IsAllowed(a)); + Assert.IsTrue(policy.IsAllowed(a1)); + Assert.IsTrue(policy.IsAllowed(b)); + Assert.IsFalse(policy.IsAllowed(c)); + Assert.IsFalse(policy.IsAllowed(d)); } [TestMethod] - public async Task Allowed_Alias() + public void Allowed_Alias() { var policy = new MultiAddressWhiteList(); policy.Add(a); - Assert.IsTrue(await policy.IsAllowedAsync(a)); - Assert.IsTrue(await policy.IsAllowedAsync(a1)); - Assert.IsTrue(await policy.IsAllowedAsync(b)); - Assert.IsFalse(await policy.IsAllowedAsync(c)); - Assert.IsFalse(await policy.IsAllowedAsync(d)); + Assert.IsTrue(policy.IsAllowed(a)); + Assert.IsTrue(policy.IsAllowed(a1)); + Assert.IsTrue(policy.IsAllowed(b)); + Assert.IsFalse(policy.IsAllowed(c)); + Assert.IsFalse(policy.IsAllowed(d)); } [TestMethod] - public async Task NotAllowed() + public void Empty() { var policy = new MultiAddressWhiteList(); - policy.Add(a); - policy.Add(b); - Assert.IsFalse(await policy.IsNotAllowedAsync(a)); - Assert.IsFalse(await policy.IsNotAllowedAsync(a1)); - Assert.IsFalse(await policy.IsNotAllowedAsync(b)); - Assert.IsTrue(await policy.IsNotAllowedAsync(c)); - Assert.IsTrue(await policy.IsNotAllowedAsync(d)); - } - - [TestMethod] - public async Task Empty() - { - var policy = new MultiAddressWhiteList(); - Assert.IsTrue(await policy.IsAllowedAsync(a)); - Assert.IsFalse(await policy.IsNotAllowedAsync(a)); + Assert.IsTrue(policy.IsAllowed(a)); } } } diff --git a/test/PeerManagerTest.cs b/test/PeerManagerTest.cs index 5313e06..481b2ea 100644 --- a/test/PeerManagerTest.cs +++ b/test/PeerManagerTest.cs @@ -40,17 +40,17 @@ public void IsNotReachable() } [TestMethod] - public async Task BlackListsThePeer() + public void BlackListsThePeer() { var peer = new Peer { Id = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb" }; var manager = new PeerManager { Swarm = new Swarm() }; Assert.AreEqual(0, manager.DeadPeers.Count); manager.SetNotReachable(peer); - Assert.IsFalse(await manager.Swarm.IsAllowedAsync("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb")); + Assert.IsFalse(manager.Swarm.IsAllowed("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb")); manager.SetReachable(peer); - Assert.IsTrue(await manager.Swarm.IsAllowedAsync("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb")); + Assert.IsTrue(manager.Swarm.IsAllowed("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb")); } [TestMethod] diff --git a/test/PolicyTest.cs b/test/PolicyTest.cs index ed82a6b..68b977b 100644 --- a/test/PolicyTest.cs +++ b/test/PolicyTest.cs @@ -10,19 +10,17 @@ namespace PeerTalk public class PolicyTest { [TestMethod] - public async Task Always() + public void Always() { var policy = new PolicyAlways(); - Assert.IsTrue(await policy.IsAllowedAsync("foo")); - Assert.IsFalse(await policy.IsNotAllowedAsync("foo")); + Assert.IsTrue(policy.IsAllowed("foo")); } [TestMethod] - public async Task Never() + public void Never() { var policy = new PolicyNever(); - Assert.IsFalse(await policy.IsAllowedAsync("foo")); - Assert.IsTrue(await policy.IsNotAllowedAsync("foo")); + Assert.IsFalse(policy.IsAllowed("foo")); } } } diff --git a/test/WhiteList.cs b/test/WhiteList.cs index 1da5621..bb9c9a4 100644 --- a/test/WhiteList.cs +++ b/test/WhiteList.cs @@ -10,35 +10,22 @@ namespace PeerTalk public class WhiteListTest { [TestMethod] - public async Task Allowed() + public void Allowed() { var policy = new WhiteList(); policy.Add("a"); policy.Add("b"); - Assert.IsTrue(await policy.IsAllowedAsync("a")); - Assert.IsTrue(await policy.IsAllowedAsync("b")); - Assert.IsFalse(await policy.IsAllowedAsync("c")); - Assert.IsFalse(await policy.IsAllowedAsync("d")); + Assert.IsTrue(policy.IsAllowed("a")); + Assert.IsTrue(policy.IsAllowed("b")); + Assert.IsFalse(policy.IsAllowed("c")); + Assert.IsFalse(policy.IsAllowed("d")); } [TestMethod] - public async Task NotAllowed() + public void Empty() { var policy = new WhiteList(); - policy.Add("a"); - policy.Add("b"); - Assert.IsFalse(await policy.IsNotAllowedAsync("a")); - Assert.IsFalse(await policy.IsNotAllowedAsync("b")); - Assert.IsTrue(await policy.IsNotAllowedAsync("c")); - Assert.IsTrue(await policy.IsNotAllowedAsync("d")); - } - - [TestMethod] - public async Task Empty() - { - var policy = new WhiteList(); - Assert.IsTrue(await policy.IsAllowedAsync("a")); - Assert.IsFalse(await policy.IsNotAllowedAsync("a")); + Assert.IsTrue(policy.IsAllowed("a")); } } }