chore: update testutil chan helpers (#17408)

This commit is contained in:
ケイラ
2025-04-16 10:37:09 -06:00
committed by GitHub
parent 2a76f5028e
commit f670bc31f5
45 changed files with 582 additions and 559 deletions
+7 -7
View File
@@ -35,7 +35,7 @@ func TestPrompt(t *testing.T) {
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("hello")
resp := testutil.RequireRecvCtx(ctx, t, msgChan)
resp := testutil.TryReceive(ctx, t, msgChan)
require.Equal(t, "hello", resp)
})
@@ -54,7 +54,7 @@ func TestPrompt(t *testing.T) {
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("yes")
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, "yes", resp)
})
@@ -91,7 +91,7 @@ func TestPrompt(t *testing.T) {
doneChan <- resp
}()
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, "yes", resp)
// Close the reader to end the io.Copy
require.NoError(t, ptty.Close(), "close eof reader")
@@ -115,7 +115,7 @@ func TestPrompt(t *testing.T) {
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("{}")
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, "{}", resp)
})
@@ -133,7 +133,7 @@ func TestPrompt(t *testing.T) {
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("{a")
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, "{a", resp)
})
@@ -153,7 +153,7 @@ func TestPrompt(t *testing.T) {
ptty.WriteLine(`{
"test": "wow"
}`)
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, `{"test":"wow"}`, resp)
})
@@ -178,7 +178,7 @@ func TestPrompt(t *testing.T) {
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("foo\nbar\nbaz\n\n\nvalid\n")
resp := testutil.RequireRecvCtx(ctx, t, doneChan)
resp := testutil.TryReceive(ctx, t, doneChan)
require.Equal(t, "valid", resp)
})
}
+8 -8
View File
@@ -192,8 +192,8 @@ func TestPortForward(t *testing.T) {
require.ErrorIs(t, err, context.Canceled)
flushCtx := testutil.Context(t, testutil.WaitShort)
testutil.RequireSendCtx(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.RequireRecvCtx(flushCtx, t, wuFlush)
testutil.RequireSend(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.TryReceive(flushCtx, t, wuFlush)
updated, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)
require.Greater(t, updated.LastUsedAt, workspace.LastUsedAt)
@@ -247,8 +247,8 @@ func TestPortForward(t *testing.T) {
require.ErrorIs(t, err, context.Canceled)
flushCtx := testutil.Context(t, testutil.WaitShort)
testutil.RequireSendCtx(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.RequireRecvCtx(flushCtx, t, wuFlush)
testutil.RequireSend(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.TryReceive(flushCtx, t, wuFlush)
updated, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)
require.Greater(t, updated.LastUsedAt, workspace.LastUsedAt)
@@ -315,8 +315,8 @@ func TestPortForward(t *testing.T) {
require.ErrorIs(t, err, context.Canceled)
flushCtx := testutil.Context(t, testutil.WaitShort)
testutil.RequireSendCtx(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.RequireRecvCtx(flushCtx, t, wuFlush)
testutil.RequireSend(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.TryReceive(flushCtx, t, wuFlush)
updated, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)
require.Greater(t, updated.LastUsedAt, workspace.LastUsedAt)
@@ -372,8 +372,8 @@ func TestPortForward(t *testing.T) {
require.ErrorIs(t, err, context.Canceled)
flushCtx := testutil.Context(t, testutil.WaitShort)
testutil.RequireSendCtx(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.RequireRecvCtx(flushCtx, t, wuFlush)
testutil.RequireSend(flushCtx, t, wuTick, dbtime.Now())
_ = testutil.TryReceive(flushCtx, t, wuFlush)
updated, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)
require.Greater(t, updated.LastUsedAt, workspace.LastUsedAt)
+7 -7
View File
@@ -98,7 +98,7 @@ func TestCloserStack_Empty(t *testing.T) {
defer close(closed)
uut.close(nil)
}()
testutil.RequireRecvCtx(ctx, t, closed)
testutil.TryReceive(ctx, t, closed)
}
func TestCloserStack_Context(t *testing.T) {
@@ -157,7 +157,7 @@ func TestCloserStack_CloseAfterContext(t *testing.T) {
err := uut.push("async", ac)
require.NoError(t, err)
cancel()
testutil.RequireRecvCtx(testCtx, t, ac.started)
testutil.TryReceive(testCtx, t, ac.started)
closed := make(chan struct{})
go func() {
@@ -174,7 +174,7 @@ func TestCloserStack_CloseAfterContext(t *testing.T) {
}
ac.complete()
testutil.RequireRecvCtx(testCtx, t, closed)
testutil.TryReceive(testCtx, t, closed)
}
func TestCloserStack_Timeout(t *testing.T) {
@@ -204,20 +204,20 @@ func TestCloserStack_Timeout(t *testing.T) {
}()
trap.MustWait(ctx).Release()
// top starts right away, but it hangs
testutil.RequireRecvCtx(ctx, t, ac[2].started)
testutil.TryReceive(ctx, t, ac[2].started)
// timer pops and we start the middle one
mClock.Advance(gracefulShutdownTimeout).MustWait(ctx)
testutil.RequireRecvCtx(ctx, t, ac[1].started)
testutil.TryReceive(ctx, t, ac[1].started)
// middle one finishes
ac[1].complete()
// bottom starts, but also hangs
testutil.RequireRecvCtx(ctx, t, ac[0].started)
testutil.TryReceive(ctx, t, ac[0].started)
// timer has to pop twice to time out.
mClock.Advance(gracefulShutdownTimeout).MustWait(ctx)
mClock.Advance(gracefulShutdownTimeout).MustWait(ctx)
testutil.RequireRecvCtx(ctx, t, closed)
testutil.TryReceive(ctx, t, closed)
}
type fakeCloser struct {
+5 -5
View File
@@ -271,12 +271,12 @@ func TestSSH(t *testing.T) {
}
// Allow one build to complete.
testutil.RequireSendCtx(ctx, t, buildPause, true)
testutil.RequireRecvCtx(ctx, t, buildDone)
testutil.RequireSend(ctx, t, buildPause, true)
testutil.TryReceive(ctx, t, buildDone)
// Allow the remaining builds to continue.
for i := 0; i < len(ptys)-1; i++ {
testutil.RequireSendCtx(ctx, t, buildPause, false)
testutil.RequireSend(ctx, t, buildPause, false)
}
var foundConflict int
@@ -1017,14 +1017,14 @@ func TestSSH(t *testing.T) {
}
}()
msg := testutil.RequireRecvCtx(ctx, t, msgs)
msg := testutil.TryReceive(ctx, t, msgs)
require.Equal(t, "test", msg)
close(success)
fsn.Notify()
<-cmdDone
fsn.AssertStopped()
// wait for dial goroutine to complete
_ = testutil.RequireRecvCtx(ctx, t, done)
_ = testutil.TryReceive(ctx, t, done)
// wait for the remote socket to get cleaned up before retrying,
// because cleaning up the socket happens asynchronously, and we
+3 -3
View File
@@ -408,7 +408,7 @@ func TestStart_AlreadyRunning(t *testing.T) {
}()
pty.ExpectMatch("workspace is already running")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
}
func TestStart_Starting(t *testing.T) {
@@ -441,7 +441,7 @@ func TestStart_Starting(t *testing.T) {
_ = dbfake.JobComplete(t, store, r.Build.JobID).Pubsub(ps).Do()
pty.ExpectMatch("workspace has been started")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
}
func TestStart_NoWait(t *testing.T) {
@@ -474,5 +474,5 @@ func TestStart_NoWait(t *testing.T) {
}()
pty.ExpectMatch("workspace has been started in no-wait mode")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
}
+10 -10
View File
@@ -345,7 +345,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
pty.ExpectMatch("does not match")
pty.ExpectMatch("> Enter a value (default: \"\"): ")
pty.WriteLine("abc")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ValidateNumber", func(t *testing.T) {
@@ -391,7 +391,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
pty.ExpectMatch("is not a number")
pty.ExpectMatch("> Enter a value (default: \"\"): ")
pty.WriteLine("8")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ValidateBool", func(t *testing.T) {
@@ -437,7 +437,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
pty.ExpectMatch("boolean value can be either \"true\" or \"false\"")
pty.ExpectMatch("> Enter a value (default: \"\"): ")
pty.WriteLine("false")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("RequiredParameterAdded", func(t *testing.T) {
@@ -508,7 +508,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
pty.WriteLine(value)
}
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("OptionalParameterAdded", func(t *testing.T) {
@@ -568,7 +568,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}()
pty.ExpectMatch("Planning workspace...")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ParameterOptionChanged", func(t *testing.T) {
@@ -640,7 +640,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ParameterOptionDisappeared", func(t *testing.T) {
@@ -713,7 +713,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ParameterOptionFailsMonotonicValidation", func(t *testing.T) {
@@ -770,7 +770,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
pty.ExpectMatch(match)
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) {
@@ -838,7 +838,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
t.Run("MutableRequiredParameterExists_ImmutableRequiredParameterAdded", func(t *testing.T) {
@@ -910,6 +910,6 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
})
}
+2 -2
View File
@@ -39,7 +39,7 @@ func TestUserCreate(t *testing.T) {
pty.ExpectMatch(match)
pty.WriteLine(value)
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
created, err := client.User(ctx, matches[1])
require.NoError(t, err)
assert.Equal(t, matches[1], created.Username)
@@ -72,7 +72,7 @@ func TestUserCreate(t *testing.T) {
pty.ExpectMatch(match)
pty.WriteLine(value)
}
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
_ = testutil.TryReceive(ctx, t, doneChan)
created, err := client.User(ctx, matches[1])
require.NoError(t, err)
assert.Equal(t, matches[1], created.Username)