2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2009, 2012 Oracle and/or its affiliates. All rights reserved.
8 using System.Collections;
9 using System.Collections.Generic;
13 using NUnit.Framework;
16 namespace CsharpAPITest
19 public class LogConfigTest : CSharpTestFixture
23 public void SetUpTestFixture()
25 testFixtureName = "LogConfigTest";
26 base.SetUpTestfixture();
30 public void TestConfig()
32 testName = "TestConfig";
35 * Configure the fields/properties and see if
36 * they are updated successfully.
38 LogConfig logConfig = new LogConfig();
39 XmlElement xmlElem = Configuration.TestSetUp(testFixtureName, testName);
40 Config(xmlElem, ref logConfig, true);
41 Confirm(xmlElem, logConfig, true);
44 [Test, ExpectedException(typeof(ExpectedTestException))]
45 public void TestFullLogBufferException()
47 testName = "TestFullLogBufferException";
50 // Open an environment and configured log subsystem.
51 DatabaseEnvironmentConfig cfg =
52 new DatabaseEnvironmentConfig();
56 cfg.UseLocking = 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);
71 Transaction openTxn = env.BeginTransaction();
74 BTreeDatabaseConfig dbConfig =
75 new BTreeDatabaseConfig();
76 dbConfig.Creation = CreatePolicy.IF_NEEDED;
78 db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
81 catch (DatabaseException e)
87 Transaction writeTxn = env.BeginTransaction();
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.
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);
111 catch (FullLogBufferException e)
113 Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode);
114 throw new ExpectedTestException();
123 public void TestLoggingSystemStats()
125 testName = "TestLoggingSystemStats";
127 string logDir = "./";
129 Directory.CreateDirectory(testHome + "/" + logDir);
131 DatabaseEnvironmentConfig cfg =
132 new DatabaseEnvironmentConfig();
135 cfg.AutoCommit = true;
136 cfg.UseLocking = true;
138 cfg.UseLogging = true;
139 cfg.MPoolSystemCfg = new MPoolConfig();
140 cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1);
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;
154 DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);
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);
172 Transaction openTxn = env.BeginTransaction();
173 BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
174 dbConfig.Creation = CreatePolicy.IF_NEEDED;
176 BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
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);
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);
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);
214 env.PrintLoggingSystemStats(true, true);
221 public void TestLsn()
223 testName = "TestLsn";
226 LSN lsn = new LSN(12, 411);
227 Assert.AreEqual(12, lsn.LogFileNumber);
228 Assert.AreEqual(411, lsn.Offset);
230 LSN newLsn = new LSN(15, 410);
231 Assert.AreEqual(0, LSN.Compare(lsn, lsn));
232 Assert.Greater(0, LSN.Compare(lsn, newLsn));
235 public static void Confirm(XmlElement
236 xmlElement, LogConfig logConfig, bool compulsory)
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);
260 public static void Config(XmlElement
261 xmlElement, ref LogConfig logConfig, bool compulsory)
263 uint uintValue = new uint();
264 int intValue = new int();
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);