Skip to content

Commit

Permalink
Handle nil TaskInfo in task.Wait callback #2 (#666)
Browse files Browse the repository at this point in the history
We loop over []types.PropertyChange slice and look for a certain name,
op and value before setting t.info.

t.info could be nil if we can't find a PropertyChange that fits into ruleset.
Handle that and also add a test to test that.
  • Loading branch information
caglar10ur authored Feb 16, 2017
1 parent f1f5b6c commit 93064c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion task/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (t taskProgress) Detail() string {
}

func (t taskProgress) Error() error {
if t.info != nil && t.info.Error != nil {
if t.info.Error != nil {
return Error{t.info.Error}
}

Expand Down Expand Up @@ -68,6 +68,11 @@ func (t *taskCallback) fn(pc []types.PropertyChange) bool {
t.info = &ti
}

// t.info could be nil if pc can't satify the rules above
if t.info == nil {
return false
}

pr := taskProgress{t.info}

// Store copy of error, so Wait() can return it as well.
Expand Down
41 changes: 41 additions & 0 deletions task/wait_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package task

import (
"testing"

"github.com/vmware/govmomi/vim25/types"
)

func TestCallbackFn(t *testing.T) {
cb := &taskCallback{}

for _, o := range []types.PropertyChangeOp{types.PropertyChangeOpAdd, types.PropertyChangeOpRemove, types.PropertyChangeOpAssign, types.PropertyChangeOpIndirectRemove} {
for _, s := range []types.TaskInfoState{types.TaskInfoStateQueued, types.TaskInfoStateRunning, types.TaskInfoStateSuccess, types.TaskInfoStateError} {
c := types.PropertyChange{
Name: "info",
Op: o,
Val: types.TaskInfo{
State: s,
},
}
t.Logf("Op: %s State: %s", o, s)
cb.fn([]types.PropertyChange{c})
}
}
}

0 comments on commit 93064c0

Please sign in to comment.