public void MsgQueueFileStore_Corrupt_ExtraMessage() { MsgQueueFileStore store = null; int count; QueuedMsgInfo msgInfo; QueuedMsg msg; object persistID; Guid[] ids; // Create a store with a bunch of messages then close // it and explicitly add a message file. Then reopen // the store and verify that it loaded the metadata // for the new message. ClearFolders(); try { store = new MsgQueueFileStore(root); store.Open(); ids = new Guid[MessageCount]; for (int i = 0; i < MessageCount; i++) { msg = new QueuedMsg(); msg.TargetEP = "logical://test/" + i.ToString(); msg.Body = i; msgInfo = new QueuedMsgInfo(null, msg); store.Add(msgInfo, msg); ids[i] = msg.ID; } Assert.AreEqual(MessageCount, store.Count); count = 0; foreach (QueuedMsgInfo i in store) { count++; } Assert.AreEqual(MessageCount, count); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } // Close the store and explicitly create a new message file. Guid newID = Helper.NewGuid(); string newPath = store.GetMessagePath(newID, true); msg = new QueuedMsg(); msg.ID = newID; msg.TargetEP = "logical://foo/bar"; msg.Body = "Hello World!"; msgInfo = new QueuedMsgInfo(null, msg); store.WriteMessage(newPath, msg, msgInfo); // I'm calling this internal write method // so that the metadata won't be saved to the index. store.Close(); // Reopen the store and verify that it loaded the metadata // for the new message and that the other message metadata is // still intact. store.Open(); Assert.AreEqual(MessageCount + 1, store.Count); persistID = store.GetPersistID(newID); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)"logical://foo/bar", msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual("Hello World!", (string)msg.Body); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } } finally { if (store != null) { store.Close(); } } }
public void MsgQueueFileStore_Corrupt_Messages() { MsgQueueFileStore store = null; int count; QueuedMsgInfo msgInfo; QueuedMsg msg; object persistID; Guid[] ids; Dictionary <Guid, bool> corruptIDs; List <string> corruptFiles; // Create a store with a bunch of messages then close // it and corrupt some of the message files. Then attempt // to open the files and verify that the corrupted files // are detected. ClearFolders(); try { store = new MsgQueueFileStore(root); store.Open(); ids = new Guid[MessageCount]; for (int i = 0; i < MessageCount; i++) { msg = new QueuedMsg(); msg.TargetEP = "logical://test/" + i.ToString(); msg.Body = i; msgInfo = new QueuedMsgInfo(null, msg); store.Add(msgInfo, msg); ids[i] = msg.ID; } Assert.AreEqual(MessageCount, store.Count); count = 0; foreach (QueuedMsgInfo i in store) { count++; } Assert.AreEqual(MessageCount, count); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } // Corrupt 1/2 of the message files in various ways. corruptIDs = new Dictionary <Guid, bool>(); corruptFiles = new List <string>(); for (int i = 0; i < MessageCount / 2; i++) { corruptIDs.Add(ids[i], true); corruptFiles.Add(store.GetMessagePath(ids[i], false)); } int cbTruncate = 0; for (int i = 0; i < corruptFiles.Count; i++) { string file = corruptFiles[i]; using (EnhancedFileStream fs = new EnhancedFileStream(file, FileMode.Open, FileAccess.ReadWrite)) { if ((i & 1) == 0) { // Truncate the file by at least one byte int cb; cb = Math.Min((int)fs.Length - 1, cbTruncate++); fs.SetLength(cb); } else { // Mess with a byte at random position in the file. int pos = Helper.Rand((int)fs.Length); byte b; fs.Position = pos; b = (byte)fs.ReadByte(); fs.Position = pos; fs.WriteByte((byte)(~b)); } } } // Load all of the message files and verify that the corrupt files // are detected. for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; if (corruptIDs.ContainsKey(id)) { try { persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); Assert.Fail("Exception expected"); } catch { // Expecting an exception } } else { persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } } } finally { if (store != null) { store.Close(); } } }
public void MsgQueueFileStore_Corrupt_MissingMessage() { MsgQueueFileStore store = null; int count; QueuedMsgInfo msgInfo; QueuedMsg msg; object persistID; Guid[] ids; Dictionary <Guid, bool> deletedIDs; List <string> deletedFiles; // Create a store with a bunch of messages then close // it and delete some of the message files. Then reopen // the store and verify that it deleted metadata entries // for the removed messages. ClearFolders(); try { store = new MsgQueueFileStore(root); store.Open(); ids = new Guid[MessageCount]; for (int i = 0; i < MessageCount; i++) { msg = new QueuedMsg(); msg.TargetEP = "logical://test/" + i.ToString(); msg.Body = i; msgInfo = new QueuedMsgInfo(null, msg); store.Add(msgInfo, msg); ids[i] = msg.ID; } Assert.AreEqual(MessageCount, store.Count); count = 0; foreach (QueuedMsgInfo i in store) { count++; } Assert.AreEqual(MessageCount, count); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } // Close the store and then delete 1/2 of the message files. deletedIDs = new Dictionary <Guid, bool>(); deletedFiles = new List <string>(); for (int i = 0; i < MessageCount / 2; i++) { deletedIDs.Add(ids[i], true); deletedFiles.Add(store.GetMessagePath(ids[i], false)); } store.Close(); foreach (string file in deletedFiles) { File.Delete(file); } // Reopen the store and verify that it noticed the deleted files // and that their metadata was deleted from the index and remaining message // metadata is still intact. store.Open(); Assert.AreEqual(MessageCount - MessageCount / 2, store.Count); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; if (deletedIDs.ContainsKey(id)) { Assert.IsNull(store.GetPersistID(id)); } else { persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } } } finally { if (store != null) { store.Close(); } } }