summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Albright <eric_albright@sil.org>2008-06-11 05:23:36 +0000
committerEric Albright <eric_albright@sil.org>2008-06-11 05:23:36 +0000
commite6814b614036f5aff972953c6f73595d7d4bb0be (patch)
tree2f35ae19476c485aa0209de6683c57685fa67ee2
parentf26a55e59a9c6587fac03362d57617b93981ec6e (diff)
downloadenchant-e6814b614036f5aff972953c6f73595d7d4bb0be.tar.gz
Add broker singleton
git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@24227 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
-rw-r--r--src/bindings/Enchant.Net/Broker.cs16
-rw-r--r--unittests/Enchant.Net.Tests/BrokerTests.cs50
2 files changed, 64 insertions, 2 deletions
diff --git a/src/bindings/Enchant.Net/Broker.cs b/src/bindings/Enchant.Net/Broker.cs
index e7a53d2..186c860 100644
--- a/src/bindings/Enchant.Net/Broker.cs
+++ b/src/bindings/Enchant.Net/Broker.cs
@@ -34,6 +34,22 @@ namespace Enchant
private readonly Dictionary<string, WeakReference> _pwlDictionaryCache;
private bool _cacheDictionaries = true;
+ private static Broker _defaultBroker;
+ /// <summary>
+ /// Singleton for apps that want to use that pattern
+ /// </summary>
+ public static Broker Default
+ {
+ get
+ {
+ if(_defaultBroker == null || _defaultBroker._disposed)
+ {
+ _defaultBroker = new Broker();
+ }
+ return _defaultBroker;
+ }
+ }
+
public Broker()
{
_handle = Bindings.enchant_broker_init();
diff --git a/unittests/Enchant.Net.Tests/BrokerTests.cs b/unittests/Enchant.Net.Tests/BrokerTests.cs
index 288fb69..a53afd0 100644
--- a/unittests/Enchant.Net.Tests/BrokerTests.cs
+++ b/unittests/Enchant.Net.Tests/BrokerTests.cs
@@ -215,7 +215,7 @@ namespace Enchant.Tests
using (dictionaryFirstRequest = broker.RequestDictionary("en_US")) {}
Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
- Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
+ Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
}
}
@@ -294,6 +294,24 @@ namespace Enchant.Tests
}
[Test]
+ public void DictionaryKeepsBrokerAlive()
+ {
+ WeakReference brokerReference;
+ Dictionary dictionary = GetDictionaryAllowingBrokerToGoOutOfScope(out brokerReference);
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ Assert.IsTrue(brokerReference.IsAlive);
+ GC.KeepAlive(dictionary);
+ }
+
+ private static Dictionary GetDictionaryAllowingBrokerToGoOutOfScope(out WeakReference brokerReference)
+ {
+ Broker broker = new Broker();
+ brokerReference = new WeakReference(broker);
+ return broker.RequestDictionary("en_US");
+ }
+
+ [Test]
public void Finalize_DictionaryGoesOutOfScope_Finalized()
{
using (Broker broker = new Broker())
@@ -302,7 +320,6 @@ namespace Enchant.Tests
WeakReference dictionaryReference = GetDictionaryReference(broker);
GC.Collect();
GC.WaitForPendingFinalizers();
- GC.Collect();
Assert.IsFalse(dictionaryReference.IsAlive);
}
}
@@ -313,5 +330,34 @@ namespace Enchant.Tests
Dictionary dictionary = broker.RequestDictionary("en_US");
return new WeakReference(dictionary);
}
+
+ [Test]
+ public void Default_ReturnsNonNull()
+ {
+ Assert.IsNotNull(Broker.Default);
+ }
+
+ [Test]
+ public void Default_CalledTwice_ReturnsSame()
+ {
+ Assert.AreSame(Broker.Default, Broker.Default);
+ }
+
+ [Test]
+ public void Default_Disposed_ReturnsNonNull()
+ {
+ Broker.Default.Dispose();
+ Assert.IsNotNull(Broker.Default);
+ }
+
+ [Test]
+ public void Default_Disposed_ReturnsNewObject()
+ {
+ Broker originalBroker = Broker.Default;
+ originalBroker.Dispose();
+
+ Assert.AreNotSame(originalBroker, Broker.Default);
+ }
+
}
}