Skip to content

Commit

Permalink
chore: fix tester
Browse files Browse the repository at this point in the history
  • Loading branch information
ucpr committed Jul 11, 2024
1 parent 987aa8f commit 5cb72fa
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions hack/tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/ucpr/mongo-streamer/internal/mongo"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (a *App) Run(ctx context.Context) error {
select {
case <-ctx.Done():
log.Info("Received signal to stop the application...")
break
return nil
default:
operation := rand.Intn(4) // 0: insert, 1: insert & update, 2: upsert, 4: insert and delete
tweet := NewRandomTweet()
Expand All @@ -66,23 +67,32 @@ func (a *App) Run(ctx context.Context) error {
log.Error("Failed to insert tweet", log.Ferror(err))
continue
}
tweet.Text = "Hello, World! Updated"
if _, err := col.UpdateOne(ctx, tweet.ID, tweet); err != nil {
updated := "Hello, World! Updated"
if _, err := col.UpdateOne(ctx, bson.M{"_id": tweet.ID}, bson.M{
"$set": bson.M{
"text": updated,
},
}); err != nil {
log.Error("Failed to update tweet", log.Ferror(err))
}
case 2:
opts := &options.UpdateOptions{
Upsert: &[]bool{true}[0],
}
if _, err := col.UpdateOne(ctx, tweet.ID, tweet, opts); err != nil {
if _, err := col.UpdateOne(ctx, bson.M{"_id": tweet.ID},
bson.M{
"$set": tweet,
}, opts); err != nil {
log.Error("Failed to upsert tweet", log.Ferror(err))
}
case 3:
if _, err := col.InsertOne(ctx, tweet); err != nil {
log.Error("Failed to insert tweet", log.Ferror(err))
continue
}
if _, err := col.DeleteOne(ctx, tweet.ID); err != nil {
if _, err := col.DeleteOne(ctx, bson.M{
"_id": tweet.ID,
}); err != nil {
log.Error("Failed to delete tweet", log.Ferror(err))
}
}
Expand All @@ -91,7 +101,7 @@ func (a *App) Run(ctx context.Context) error {
select {
case <-ctx.Done():
log.Info("Received signal to stop the application...")
break
return nil
case <-time.After(time.Duration(rand.Intn(5)+1) * time.Second):
}
}
Expand All @@ -102,6 +112,7 @@ func (a *App) Close(ctx context.Context) error {
}

func main() {
log.Info("Starting hack/tester...")
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
defer stop()

Expand Down

0 comments on commit 5cb72fa

Please sign in to comment.