Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
api: use 'now' with Stripe instead of time.Now() (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Feb 16, 2023
1 parent 1c512b3 commit 0838db6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
4 changes: 1 addition & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/kr/pretty"
"golang.org/x/exp/slices"
Expand All @@ -16,7 +15,6 @@ import (
"tier.run/refs"
"tier.run/stripe"
"tier.run/trweb"
"tier.run/values"
)

func init() {
Expand Down Expand Up @@ -246,7 +244,7 @@ func (h *Handler) serveReport(w http.ResponseWriter, r *http.Request) error {

return h.c.ReportUsage(r.Context(), rr.Org, rr.Feature, control.Report{
N: rr.N,
At: values.Coalesce(rr.At, time.Now()),
At: rr.At,
Clobber: rr.Clobber,
})
}
Expand Down
16 changes: 7 additions & 9 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ func TestTierReport(t *testing.T) {
t.Parallel()

ctx := context.Background()
tc, _ := newTestClient(t)
tc, cc := newTestClient(t)

farIntoTheFuture := time.Now().Add(24 * time.Hour)
clock := stroke.NewClock(t, cc.Stripe, t.Name(), farIntoTheFuture)
cc.Clock = clock.ID()

pr, err := tc.PushJSON(ctx, []byte(`
{
Expand Down Expand Up @@ -436,14 +440,8 @@ func TestTierReport(t *testing.T) {
}

if err := tc.ReportUsage(ctx, "org:test", "feature:t", 10, &tier.ReportParams{
// Report the usage at a time in the near future to avoid
// complaints from Stripe about being too early (e.g. the same
// start time as the current phase) or too late (e.g. > 5mins
// into the future.
//
// If this test becomes flaky, we should use Test Clocks. For
// now, avoid the slowness of the Test Clock API.
At: time.Now().Add(1 * time.Minute),
// Force 'now' at Stripe.
At: time.Time{}, // for 'now' on the server

Clobber: false,
}); err != nil {
Expand Down
1 change: 0 additions & 1 deletion client/tier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func (c *Client) Subscribe(ctx context.Context, org string, featuresAndPlans ...
Org: org,
Phases: []apitypes.Phase{{Features: featuresAndPlans}},
})

return err
}

Expand Down
1 change: 0 additions & 1 deletion cmd/tier/tier.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ func runTier(cmd string, args []string) (err error) {
return err
}
return tc().ReportUsage(ctx, org, feature, n, &tier.ReportParams{
At: time.Now(),
Clobber: *clobber,
})
case "whoami":
Expand Down

0 comments on commit 0838db6

Please sign in to comment.