public void FailingResource_TxStillRolledBack() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction()) { tx.Enlist(new R()); tx.Begin(); try { try { tx.Rollback(); Assert.Fail("Tests is wrong or the transaction doesn't rollback resources."); } catch (Exception) { } Assert.That(tx.Status == TransactionStatus.RolledBack); } catch (RollbackResourceException rex) { // good. Assert.That(rex.FailedResources[0].First, Is.InstanceOf(typeof(R))); } } }
public void CanMoveDirectory() { string dir1 = dllPath.CombineAssert("a"); string dir2 = dllPath.Combine("b"); Assert.That(Directory.Exists(dir2), Is.False); Assert.That(File.Exists(dir2), Is.False, "Lingering files should not be allowed to disrupt the testing."); string aFile = dir1.Combine("file"); File.WriteAllText(aFile, "I should also be moved."); infosCreated.Add(aFile); using (var t = new FileTransaction("moving tx")) { t.Begin(); (t as IDirectoryAdapter).Move(dir1, dir2); Assert.IsFalse(Directory.Exists(dir2), "The directory should not yet exist."); t.Commit(); Assert.That(Directory.Exists(dir2), "Now after committing it should."); infosCreated.Add(dir2); Assert.That(File.Exists(dir2.Combine(Path.GetFileName(aFile))), "And so should the file in the directory."); } }
public void CreateFileTransactionally_Rollback() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filePath = testFixturePath.CombineAssert("temp").Combine("temp2"); infosCreated.Add(filePath); // simply write something to to file. using (StreamWriter wr = File.CreateText(filePath)) wr.WriteLine("Hello"); using (var tx = new FileTransaction("Rollback tx")) { tx.Begin(); using (FileStream fs = tx.Open(filePath, FileMode.Truncate)) { byte[] str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Rollback(); } Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Hello")); }
public void CreateFileTranscationally_Commit() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filepath = testFixturePath.CombineAssert("temp").Combine("test"); if (File.Exists(filepath)) { File.Delete(filepath); } infosCreated.Add(filepath); using (var tx = new FileTransaction("Commit TX")) { tx.Begin(); tx.WriteAllText(filepath, "Transactioned file."); tx.Commit(); Assert.That(tx.Status == TransactionStatus.Committed); } Assert.That(File.Exists(filepath), "The file should exists after the transaction."); Assert.That(File.ReadAllLines(filepath)[0], Is.EqualTo("Transactioned file.")); }
public void CreateFileAndReplaceContents() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filePath = testFixturePath.CombineAssert("temp").Combine("temp__"); infosCreated.Add(filePath); // simply write something to to file. using (StreamWriter wr = File.CreateText(filePath)) wr.WriteLine("Hello"); using (var tx = new FileTransaction()) { tx.Begin(); using (FileStream fs = (tx as IFileAdapter).Create(filePath)) { byte[] str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Commit(); } Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Goodbye")); }
public void CanDelete_Recursively() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. Create directory string pr = dllPath.Combine("testing"); Directory.CreateDirectory(pr); Directory.CreateDirectory(pr.Combine("one")); Directory.CreateDirectory(pr.Combine("two")); Directory.CreateDirectory(pr.Combine("three")); // 2. Write contents File.WriteAllLines(Exts.Combine(pr, "one").Combine("fileone"), new[] { "Hello world", "second line" }); File.WriteAllLines(Exts.Combine(pr, "one").Combine("filetwo"), new[] { "two", "second line" }); File.WriteAllLines(Exts.Combine(pr, "two").Combine("filethree"), new[] { "three", "second line" }); // 3. test using (var t = new FileTransaction()) { t.Begin(); Assert.IsTrue((t as IDirectoryAdapter).Delete(pr, true)); t.Commit(); } }
public void CanNotDelete_NonRecursively_NonEmptyDir() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. create dir and file string dir = dllPath.CombineAssert("testing"); string file = dir.Combine("file"); File.WriteAllText(file, "hello"); // 2. test it using (var t = new FileTransaction("Can not delete non-empty directory")) { IDirectoryAdapter da = t; t.Begin(); Assert.That(da.Delete(dir, false), Is.False, "Did not delete non-empty dir."); IFileAdapter fa = t; fa.Delete(file); Assert.That(da.Delete(dir, false), "After deleting the file in the folder, the folder is also deleted."); t.Commit(); } }
public void CanMove_File() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string folder = dllPath.CombineAssert("testing"); Console.WriteLine(string.Format("Directory \"{0}\"", folder)); string toFolder = dllPath.CombineAssert("testing2"); string file = folder.Combine("file"); Assert.That(File.Exists(file), Is.False); string file2 = folder.Combine("file2"); Assert.That(File.Exists(file2), Is.False); File.WriteAllText(file, "hello world"); File.WriteAllText(file2, "hello world 2"); infosCreated.Add(file); infosCreated.Add(file2); infosCreated.Add(toFolder.Combine("file2")); infosCreated.Add(toFolder.Combine("file")); infosCreated.Add(toFolder); using (var t = new FileTransaction("moving file")) { t.Begin(); Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not exist before move"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not exist before move"); (t as IFileAdapter).Move(file, toFolder); // moving file to folder (t as IFileAdapter).Move(file2, toFolder.Combine("file2")); // moving file to folder+new file name. Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not be visible to the outside"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not be visible to the outside"); t.Commit(); Assert.That(File.Exists(toFolder.Combine("file")), Is.True, "Should be visible to the outside now and since we tried to move it to an existing folder, it should put itself in that folder with its current name."); Assert.That(File.Exists(toFolder.Combine("file2")), Is.True, "Should be visible to the outside now."); } Assert.That(File.ReadAllText(toFolder.Combine("file2")), Is.EqualTo("hello world 2"), "Make sure we moved the contents."); }
public void NotUsingTransactionScope_IsNotDistributed_AboveNegated() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction("Not distributed transaction")) { tx.Begin(); Assert.That(tx.IsAmbient, Is.False); tx.Commit(); } }
public void CannotCommitAfterSettingRollbackOnly() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction()) { tx.Begin(); tx.SetRollbackOnly(); Assert.Throws(typeof(TransactionException), tx.Commit, "Should not be able to commit after rollback is set."); } }
public void Using_NormalStates() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction()) { Assert.That(tx.Status, Is.EqualTo(TransactionStatus.NoTransaction)); tx.Begin(); Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Active)); tx.Commit(); Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Committed)); } }
public void CanCreate_AndFind_Directory_WithinTx() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction("s")) { tx.Begin(); Assert.That((tx as IDirectoryAdapter).Exists("something"), Is.False); (tx as IDirectoryAdapter).Create("something"); Assert.That((tx as IDirectoryAdapter).Exists("something")); tx.Rollback(); } }
public void NoCommit_MeansNoDirectory() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string directoryPath = "testing"; Assert.That(Directory.Exists(directoryPath), Is.False); using (var tx = new FileTransaction()) { tx.Begin(); (tx as IDirectoryAdapter).Create(directoryPath); } Assert.That(!Directory.Exists(directoryPath)); }
public void NonExistentDir() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var t = new FileTransaction()) { t.Begin(); var dir = (t as IDirectoryAdapter); Assert.IsFalse(dir.Exists("/hahaha")); Assert.IsFalse(dir.Exists("another_non_existent")); dir.Create("existing"); Assert.IsTrue(dir.Exists("existing")); } // no commit Assert.IsFalse(Directory.Exists("existing")); }
public void CanDelete_NonRecursively_EmptyDir() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. create dir string dir = dllPath.CombineAssert("testing"); // 2. test it using (var t = new FileTransaction("Can delete empty directory")) { IDirectoryAdapter da = t; t.Begin(); Assert.That(da.Delete(dir, false), "Successfully deleted."); t.Commit(); } }
public void ExistingDirWithTrailingBackslash() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // From http://msdn.microsoft.com/en-us/library/aa364419(VS.85).aspx // An attempt to open a search with a trailing backslash always fails. // --> So I need to make it succeed. using (var t = new FileTransaction()) { t.Begin(); var dir = t as IDirectoryAdapter; dir.Create("something"); Assert.That(dir.Exists("something")); Assert.That(dir.Exists("something\\")); } }
public void CanCreateDirectory_NLengths_DownInNonExistentDirectory() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string directoryPath = "testing/apa/apa2"; Assert.That(Directory.Exists(directoryPath), Is.False); using (var t = new FileTransaction()) { t.Begin(); (t as IDirectoryAdapter).Create(directoryPath); t.Commit(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
public void Using_TransactionScope_IsDistributed_AlsoTestingStatusWhenRolledBack() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (new TransactionScope()) { using (var tx = new FileTransaction()) { tx.Begin(); Assert.That(tx.IsAmbient); tx.Rollback(); Assert.That(tx.IsRollbackOnlySet); Assert.That(tx.Status, Is.EqualTo(TransactionStatus.RolledBack)); } } }
public void CreatingFolder_InTransaction_AndCommitting_MeansExistsAfter() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string directoryPath = "testing"; Assert.That(Directory.Exists(directoryPath), Is.False); using (var tx = new FileTransaction()) { tx.Begin(); (tx as IDirectoryAdapter).Create(directoryPath); tx.Commit(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
public void CreateFileTranscationally_Commit() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filepath = testFixturePath.CombineAssert("temp").Combine("test"); if (File.Exists(filepath)) File.Delete(filepath); infosCreated.Add(filepath); using (var tx = new FileTransaction("Commit TX")) { tx.Begin(); tx.WriteAllText(filepath, "Transactioned file."); tx.Commit(); Assert.That(tx.Status == TransactionStatus.Committed); } Assert.That(File.Exists(filepath), "The file should exists after the transaction."); Assert.That(File.ReadAllLines(filepath)[0], Is.EqualTo("Transactioned file.")); }
public void TwoTransactions_SameName_FirstSleeps() { var t1_started = new ManualResetEvent(false); var t2_started = new ManualResetEvent(false); var t2_done = new ManualResetEvent(false); Exception e = null; // non transacted thread var t1 = new Thread(() => { try { // modifies the file using (var fs = File.OpenWrite("abb")) { Console.WriteLine("t2 start"); Console.Out.Flush(); t2_started.Set(); // before the transacted thread does Console.WriteLine("t2 wait for t1 to start"); Console.Out.Flush(); t1_started.WaitOne(); fs.Write(new byte[] { 0x1 }, 0, 1); fs.Close(); } } catch (Exception ee) { e = ee; } finally { Console.WriteLine("t2 finally"); Console.Out.Flush(); t2_started.Set(); } }); t1.Start(); using (var t = new FileTransaction()) { t.Begin(); Console.WriteLine("t1 wait for t2 to start"); Console.Out.Flush(); t2_started.WaitOne(); try { Console.WriteLine("t1 started"); // the transacted thread should receive ERROR_TRANSACTIONAL_CONFLICT, but it gets permission denied. using (var fs = (t as IFileAdapter).Create("abb")) { fs.WriteByte(0x2); } } finally { Console.WriteLine("t1 finally"); Console.Out.Flush(); t1_started.Set(); } t.Commit(); } if (e != null) { Console.WriteLine(e); Assert.Fail(e.Message); } }
public void FailingResource_TxStillRolledBack() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction()) { tx.Enlist(new R()); tx.Begin(); try { try { tx.Rollback(); Assert.Fail("Tests is wrong or the transaction doesn't rollback resources."); } catch (Exception) { } Assert.That(tx.Status == TransactionStatus.RolledBack); } catch (RollbackResourceException rex) { // good. Assert.That(rex.FailedResources[0].First, Is.InstanceOf(typeof (R))); } } }