snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
65
oryx/clock/clock.go
Normal file
65
oryx/clock/clock.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright © 2026 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package clock provides a small, injectable time source. Production code reads
|
||||
// the current time through a Clock instead of calling time.Now() directly, so
|
||||
// time-dependent behavior (such as flow expiry) can be tested deterministically.
|
||||
package clock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Clock is a source of the current time.
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
}
|
||||
|
||||
// Provider is implemented by dependency containers (such as the Kratos registry)
|
||||
// that expose a Clock. Embed it in a component's dependency interface to read
|
||||
// the current time through the injected clock.
|
||||
type Provider interface {
|
||||
Clock() Clock
|
||||
}
|
||||
|
||||
// System is the production Clock backed by the real wall clock.
|
||||
type System struct{}
|
||||
|
||||
// Now returns the current wall-clock time.
|
||||
func (System) Now() time.Time { return time.Now() }
|
||||
|
||||
// New returns the production system clock.
|
||||
func New() Clock { return System{} }
|
||||
|
||||
// Mock is a Clock whose time only changes when the caller advances it. It is
|
||||
// safe for concurrent use: a test goroutine may Add while a request goroutine
|
||||
// reads Now.
|
||||
type Mock struct {
|
||||
mu sync.Mutex
|
||||
t time.Time
|
||||
}
|
||||
|
||||
// NewMock returns a Mock fixed at the given instant.
|
||||
func NewMock(t time.Time) *Mock { return &Mock{t: t} }
|
||||
|
||||
// Now returns the Mock's current instant.
|
||||
func (m *Mock) Now() time.Time {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.t
|
||||
}
|
||||
|
||||
// Add advances the Mock by d.
|
||||
func (m *Mock) Add(d time.Duration) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.t = m.t.Add(d)
|
||||
}
|
||||
|
||||
// Set moves the Mock to the given instant.
|
||||
func (m *Mock) Set(t time.Time) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.t = t
|
||||
}
|
||||
Reference in New Issue
Block a user