Skip to content

Commit

Permalink
add flapping check in client wait
Browse files Browse the repository at this point in the history
  • Loading branch information
zpatrick committed Dec 8, 2016
1 parent 793b528 commit eee12b2
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 27 deletions.
7 changes: 6 additions & 1 deletion cli/client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"
)

const REQUIRED_SUCCESS_WAIT_COUNT = 3

func (c *APIClient) CreateService(name, environmentID, deployID, serviceID string) (*models.Service, error) {
req := models.CreateServiceRequest{
ServiceName: name,
Expand Down Expand Up @@ -91,6 +93,8 @@ func (c *APIClient) ScaleService(id string, count int) (*models.Service, error)
}

func (c *APIClient) WaitForDeployment(serviceID string, timeout time.Duration) (*models.Service, error) {
var successCount int

waiter := waitutils.Waiter{
Name: "WaitForDeployment",
Timeout: timeout,
Expand All @@ -108,7 +112,8 @@ func (c *APIClient) WaitForDeployment(serviceID string, timeout time.Duration) (
}
}

return true, nil
successCount++
return successCount >= REQUIRED_SUCCESS_WAIT_COUNT, nil
},
}

Expand Down
62 changes: 62 additions & 0 deletions cli/client/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/quintilesims/layer0/common/testutils"
"net/http"
"testing"
"time"
)

func TestCreateService(t *testing.T) {
Expand Down Expand Up @@ -177,3 +178,64 @@ func TestUpdateService(t *testing.T) {

testutils.AssertEqual(t, service.ServiceID, "id")
}

func TestWaitForDeployment(t *testing.T) {
var count int

handler := func(w http.ResponseWriter, r *http.Request) {
count++
testutils.AssertEqual(t, r.Method, "GET")
testutils.AssertEqual(t, r.URL.Path, "/service/id")

var runningCount int64 = 1
var desiredCount int64 = 2

// simulate flapping success
if count == 0 || count > 3 {
runningCount = 2
}

service := models.Service{
ServiceID: "id",
Deployments: []models.Deployment{
{DesiredCount: desiredCount, RunningCount: runningCount},
{DesiredCount: desiredCount, RunningCount: runningCount},
},
}

MarshalAndWrite(t, w, service, 200)
}

client, server := newClientAndServer(handler)
defer server.Close()

service, err := client.WaitForDeployment("id", time.Minute*15)
if err != nil {
t.Fatal(err)
}

testutils.AssertEqual(t, service.ServiceID, "id")
if count < REQUIRED_SUCCESS_WAIT_COUNT {
t.Fatalf("Retry count was less than required (%d)", count)
}
}

func TestWaitForDeployment_timeout(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
service := models.Service{
Deployments: []models.Deployment{
{DesiredCount: 1, RunningCount: 0},
},
}

MarshalAndWrite(t, w, service, 200)
}

client, server := newClientAndServer(handler)
defer server.Close()

if _, err := client.WaitForDeployment("id", time.Millisecond); err == nil {
t.Fatal("Error was nil!")
}
}

2 changes: 1 addition & 1 deletion cli/command/admin_command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/config"
"github.com/urfave/cli"
)

type AdminCommand struct {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/certificate_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"io/ioutil"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/command.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/client"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/cli/printer"
"github.com/urfave/cli"
)

type CommandGroup interface {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"flag"
"fmt"
"github.com/golang/mock/gomock"
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/client/mock_client"
"github.com/quintilesims/layer0/cli/command/mock_command"
"github.com/quintilesims/layer0/cli/printer"
"github.com/urfave/cli"
"io/ioutil"
"os"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion cli/command/deploy_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"io/ioutil"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/deploy_command_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/environment_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"io/ioutil"
"strconv"
)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/environment_command_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/job_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
)

type JobCommand struct {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/job_command_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/load_balancer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package command

import (
"fmt"
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"strconv"
"strings"
)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/load_balancer_command_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/quintilesims/layer0/common/testutils"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/service_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"strconv"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/service_command_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/task_command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/common/models"
"github.com/urfave/cli"
"strings"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/command/task_command_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"github.com/urfave/cli"
"github.com/quintilesims/layer0/common/models"
"github.com/quintilesims/layer0/common/testutils"
"github.com/urfave/cli"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/urfave/cli"
"github.com/quintilesims/layer0/cli/client"
"github.com/quintilesims/layer0/cli/command"
"github.com/quintilesims/layer0/cli/printer"
"github.com/quintilesims/layer0/common/config"
"github.com/quintilesims/layer0/common/waitutils"
"github.com/urfave/cli"
"os"
"sync"
"time"
Expand Down
2 changes: 1 addition & 1 deletion cli/printer/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package printer
import (
"fmt"
"github.com/briandowns/spinner"
"github.com/ryanuber/columnize"
"github.com/quintilesims/layer0/cli/entity"
"github.com/quintilesims/layer0/cli/printer/table"
"github.com/quintilesims/layer0/common/models"
"github.com/ryanuber/columnize"
"os"
"time"
)
Expand Down
30 changes: 22 additions & 8 deletions common/testutils/stubclock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ package testutils

import (
"time"
"sync"
)

type StubClock struct {
InnerTime time.Time
Time time.Time
once sync.Once
}

func (this *StubClock) Now() time.Time {
this.InnerTime = this.InnerTime.Add(time.Millisecond * 20)
return this.InnerTime
func (s *StubClock) init(){
if s.Time.IsZero(){
s.Time = time.Now()
}
}

func (this *StubClock) Sleep(s time.Duration) {
this.InnerTime = this.InnerTime.Add(s)
func (s *StubClock) Now() time.Time {
s.once.Do(s.init)

s.Time = s.Time.Add(time.Millisecond * 20)
return s.Time
}

func (this *StubClock) Since(t time.Time) time.Duration {
return this.Now().Sub(t)
func (s *StubClock) Sleep(d time.Duration) {
s.once.Do(s.init)

s.Time = s.Time.Add(d)
}

func (s *StubClock) Since(t time.Time) time.Duration {
s.once.Do(s.init)

return s.Now().Sub(t)
}

0 comments on commit eee12b2

Please sign in to comment.