Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / csharp / LogConfigTest.cs
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2009, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 using System;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.IO;
11 using System.Text;
12 using System.Xml;
13 using NUnit.Framework;
14 using BerkeleyDB;
15
16 namespace CsharpAPITest
17 {
18         [TestFixture]
19         public class LogConfigTest : CSharpTestFixture
20         {
21
22                 [TestFixtureSetUp]
23                 public void SetUpTestFixture()
24                 {
25                         testFixtureName = "LogConfigTest";
26                         base.SetUpTestfixture();
27                 }
28
29                 [Test]
30                 public void TestConfig()
31                 {
32                         testName = "TestConfig";
33                         SetUpTest(false);
34                         /* 
35                          * Configure the fields/properties and see if 
36                          * they are updated successfully.
37                          */
38                         LogConfig logConfig = new LogConfig();
39                         XmlElement xmlElem = Configuration.TestSetUp(testFixtureName, testName);
40                         Config(xmlElem, ref logConfig, true);
41                         Confirm(xmlElem, logConfig, true);
42                 }
43
44                 [Test, ExpectedException(typeof(ExpectedTestException))]
45                 public void TestFullLogBufferException()
46                 {
47                         testName = "TestFullLogBufferException";
48                         SetUpTest(true);
49
50                         // Open an environment and configured log subsystem.
51                         DatabaseEnvironmentConfig cfg =
52                             new DatabaseEnvironmentConfig();
53                         cfg.Create = true;
54                         cfg.TxnNoSync = true;
55                         cfg.UseTxns = true;
56                         cfg.UseLocking = true;
57                         cfg.UseMPool = true;
58                         cfg.UseLogging = true;
59                         cfg.LogSystemCfg = new LogConfig();
60                         cfg.LogSystemCfg.AutoRemove = false;
61                         cfg.LogSystemCfg.BufferSize = 409600;
62                         cfg.LogSystemCfg.MaxFileSize = 10480;
63                         cfg.LogSystemCfg.NoBuffer = false;
64                         cfg.LogSystemCfg.ZeroOnCreate = true;
65                         cfg.LogSystemCfg.InMemory = true;
66                         DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);
67
68                         BTreeDatabase db;
69                         try
70                         {
71                                 Transaction openTxn = env.BeginTransaction();
72                                 try
73                                 {
74                                         BTreeDatabaseConfig dbConfig =
75                                             new BTreeDatabaseConfig();
76                                         dbConfig.Creation = CreatePolicy.IF_NEEDED;
77                                         dbConfig.Env = env;
78                                         db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
79                                         openTxn.Commit();
80                                 }
81                                 catch (DatabaseException e)
82                                 {
83                                         openTxn.Abort();
84                                         throw e;
85                                 }
86
87                                 Transaction writeTxn = env.BeginTransaction();
88                                 try
89                                 {
90                                         /*
91                                          * Writing 10 large records into in-memory logging 
92                                          * database should throw FullLogBufferException since 
93                                          * the amount of put data is larger than buffer size.
94                                          */
95                                         byte[] byteArr = new byte[204800];
96                                         for (int i = 0; i < 10; i++)
97                                                 db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
98                                                     new DatabaseEntry(byteArr), writeTxn);
99                                         writeTxn.Commit();
100                                 }
101                                 catch (Exception e)
102                                 {
103                                         writeTxn.Abort();
104                                         throw e;
105                                 }
106                                 finally
107                                 {
108                                         db.Close(true);
109                                 }
110                         }
111                         catch (FullLogBufferException e)
112                         {
113                                 Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode);
114                                 throw new ExpectedTestException();
115                         }
116                         finally
117                         {
118                                 env.Close();
119                         }
120                 }
121
122                 [Test]
123                 public void TestLoggingSystemStats()
124                 {
125                         testName = "TestLoggingSystemStats";
126                         SetUpTest(true);
127                         string logDir = "./";
128
129                         Directory.CreateDirectory(testHome + "/" + logDir);
130
131                         DatabaseEnvironmentConfig cfg =
132                             new DatabaseEnvironmentConfig();
133                         cfg.Create = true;
134                         cfg.UseTxns = true;
135                         cfg.AutoCommit = true;
136                         cfg.UseLocking = true;
137                         cfg.UseMPool = true;
138                         cfg.UseLogging = true;
139                         cfg.MPoolSystemCfg = new MPoolConfig();
140                         cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1);
141
142                         cfg.LogSystemCfg = new LogConfig();
143                         cfg.LogSystemCfg.AutoRemove = false;
144                         cfg.LogSystemCfg.BufferSize = 10240;
145                         cfg.LogSystemCfg.Dir = logDir;
146                         cfg.LogSystemCfg.FileMode = 755;
147                         cfg.LogSystemCfg.ForceSync = true;
148                         cfg.LogSystemCfg.InMemory = false;
149                         cfg.LogSystemCfg.MaxFileSize = 1048576;
150                         cfg.LogSystemCfg.NoBuffer = false;
151                         cfg.LogSystemCfg.RegionSize = 204800;
152                         cfg.LogSystemCfg.ZeroOnCreate = true;
153
154                         DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);
155
156                         LogStats stats = env.LoggingSystemStats();
157                         env.PrintLoggingSystemStats();
158                         Assert.AreEqual(10240, stats.BufferSize);
159                         Assert.AreEqual(1, stats.CurrentFile);
160                         Assert.AreNotEqual(0, stats.CurrentOffset);
161                         Assert.AreEqual(0, stats.FileId);
162                         Assert.AreEqual(1048576, stats.FileSize);
163                         Assert.AreEqual(0, stats.InitFileId);
164                         Assert.AreNotEqual(0, stats.MagicNumber);
165                         Assert.AreEqual(0, stats.MaxFileId);
166                         Assert.AreNotEqual(0, stats.PermissionsMode);
167                         Assert.AreEqual(1, stats.Records);
168                         Assert.AreNotEqual(0, stats.RegionLockNoWait);
169                         Assert.LessOrEqual(204800, stats.RegionSize);
170                         Assert.AreNotEqual(0, stats.Version);
171
172                         Transaction openTxn = env.BeginTransaction();
173                         BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
174                         dbConfig.Creation = CreatePolicy.IF_NEEDED;
175                         dbConfig.Env = env;
176                         BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
177                         openTxn.Commit();
178
179                         Transaction writeTxn = env.BeginTransaction();
180                         byte[] byteArr = new byte[1024];
181                         for (int i = 0; i < 1000; i++)
182                                 db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
183                                     new DatabaseEntry(byteArr), writeTxn);
184                         writeTxn.Commit();
185
186                         stats = env.LoggingSystemStats();
187                         Assert.AreNotEqual(0, stats.Bytes);
188                         Assert.AreNotEqual(0, stats.BytesSinceCheckpoint);
189                         Assert.AreNotEqual(0, stats.DiskFileNumber);
190                         Assert.AreNotEqual(0, stats.DiskOffset);
191                         Assert.AreNotEqual(0, stats.MaxCommitsPerFlush);
192                         Assert.AreNotEqual(0, stats.MBytes);
193                         Assert.AreNotEqual(0, stats.MBytesSinceCheckpoint);
194                         Assert.AreNotEqual(0, stats.MinCommitsPerFlush);
195                         Assert.AreNotEqual(0, stats.OverflowWrites);
196                         Assert.AreNotEqual(0, stats.Syncs);
197                         Assert.AreNotEqual(0, stats.Writes);
198                         Assert.AreEqual(0, stats.Reads);
199                         Assert.AreEqual(0, stats.RegionLockWait);
200
201                         stats = env.LoggingSystemStats(true);
202                         stats = env.LoggingSystemStats();
203                         Assert.AreEqual(0, stats.Bytes);
204                         Assert.AreEqual(0, stats.BytesSinceCheckpoint);
205                         Assert.AreEqual(0, stats.MaxCommitsPerFlush);
206                         Assert.AreEqual(0, stats.MBytes);
207                         Assert.AreEqual(0, stats.MBytesSinceCheckpoint);
208                         Assert.AreEqual(0, stats.MinCommitsPerFlush);
209                         Assert.AreEqual(0, stats.OverflowWrites);
210                         Assert.AreEqual(0, stats.Syncs);
211                         Assert.AreEqual(0, stats.Writes);
212                         Assert.AreEqual(0, stats.Reads);
213
214                         env.PrintLoggingSystemStats(true, true);
215
216                         db.Close();
217                         env.Close();
218                 }
219
220                 [Test]
221                 public void TestLsn()
222                 {
223                         testName = "TestLsn";
224                         SetUpTest(true);
225
226                         LSN lsn = new LSN(12, 411);
227                         Assert.AreEqual(12, lsn.LogFileNumber);
228                         Assert.AreEqual(411, lsn.Offset);
229
230                         LSN newLsn = new LSN(15, 410);
231                         Assert.AreEqual(0, LSN.Compare(lsn, lsn));
232                         Assert.Greater(0, LSN.Compare(lsn, newLsn));
233                 }
234                 
235                 public static void Confirm(XmlElement
236                     xmlElement, LogConfig logConfig, bool compulsory)
237                 {
238                         Configuration.ConfirmBool(xmlElement, "AutoRemove",
239                             logConfig.AutoRemove, compulsory);
240                         Configuration.ConfirmUint(xmlElement, "BufferSize",
241                             logConfig.BufferSize, compulsory);
242                         Configuration.ConfirmString(xmlElement, "Dir",
243                             logConfig.Dir, compulsory);
244                         Configuration.ConfirmInt(xmlElement, "FileMode",
245                             logConfig.FileMode, compulsory);
246                         Configuration.ConfirmBool(xmlElement, "ForceSync",
247                             logConfig.ForceSync, compulsory);
248                         Configuration.ConfirmBool(xmlElement, "InMemory",
249                             logConfig.InMemory, compulsory);
250                         Configuration.ConfirmUint(xmlElement, "MaxFileSize",
251                             logConfig.MaxFileSize, compulsory);
252                         Configuration.ConfirmBool(xmlElement, "NoBuffer",
253                             logConfig.NoBuffer, compulsory);
254                         Configuration.ConfirmUint(xmlElement, "RegionSize",
255                             logConfig.RegionSize, compulsory);
256                         Configuration.ConfirmBool(xmlElement, "ZeroOnCreate",
257                             logConfig.ZeroOnCreate, compulsory);
258                 }
259
260                 public static void Config(XmlElement
261                     xmlElement, ref LogConfig logConfig, bool compulsory)
262                 {
263                         uint uintValue = new uint();
264                         int intValue = new int();
265
266                         Configuration.ConfigBool(xmlElement, "AutoRemove",
267                             ref logConfig.AutoRemove, compulsory);
268                         if (Configuration.ConfigUint(xmlElement, "BufferSize",
269                             ref uintValue, compulsory))
270                                 logConfig.BufferSize = uintValue;
271                         Configuration.ConfigString(xmlElement, "Dir",
272                             ref logConfig.Dir, compulsory);
273                         if (Configuration.ConfigInt(xmlElement, "FileMode",
274                             ref intValue, compulsory))
275                                 logConfig.FileMode = intValue;
276                         Configuration.ConfigBool(xmlElement, "ForceSync",
277                             ref logConfig.ForceSync, compulsory);
278                         Configuration.ConfigBool(xmlElement, "InMemory",
279                             ref logConfig.InMemory, compulsory);
280                         if (Configuration.ConfigUint(xmlElement, "MaxFileSize",
281                             ref uintValue, compulsory))
282                                 logConfig.MaxFileSize = uintValue;
283                         Configuration.ConfigBool(xmlElement, "NoBuffer",
284                             ref logConfig.NoBuffer, compulsory);
285                         if (Configuration.ConfigUint(xmlElement, "RegionSize",
286                             ref uintValue, compulsory))
287                                 logConfig.RegionSize = uintValue;
288                         Configuration.ConfigBool(xmlElement, "ZeroOnCreate",
289                             ref logConfig.ZeroOnCreate, compulsory);
290                 }
291         }
292 }