Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/chipingress/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ func NewEvent(domain, entity string, payload []byte, attributes map[string]any)
attributes = make(map[string]any)
}

if val, ok := attributes["recordedtime"].(time.Time); ok {
event.SetExtension("recordedtime", val.UTC())
} else {
event.SetExtension("recordedtime", ce.Timestamp{Time: time.Now().UTC()})
recordedTime := time.Now()
if val, ok := attributes["recordedtime"].(time.Time); ok && !val.IsZero() {
recordedTime = val
}
recordedTime = recordedTime.UTC().Truncate(time.Millisecond)
event.SetExtension("recordedtime", ce.Timestamp{Time: recordedTime})

if val, ok := attributes["time"].(time.Time); ok {
if val, ok := attributes["time"].(time.Time); ok && !val.IsZero() {
event.SetTime(val.UTC())
}
if val, ok := attributes["datacontenttype"].(string); ok {
Expand Down
3 changes: 1 addition & 2 deletions pkg/chipingress/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestNewEvent(t *testing.T) {
"datacontenttype": "application/protobuf",
"dataschema": "https://example.com/schema",
"subject": "example-subject",
"time": time.Now(),
"time": time.Now().Add(-5 * time.Second),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does adding -5 seconds do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its setting the time attribute to 5 seconds ago.
recordedtime is set by default to time.now and it should always occur after time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the change to milliseconds the difference has to be greater for the testcase to pass (line 90)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, so truncating to millisecond causes the test to fail b/c in the older version the nano second difference in time.Now() took care of recordedtime occuring after time?

}
assert.NoError(t, err)

Expand All @@ -87,7 +87,6 @@ func TestNewEvent(t *testing.T) {
assert.Equal(t, "example-subject", event.Subject())
assert.Equal(t, attributes["time"].(time.Time).UTC(), event.Time())
assert.NotEmpty(t, event.Extensions()["recordedtime"])
assert.NotEmpty(t, event.Extensions()["recordedtime"])
assert.True(t, event.Extensions()["recordedtime"].(ce.Timestamp).Time.After(attributes["time"].(time.Time)))

// Assert the event data was set as expected
Expand Down
Loading