From 9afd320b9e059ea23a07ba83205f9ed0f7fe288b Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 25 Jun 2022 16:05:20 -0500 Subject: [PATCH] Fix flickering test in CI While this test always worked on my machine, it flickered in CI. And to be fair the test can't guarantee the condition it is testing. Work around this by trying many times before admitting failure. --- internal/iobufpool/iobufpool_test.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/iobufpool/iobufpool_test.go b/internal/iobufpool/iobufpool_test.go index 51b08215..09e258bb 100644 --- a/internal/iobufpool/iobufpool_test.go +++ b/internal/iobufpool/iobufpool_test.go @@ -5,7 +5,6 @@ import ( "github.com/jackc/pgx/v5/internal/iobufpool" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestGetCap(t *testing.T) { @@ -70,9 +69,17 @@ func TestPutHandlesWrongSizedBuffers(t *testing.T) { } func TestPutGetBufferReuse(t *testing.T) { - buf := iobufpool.Get(4) - buf[0] = 1 - iobufpool.Put(buf) - buf = iobufpool.Get(4) - require.Equal(t, byte(1), buf[0]) + // There is no way to guarantee a buffer will be reused. It should be, but a GC between the Put and the Get will cause + // it not to be. So try many times. + for i := 0; i < 100000; i++ { + buf := iobufpool.Get(4) + buf[0] = 1 + iobufpool.Put(buf) + buf = iobufpool.Get(4) + if buf[0] == 1 { + return + } + } + + t.Error("buffer was never reused") }