Ejemplo n.º 1
0
        public async Task CanHandleAttachmentExceptionsGracefully_Smuggler()
        {
            var backupPath = NewDataPath("BackupFolder");
            var server = GetNewServer();

            var resetCount = 0;

            var forwarder = new PortForwarder(8070, 8079, (totalRead, bytes, offset, count) =>
            {
                var payload = System.Text.Encoding.UTF8.GetString(bytes, offset, count);
                //reset count is requred as raven can retry attachment download
                if (payload.Contains("GET /static/users/678 ") && resetCount < 5)
                {
                    resetCount++;
                    return true;
                }
                return false;
            });
            forwarder.Forward();
            try
            {
                using (var store = new DocumentStore
                {
                    Url = "http://localhost:8079"
                }.Initialize())
                {
                    InsertAttachments(store, 2000);
                }

                var options = new SmugglerOptions
                {
                    Limit = 1500,
                    BackupPath = backupPath,
                };
                var dumper = new SmugglerApi(options, new RavenConnectionStringOptions
                {
                    Url = "http://localhost:8070",
                });

                var allAttachments = new List<RavenJObject>();

                var memoryStream = new MemoryStream();
                Assert.Throws<AggregateException>(() => dumper.ExportData(memoryStream, null, true).Wait());

                memoryStream.Position = 0;
                using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var att1 = chunk1["Attachments"] as RavenJArray;
                    allAttachments.AddRange(att1.Values<RavenJObject>());
                }

                var memoryStream2 = new MemoryStream();
                await dumper.ExportData(memoryStream2, null, true);
                memoryStream2.Position = 0;
                using (var stream = new GZipStream(memoryStream2, CompressionMode.Decompress))
                {
                    var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var attr2 = chunk2["Attachments"] as RavenJArray;
                    allAttachments.AddRange(attr2.Values<RavenJObject>());
                }

                Assert.Equal(2000, allAttachments.Count());

                IOExtensions.DeleteDirectory(backupPath);
            }
            finally
            {
                forwarder.Stop();
                server.Dispose();
            }
        }
Ejemplo n.º 2
0
        public async Task CanHandleAttachmentExceptionsGracefully_Smuggler()
        {
            var backupPath = NewDataPath("BackupFolder");
            var server     = GetNewServer();

            var resetCount = 0;

            var forwarder = new PortForwarder(8070, 8079, (totalRead, bytes, offset, count) =>
            {
                var payload = System.Text.Encoding.UTF8.GetString(bytes, offset, count);
                //reset count is requred as raven can retry attachment download
                if (payload.Contains("GET /static/users/678 ") && resetCount < 5)
                {
                    resetCount++;
                    return(true);
                }
                return(false);
            });

            forwarder.Forward();
            try
            {
                using (var store = new DocumentStore
                {
                    Url = "http://localhost:8079"
                }.Initialize())
                {
                    InsertAttachments(store, 2000);
                }

                var options = new SmugglerOptions
                {
                    Limit      = 1500,
                    BackupPath = backupPath,
                };
                var dumper = new SmugglerApi(options, new RavenConnectionStringOptions
                {
                    Url = "http://localhost:8070",
                });

                var allAttachments = new List <RavenJObject>();

                var memoryStream = new MemoryStream();
                Assert.Throws <AggregateException>(() => dumper.ExportData(memoryStream, null, true).Wait());

                memoryStream.Position = 0;
                using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var att1   = chunk1["Attachments"] as RavenJArray;
                    allAttachments.AddRange(att1.Values <RavenJObject>());
                }

                var memoryStream2 = new MemoryStream();
                await dumper.ExportData(memoryStream2, null, true);

                memoryStream2.Position = 0;
                using (var stream = new GZipStream(memoryStream2, CompressionMode.Decompress))
                {
                    var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var attr2  = chunk2["Attachments"] as RavenJArray;
                    allAttachments.AddRange(attr2.Values <RavenJObject>());
                }

                Assert.Equal(2000, allAttachments.Count());

                IOExtensions.DeleteDirectory(backupPath);
            }
            finally
            {
                forwarder.Stop();
                server.Dispose();
            }
        }
Ejemplo n.º 3
0
        public async Task CanHandleDocumentExceptionsGracefully_Smuggler()
        {
            var backupPath = NewDataPath("BackupFolder");
            var server = GetNewServer();

            var alreadyReset = false;

            var forwarder = new PortForwarder(8070, 8079, (totalRead, bytes, offset, count) =>
            {
                if (alreadyReset == false && totalRead > 10000)
                {
                    alreadyReset = true;
                    return true;
                }
                return false;
            });
            forwarder.Forward();
            try
            {
                using (var store = new DocumentStore
                {
                    Url = "http://localhost:8079"
                }.Initialize())
                {
                    InsertUsers(store, 0, 2000);
                }

                var options = new SmugglerOptions
                {
                    Limit = 1500,
                    BackupPath = backupPath,
                };
                var dumper = new SmugglerApi(options, new RavenConnectionStringOptions
                {
                    Url = "http://localhost:8070",
                });

                var allDocs = new List<RavenJObject>();

                var memoryStream = new MemoryStream();
                Assert.Throws<AggregateException>(() => dumper.ExportData(memoryStream, null, true).Wait());

                memoryStream.Position = 0;
                using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var doc1 = chunk1["Docs"] as RavenJArray;
                    allDocs.AddRange(doc1.Values<RavenJObject>());
                }

                var memoryStream2 = new MemoryStream();
                await dumper.ExportData(memoryStream2, null, true);
                memoryStream2.Position = 0;
                using (var stream = new GZipStream(memoryStream2, CompressionMode.Decompress))
                {
                    var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var doc2 = chunk2["Docs"] as RavenJArray;
                    allDocs.AddRange(doc2.Values<RavenJObject>());
                }

                Assert.Equal(2000, allDocs.Count(d => (d.Value<string>("Name") ?? String.Empty).StartsWith("User")));
                
                IOExtensions.DeleteDirectory(backupPath);
            }
            finally
            {
                forwarder.Stop();
                server.Dispose();
            }
        }
Ejemplo n.º 4
0
        public async Task CanHandleDocumentExceptionsGracefully_Smuggler()
        {
            var backupPath = NewDataPath("BackupFolder");
            var server     = GetNewServer();


            var shouldBreak = true;
            var afterStream = false;
            var bytesCount  = 0;

            var forwarder = new PortForwarder(8070, 8079, (totalRead, bytes, offset, count) =>
            {
                var str = Encoding.UTF8.GetString(bytes, offset, count);
                if (str.StartsWith("GET /streams/docs") && shouldBreak)
                {
                    afterStream = true;
                    bytesCount  = totalRead;
                }
                if (shouldBreak && totalRead > bytesCount + 14000 && afterStream)
                {
                    shouldBreak = false;
                    return(true);
                }

                return(false);
            });

            forwarder.Forward();
            try
            {
                using (var store = new DocumentStore
                {
                    Url = "http://localhost:8079"
                }.Initialize())
                {
                    InsertUsers(store, 0, 2000);
                }

                var options = new SmugglerOptions
                {
                    Limit      = 1500,
                    BackupPath = backupPath,
                };
                var dumper = new SmugglerApi(options, new RavenConnectionStringOptions
                {
                    Url = "http://localhost:8070",
                });

                var allDocs = new List <RavenJObject>();

                var memoryStream = new MemoryStream();
                Assert.Throws <AggregateException>(() => dumper.ExportData(memoryStream, null, true).Wait());

                memoryStream.Position = 0;
                using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var doc1   = chunk1["Docs"] as RavenJArray;
                    allDocs.AddRange(doc1.Values <RavenJObject>());
                }

                var memoryStream2 = new MemoryStream();
                await dumper.ExportData(memoryStream2, null, true);

                memoryStream2.Position = 0;
                using (var stream = new GZipStream(memoryStream2, CompressionMode.Decompress))
                {
                    var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject;
                    var doc2   = chunk2["Docs"] as RavenJArray;
                    allDocs.AddRange(doc2.Values <RavenJObject>());
                }

                Assert.Equal(2000, allDocs.Count(d => (d.Value <string>("Name") ?? String.Empty).StartsWith("User")));

                IOExtensions.DeleteDirectory(backupPath);
            }
            finally
            {
                forwarder.Stop();
                server.Dispose();
            }
        }