Skip to content

Commit 78082aa

Browse files
committed
refactor(event/handler)!: change merging behavior of Startup() option
1 parent f739578 commit 78082aa

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

event/handler/handler.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ type Option func(*Handler)
6262
// events are fetched from the store. The returned [Option] can be used when
6363
// creating a new [Handler].
6464
//
65-
// If [query.Option]s are provided, they will be merged with the default query
66-
// using [query.Merge]. If you want to _replace_ the default query, use the
67-
// [StartupQuery] option instead of providing [query.Option]s to [Startup].
65+
// If [query.Option]s are provided, they will be used to construct a new query
66+
// that replaces the default query. If you want to modify the default query
67+
// instead, use the [StartupQuery] option.
6868
func Startup(store event.Store, opts ...query.Option) Option {
6969
return func(h *Handler) {
7070
h.startupStore = store
7171
if len(opts) > 0 {
72-
StartupQuery(func(q event.Query) event.Query {
73-
return query.Merge(q, query.New(opts...))
72+
StartupQuery(func(event.Query) event.Query {
73+
return query.New(opts...)
7474
})(h)
7575
}
7676
}
@@ -79,9 +79,7 @@ func Startup(store event.Store, opts ...query.Option) Option {
7979
// StartupQuery is a function that configures a [Handler]'s startup query. It
8080
// accepts a function that takes and returns an event.Query as its argument. The
8181
// provided function will be used by the [Handler] to modify the default query
82-
// used when fetching events from the event store during startup. The resulting
83-
// [Option] can be used when constructing a new [Handler], allowing
84-
// customization of the startup behavior of the [Handler].
82+
// used when fetching events from the event store during startup.
8583
func StartupQuery(fn func(event.Query) event.Query) Option {
8684
return func(h *Handler) {
8785
h.startupQuery = fn

event/handler/handler_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestStartupQuery(t *testing.T) {
151151
}
152152
}
153153

154-
func TestStartup_withQuery_merges_names(t *testing.T) {
154+
func TestStartup_withQuery_replaces_names(t *testing.T) {
155155
bus := eventbus.New()
156156
store := eventstore.New()
157157

@@ -195,13 +195,13 @@ func TestStartup_withQuery_merges_names(t *testing.T) {
195195
}()
196196

197197
select {
198-
case <-time.After(time.Second):
199-
t.Fatalf("foo event was not handled")
198+
case <-time.After(50 * time.Millisecond):
200199
case <-fooHandled:
200+
t.Fatalf("foo event was handled")
201201
}
202202

203203
select {
204-
case <-time.After(time.Second):
204+
case <-time.After(50 * time.Millisecond):
205205
t.Fatalf("bar event was not handled #1")
206206
case evt := <-barHandled:
207207
if evt.ID() != testID {
@@ -210,7 +210,7 @@ func TestStartup_withQuery_merges_names(t *testing.T) {
210210
}
211211

212212
select {
213-
case <-time.After(time.Second):
213+
case <-time.After(50 * time.Millisecond):
214214
t.Fatalf("bar event was not handled #2")
215215
case evt := <-barHandled:
216216
if evt.ID() == testID {
@@ -219,7 +219,7 @@ func TestStartup_withQuery_merges_names(t *testing.T) {
219219
}
220220
}
221221

222-
func TestStartup_withQuery_merges_ids(t *testing.T) {
222+
func TestStartup_withQuery_replaces_ids(t *testing.T) {
223223
bus := eventbus.New()
224224
store := eventstore.New()
225225

@@ -243,6 +243,7 @@ func TestStartup_withQuery_merges_ids(t *testing.T) {
243243
context.Background(),
244244
event.New("foo", test.FooEventData{}).Any(),
245245
event.New("bar", test.BarEventData{}, event.ID(testID)).Any(),
246+
event.New("bar", test.BarEventData{}).Any(), // Add another bar event without testID
246247
); err != nil {
247248
t.Fatalf("Insert() failed with %q", err)
248249
}
@@ -261,15 +262,23 @@ func TestStartup_withQuery_merges_ids(t *testing.T) {
261262
select {
262263
case <-time.After(50 * time.Millisecond):
263264
case <-fooHandled:
264-
t.Fatalf("foo event was handled")
265+
t.Fatalf("foo event was handled when it should have been filtered out")
265266
}
266267

267268
select {
268-
case <-time.After(time.Second):
269-
t.Fatalf("bar event was not handled")
269+
case <-time.After(50 * time.Millisecond):
270+
t.Fatalf("bar event with matching ID was not handled")
270271
case evt := <-barHandled:
271272
if evt.ID() != testID {
272273
t.Fatalf("expected event ID %q; got %q", testID, evt.ID())
273274
}
274275
}
276+
277+
// Verify that the other bar event without testID is not handled
278+
select {
279+
case <-time.After(50 * time.Millisecond):
280+
// This is the expected behavior - event without matching ID should not be handled
281+
case <-barHandled:
282+
t.Fatalf("bar event without matching ID was incorrectly handled")
283+
}
275284
}

event/query/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func SortByMulti(sorts ...event.SortOptions) Option {
166166
// SortByAggregate returns an Option that sorts the a Query by aggregates.
167167
//
168168
// Order of sortings is:
169-
// 1. aggregateName (ascending)
170-
// 2. aggregateID (ascending)
171-
// 3. aggregateVersion (ascending)
169+
// 1. aggregateName (ascending)
170+
// 2. aggregateID (ascending)
171+
// 3. aggregateVersion (ascending)
172172
func SortByAggregate() Option {
173173
return SortByMulti(
174174
event.SortOptions{Sort: event.SortAggregateName, Dir: event.SortAsc},

go.work.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
619619
github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
620620
github.com/klauspost/compress v1.15.4/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
621621
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
622+
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
622623
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
623624
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
624625
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -740,6 +741,8 @@ golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU
740741
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
741742
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
742743
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
744+
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
745+
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
743746
golang.org/x/exp v0.0.0-20190121172915-509febef88a4 h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA=
744747
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
745748
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -849,6 +852,7 @@ golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJ
849852
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
850853
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
851854
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
855+
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
852856
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
853857
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
854858
golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -891,6 +895,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
891895
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
892896
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
893897
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
898+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
899+
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
894900
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
895901
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
896902
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
@@ -900,6 +906,7 @@ golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
900906
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
901907
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
902908
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
909+
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
903910
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
904911
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
905912
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -908,6 +915,8 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
908915
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
909916
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
910917
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
918+
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
919+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
911920
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
912921
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
913922
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -1110,6 +1119,7 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
11101119
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
11111120
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
11121121
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
1122+
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
11131123
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
11141124
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
11151125
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec h1:RlWgLqCMMIYYEVcAR5MDsuHlVkaIPDAF+5Dehzg8L5A=

0 commit comments

Comments
 (0)