zitadel/internal/query/projection/event_test.go
Tim Möhlmann 32bad3feb3
Some checks are pending
ZITADEL CI/CD / core (push) Waiting to run
ZITADEL CI/CD / console (push) Waiting to run
ZITADEL CI/CD / version (push) Waiting to run
ZITADEL CI/CD / compile (push) Blocked by required conditions
ZITADEL CI/CD / core-unit-test (push) Blocked by required conditions
ZITADEL CI/CD / core-integration-test (push) Blocked by required conditions
ZITADEL CI/CD / lint (push) Blocked by required conditions
ZITADEL CI/CD / container (push) Blocked by required conditions
ZITADEL CI/CD / e2e (push) Blocked by required conditions
ZITADEL CI/CD / release (push) Blocked by required conditions
Code Scanning / CodeQL-Build (go) (push) Waiting to run
Code Scanning / CodeQL-Build (javascript) (push) Waiting to run
perf(milestones): refactor (#8788)
# Which Problems Are Solved

Milestones used existing events from a number of aggregates. OIDC
session is one of them. We noticed in load-tests that the reduction of
the oidc_session.added event into the milestone projection is a costly
business with payload based conditionals. A milestone is reached once,
but even then we remain subscribed to the OIDC events. This requires the
projections.current_states to be updated continuously.


# How the Problems Are Solved

The milestone creation is refactored to use dedicated events instead.
The command side decides when a milestone is reached and creates the
reached event once for each milestone when required.

# Additional Changes

In order to prevent reached milestones being created twice, a migration
script is provided. When the old `projections.milestones` table exist,
the state is read from there and `v2` milestone aggregate events are
created, with the original reached and pushed dates.

# Additional Context

- Closes https://github.com/zitadel/zitadel/issues/8800
2024-10-28 08:29:34 +00:00

109 lines
2.5 KiB
Go

package projection
import (
"database/sql"
"testing"
"time"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/eventstore/repository"
)
func testEvent(
eventType eventstore.EventType,
aggregateType eventstore.AggregateType,
data []byte,
opts ...eventOption,
) *repository.Event {
return timedTestEvent(eventType, aggregateType, data, time.Now(), opts...)
}
func toSystemEvent(event *repository.Event) *repository.Event {
event.EditorUser = "SYSTEM"
return event
}
func timedTestEvent(
eventType eventstore.EventType,
aggregateType eventstore.AggregateType,
data []byte,
creationDate time.Time,
opts ...eventOption,
) *repository.Event {
e := &repository.Event{
Seq: 15,
CreationDate: creationDate,
Typ: eventType,
AggregateType: aggregateType,
Data: data,
Version: "v1",
AggregateID: "agg-id",
ResourceOwner: sql.NullString{String: "ro-id", Valid: true},
InstanceID: "instance-id",
ID: "event-id",
EditorUser: "editor-user",
}
for _, opt := range opts {
opt(e)
}
return e
}
type eventOption func(e *repository.Event)
func withVersion(v eventstore.Version) eventOption {
return func(e *repository.Event) {
e.Version = v
}
}
func baseEvent(*testing.T) eventstore.Event {
return &eventstore.BaseEvent{}
}
func getEvent(event *repository.Event, mapper func(eventstore.Event) (eventstore.Event, error)) func(t *testing.T) eventstore.Event {
return func(t *testing.T) eventstore.Event {
e, err := mapper(event)
if err != nil {
t.Fatalf("mapper failed: %v", err)
}
return e
}
}
type wantReduce struct {
aggregateType eventstore.AggregateType
sequence uint64
executer *testExecuter
err func(error) bool
}
func assertReduce(t *testing.T, stmt *handler.Statement, err error, projection string, want wantReduce) {
t.Helper()
if want.err == nil && err != nil {
t.Errorf("unexpected error of type %T: %v", err, err)
return
}
if want.err != nil && want.err(err) {
return
}
if stmt.Aggregate.Type != want.aggregateType {
t.Errorf("wrong aggregate type: want: %q got: %q", want.aggregateType, stmt.Aggregate.Type)
}
if stmt.Sequence != want.sequence {
t.Errorf("wrong sequence: want: %d got: %d", want.sequence, stmt.Sequence)
}
if stmt.Execute == nil {
want.executer.Validate(t)
return
}
err = stmt.Execute(want.executer, projection)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want.executer.Validate(t)
}