-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SCP : limit the used bandwidth #208
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
782ff9b
Add an option to limit the used bandwidth with SCP upload and downloa…
lguerin e81fdb8
Verbose failed tests
lguerin dec00ef
Fix typo and clarify rate transfer comment
lguerin 9c424f9
Remove underscores from Test method's name
lguerin 84990ad
Fix typo
lguerin d18e9d9
Refactor SCP arguments
lguerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/net/schmizz/sshj/xfer/scp/AbstractSCPClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.schmizz.sshj.xfer.scp; | ||
|
||
abstract class AbstractSCPClient { | ||
|
||
protected final SCPEngine engine; | ||
protected int bandwidthLimit; | ||
|
||
AbstractSCPClient(SCPEngine engine) { | ||
this.engine = engine; | ||
} | ||
|
||
AbstractSCPClient(SCPEngine engine, int bandwidthLimit) { | ||
this.engine = engine; | ||
this.bandwidthLimit = bandwidthLimit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.schmizz.sshj.xfer.scp; | ||
|
||
import com.hierynomus.sshj.test.SshFixture; | ||
import com.hierynomus.sshj.test.util.FileUtil; | ||
import net.schmizz.sshj.SSHClient; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
public class SCPFileTransferTest { | ||
|
||
public static final String DEFAULT_FILE_NAME = "my_file.txt"; | ||
File targetDir; | ||
File sourceFile; | ||
File targetFile; | ||
SSHClient sshClient; | ||
|
||
@Rule | ||
public SshFixture fixture = new SshFixture(); | ||
|
||
@Rule | ||
public TemporaryFolder tempFolder = new TemporaryFolder(); | ||
|
||
@Before | ||
public void init() throws IOException { | ||
sourceFile = tempFolder.newFile(DEFAULT_FILE_NAME); | ||
FileUtil.writeToFile(sourceFile, "This is my file"); | ||
targetDir = tempFolder.newFolder(); | ||
targetFile = new File(targetDir + File.separator + DEFAULT_FILE_NAME); | ||
sshClient = fixture.setupConnectedDefaultClient(); | ||
sshClient.authPassword("test", "test"); | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
if (targetFile.exists()) { | ||
targetFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void should_SCP_Upload_File() throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the tests, but could you name them without the underscores and with just camelcasing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void should_SCP_Upload_File_With_Bandwidth_Limit() throws IOException { | ||
// Limit upload transfert at 2Mo/s | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(16000); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void should_SCP_Download_File() throws IOException { | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.download(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void should_SCP_Download_File_With_Bandwidth_Limit() throws IOException { | ||
// Limit download transfert at 128Ko/s | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(1024); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.download(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the extra 'v' here? It is not used currently. The
LIMIT
seems to use it, but as it is an enum it is always the empty string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some scp options needs a key/value structure. The value is initialized with an empty string but can be set through the helper class SCPArguments. This the way I have used to set the value of the LIMIT arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh but this is even more evil, as this is an enum, setting the value for 1 process means that you're setting it (inadvertently) for all processes. Because there is only 1 instance of the same enum key in a JVM. So please refactor that and remove the
setValue(..)