MCPcopy Index your code
hub / github.com/coder/coder / TestPoll

Function TestPoll

scripts/develop/main_test.go:739–795  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

737}
738
739func TestPoll(t *testing.T) {
740 t.Parallel()
741
742 t.Run("ImmediateSuccess", func(t *testing.T) {
743 t.Parallel()
744 val, err := poll(t.Context(), 10*time.Millisecond,
745 func(_ context.Context) (string, bool, error) {
746 return "done", true, nil
747 })
748 require.NoError(t, err)
749 assert.Equal(t, "done", val)
750 })
751
752 t.Run("EventualSuccess", func(t *testing.T) {
753 t.Parallel()
754 calls := 0
755 val, err := poll(t.Context(), 10*time.Millisecond,
756 func(_ context.Context) (int, bool, error) {
757 calls++
758 if calls >= 3 {
759 return calls, true, nil
760 }
761 return 0, false, nil
762 })
763 require.NoError(t, err)
764 assert.Equal(t, 3, val)
765 })
766
767 t.Run("ContextCanceled", func(t *testing.T) {
768 t.Parallel()
769 ctx, cancel := context.WithCancel(t.Context())
770 cancel()
771
772 _, err := poll(ctx, 10*time.Millisecond,
773 func(_ context.Context) (struct{}, bool, error) {
774 t.Fatal("cond should not be called")
775 return struct{}{}, false, nil
776 })
777 require.ErrorIs(t, err, context.Canceled)
778 })
779
780 t.Run("ErrorStopsPolling", func(t *testing.T) {
781 t.Parallel()
782 calls := 0
783 _, err := poll(t.Context(), 10*time.Millisecond,
784 func(_ context.Context) (string, bool, error) {
785 calls++
786 if calls == 2 {
787 return "", false, xerrors.New("boom")
788 }
789 return "", false, nil
790 })
791 require.Error(t, err)
792 assert.Contains(t, err.Error(), "boom")
793 assert.Equal(t, 2, calls)
794 })
795}
796

Callers

nothing calls this directly

Calls 8

pollFunction · 0.85
FatalMethod · 0.80
RunMethod · 0.65
ContextMethod · 0.65
NewMethod · 0.65
EqualMethod · 0.45
ErrorMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected