From a3a041df2c370e51fe9eb379e9e82958840f9822 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Mon, 12 Aug 2024 12:10:02 -0400 Subject: [PATCH 1/3] [-] fix panic for double unlock for unexpected method calls (#216) * avoid panic for double unlock * refactor `findExpectationFunc` --------- Signed-off-by: Steve Coffman Co-authored-by: Pavlo Golub --- pgxmock.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pgxmock.go b/pgxmock.go index 377c7a5..397b316 100644 --- a/pgxmock.go +++ b/pgxmock.go @@ -554,25 +554,22 @@ func findExpectationFunc[ET expectationType[t], t any](c *pgxmock, method string fulfilled++ continue } - if expected, ok = next.(ET); ok { - err = cmp(expected) - if err == nil { + if err = cmp(expected); err == nil { break } } + expected = nil + next.Unlock() if c.ordered { - if (!ok || err != nil) && !next.required() { - next.Unlock() + if !next.required() { continue } - next.Unlock() if err != nil { return nil, err } return nil, fmt.Errorf("call to method %s, was not expected, next expectation is: %s", method, next) } - next.Unlock() } if expected == nil { From c579632ebb310851baad5b76a6e16a662ab42d99 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Mon, 12 Aug 2024 18:14:05 +0200 Subject: [PATCH 2/3] [+] add test case for double unlock (#217) --- pgxmock_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pgxmock_test.go b/pgxmock_test.go index 5e6f74c..af83b99 100644 --- a/pgxmock_test.go +++ b/pgxmock_test.go @@ -1216,3 +1216,16 @@ func TestExpectReset(t *testing.T) { mock.ExpectReset() a.Error(mock.ExpectationsWereMet()) } + +func TestDoubleUnlock(t *testing.T) { + mock, _ := NewConn() + mock.MatchExpectationsInOrder(false) + a := assert.New(t) + + mock.ExpectExec("insert").WillReturnResult(NewResult("ok", 1)) + mock.ExpectExec("update").WillReturnResult(NewResult("ok", 1)) + + _, err := mock.Exec(ctx, "foo") + a.Error(err) + a.NotPanics(func() { _ = mock.Ping(ctx) }) +} From e681dfcab6f87e374775aee75a2becf625b9767f Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Sat, 31 Aug 2024 09:01:54 +0200 Subject: [PATCH 3/3] [-] fix `govet` linter warning (#219) --- go.mod | 2 ++ go.sum | 5 +++-- pgxmock.go | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 09632d3..fe02cdf 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,9 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect golang.org/x/crypto v0.23.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/text v0.15.0 // indirect diff --git a/go.sum b/go.sum index 21c4c64..dd3ec6b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -15,8 +16,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pgxmock.go b/pgxmock.go index 397b316..5cd2b94 100644 --- a/pgxmock.go +++ b/pgxmock.go @@ -12,6 +12,7 @@ package pgxmock import ( "context" + "errors" "fmt" "reflect" @@ -577,7 +578,7 @@ func findExpectationFunc[ET expectationType[t], t any](c *pgxmock, method string if fulfilled == len(c.expectations) { msg = "all expectations were already fulfilled, " + msg } - return nil, fmt.Errorf(msg) + return nil, errors.New(msg) } defer expected.Unlock()