Repository: kubernetes/kubernetes
File: pkg/controller/replicaset/replica_set_test.go (Go)

Code before:
		case core.UpdateAction:
			var ok bool
			if updatedRs, ok = action.GetObject().(*apps.ReplicaSet); !ok {
				t.Errorf("Expected a ReplicaSet as the argument to update, got %T", updatedRs)
			}
		default:
			t.Errorf("Unexpected action %+v", a)
		}
	}

	// one pod is not ready
	if updatedRs.Status.ReadyReplicas != 3 {
		t.Errorf("Expected updated ReplicaSet to contain ready replicas %v, got %v instead",
			3, updatedRs.Status.ReadyReplicas)
	}
	if updatedRs.Status.AvailableReplicas != 0 {
		t.Errorf("Expected updated ReplicaSet to contain available replicas %v, got %v instead",
			0, updatedRs.Status.AvailableReplicas)
	}

	if got, want := manager.queue.Len(), 0; got != want {
		t.Errorf("queue.Len() = %v, want %v", got, want)
	}

	// RS should be re-queued after 700ms to recompute .status.availableReplicas (200ms extra for the test).
	tCtx.Eventually(func(tCtx ktesting.TContext) int {
		return manager.queue.Len()
	}).WithTimeout(900*time.Millisecond).
		WithPolling(10*time.Millisecond).
		Should(gomega.Equal(1), " RS should be re-queued to recompute .status.availableReplicas")

}

var (
	imagePullBackOff apps.ReplicaSetConditionType = "ImagePullBackOff"

	condImagePullBackOff = func() apps.ReplicaSetCondition {
		return apps.ReplicaSetCondition{
			Type:   imagePullBackOff,
			Status: v1.ConditionTrue,
			Reason: "NonExistentImage",
		}
	}

	condReplicaFailure = func() apps.ReplicaSetCondition {
		return apps.ReplicaSetCondition{
			Type:   apps.ReplicaSetReplicaFailure,
			Status: v1.ConditionTrue,
			Reason: "OtherFailure",
		}
	}

Review comment (refactor):
You can simplify further: `tCtx.Eventually` accepts any function that returns one or more result values, exactly like `gomega.Eventually`. Therefore this can be written as `tCtx.Eventually(manager.queue.Len). ...`

I haven't checked the other locations, maybe the same works there?



Code after:
		case core.UpdateAction:
			var ok bool
			if updatedRs, ok = action.GetObject().(*apps.ReplicaSet); !ok {
				t.Errorf("Expected a ReplicaSet as the argument to update, got %T", updatedRs)
			}
		default:
			t.Errorf("Unexpected action %+v", a)
		}
	}

	// one pod is not ready
	if updatedRs.Status.ReadyReplicas != 3 {
		t.Errorf("Expected updated ReplicaSet to contain ready replicas %v, got %v instead",
			3, updatedRs.Status.ReadyReplicas)
	}
	if updatedRs.Status.AvailableReplicas != 0 {
		t.Errorf("Expected updated ReplicaSet to contain available replicas %v, got %v instead",
			0, updatedRs.Status.AvailableReplicas)
	}

	if got, want := manager.queue.Len(), 0; got != want {
		t.Errorf("queue.Len() = %v, want %v", got, want)
	}

	// RS should be re-queued after 700ms to recompute .status.availableReplicas (200ms extra for the test).
	tCtx.Eventually(manager.queue.Len).WithTimeout(900*time.Millisecond).
		WithPolling(10*time.Millisecond).
		Should(gomega.Equal(1), " RS should be re-queued to recompute .status.availableReplicas")

}

var (
	imagePullBackOff apps.ReplicaSetConditionType = "ImagePullBackOff"

	condImagePullBackOff = func() apps.ReplicaSetCondition {
		return apps.ReplicaSetCondition{
			Type:   imagePullBackOff,
			Status: v1.ConditionTrue,
			Reason: "NonExistentImage",
		}
	}

	condReplicaFailure = func() apps.ReplicaSetCondition {
		return apps.ReplicaSetCondition{
			Type:   apps.ReplicaSetReplicaFailure,
			Status: v1.ConditionTrue,
			Reason: "OtherFailure",
		}
	}

	condReplicaFailure2 = func() apps.ReplicaSetCondition {