Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #235 from pdalpra/timeout-as-time.Duration
Browse files Browse the repository at this point in the history
Use time.Duration as type for timeouts
  • Loading branch information
calavera committed Jun 7, 2016
2 parents 9245c24 + da7b0e7 commit 8c2141e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
7 changes: 4 additions & 3 deletions client/container_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package client

import (
"net/url"
"strconv"
"time"

timetypes "github.com/docker/engine-api/types/time"
"golang.org/x/net/context"
)

// ContainerRestart stops and starts a container again.
// It makes the daemon to wait for the container to be up again for
// a specific amount of time, given the timeout.
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout int) error {
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout time.Duration) error {
query := url.Values{}
query.Set("t", strconv.Itoa(timeout))
query.Set("t", timetypes.DurationToSecondsString(timeout))
resp, err := cli.post(ctx, "/containers/"+containerID+"/restart", query, nil, nil)
ensureReaderClosed(resp)
return err
Expand Down
5 changes: 3 additions & 2 deletions client/container_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"testing"
"time"

"golang.org/x/net/context"
)
Expand All @@ -15,7 +16,7 @@ func TestContainerRestartError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRestart(context.Background(), "nothing", 0)
err := client.ContainerRestart(context.Background(), "nothing", 0*time.Second)
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
Expand All @@ -39,7 +40,7 @@ func TestContainerRestart(t *testing.T) {
}),
}

err := client.ContainerRestart(context.Background(), "container_id", 100)
err := client.ContainerRestart(context.Background(), "container_id", 100*time.Second)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions client/container_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package client

import (
"net/url"
"strconv"
"time"

timetypes "github.com/docker/engine-api/types/time"
"golang.org/x/net/context"
)

// ContainerStop stops a container without terminating the process.
// The process is blocked until the container stops or the timeout expires.
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout int) error {
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout time.Duration) error {
query := url.Values{}
query.Set("t", strconv.Itoa(timeout))
query.Set("t", timetypes.DurationToSecondsString(timeout))
resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
ensureReaderClosed(resp)
return err
Expand Down
5 changes: 3 additions & 2 deletions client/container_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"testing"
"time"

"golang.org/x/net/context"
)
Expand All @@ -15,7 +16,7 @@ func TestContainerStopError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerStop(context.Background(), "nothing", 0)
err := client.ContainerStop(context.Background(), "nothing", 0*time.Second)
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
Expand All @@ -39,7 +40,7 @@ func TestContainerStop(t *testing.T) {
}),
}

err := client.ContainerStop(context.Background(), "container_id", 100)
err := client.ContainerStop(context.Background(), "container_id", 100*time.Second)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"io"
"time"

"golang.org/x/net/context"

Expand Down Expand Up @@ -37,11 +38,11 @@ type APIClient interface {
ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error
ContainerRename(ctx context.Context, container, newContainerName string) error
ContainerResize(ctx context.Context, container string, options types.ResizeOptions) error
ContainerRestart(ctx context.Context, container string, timeout int) error
ContainerRestart(ctx context.Context, container string, timeout time.Duration) error
ContainerStatPath(ctx context.Context, container, path string) (types.ContainerPathStat, error)
ContainerStats(ctx context.Context, container string, stream bool) (io.ReadCloser, error)
ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error
ContainerStop(ctx context.Context, container string, timeout int) error
ContainerStop(ctx context.Context, container string, timeout time.Duration) error
ContainerTop(ctx context.Context, container string, arguments []string) (types.ContainerProcessList, error)
ContainerUnpause(ctx context.Context, container string) error
ContainerUpdate(ctx context.Context, container string, updateConfig container.UpdateConfig) error
Expand Down
12 changes: 12 additions & 0 deletions types/time/duration_convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package time

import (
"strconv"
"time"
)

// DurationToSecondsString converts the specified duration to the number
// seconds it represents, formatted as a string.
func DurationToSecondsString(duration time.Duration) string {
return strconv.FormatFloat(duration.Seconds(), 'f', 0, 64)
}
26 changes: 26 additions & 0 deletions types/time/duration_convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package time

import (
"testing"
"time"
)

func TestDurationToSecondsString(t *testing.T) {
cases := []struct {
in time.Duration
expected string
}{
{0 * time.Second, "0"},
{1 * time.Second, "1"},
{1 * time.Minute, "60"},
{24 * time.Hour, "86400"},
}

for _, c := range cases {
s := DurationToSecondsString(c.in)
if s != c.expected {
t.Errorf("wrong value for input `%v`: expected `%s`, got `%s`", c.in, c.expected, s)
t.Fail()
}
}
}

0 comments on commit 8c2141e

Please sign in to comment.