Skip to content

Commit

Permalink
[YUNIKORN-2658] add nolint:funlen to long functions to supress the li…
Browse files Browse the repository at this point in the history
…nt warnings (#861)

Closes: #861

Signed-off-by: Chia-Ping Tsai <[email protected]>
  • Loading branch information
rich7420 authored and chia7712 committed Jun 25, 2024
1 parent 24efbed commit 1014ba5
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 121 deletions.
2 changes: 2 additions & 0 deletions pkg/cache/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ func assertAppState(t *testing.T, app *Application, expectedState string, durati
}
}

//nolint:funlen
func TestGetNonTerminatedTaskAlias(t *testing.T) {
context := initContextForTest()
app := NewApplication(appID, "root.a", "testuser", testGroups, map[string]string{}, newMockSchedulerAPI())
Expand Down Expand Up @@ -749,6 +750,7 @@ func TestTryReserve(t *testing.T) {
assert.NilError(t, err, "placeholders are not created")
}

//nolint:funlen
func TestTryReservePostRestart(t *testing.T) {
context := initContextForTest()
dispatcher.RegisterEventHandler("TestAppHandler", dispatcher.EventTypeApp, context.ApplicationEventHandler())
Expand Down
2 changes: 2 additions & 0 deletions pkg/cache/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ func TestAddTask(t *testing.T) {
assert.Equal(t, len(context.applications["app00001"].GetNewTasks()), 2)
}

//nolint:funlen
func TestRecoverTask(t *testing.T) {
context, apiProvider := initContextAndAPIProviderForTest()
dispatcher.Start()
Expand Down Expand Up @@ -1481,6 +1482,7 @@ func TestPublishEventsCorrectly(t *testing.T) {
assert.NilError(t, err, "event should have been emitted")
}

//nolint:funlen
func TestAddApplicationsWithTags(t *testing.T) {
context := initContextForTest()

Expand Down
1 change: 1 addition & 0 deletions pkg/cache/external/scheduler_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func TestGetNodesInfo(t *testing.T) {
expectHost(t, host2, nodesInfo)
}

//nolint:funlen
func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
cache := NewSchedulerCache(client.NewMockedAPIProvider(false).GetAPIs())
assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil)
Expand Down
241 changes: 120 additions & 121 deletions pkg/cache/task_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,129 +316,128 @@ func TaskStates() *TStates {

func newTaskState() *fsm.FSM {
states := TaskStates()
return fsm.NewFSM(
states.New, fsm.Events{
{
Name: InitTask.String(),
Src: []string{states.New},
Dst: states.Pending,
},
{
Name: SubmitTask.String(),
Src: []string{states.Pending},
Dst: states.Scheduling,
},
{
Name: TaskAllocated.String(),
Src: []string{states.Scheduling},
Dst: states.Allocated,
},
{
Name: TaskAllocated.String(),
Src: []string{states.Completed},
Dst: states.Completed,
},
{
Name: TaskBound.String(),
Src: []string{states.Allocated},
Dst: states.Bound,
},
{
Name: CompleteTask.String(),
Src: states.Any,
Dst: states.Completed,
},
{
Name: KillTask.String(),
Src: []string{states.Pending, states.Scheduling, states.Allocated, states.Bound},
Dst: states.Killing,
},
{
Name: TaskKilled.String(),
Src: []string{states.Killing},
Dst: states.Killed,
},
{
Name: TaskRejected.String(),
Src: []string{states.New, states.Pending, states.Scheduling},
Dst: states.Rejected,
},
{
Name: TaskFail.String(),
Src: []string{states.New, states.Pending, states.Scheduling, states.Rejected, states.Allocated},
Dst: states.Failed,
},
return fsm.NewFSM(states.New, eventDesc(states), callbacks(states))
}

func eventDesc(states *TStates) fsm.Events {
return fsm.Events{
{
Name: InitTask.String(),
Src: []string{states.New},
Dst: states.Pending,
},
fsm.Callbacks{
// The state machine is tightly tied to the Task object.
//
// The first argument must always be a Task and if there are more arguments,
// they must be a string. If this precondition is not met, a runtime panic
// could occur.
events.EnterState: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
log.Log(log.ShimFSM).Info("Task state transition",
zap.String("app", task.applicationID),
zap.String("task", task.taskID),
zap.String("taskAlias", task.alias),
zap.String("source", event.Src),
zap.String("destination", event.Dst),
zap.String("event", event.Event))
},
states.Pending: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskPending()
},
states.Allocated: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskAllocated()
},
states.Rejected: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskRejected()
},
states.Failed: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
eventArgs := make([]string, 1)
reason := ""
if err := events.GetEventArgsAsStrings(eventArgs, event.Args[1].([]interface{})); err != nil {
log.Log(log.ShimFSM).Error("failed to parse event arg", zap.Error(err))
reason = err.Error()
} else {
reason = eventArgs[0]
}
task.postTaskFailed(reason)
},
states.Bound: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskBound()
},
beforeHook(TaskFail): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.beforeTaskFail()
},
beforeHook(TaskAllocated): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
// All allocation events must include the allocationKey and nodeID passed from the core
eventArgs := make([]string, 2)
if err := events.GetEventArgsAsStrings(eventArgs, event.Args[1].([]interface{})); err != nil {
log.Log(log.ShimFSM).Error("failed to parse event arg", zap.Error(err))
return
}
allocationKey := eventArgs[0]
nodeID := eventArgs[1]
task.beforeTaskAllocated(event.Src, allocationKey, nodeID)
},
beforeHook(CompleteTask): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.beforeTaskCompleted()
},
SubmitTask.String(): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.handleSubmitTaskEvent()
},
{
Name: SubmitTask.String(),
Src: []string{states.Pending},
Dst: states.Scheduling,
},
)
{
Name: TaskAllocated.String(),
Src: []string{states.Scheduling},
Dst: states.Allocated,
},
{
Name: TaskAllocated.String(),
Src: []string{states.Completed},
Dst: states.Completed,
},
{
Name: TaskBound.String(),
Src: []string{states.Allocated},
Dst: states.Bound,
},
{
Name: CompleteTask.String(),
Src: states.Any,
Dst: states.Completed,
},
{
Name: KillTask.String(),
Src: []string{states.Pending, states.Scheduling, states.Allocated, states.Bound},
Dst: states.Killing,
},
{
Name: TaskKilled.String(),
Src: []string{states.Killing},
Dst: states.Killed,
},
{
Name: TaskRejected.String(),
Src: []string{states.New, states.Pending, states.Scheduling},
Dst: states.Rejected,
},
{
Name: TaskFail.String(),
Src: []string{states.New, states.Pending, states.Scheduling, states.Rejected, states.Allocated},
Dst: states.Failed,
},
}
}

func callbacks(states *TStates) fsm.Callbacks {
return fsm.Callbacks{
events.EnterState: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
log.Log(log.ShimFSM).Info("Task state transition",
zap.String("app", task.applicationID),
zap.String("task", task.taskID),
zap.String("taskAlias", task.alias),
zap.String("source", event.Src),
zap.String("destination", event.Dst),
zap.String("event", event.Event))
},
states.Pending: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskPending()
},
states.Allocated: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskAllocated()
},
states.Rejected: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskRejected()
},
states.Failed: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
eventArgs := make([]string, 1)
reason := ""
if err := events.GetEventArgsAsStrings(eventArgs, event.Args[1].([]interface{})); err != nil {
log.Log(log.ShimFSM).Error("failed to parse event arg", zap.Error(err))
reason = err.Error()
} else {
reason = eventArgs[0]
}
task.postTaskFailed(reason)
},
states.Bound: func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.postTaskBound()
},
beforeHook(TaskFail): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.beforeTaskFail()
},
beforeHook(TaskAllocated): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
eventArgs := make([]string, 2)
if err := events.GetEventArgsAsStrings(eventArgs, event.Args[1].([]interface{})); err != nil {
log.Log(log.ShimFSM).Error("failed to parse event arg", zap.Error(err))
return
}
allocationKey := eventArgs[0]
nodeID := eventArgs[1]
task.beforeTaskAllocated(event.Src, allocationKey, nodeID)
},
beforeHook(CompleteTask): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.beforeTaskCompleted()
},
SubmitTask.String(): func(_ context.Context, event *fsm.Event) {
task := event.Args[0].(*Task) //nolint:errcheck
task.handleSubmitTaskEvent()
},
}
}

func beforeHook(event TaskEventType) string {
Expand Down
1 change: 1 addition & 0 deletions pkg/cache/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ func TestSetTaskGroup(t *testing.T) {
assert.Equal(t, task.getTaskGroupName(), "test-group")
}

//nolint:funlen
func TestHandleSubmitTaskEvent(t *testing.T) {
mockedContext, mockedSchedulerAPI := initContextAndAPIProviderForTest()
var allocRequest *si.AllocationRequest
Expand Down
1 change: 1 addition & 0 deletions pkg/common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestEquals(t *testing.T) {
assert.Equal(t, Equals(r1, r2), false)
}

//nolint:funlen
func TestParsePodResource(t *testing.T) {
containers := make([]v1.Container, 0, 2)

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/predicates/predicate_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func TestPodFitsHostPorts(t *testing.T) {
}
}

//nolint:funlen
func TestPodFitsSelector(t *testing.T) {
clientSet := clientSet()
informerFactory := informerFactory(clientSet)
Expand Down Expand Up @@ -1202,6 +1203,7 @@ func TestRunGeneralPredicates(t *testing.T) {
}
}

//nolint:funlen
func TestInterPodAffinity(t *testing.T) {
clientSet := clientSet()
informerFactory := informerFactory(clientSet)
Expand Down

0 comments on commit 1014ba5

Please sign in to comment.