//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTrackOffsetAfterRewind() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldTrackOffsetAfterRewind() { // given Stream actual = mock(typeof(Stream)); ProgressListener progressListener = mock(typeof(ProgressListener)); ProgressTrackingOutputStream.Progress progress = new ProgressTrackingOutputStream.Progress(progressListener, 0); using (ProgressTrackingOutputStream @out = new ProgressTrackingOutputStream(actual, progress)) { @out.WriteByte(new sbyte[20]); // when progress.RewindTo(15); // i.e. the next 5 bytes we don't track @out.WriteByte(new sbyte[3]); // now there should be 2 untracked bytes left @out.WriteByte(new sbyte[9]); // this one should report 7 } progress.Done(); // then InOrder inOrder = inOrder(progressListener); inOrder.verify(progressListener).add(20); inOrder.verify(progressListener).add(7); inOrder.verify(progressListener).done(); verifyNoMoreInteractions(progressListener); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private void doStatusPolling(boolean verbose, String consoleURL, String bearerToken) throws java.io.IOException, InterruptedException, org.neo4j.commandline.admin.CommandFailed private void DoStatusPolling(bool verbose, string consoleURL, string bearerToken) { _outsideWorld.stdOutLine("We have received your export and it is currently being loaded into your cloud instance."); _outsideWorld.stdOutLine("You can wait here, or abort this command and head over to the console to be notified of when your database is running."); string bearerTokenHeader = "Bearer " + bearerToken; ProgressTrackingOutputStream.Progress statusProgress = new ProgressTrackingOutputStream.Progress(_progressListenerFactory.create("Import status", 3L), 0); bool firstRunning = true; long importStartedTimeout = DateTimeHelper.CurrentUnixTimeMillis() + 90 * 1000; // timeout to switch from first running to loading = 1.5 minute while (!statusProgress.Done) { string status = GetDatabaseStatus(verbose, SafeUrl(consoleURL + "/import/status"), bearerTokenHeader); switch (status) { case "running": // It could happen that the very first call of this method is so fast, that the database is still in state // "running". So we need to check if this is the case and ignore the result in that case and only // take this result as valid, once the status loading or restoring was seen before. if (!firstRunning) { statusProgress.RewindTo(0); statusProgress.Add(3); statusProgress.Done(); } else { bool passedStartImportTimeout = DateTimeHelper.CurrentUnixTimeMillis() > importStartedTimeout; if (passedStartImportTimeout) { throw new CommandFailed("We're sorry, it couldn't be detected that the import was started, " + "please check the console for further details."); } } break; case "loading": firstRunning = false; statusProgress.RewindTo(0); statusProgress.Add(1); break; case "restoring": firstRunning = false; statusProgress.RewindTo(0); statusProgress.Add(2); break; case "loading failed": throw new CommandFailed("We're sorry, something has gone wrong. We did not recognize the file you uploaded as a valid Neo4j dump file. " + "Please check the file and try again. If you have received this error after confirming the type of file being uploaded," + "please open a support case."); default: throw new CommandFailed(string.Format("We're sorry, something has failed during the loading of your database. " + "Please try again and if this problem persists, please open up a support case. Database status: {0}", status)); } _sleeper.sleep(2000); } _outsideWorld.stdOutLine("Your data was successfully pushed to cloud and is now running."); }
/// <summary> /// Do the actual transfer of the source (a Neo4j database dump) to the target. /// </summary> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public void copy(boolean verbose, String consoleURL, java.nio.file.Path source, String bearerToken) throws org.neo4j.commandline.admin.CommandFailed public override void Copy(bool verbose, string consoleURL, Path source, string bearerToken) { try { string bearerTokenHeader = "Bearer " + bearerToken; long crc32Sum = CalculateCrc32HashOfFile(source); URL signedURL = InitiateCopy(verbose, SafeUrl(consoleURL + "/import"), crc32Sum, bearerTokenHeader); URL uploadLocation = InitiateResumableUpload(verbose, signedURL); long sourceLength = _outsideWorld.fileSystem().getFileSize(source.toFile()); // Enter the resume:able upload loop long position = 0; int retries = 0; ThreadLocalRandom random = ThreadLocalRandom.current(); ProgressTrackingOutputStream.Progress uploadProgress = new ProgressTrackingOutputStream.Progress(_progressListenerFactory.create("Upload", sourceLength), position); while (!ResumeUpload(verbose, source, sourceLength, position, uploadLocation, uploadProgress)) { position = GetResumablePosition(verbose, sourceLength, uploadLocation); if (position == POSITION_UPLOAD_COMPLETED) { // This is somewhat unexpected, we didn't get an OK from the upload, but when we asked about how far the upload // got it responded that it was fully uploaded. I'd guess we're fine here. break; } // Truncated exponential backoff if (retries > 50) { throw new CommandFailed("Upload failed after numerous attempts. The upload can be resumed with this command: TODO"); } long backoffFromRetryCount = SECONDS.toMillis(1 << retries++) + random.Next(1_000); _sleeper.sleep(min(backoffFromRetryCount, _maximumRetryBackoff)); } uploadProgress.Done(); TriggerImportProtocol(verbose, SafeUrl(consoleURL + "/import/upload-complete"), crc32Sum, bearerTokenHeader); DoStatusPolling(verbose, consoleURL, bearerToken); } catch (Exception e) when(e is InterruptedException || e is IOException) { throw new CommandFailed(e.Message, e); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTrackSingleByteWrites() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldTrackSingleByteWrites() { // given Stream actual = mock(typeof(Stream)); ProgressListener progressListener = mock(typeof(ProgressListener)); ProgressTrackingOutputStream.Progress progress = new ProgressTrackingOutputStream.Progress(progressListener, 0); using (ProgressTrackingOutputStream @out = new ProgressTrackingOutputStream(actual, progress)) { // when @out.WriteByte(10); } progress.Done(); // then verify(progressListener).add(1); verify(progressListener).done(); verifyNoMoreInteractions(progressListener); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTrackOffsetByteArrayWrites() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldTrackOffsetByteArrayWrites() { // given Stream actual = mock(typeof(Stream)); ProgressListener progressListener = mock(typeof(ProgressListener)); ProgressTrackingOutputStream.Progress progress = new ProgressTrackingOutputStream.Progress(progressListener, 0); int length = 5; using (ProgressTrackingOutputStream @out = new ProgressTrackingOutputStream(actual, progress)) { // when @out.Write(new sbyte[length * 2], 2, length); } progress.Done(); // then verify(progressListener).add(length); verify(progressListener).done(); verifyNoMoreInteractions(progressListener); }
/// <summary> /// Uploads source from the given position to the upload location. /// </summary> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private boolean resumeUpload(boolean verbose, java.nio.file.Path source, long sourceLength, long position, java.net.URL uploadLocation, ProgressTrackingOutputStream.Progress uploadProgress) throws java.io.IOException, org.neo4j.commandline.admin.CommandFailed private bool ResumeUpload(bool verbose, Path source, long sourceLength, long position, URL uploadLocation, ProgressTrackingOutputStream.Progress uploadProgress) { HttpURLConnection connection = ( HttpURLConnection )uploadLocation.openConnection(); try { connection.RequestMethod = "PUT"; long contentLength = sourceLength - position; connection.setRequestProperty("Content-Length", contentLength.ToString()); connection.FixedLengthStreamingMode = contentLength; if (position > 0) { // If we're not starting from the beginning we need to specify what range we're uploading in this format connection.setRequestProperty("Content-Range", format("bytes %d-%d/%d", position, sourceLength - 1, sourceLength)); } connection.DoOutput = true; uploadProgress.RewindTo(position); using (Stream sourceStream = new FileStream(source.toFile(), FileMode.Open, FileAccess.Read), Stream targetStream = connection.OutputStream) { SafeSkip(sourceStream, position); IOUtils.copy(new BufferedInputStream(sourceStream), new ProgressTrackingOutputStream(targetStream, uploadProgress)); } int responseCode = connection.ResponseCode; switch (responseCode) { case HTTP_OK: return(true); // the file is now uploaded, all good case HTTP_INTERNAL_ERROR: case HTTP_UNAVAILABLE: DebugErrorResponse(verbose, connection); return(false); default: throw UnexpectedResponse(verbose, connection, "Resumable database upload"); } } finally { connection.disconnect(); } }