Skip to content

Commit

Permalink
Fix client test reconnect test; Remove guard test (#12173)
Browse files Browse the repository at this point in the history
* Update reconnect test to new algorithm and interface; remove guard test
  • Loading branch information
DerekStrickland committed Mar 2, 2022
1 parent 91f70a6 commit ef4f8b7
Showing 1 changed file with 20 additions and 96 deletions.
116 changes: 20 additions & 96 deletions client/allocrunner/alloc_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,12 +1605,17 @@ func TestAllocRunner_Reconnect(t *testing.T) {
t.Run(tc.clientStatus, func(t *testing.T) {
// create a running alloc
alloc := mock.BatchAlloc()
alloc.AllocModifyIndex = 10
alloc.ModifyIndex = 10
alloc.ModifyTime = time.Now().UnixNano()

// Ensure task takes some time
task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "mock_driver"
task.Config["run_for"] = "30s"

original := alloc.Copy()

conf, cleanup := testAllocRunnerConfig(t, alloc)
defer cleanup()

Expand All @@ -1624,10 +1629,24 @@ func TestAllocRunner_Reconnect(t *testing.T) {
taskRunner.UpdateState(tc.taskState, tc.taskEvent)
}

ar.Reconnect()
update := ar.Alloc().Copy()

update.ClientStatus = structs.AllocClientStatusUnknown
update.AllocModifyIndex = original.AllocModifyIndex + 10
update.ModifyIndex = original.ModifyIndex + 10
update.ModifyTime = original.ModifyTime + 10

err = ar.Reconnect(update)
require.NoError(t, err)

require.Equal(t, tc.clientStatus, ar.AllocState().ClientStatus)


// Make sure the runner's alloc indexes match the update.
require.Equal(t, update.AllocModifyIndex, ar.Alloc().AllocModifyIndex)
require.Equal(t, update.ModifyIndex, ar.Alloc().ModifyIndex)
require.Equal(t, update.ModifyTime, ar.Alloc().ModifyTime)

found := false

updater := conf.StateUpdater.(*MockStateUpdater)
Expand Down Expand Up @@ -1657,98 +1676,3 @@ func TestAllocRunner_Reconnect(t *testing.T) {
})
}
}

func TestAllocRunner_MaybeHasPendingReconnect(t *testing.T) {
t.Parallel()

type tcase struct {
name string
timestamp int64
expectedDiff int
}
tcases := []tcase{
{
"should guard now",
time.Now().UnixNano(),
1,
},
{
"should guard 3 seconds",
time.Now().Add(-(3 * time.Second)).UnixNano(),
1,
},
{
"should not guard 6 seconds",
time.Now().Add(-(6 * time.Second)).UnixNano(),
2,
},
}

for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
alloc := mock.BatchAlloc()

task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "mock_driver"
task.Config["run_for"] = "30s"

conf, cleanup := testAllocRunnerConfig(t, alloc)
defer cleanup()

ar, err := NewAllocRunner(conf)
require.NoError(t, err)
defer destroy(ar)

go ar.Run()

reconnectEvent := structs.NewTaskEvent(structs.TaskClientReconnected)
reconnectEvent.Time = tc.timestamp
for _, tr := range ar.tasks {
tr.EmitEvent(reconnectEvent)
}

updater := conf.StateUpdater.(*MockStateUpdater)
// get a copy of the first states so that we can compare lengths to
// determine how many events were appended.
var firstStates map[string]*structs.TaskState
testutil.WaitForResult(func() (bool, error) {
last := updater.Last()
if last == nil {
return false, errors.New("last update nil")
}
states := last.TaskStates
for _, s := range states {
for _, e := range s.Events {
if e.Type == structs.TaskClientReconnected {
firstStates = states
return true, nil
}
}
}

return false, errors.New("no reconnect event found")
}, func(err error) {
require.NoError(t, err)
})

ar.Reconnect()

testutil.WaitForResult(func() (bool, error) {
last := updater.Last()
if last == nil {
return false, errors.New("last update nil")
}

for k, taskState := range last.TaskStates {
if len(taskState.Events) != len(firstStates[k].Events)+tc.expectedDiff {
return false, fmt.Errorf("expected %d reconnect events", tc.expectedDiff)
}
}

return true, nil
}, func(err error) {
require.NoError(t, err)
})
})
}
}

0 comments on commit ef4f8b7

Please sign in to comment.