From efd6c91921802b2ad361f28f52f46c98be662b03 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 27 Nov 2019 13:38:53 +0100 Subject: [PATCH 1/4] Plugins from List to array --- src/neo/Plugins/Plugin.cs | 31 +++++++++++++-------- tests/neo.UnitTests/Ledger/UT_MemoryPool.cs | 6 ---- tests/neo.UnitTests/Plugins/UT_Plugin.cs | 1 - 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/neo/Plugins/Plugin.cs b/src/neo/Plugins/Plugin.cs index 14345d9de1..bd69ba4e51 100644 --- a/src/neo/Plugins/Plugin.cs +++ b/src/neo/Plugins/Plugin.cs @@ -10,12 +10,12 @@ namespace Neo.Plugins { public abstract class Plugin : IDisposable { - public static readonly List Plugins = new List(); - private static readonly List Loggers = new List(); - internal static readonly List RpcPlugins = new List(); - internal static readonly List PersistencePlugins = new List(); - internal static readonly List P2PPlugins = new List(); - internal static readonly List TxObserverPlugins = new List(); + public static Plugin[] Plugins { get; private set; } = new Plugin[0]; + internal static ILogPlugin[] Loggers { get; private set; } = new ILogPlugin[0]; + internal static IRpcPlugin[] RpcPlugins { get; private set; } = new IRpcPlugin[0]; + internal static IPersistencePlugin[] PersistencePlugins { get; private set; } = new IPersistencePlugin[0]; + internal static IP2PPlugin[] P2PPlugins { get; private set; } = new IP2PPlugin[0]; + internal static IMemoryPoolTxObserverPlugin[] TxObserverPlugins { get; private set; } = new IMemoryPoolTxObserverPlugin[0]; private static readonly string pluginsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Plugins"); private static readonly FileSystemWatcher configWatcher; @@ -45,17 +45,24 @@ static Plugin() protected Plugin() { - Plugins.Add(this); + Plugins = Add(Plugins, this); - if (this is ILogPlugin logger) Loggers.Add(logger); - if (this is IP2PPlugin p2p) P2PPlugins.Add(p2p); - if (this is IRpcPlugin rpc) RpcPlugins.Add(rpc); - if (this is IPersistencePlugin persistence) PersistencePlugins.Add(persistence); - if (this is IMemoryPoolTxObserverPlugin txObserver) TxObserverPlugins.Add(txObserver); + if (this is ILogPlugin logger) Loggers = Add(Loggers, logger); + if (this is IP2PPlugin p2p) P2PPlugins = Add(P2PPlugins, p2p); + if (this is IRpcPlugin rpc) RpcPlugins = Add(RpcPlugins, rpc); + if (this is IPersistencePlugin persistence) PersistencePlugins = Add(PersistencePlugins, persistence); + if (this is IMemoryPoolTxObserverPlugin txObserver) TxObserverPlugins = Add(TxObserverPlugins, txObserver); Configure(); } + private T[] Add(T[] plugins, T plugin) + { + var list = new List(plugins); + list.Add(plugin); + return list.ToArray(); + } + public abstract void Configure(); protected virtual void OnPluginsLoaded() diff --git a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs index c68504f4c6..74936595dc 100644 --- a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs +++ b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs @@ -55,12 +55,6 @@ public void TestSetup() plugin = new TestIMemoryPoolTxObserverPlugin(); } - [TestCleanup] - public void CleanUp() - { - Plugin.TxObserverPlugins.Remove(plugin); - } - long LongRandom(long min, long max, Random rand) { // Only returns positive random long values. diff --git a/tests/neo.UnitTests/Plugins/UT_Plugin.cs b/tests/neo.UnitTests/Plugins/UT_Plugin.cs index 7c3224c82a..dcfee1b5e3 100644 --- a/tests/neo.UnitTests/Plugins/UT_Plugin.cs +++ b/tests/neo.UnitTests/Plugins/UT_Plugin.cs @@ -46,7 +46,6 @@ public void TestSendMessage() { lock (locker) { - Plugin.Plugins.Clear(); Plugin.SendMessage("hey1").Should().BeFalse(); var lp = new TestLogPlugin(); From 63e706b3b833c650c75fb108374b5f022a068773 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 27 Nov 2019 13:44:37 +0100 Subject: [PATCH 2/4] Move false to init --- tests/neo.UnitTests/Plugins/UT_Plugin.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/neo.UnitTests/Plugins/UT_Plugin.cs b/tests/neo.UnitTests/Plugins/UT_Plugin.cs index dcfee1b5e3..860823d5f0 100644 --- a/tests/neo.UnitTests/Plugins/UT_Plugin.cs +++ b/tests/neo.UnitTests/Plugins/UT_Plugin.cs @@ -10,6 +10,12 @@ public class UT_Plugin { private static readonly object locker = new object(); + [TestInitialize] + public void Init() + { + Plugin.SendMessage("hey1").Should().BeFalse(); + } + [TestMethod] public void TestGetConfigFile() { @@ -46,8 +52,6 @@ public void TestSendMessage() { lock (locker) { - Plugin.SendMessage("hey1").Should().BeFalse(); - var lp = new TestLogPlugin(); Plugin.SendMessage("hey2").Should().BeTrue(); } From e91b933bf083a5f91268e2108f0719ffc64670eb Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 27 Nov 2019 13:48:02 +0100 Subject: [PATCH 3/4] Fix UT --- tests/neo.UnitTests/Plugins/UT_Plugin.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/neo.UnitTests/Plugins/UT_Plugin.cs b/tests/neo.UnitTests/Plugins/UT_Plugin.cs index 860823d5f0..ee59976dd0 100644 --- a/tests/neo.UnitTests/Plugins/UT_Plugin.cs +++ b/tests/neo.UnitTests/Plugins/UT_Plugin.cs @@ -13,7 +13,10 @@ public class UT_Plugin [TestInitialize] public void Init() { - Plugin.SendMessage("hey1").Should().BeFalse(); + if (Plugin.Plugins.Length == 0) + { + Plugin.SendMessage("hey1").Should().BeFalse(); + } } [TestMethod] From 70cd6ad0e3e1409821c7506dda28fc8c8a499cbd Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 27 Nov 2019 19:47:21 +0100 Subject: [PATCH 4/4] Refactor --- src/neo/Plugins/Plugin.cs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/neo/Plugins/Plugin.cs b/src/neo/Plugins/Plugin.cs index bd69ba4e51..25f28d9459 100644 --- a/src/neo/Plugins/Plugin.cs +++ b/src/neo/Plugins/Plugin.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -45,24 +44,17 @@ static Plugin() protected Plugin() { - Plugins = Add(Plugins, this); + Plugins = Plugins.Concat(new Plugin[] { this }).ToArray(); - if (this is ILogPlugin logger) Loggers = Add(Loggers, logger); - if (this is IP2PPlugin p2p) P2PPlugins = Add(P2PPlugins, p2p); - if (this is IRpcPlugin rpc) RpcPlugins = Add(RpcPlugins, rpc); - if (this is IPersistencePlugin persistence) PersistencePlugins = Add(PersistencePlugins, persistence); - if (this is IMemoryPoolTxObserverPlugin txObserver) TxObserverPlugins = Add(TxObserverPlugins, txObserver); + Loggers = Plugins.Where(u => u is ILogPlugin).Cast().ToArray(); + P2PPlugins = Plugins.Where(u => u is IP2PPlugin).Cast().ToArray(); + RpcPlugins = Plugins.Where(u => u is IRpcPlugin).Cast().ToArray(); + PersistencePlugins = Plugins.Where(u => u is IPersistencePlugin).Cast().ToArray(); + TxObserverPlugins = Plugins.Where(u => u is IMemoryPoolTxObserverPlugin).Cast().ToArray(); Configure(); } - private T[] Add(T[] plugins, T plugin) - { - var list = new List(plugins); - list.Add(plugin); - return list.ToArray(); - } - public abstract void Configure(); protected virtual void OnPluginsLoaded()