From ce7eee07fe3dd3e005ca75d3c1f29dbd2bb8f255 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Wed, 2 Jul 2025 14:57:26 +0200 Subject: [PATCH 1/6] pkg/currency: add error tags on currency exchange errors --- currency/rate_fetcher.go | 18 +++++++++++++----- currency/rate_fetcher_test.go | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/currency/rate_fetcher.go b/currency/rate_fetcher.go index 523b99d..ceef2d6 100644 --- a/currency/rate_fetcher.go +++ b/currency/rate_fetcher.go @@ -25,6 +25,15 @@ type Exchanger struct { } func (e *Exchanger) Exchange(ctx context.Context, m Money, c Currency) (Money, error) { + exm, err := e.exchange(ctx, m, c) + + return exm, errors.WithTags(err, map[string]interface{}{ + "target_currency": c, + "source_currency": m.Currency, + "source_cents": m.Cents, + }) +} +func (e *Exchanger) exchange(ctx context.Context, m Money, c Currency) (Money, error) { if c == m.Currency { return m, nil } @@ -34,27 +43,26 @@ func (e *Exchanger) Exchange(ctx context.Context, m Money, c Currency) (Money, e } var ( + err error + fromRate = 1. toRate = 1. ) if m.Currency != e.BaseCurrency() { - var err error fromRate, err = e.Rate(ctx, m.Currency) if err != nil { - return Money{}, err + return Money{}, errors.Wrap(err, "could not get from rate") } } if c != e.BaseCurrency() { - var err error - toRate, err = e.Rate(ctx, c) if err != nil { - return Money{}, err + return Money{}, errors.Wrap(err, "could not get to rate") } } diff --git a/currency/rate_fetcher_test.go b/currency/rate_fetcher_test.go index 4645a3d..444f518 100644 --- a/currency/rate_fetcher_test.go +++ b/currency/rate_fetcher_test.go @@ -54,7 +54,7 @@ func TestExchange(t *testing.T) { res, err := e.Exchange(context.Background(), tt.in, tt.t) - assert.Equal(t, tt.wantErr, err) + assert.ErrorIs(t, err, tt.wantErr) assert.Equal(t, tt.wantMoney, res) } } From 95d237c536e6be4b3f00df2439a0ee78b8ffcd09 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Wed, 2 Jul 2025 16:07:33 +0200 Subject: [PATCH 2/6] Update currency/rate_fetcher.go Co-authored-by: Xavier Goffin <86716549+xgoffin@users.noreply.github.com> --- currency/rate_fetcher.go | 1 + 1 file changed, 1 insertion(+) diff --git a/currency/rate_fetcher.go b/currency/rate_fetcher.go index ceef2d6..c9a5f81 100644 --- a/currency/rate_fetcher.go +++ b/currency/rate_fetcher.go @@ -33,6 +33,7 @@ func (e *Exchanger) Exchange(ctx context.Context, m Money, c Currency) (Money, e "source_cents": m.Cents, }) } + func (e *Exchanger) exchange(ctx context.Context, m Money, c Currency) (Money, error) { if c == m.Currency { return m, nil From b79641929b022225aa4103d12896dd5148529195 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Wed, 2 Jul 2025 16:48:04 +0200 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: PL Pery --- currency/rate_fetcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/currency/rate_fetcher.go b/currency/rate_fetcher.go index c9a5f81..8522b40 100644 --- a/currency/rate_fetcher.go +++ b/currency/rate_fetcher.go @@ -55,7 +55,7 @@ func (e *Exchanger) exchange(ctx context.Context, m Money, c Currency) (Money, e fromRate, err = e.Rate(ctx, m.Currency) if err != nil { - return Money{}, errors.Wrap(err, "could not get from rate") + return Money{}, errors.Wrap(err, "could not get source rate") } } @@ -63,7 +63,7 @@ func (e *Exchanger) exchange(ctx context.Context, m Money, c Currency) (Money, e toRate, err = e.Rate(ctx, c) if err != nil { - return Money{}, errors.Wrap(err, "could not get to rate") + return Money{}, errors.Wrap(err, "could not get target rate") } } From 26915e7995386289aa1316e7853df96c1ce3b3c3 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Wed, 2 Jul 2025 16:50:54 +0200 Subject: [PATCH 4/6] ci: bump run os --- .github/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4123f65..ce568df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,12 +9,12 @@ on: jobs: test: name: Run Tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 steps: - name: Install Go uses: actions/setup-go@v2 with: - go-version: 1.21.x + go-version: 1.24.x - name: Checkout uses: actions/checkout@v3 - name: Cache Modules @@ -26,3 +26,4 @@ jobs: ${{ runner.os }}-go- - name: Run tests run: go test -v ./... + From ecf3443ba2ab8a4d4a652e96fd08318bd2ac8532 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Fri, 4 Jul 2025 11:46:58 +0200 Subject: [PATCH 5/6] ci: use public actions --- .github/workflows/ci.yml | 25 +++++++------------------ .github/workflows/lint.yml | 16 ++-------------- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce568df..b05e2e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,22 +8,11 @@ on: jobs: test: - name: Run Tests - runs-on: ubuntu-24.04 - steps: - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: 1.24.x - - name: Checkout - uses: actions/checkout@v3 - - name: Cache Modules - uses: actions/cache@v3 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-v1-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - name: Run tests - run: go test -v ./... + uses: upfluence/actions/.github/workflows/lib-go-test.yml@master + secrets: inherit + release: + needs: test + if: github.ref == 'refs/heads/master' + uses: upfluence/actions/.github/workflows/lib-any-release.yml@master + secrets: inherit diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 37f9302..44b4787 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -3,17 +3,5 @@ on: [pull_request] jobs: lint: - name: runner / golangci-lint - runs-on: ubuntu-20.04 - timeout-minutes: 30 - steps: - - name: Install Go - uses: actions/setup-go@v3 - with: - go-version: 1.21.x - - name: Checkout - uses: actions/checkout@v3 - - name: golanci-lint - uses: upfluence/action-golangci-lint@master - with: - github_token: ${{ secrets.github_token }} + uses: upfluence/actions/.github/workflows/lint.yml@master + secrets: inherit From b44ef8b258f1b54782fd3f3b31f6a0bf550d1227 Mon Sep 17 00:00:00 2001 From: Sylvain Prost Date: Wed, 9 Jul 2025 10:38:23 +0200 Subject: [PATCH 6/6] pkg/syncutil: fix singleflight test --- syncutil/singleflight_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/syncutil/singleflight_test.go b/syncutil/singleflight_test.go index 14a11ec..514ffd3 100644 --- a/syncutil/singleflight_test.go +++ b/syncutil/singleflight_test.go @@ -114,6 +114,7 @@ func TestSingleflightStopOnClose(t *testing.T) { var ( sf Singleflight ctr int32 + wg sync.WaitGroup ctx = context.Background() donec = make(chan struct{}) @@ -130,17 +131,23 @@ func TestSingleflightStopOnClose(t *testing.T) { return nil } + wg.Add(1) + go func() { ok, err := sf.Do(ctx, fn) assert.True(t, ok) assert.Equal(t, context.Canceled, err) + + wg.Done() }() time.Sleep(10 * time.Millisecond) + sf.Close() close(donec) - assert.Equal(t, int32(0), atomic.LoadInt32(&ctr)) - sf.Close() + wg.Wait() + + assert.Equal(t, int32(0), atomic.LoadInt32(&ctr)) }