Skip to content

Commit

Permalink
Switch filters to use rfc-3339 for time string encoding (#7466)
Browse files Browse the repository at this point in the history
* Switch filters to use rfc-3339 for time string encoding

Signed-off-by: Calum Murray <cmurray@redhat.com>

* Unit test that the exact time match is stable

Signed-off-by: Calum Murray <cmurray@redhat.com>

* cleanup: switched 10_000 to 1000 for test iterations

Signed-off-by: Calum Murray <cmurray@redhat.com>

---------

Signed-off-by: Calum Murray <cmurray@redhat.com>
  • Loading branch information
Cali0707 authored Dec 5, 2023
1 parent 83125a9 commit 2ee2699
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/eventfilter/attributes/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func LookupAttribute(event cloudevents.Event, attr string) (interface{}, bool) {
case "id":
return event.ID(), true
case "time":
return event.Time().String(), true
b, err := event.Time().MarshalText()
if err != nil {
return nil, false
}
return string(b), true
case "dataschema":
return event.DataSchema(), true
case "schemaurl":
Expand Down
3 changes: 2 additions & 1 deletion pkg/eventfilter/attributes/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,15 @@ func TestAllSupportedAttributeFieldsV1(t *testing.T) {
e.SetDataSchema("wow")
e.SetSubject("cool")
e.SetDataContentType("cheers;mate")
timeBytes, _ := e.Time().MarshalText()

attributes := map[string]string{
"specversion": e.SpecVersion(),
"type": e.Type(),
"source": e.Source(),
"subject": e.Subject(),
"id": e.ID(),
"time": e.Time().String(),
"time": string(timeBytes),
"dataschema": e.DataSchema(),
"schemaurl": e.DataSchema(),
"datacontenttype": e.DataContentType(),
Expand Down
26 changes: 26 additions & 0 deletions pkg/eventfilter/subscriptionsapi/exact_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

cloudevents "github.com/cloudevents/sdk-go/v2"
cetest "github.com/cloudevents/sdk-go/v2/test"

"knative.dev/eventing/pkg/eventfilter"
)
Expand Down Expand Up @@ -139,3 +140,28 @@ func makeEventWithExtension(extName, extValue string) *cloudevents.Event {
e.SetExtension(extName, extValue)
return e
}

func TestExactTimeMatchIsStable(t *testing.T) {
event := cetest.FullEvent()
time, err := event.Time().MarshalText()
if err != nil {
t.Fatalf("error while marshalling time to text: %v", err)
}
f, err := NewExactFilter(map[string]string{
"time": string(time),
})
if err != nil {
t.Fatalf("failed to create filter")
}
// the filter should consistently pass here, let's check that it does
failCount := 0
for i := 0; i < 1000; i++ {
if got := f.Filter(context.TODO(), event); got != eventfilter.PassFilter {
failCount += 1
}
}
if failCount != 0 {
t.Errorf("exact filter match on time should be consistent, failed %d / 10,000 times for same event", failCount)
}

}

0 comments on commit 2ee2699

Please sign in to comment.