1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2009 Oracle. All rights reserved. 5 * 6 */ 7using System; 8using System.Collections; 9using System.Collections.Generic; 10using System.IO; 11using System.Text; 12using System.Threading; 13using System.Xml; 14using NUnit.Framework; 15using BerkeleyDB; 16 17namespace CsharpAPITest 18{ 19 [TestFixture] 20 public class MutexTest 21 { 22 private string testFixtureHome; 23 private string testFixtureName; 24 private string testName; 25 private string testHome; 26 27 private BTreeDatabase TestDB; 28 private BerkeleyDB.Mutex TestMutex; 29 30 [TestFixtureSetUp] 31 public void RunBeforeTests() 32 { 33 testFixtureName = "MutexTest"; 34 testFixtureHome = "./TestOut/" + testFixtureName; 35 36 Configuration.ClearDir(testFixtureHome); 37 } 38 39 [Test] 40 public void TestGetAndFreeMutex() 41 { 42 testName = "TestGetAndFreeMutex"; 43 testHome = testFixtureHome + "/" + testName; 44 Configuration.ClearDir(testHome); 45 46 DatabaseEnvironmentConfig envConfig = 47 new DatabaseEnvironmentConfig(); 48 envConfig.Create = true; 49 envConfig.UseMPool = true; 50 DatabaseEnvironment env = DatabaseEnvironment.Open( 51 testHome, envConfig); 52 BerkeleyDB.Mutex mutex = env.GetMutex(true, true); 53 mutex.Dispose(); 54 env.Close(); 55 } 56 57 [Test] 58 public void TestLockAndUnlockMutex() 59 { 60 testName = "TestLockAndUnlockMutex"; 61 testHome = testFixtureHome + "/" + testName; 62 Configuration.ClearDir(testHome); 63 64 /* 65 * Open an environment without locking and 66 * deadlock detection. 67 */ 68 DatabaseEnvironmentConfig envConfig = 69 new DatabaseEnvironmentConfig(); 70 envConfig.FreeThreaded = true; 71 envConfig.UseLogging = true; 72 envConfig.Create = true; 73 envConfig.UseMPool = true; 74 DatabaseEnvironment env = DatabaseEnvironment.Open( 75 testHome, envConfig); 76 77 // Open a database. 78 BTreeDatabaseConfig dbConfig = 79 new BTreeDatabaseConfig(); 80 dbConfig.Creation = CreatePolicy.IF_NEEDED; 81 dbConfig.Env = env; 82 TestDB = BTreeDatabase.Open( 83 testName + ".db", dbConfig); 84 85 // Get a mutex which will be used in two threads. 86 TestMutex = env.GetMutex(true, false); 87 88 // Begin two threads to write records into database. 89 Thread mutexThread1 = new Thread( 90 new ThreadStart(MutexThread1)); 91 Thread mutexThread2 = new Thread( 92 new ThreadStart(MutexThread2)); 93 mutexThread1.Start(); 94 mutexThread2.Start(); 95 mutexThread1.Join(); 96 mutexThread2.Join(); 97 98 // Free the mutex. 99 TestMutex.Dispose(); 100 101 // Close all. 102 TestDB.Close(); 103 env.Close(); 104 } 105 106 public void MutexThread1() 107 { 108 TestMutex.Lock(); 109 for (int i = 0; i < 100; i++) 110 TestDB.Put(new DatabaseEntry( 111 BitConverter.GetBytes(i)), 112 new DatabaseEntry(new byte[102400])); 113 TestMutex.Unlock(); 114 } 115 116 public void MutexThread2() 117 { 118 TestMutex.Lock(); 119 for (int i = 0; i < 100; i++) 120 TestDB.Put(new DatabaseEntry( 121 BitConverter.GetBytes(i)), 122 new DatabaseEntry(new byte[102400])); 123 TestMutex.Unlock(); 124 } 125 } 126} 127