diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4123f65..b05e2e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,21 +8,11 @@ on: jobs: test: - name: Run Tests - runs-on: ubuntu-20.04 - steps: - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: 1.21.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 diff --git a/currency/rate_fetcher.go b/currency/rate_fetcher.go index 523b99d..8522b40 100644 --- a/currency/rate_fetcher.go +++ b/currency/rate_fetcher.go @@ -25,6 +25,16 @@ 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 +44,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 source 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 target 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) } } 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)) }