-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
197 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package sqlite | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
watermillsql "github.com/ThreeDotsLabs/watermill-sql/v2/pkg/sql" | ||
"github.com/ThreeDotsLabs/watermill/message" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Subscriber struct { | ||
watermillSubscriber *watermillsql.Subscriber | ||
offsetsAdapter watermillsql.OffsetsAdapter | ||
schema SqliteSchema | ||
db *sql.DB | ||
} | ||
|
||
func NewSubscriber( | ||
watermillSubscriber *watermillsql.Subscriber, | ||
offsetsAdapter watermillsql.OffsetsAdapter, | ||
schema SqliteSchema, | ||
db *sql.DB, | ||
) *Subscriber { | ||
return &Subscriber{ | ||
watermillSubscriber: watermillSubscriber, | ||
offsetsAdapter: offsetsAdapter, | ||
schema: schema, | ||
db: db, | ||
} | ||
} | ||
|
||
func (s *Subscriber) SubscribeToTweetCreated(ctx context.Context) (<-chan *message.Message, error) { | ||
return s.watermillSubscriber.Subscribe(ctx, TweetCreatedTopic) | ||
} | ||
|
||
func (s *Subscriber) TweetCreatedQueueLength(ctx context.Context) (int, error) { | ||
offsetsQuery, offsetsQueryArgs := s.offsetsAdapter.NextOffsetQuery(TweetCreatedTopic, consumerGroupName) | ||
|
||
selectQuery := ` | ||
SELECT COUNT(*) | ||
FROM ` + s.schema.MessagesTable(TweetCreatedTopic) + ` | ||
WHERE offset > (` + offsetsQuery + `)` | ||
|
||
row := s.db.QueryRowContext(ctx, selectQuery, offsetsQueryArgs...) | ||
|
||
var count int | ||
if err := row.Scan(&count); err != nil { | ||
return 0, errors.Wrap(err, "error calling row scan") | ||
} | ||
|
||
return count, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package sqlite_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/planetary-social/nos-crossposting-service/internal/fixtures" | ||
"github.com/planetary-social/nos-crossposting-service/service/adapters/sqlite" | ||
"github.com/planetary-social/nos-crossposting-service/service/domain" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSubscriber_TweetCreatedQueueLength(t *testing.T) { | ||
ctx := fixtures.TestContext(t) | ||
adapters := NewTestAdapters(ctx, t) | ||
|
||
n, err := adapters.Subscriber.TweetCreatedQueueLength(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, n) | ||
|
||
err = adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error { | ||
err := adapters.Publisher.PublishTweetCreated(fixtures.SomeAccountID(), domain.NewTweet(fixtures.SomeString())) | ||
require.NoError(t, err) | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error { | ||
err := adapters.Publisher.PublishTweetCreated(fixtures.SomeAccountID(), domain.NewTweet(fixtures.SomeString())) | ||
require.NoError(t, err) | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
n, err = adapters.Subscriber.TweetCreatedQueueLength(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, n) | ||
|
||
go func() { | ||
ch, err := adapters.Subscriber.SubscribeToTweetCreated(ctx) | ||
require.NoError(t, err) | ||
|
||
for msg := range ch { | ||
msg.Ack() | ||
} | ||
}() | ||
|
||
require.EventuallyWithT(t, func(t *assert.CollectT) { | ||
n, err := adapters.Subscriber.TweetCreatedQueueLength(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, n) | ||
}, 5*time.Second, 100*time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters