Skip to content

Commit

Permalink
disable timeout by default, fixes #47
Browse files Browse the repository at this point in the history
  • Loading branch information
bramvdbogaerde committed Aug 4, 2021
1 parent acf430e commit 2799392
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
20 changes: 14 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Client struct {
// stores the SSH connection itself in order to close it after transfer
Conn ssh.Conn

// the clients waits for the given timeout until given up the connection
// the maximal amount of time to wait for a file transfer to complete
Timeout time.Duration

// the absolute path to the remote SCP binary
Expand Down Expand Up @@ -96,11 +96,19 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
if timeout > 0 {
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
} else {
// only wait for waitgroup to complete
select {
case <-c:
return false
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewConfigurer(host string, config *ssh.ClientConfig) *ClientConfigurer {
return &ClientConfigurer{
host: host,
clientConfig: config,
timeout: time.Minute,
timeout: 0, // no timeout by default
remoteBinary: "scp",
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"time"

scp "github.com/bramvdbogaerde/go-scp"
"github.com/bramvdbogaerde/go-scp/auth"
Expand Down Expand Up @@ -106,3 +107,24 @@ func TestDownloadFile(t *testing.T) {
t.Errorf("Got different text than expected, expected %q got, %q", expected, text)
}
}

// TestTimeoutDownload tests that a timeout error is produced if the file is not copied in the given
// amount of time.
func TestTimeoutDownload(t *testing.T) {
client := establishConnection(t)
defer client.Close()
client.Timeout = 1 * time.Millisecond

// Open a file we can transfer to the remote container.
f, _ := os.Open("./data/upload_file.txt")
defer f.Close()

// Create a file name with exotic characters and spaces in them.
// If this test works for this, simpler files should not be a problem.
filename := "Exöt1ç uploaded file.txt"

err := client.CopyFile(f, "/data/"+filename, "0777")
if err == nil {
t.Errorf("Expected a timeout error but transfer succeeded without error")
}
}

0 comments on commit 2799392

Please sign in to comment.