Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Parse duration correctly and add acceptance test
Browse files Browse the repository at this point in the history
  • Loading branch information
theothertomelliott committed Mar 8, 2019
1 parent ddaeda4 commit 8e0f1f8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
36 changes: 36 additions & 0 deletions services/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package services

import (
"encoding/json"
"time"

"github.com/pkg/errors"
)

type Duration struct {
time.Duration
}

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var (
v interface{}
err error
)
if err = json.Unmarshal(b, &v); err != nil {
return errors.WithStack(err)
}
switch value := v.(type) {
case string:
d.Duration, err = time.ParseDuration(value)
if err != nil {
return errors.WithStack(err)
}
return nil
default:
return errors.New("invalid duration")
}
}
4 changes: 2 additions & 2 deletions services/serviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ServiceConfig struct {

// Timeout for terminating a service runner. If termination has not completed after this amount
// of time, the runner will be killed.
TerminationTimeout *time.Duration `json:"terminationTimeout,omitempty"`
TerminationTimeout *Duration `json:"terminationTimeout,omitempty"`
}

// GetTerminationTimeout returns the timeout for termination, if no timeout is set, the
Expand All @@ -60,7 +60,7 @@ func (c *ServiceConfig) GetTerminationTimeout() time.Duration {
if c.TerminationTimeout == nil {
return 30 * time.Second
}
return *c.TerminationTimeout
return c.TerminationTimeout.Duration
}

// Backend returns the default backend for this service
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func expectErrorFromURL(t *testing.T, url string) {

_, err := getFromURL(url)
if err == nil {
t.Error("expected an error when service stopped")
t.Error("expected an error loading URL when service stopped")
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/acceptance/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ func TestStop(t *testing.T) {
"http://127.0.0.1:51234/": "Hello",
},
},
{
name: "graceless shutdown with timeout",
dataDir: "testdata/graceless_shutdown",
startArgs: []string{"start", "gracelessWithTimeout"},
stopArgs: []string{"stop", "gracelessWithTimeout"},
expectedURLs: map[string]string{
"http://127.0.0.1:51234/": "Hello",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions test/acceptance/testdata/graceless_shutdown/edward.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
"log_properties": {
"started": "Waiting for signal"
}
},
{
"name": "gracelessWithTimeout",
"path": ".",
"commands": {
"launch": "go run main.go"
},
"log_properties": {
"started": "Waiting for signal"
},
"terminationTimeout": "5s"
}
]
}
2 changes: 1 addition & 1 deletion test/acceptance/testdata/graceless_shutdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func main() {
fmt.Println("Waiting for signal")
<-done
fmt.Println("Pretending to do some cleanup")
time.Sleep(1 * time.Hour)
time.Sleep(1 * time.Minute)
fmt.Println("exiting")
}

0 comments on commit 8e0f1f8

Please sign in to comment.