diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
commit | 54df2afaa61c6a03cbb4a33c9b90fa572b6d07b8 (patch) | |
tree | 18147b92b969d25ffbe61935fb63035cac820dd0 /db-4.8.30/test/scr037/MutexTest.cs |
Berkeley DB 4.8 with rust build script for linux.
Diffstat (limited to 'db-4.8.30/test/scr037/MutexTest.cs')
-rw-r--r-- | db-4.8.30/test/scr037/MutexTest.cs | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/db-4.8.30/test/scr037/MutexTest.cs b/db-4.8.30/test/scr037/MutexTest.cs new file mode 100644 index 0000000..0c9ddc6 --- /dev/null +++ b/db-4.8.30/test/scr037/MutexTest.cs @@ -0,0 +1,126 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2009 Oracle. All rights reserved. + * + */ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Xml; +using NUnit.Framework; +using BerkeleyDB; + +namespace CsharpAPITest +{ + [TestFixture] + public class MutexTest + { + private string testFixtureHome; + private string testFixtureName; + private string testName; + private string testHome; + + private BTreeDatabase TestDB; + private BerkeleyDB.Mutex TestMutex; + + [TestFixtureSetUp] + public void RunBeforeTests() + { + testFixtureName = "MutexTest"; + testFixtureHome = "./TestOut/" + testFixtureName; + + Configuration.ClearDir(testFixtureHome); + } + + [Test] + public void TestGetAndFreeMutex() + { + testName = "TestGetAndFreeMutex"; + testHome = testFixtureHome + "/" + testName; + Configuration.ClearDir(testHome); + + DatabaseEnvironmentConfig envConfig = + new DatabaseEnvironmentConfig(); + envConfig.Create = true; + envConfig.UseMPool = true; + DatabaseEnvironment env = DatabaseEnvironment.Open( + testHome, envConfig); + BerkeleyDB.Mutex mutex = env.GetMutex(true, true); + mutex.Dispose(); + env.Close(); + } + + [Test] + public void TestLockAndUnlockMutex() + { + testName = "TestLockAndUnlockMutex"; + testHome = testFixtureHome + "/" + testName; + Configuration.ClearDir(testHome); + + /* + * Open an environment without locking and + * deadlock detection. + */ + DatabaseEnvironmentConfig envConfig = + new DatabaseEnvironmentConfig(); + envConfig.FreeThreaded = true; + envConfig.UseLogging = true; + envConfig.Create = true; + envConfig.UseMPool = true; + DatabaseEnvironment env = DatabaseEnvironment.Open( + testHome, envConfig); + + // Open a database. + BTreeDatabaseConfig dbConfig = + new BTreeDatabaseConfig(); + dbConfig.Creation = CreatePolicy.IF_NEEDED; + dbConfig.Env = env; + TestDB = BTreeDatabase.Open( + testName + ".db", dbConfig); + + // Get a mutex which will be used in two threads. + TestMutex = env.GetMutex(true, false); + + // Begin two threads to write records into database. + Thread mutexThread1 = new Thread( + new ThreadStart(MutexThread1)); + Thread mutexThread2 = new Thread( + new ThreadStart(MutexThread2)); + mutexThread1.Start(); + mutexThread2.Start(); + mutexThread1.Join(); + mutexThread2.Join(); + + // Free the mutex. + TestMutex.Dispose(); + + // Close all. + TestDB.Close(); + env.Close(); + } + + public void MutexThread1() + { + TestMutex.Lock(); + for (int i = 0; i < 100; i++) + TestDB.Put(new DatabaseEntry( + BitConverter.GetBytes(i)), + new DatabaseEntry(new byte[102400])); + TestMutex.Unlock(); + } + + public void MutexThread2() + { + TestMutex.Lock(); + for (int i = 0; i < 100; i++) + TestDB.Put(new DatabaseEntry( + BitConverter.GetBytes(i)), + new DatabaseEntry(new byte[102400])); + TestMutex.Unlock(); + } + } +} |