From e0ad2854404c221e40ed5b2e23edb97616833918 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Thu, 8 Aug 2024 18:05:57 -0400 Subject: [PATCH 1/2] Avoid panic for double unlock Signed-off-by: Steve Coffman --- pgxmock.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pgxmock.go b/pgxmock.go index 377c7a5..ae4fc6e 100644 --- a/pgxmock.go +++ b/pgxmock.go @@ -558,9 +558,12 @@ func findExpectationFunc[ET expectationType[t], t any](c *pgxmock, method string if expected, ok = next.(ET); ok { err = cmp(expected) if err == nil { + // Note: next.Unlock not called yet, as we delay fulfill break } } + // reset expected to nil if not matched, to avoid double mutex unlock panic below + expected = nil if c.ordered { if (!ok || err != nil) && !next.required() { next.Unlock() @@ -582,6 +585,7 @@ func findExpectationFunc[ET expectationType[t], t any](c *pgxmock, method string } return nil, fmt.Errorf(msg) } + // only unlock to fulfill matching expectation defer expected.Unlock() expected.fulfill() From 9590769c4893d9db92163f8bd48a1ae65c7c7437 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Mon, 12 Aug 2024 18:00:44 +0200 Subject: [PATCH 2/2] refactor `findExpectationFunc` --- pgxmock.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/pgxmock.go b/pgxmock.go index ae4fc6e..397b316 100644 --- a/pgxmock.go +++ b/pgxmock.go @@ -554,28 +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 { - // Note: next.Unlock not called yet, as we delay fulfill + if err = cmp(expected); err == nil { break } } - // reset expected to nil if not matched, to avoid double mutex unlock panic below 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 { @@ -585,7 +579,6 @@ func findExpectationFunc[ET expectationType[t], t any](c *pgxmock, method string } return nil, fmt.Errorf(msg) } - // only unlock to fulfill matching expectation defer expected.Unlock() expected.fulfill()