summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/scr037/MutexTest.cs
blob: 0c9ddc632631ffbf6a307f2d1d368da823854a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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();
		}
	}
}