void PersistInFile(string key, byte[] buffer, long commit) { StorageFramesEvil.WriteFrame(key, commit, buffer, _currentWriter); // make sure that we persist // NB: this is not guaranteed to work on Linux _currentWriter.Flush(true); }
public void load_cache_when_incorrect_data_file() { var currentPath = Path.Combine(_storePath, "EmptyCache"); Directory.CreateDirectory(currentPath); var store = new FileAppendOnlyStore(new DirectoryInfo(currentPath)); //write frame using (var stream = new FileStream(Path.Combine(currentPath, "0.dat"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) StorageFramesEvil.WriteFrame("test-key", 0, Encoding.UTF8.GetBytes("test message"), stream); //write incorrect frame using (var sw = new StreamWriter(Path.Combine(currentPath, "1.dat"))) sw.Write("incorrect frame data"); store.LoadCaches(); var data = store.ReadRecords(0, Int32.MaxValue).ToArray(); Assert.AreEqual(1, data.Length); Assert.AreEqual("test-key", data[0].Key); Assert.AreEqual(0, data[0].StreamVersion); Assert.AreEqual("test message", Encoding.UTF8.GetString(data[0].Data)); Assert.IsTrue(File.Exists(Path.Combine(currentPath, "1.dat"))); }
public void read_write_frame() { string msg = "test message"; Stream stream = new MemoryStream(); StorageFramesEvil.WriteFrame("test-key", 555, Encoding.UTF8.GetBytes(msg), stream); stream.Seek(0, SeekOrigin.Begin); var decoded = StorageFramesEvil.ReadFrame(stream); Assert.AreEqual("test-key", decoded.Name); Assert.AreEqual(555, decoded.Stamp); Assert.AreEqual(msg, Encoding.UTF8.GetString(decoded.Bytes)); }
void CreateCacheFiles() { const string msg = "test messages"; Directory.CreateDirectory(_storePath); for (int index = 0; index < DataFileCount; index++) { using (var stream = new FileStream(Path.Combine(_storePath, index + ".dat"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { for (int i = 0; i < FileMessagesCount; i++) { StorageFramesEvil.WriteFrame("test-key" + index, i, Encoding.UTF8.GetBytes(msg + i), stream); } } } }
public void async_read_write_more_frame() { //GIVEN string msg = "test message"; var path = Path.Combine(Path.GetTempPath(), "lokad-cqrs", Guid.NewGuid() + ".pb"); Directory.CreateDirectory(Path.GetDirectoryName(path)); const int maxIndex = 100; var writeTask = Task.Factory.StartNew(() => { using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { for (int i = 0; i < maxIndex; i++) { StorageFramesEvil.WriteFrame("test-key" + i, i, Encoding.UTF8.GetBytes(msg + i), stream); } } }); //WHEN int index = 0; var readTask = Task.Factory.StartNew(() => { using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)) { while (index < maxIndex) { StorageFrameDecoded decoded; if (StorageFramesEvil.TryReadFrame(stream, out decoded)) { Assert.AreEqual("test-key" + index, decoded.Name); Assert.AreEqual(index, decoded.Stamp); Assert.AreEqual(msg + index, Encoding.UTF8.GetString(decoded.Bytes)); index++; } } } }); writeTask.Wait(); readTask.Wait(); Assert.AreEqual(maxIndex, index); }
public byte[] SaveEnvelopeData(ImmutableEnvelope envelope) { using (var mem = new MemoryStream()) { byte[] data; using (var block = new MemoryStream()) { MessageSerializer.WriteAttributes(envelope.Attributes, block); MessageSerializer.WriteMessage(envelope.Message, envelope.Message.GetType(), block); data = block.ToArray(); } MessageSerializer.WriteCompactInt(Signature, mem); StorageFramesEvil.WriteFrame(envelope.EnvelopeId, DateTime.UtcNow.Ticks, data, mem); return(mem.ToArray()); } }
public void load_cache_when_exist_empty_file() { //write frame using (var stream = new FileStream(Path.Combine(_storePath, "0.dat"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) StorageFramesEvil.WriteFrame("test-key", 0, Encoding.UTF8.GetBytes("test message"), stream); //create empty file using (var sw = new StreamWriter(Path.Combine(_storePath, "1.dat"))) sw.Write(""); _store.LoadCaches(); var data = _store.ReadRecords(0, Int32.MaxValue).ToArray(); Assert.AreEqual(1, data.Length); Assert.AreEqual("test-key", data[0].Key); Assert.AreEqual(1, data[0].StreamVersion); Assert.AreEqual("test message", Encoding.UTF8.GetString(data[0].Data)); Assert.IsFalse(File.Exists(Path.Combine(_storePath, "1.dat"))); }