Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Event Feed StartTS #182

Merged
merged 2 commits into from
Nov 11, 2024
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
3 changes: 0 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,6 @@ func (c *Client) FeedFromQuery(query *Query, opts ...FeedOptFn) (*EventFeed, err
if err != nil {
return nil, err
}
if feedOpts.Cursor != nil {
return nil, fmt.Errorf("cannot use EventFeedCursor with FeedFromQuery")
}

res, err := c.Query(query)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,20 @@ func EventFeedCursor(cursor string) FeedOptFn {
}

// EventFeedStartTime set the start time for the [fauna.EventFeed]
// cannot be used with [EventFeedCursor]
// cannot be used with [EventFeedCursor] -- expects unix micro timestamp
func EventFeedStartTime(ts int64) FeedOptFn {
return func(req *feedOptions) { req.StartTS = &ts }
}

// EventFeedStartTimeFromTime set the start time for the [fauna.EventFeed]
// from a time.Time -- cannot be used with [EventFeedCursor]
func EventFeedStartTimeFromTime(ts time.Time) FeedOptFn {
return func(req *feedOptions) {
asMicro := ts.UnixMicro()
req.StartTS = &asMicro
}
}

// EventFeedPageSize set the page size for the [fauna.EventFeed]
func EventFeedPageSize(ts int) FeedOptFn {
return func(req *feedOptions) { req.PageSize = &ts }
Expand Down
1 change: 1 addition & 0 deletions event_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (ef *EventFeed) Next(page *FeedPage) error {
}

ef.lastCursor = page.Cursor
ef.opts = &feedOptions{}

return nil
}
20 changes: 15 additions & 5 deletions event_feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func TestEventFeed(t *testing.T) {
require.ErrorContains(t, feedErr, "query should return a fauna.EventSource but got int")
})

t.Run("should error when attempting to use a cursor with a query", func(t *testing.T) {
t.Run("should allow passing a cursor with a query", func(t *testing.T) {
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")

_, feedErr := client.FeedFromQuery(query, fauna.EventFeedCursor("cursor"))
require.ErrorContains(t, feedErr, "cannot use EventFeedCursor with FeedFromQuery")
feed, feedErr := client.FeedFromQuery(query, fauna.EventFeedCursor("cursor"))
require.NoError(t, feedErr, "failed to init events feed")
require.NotNil(t, feed, "feed is nil")
})

t.Run("should error when attempting to use a start time and a cursor", func(t *testing.T) {
Expand Down Expand Up @@ -118,13 +119,22 @@ func TestEventFeed(t *testing.T) {
eventSource = getEventSource(t, client)
require.NotNil(t, eventSource, "failed to get an EventSource")

tenMinutesAgo := time.Now().Add(-10 * time.Minute)
feed, feedErr = client.Feed(eventSource, fauna.EventFeedStartTime(tenMinutesAgo.UnixMicro()))
feed, feedErr = client.Feed(eventSource, fauna.EventFeedStartTimeFromTime(time.Now().Add(-10*time.Minute)))
require.NoError(t, feedErr, "failed to init events feed")

eventsErr = feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events")
require.Equal(t, 1, len(page.Events), "unexpected number of events")
require.NotNil(t, feed)

// get a blank page
for {
eventErr := feed.Next(&page)
require.NoError(t, eventErr)

break
}
require.Empty(t, page.Events)
})

t.Run("can use page size", func(t *testing.T) {
Expand Down
Loading