Skip to content

Commit 840ef14

Browse files
committed
ktesting: doc updates and fixes
First-in-first-out is wrong for cleanup, it's LIFO. Updated some comments to make them more informative and fixed indention.
1 parent 4cb4228 commit 840ef14

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

test/utils/ktesting/assert.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ func buildDescription(explain ...interface{}) string {
118118
// is passed in. For example, errors can be checked with ExpectNoError:
119119
//
120120
// cb := func(func(tCtx ktesting.TContext) int {
121-
// value, err := doSomething(...)
122-
// ktesting.ExpectNoError(tCtx, err, "something failed")
123-
// return value
121+
// value, err := doSomething(...)
122+
// tCtx.ExpectNoError(err, "something failed")
123+
// assert(tCtx, 42, value, "the answer")
124+
// return value
124125
// }
125126
// tCtx.Eventually(cb).Should(gomega.Equal(42), "should be the answer to everything")
126127
//
127128
// If there is no value, then an error can be returned:
128129
//
129130
// cb := func(func(tCtx ktesting.TContext) error {
130-
// err := doSomething(...)
131-
// return err
131+
// err := doSomething(...)
132+
// return err
132133
// }
133134
// tCtx.Eventually(cb).Should(gomega.Succeed(), "foobar should succeed")
134135
//
@@ -143,28 +144,37 @@ func buildDescription(explain ...interface{}) string {
143144
// anymore, use [gomega.StopTrying]:
144145
//
145146
// cb := func(func(tCtx ktesting.TContext) int {
146-
// value, err := doSomething(...)
147-
// if errors.Is(err, SomeFinalErr) {
148-
// gomega.StopTrying("permanent failure).Wrap(err).Now()
149-
// }
150-
// ktesting.ExpectNoError(tCtx, err, "something failed")
151-
// return value
147+
// value, err := doSomething(...)
148+
// if errors.Is(err, SomeFinalErr) {
149+
// // This message completely replaces the normal
150+
// // failure message and thus should include all
151+
// // relevant information.
152+
// //
153+
// // github.com/onsi/gomega/format is a good way
154+
// // to format arbitrary data. It uses indention
155+
// // and falls back to YAML for Kubernetes API
156+
// // structs for readability.
157+
// gomega.StopTrying("permanent failure, last value:\n%s", format.Object(value, 1 /* indent one level */)).
158+
// Wrap(err).Now()
159+
// }
160+
// ktesting.ExpectNoError(tCtx, err, "something failed")
161+
// return value
152162
// }
153163
// tCtx.Eventually(cb).Should(gomega.Equal(42), "should be the answer to everything")
154164
//
155165
// To poll again after some specific timeout, use [gomega.TryAgainAfter]. This is
156166
// particularly useful in [Consistently] to ignore some intermittent error.
157167
//
158168
// cb := func(func(tCtx ktesting.TContext) int {
159-
// value, err := doSomething(...)
160-
// var intermittentErr SomeIntermittentError
161-
// if errors.As(err, &intermittentErr) {
162-
// gomega.TryAgainAfter(intermittentErr.RetryPeriod).Wrap(err).Now()
163-
// }
164-
// ktesting.ExpectNoError(tCtx, err, "something failed")
165-
// return value
166-
// }
167-
// tCtx.Eventually(cb).Should(gomega.Equal(42), "should be the answer to everything")
169+
// value, err := doSomething(...)
170+
// var intermittentErr SomeIntermittentError
171+
// if errors.As(err, &intermittentErr) {
172+
// gomega.TryAgainAfter(intermittentErr.RetryPeriod).Wrap(err).Now()
173+
// }
174+
// ktesting.ExpectNoError(tCtx, err, "something failed")
175+
// return value
176+
// }
177+
// tCtx.Eventually(cb).Should(gomega.Equal(42), "should be the answer to everything")
168178
func Eventually[T any](tCtx TContext, cb func(TContext) T) gomega.AsyncAssertion {
169179
tCtx.Helper()
170180
return gomega.NewWithT(tCtx).Eventually(tCtx, func(ctx context.Context) (val T, err error) {

test/utils/ktesting/tcontext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type TContext interface {
8383
Cancel(cause string)
8484

8585
// Cleanup registers a callback that will get invoked when the test
86-
// has finished. Callbacks get invoked in first-in-first-out order.
86+
// has finished. Callbacks get invoked in last-in-first-out order (LIFO).
8787
//
8888
// Beware of context cancellation. The following cleanup code
8989
// will use a canceled context, which is not desirable:

0 commit comments

Comments
 (0)