Skip to content

Commit

Permalink
attempt to fix weirdly broken tests
Browse files Browse the repository at this point in the history
Signed-off-by: Drew Erny <[email protected]>
  • Loading branch information
dperny committed Jan 16, 2019
1 parent 5432b8a commit be26111
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions manager/orchestrator/replicated/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/docker/go-events"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/manager/orchestrator/testutils"
"github.com/docker/swarmkit/manager/state"
Expand All @@ -26,14 +27,19 @@ func TestUpdaterRollback(t *testing.T) {
}

func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_FailureAction, setMonitor bool, useSpecVersion bool) {
ctx := context.Background()
// this test should complete within 20 seconds. if not, bail out
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

s := store.NewMemoryStore(nil)
assert.NotNil(t, s)
defer s.Close()

orchestrator := NewReplicatedOrchestrator(s)
defer orchestrator.Stop()

// TODO(dperny): these are used with atomic.StoreUint32 and
// atomic.LoadUint32. using atomic primitives is bad practice and easy to
// mess up
var (
failImage1 uint32
failImage2 uint32
Expand All @@ -51,7 +57,12 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa
go func() {
failedLast := false
for {
e := <-watchUpdate
var e events.Event
select {
case e = <-watchUpdate:
case <-ctx.Done():
return
}
task := e.(api.EventUpdateTask).Task
if task.DesiredState == task.Status.State {
continue
Expand Down Expand Up @@ -141,8 +152,32 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa
assert.NoError(t, err)

// Start the orchestrator.
var orchestratorError error
orchestratorDone := make(chan struct{})
// verify that the orchestrator has had a chance to run by blocking the
// main test routine until it has.
orchestratorRan := make(chan struct{})
go func() {
assert.NoError(t, orchestrator.Run(ctx))
close(orchestratorRan)
// try not to fail the test in go routines. it's racey. instead, save
// the error and check it in a defer
orchestratorError = orchestrator.Run(ctx)
close(orchestratorDone)
}()

select {
case <-orchestratorRan:
case <-ctx.Done():
t.Error("orchestrator did not start before test timed out")
}

defer func() {
orchestrator.Stop()
select {
case <-ctx.Done():
case <-orchestratorDone:
assert.NoError(t, orchestratorError)
}
}()

observedTask := testutils.WatchTaskCreate(t, watchCreate)
Expand Down Expand Up @@ -195,7 +230,13 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa

// Should get to the ROLLBACK_STARTED state
for {
e := <-watchServiceUpdate
var e events.Event
select {
case e = <-watchServiceUpdate:
case <-ctx.Done():
t.Error("test timed out before watchServiceUpdate provided an event")
return
}
if e.(api.EventUpdateService).Service.UpdateStatus == nil {
continue
}
Expand Down Expand Up @@ -224,7 +265,14 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa

// Should end up in ROLLBACK_COMPLETED state
for {
e := <-watchServiceUpdate
var e events.Event
select {
case e = <-watchServiceUpdate:
case <-ctx.Done():
t.Error("test timed out before watchServiceUpdate provided an event")
return
}

if e.(api.EventUpdateService).Service.UpdateStatus.State == api.UpdateStatus_ROLLBACK_COMPLETED {
break
}
Expand Down Expand Up @@ -265,7 +313,13 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa

// Should get to the ROLLBACK_STARTED state
for {
e := <-watchServiceUpdate
var e events.Event
select {
case e = <-watchServiceUpdate:
case <-ctx.Done():
t.Error("test timed out before watchServiceUpdate provided an event")
return
}
if e.(api.EventUpdateService).Service.UpdateStatus == nil {
continue
}
Expand All @@ -290,15 +344,27 @@ func testUpdaterRollback(t *testing.T, rollbackFailureAction api.UpdateConfig_Fa
case api.UpdateConfig_PAUSE:
// Should end up in ROLLBACK_PAUSED state
for {
e := <-watchServiceUpdate
var e events.Event
select {
case e = <-watchServiceUpdate:
case <-ctx.Done():
t.Error("test timed out before watchServiceUpdate provided an event")
return
}
if e.(api.EventUpdateService).Service.UpdateStatus.State == api.UpdateStatus_ROLLBACK_PAUSED {
return
}
}
case api.UpdateConfig_CONTINUE:
// Should end up in ROLLBACK_COMPLETE state
for {
e := <-watchServiceUpdate
var e events.Event
select {
case e = <-watchServiceUpdate:
case <-ctx.Done():
t.Error("test timed out before watchServiceUpdate provided an event")
return
}
if e.(api.EventUpdateService).Service.UpdateStatus.State == api.UpdateStatus_ROLLBACK_COMPLETED {
return
}
Expand Down

0 comments on commit be26111

Please sign in to comment.