Beispiel #1
0
        public void when_a_document_crashes_during_an_update_the_previous_version_is_still_available()
        {
            var rnd = new Random();

            // Cause a power-off like failure when writing a document.
            // We simulate this with a custom stream
            var baseStream = new MemoryStream();
            var stream     = new CutoffStream(baseStream);

            var subject = Database.TryConnect(stream);

            // Write a good document, then a failure
            subject.WriteDocument("repeat", MakeTestDocument());
            stream.CutoffAfter(rnd.Next(10, 900));
            try {
                subject.WriteDocument("repeat", MakeTestDocument());
            } catch {
                // Ignore
            }

            Assert.That(stream.HasCutoff(), Is.True, "Failed to break output stream");

            // Load a new database from the truncated data
            baseStream.Rewind();
            var rawResult = baseStream.ToArray();
            var newStream = new MemoryStream(rawResult);

            var result = Database.TryConnect(newStream);
            var ok     = result.Get("repeat", out var resultData);

            Assert.That(ok, Is.True, "Fully written document was lost");

            var temp = new MemoryStream();

            resultData.CopyTo(temp); // this will cause each page's CRC to be tested.
        }
Beispiel #2
0
        public void crashing_half_way_through_a_write_results_in_no_document_added_but_a_readable_database()
        {
            var rnd = new Random();

            // Cause a power-off like failure when writing a document.
            // We simulate this with a custom stream
            var baseStream = new MemoryStream();
            var stream     = new CutoffStream(baseStream);

            var subject = Database.TryConnect(stream);

            // Write a good document, then a failure
            subject.WriteDocument("successful", MakeTestDocument());
            stream.CutoffAfter(rnd.Next(10, 900));
            try {
                subject.WriteDocument("failure", MakeTestDocument());
            }
            catch (Exception ex) {
                Console.WriteLine("Exception was triggered, as expected: " + ex.Message);
            }

            Assert.That(stream.HasCutoff(), Is.True, "Failed to break output stream");

            // Load a new database from the truncated data
            baseStream.Rewind();
            var rawResult = baseStream.ToArray();
            var newStream = new MemoryStream(rawResult);

            var result = Database.TryConnect(newStream);
            var ok     = result.Get("successful", out _);

            Assert.That(ok, Is.True, "Fully written document was lost");

            ok = result.Get("failure", out _);
            Assert.That(ok, Is.False, "Partly written document was loaded without an error");
        }