From f2a652f29f3604f708de658af704208d67631178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 7 Mar 2023 16:48:09 +0100 Subject: [PATCH] rpcenc: More reliably failing TestReaderRedirectDrop --- lib/rpcenc/reader_test.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/rpcenc/reader_test.go b/lib/rpcenc/reader_test.go index da436068f..8f191a761 100644 --- a/lib/rpcenc/reader_test.go +++ b/lib/rpcenc/reader_test.go @@ -3,8 +3,8 @@ package rpcenc import ( "context" + "fmt" "io" - "io/ioutil" "net/http/httptest" "strings" "sync" @@ -77,7 +77,12 @@ func (h *ReaderHandler) CloseReader(ctx context.Context, r io.Reader) error { } func (h *ReaderHandler) ReadAll(ctx context.Context, r io.Reader) ([]byte, error) { - return ioutil.ReadAll(r) + b, err := io.ReadAll(r) + if err != nil { + return nil, xerrors.Errorf("readall: %w", err) + } + + return b, nil } func (h *ReaderHandler) ReadNullLen(ctx context.Context, r io.Reader) (int64, error) { @@ -219,9 +224,15 @@ func TestReaderRedirect(t *testing.T) { } func TestReaderRedirectDrop(t *testing.T) { + for i := 0; i < 10; i++ { + t.Run(fmt.Sprintf("test %d", i), testReaderRedirectDrop) + } +} + +func testReaderRedirectDrop(t *testing.T) { // lower timeout so that the dangling connection between client and reader is dropped quickly // after the test. Otherwise httptest.Close is blocked. - Timeout = 200 * time.Millisecond + Timeout = 90 * time.Millisecond var allClient struct { ReadAll func(ctx context.Context, r io.Reader) ([]byte, error) @@ -294,6 +305,8 @@ func TestReaderRedirectDrop(t *testing.T) { done.Wait() + fmt.Println("---------------------") + // Redir client drops before subcall done.Add(1) @@ -313,6 +326,8 @@ func TestReaderRedirectDrop(t *testing.T) { // kill redirecting server connection testServ.CloseClientConnections() + //time.Sleep(200 * time.Millisecond) + // ReadAllWaiting should fail done.Wait() @@ -322,5 +337,9 @@ func TestReaderRedirectDrop(t *testing.T) { // wait for subcall to finish <-contCh - require.ErrorContains(t, allServerHandler.subErr, "decoding params for 'ReaderHandler.ReadAll' (param: 0; custom decoder): context canceled") + estr := allServerHandler.subErr.Error() + + require.True(t, + strings.Contains(estr, "decoding params for 'ReaderHandler.ReadAll' (param: 0; custom decoder): context canceled") || + strings.Contains(estr, "readall: unexpected EOF"), "unexpected error: %s", estr) }