Skip to content

Commit

Permalink
Add event waiting in guest list example
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Feb 4, 2021
1 parent 698de42 commit c473902
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
18 changes: 14 additions & 4 deletions examples/guestlist/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"log"
"sort"
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -57,6 +58,17 @@ func Example() {
eh.NewContextWithNamespace(context.Background(), "simple"),
)

// Setup a test utility waiter that waits for all 11 events to occur before
// evaluating results.
var wg sync.WaitGroup
wg.Add(11)
eventBus.AddHandler(ctx, eh.MatchAll{}, eh.EventHandlerFunc(
func(ctx context.Context, e eh.Event) error {
wg.Done()
return nil
},
))

// Setup the guestlist.
eventID := uuid.New()
guestlist.Setup(
Expand Down Expand Up @@ -89,7 +101,6 @@ func Example() {
if err := commandBus.HandleCommand(ctx, &guestlist.CreateInvite{ID: poseidonID, Name: "Poseidon"}); err != nil {
log.Println("error:", err)
}
time.Sleep(100 * time.Millisecond)

// The invited guests accept and decline the event.
// Note that Athena tries to decline the event after first accepting, but
Expand All @@ -110,14 +121,13 @@ func Example() {
}

// Poseidon is a bit late to the party...
// TODO: Remove sleeps.
time.Sleep(10 * time.Millisecond)
if err := commandBus.HandleCommand(ctx, &guestlist.AcceptInvite{ID: poseidonID}); err != nil {
log.Println("error:", err)
}

// Wait for simulated eventual consistency before reading.
time.Sleep(10 * time.Millisecond)
wg.Wait()
time.Sleep(100 * time.Millisecond)

// Read all invites.
invitationStrs := []string{}
Expand Down
20 changes: 15 additions & 5 deletions examples/guestlist/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"os"
"sort"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -81,6 +82,17 @@ func ExampleIntegration() {
eh.NewContextWithNamespace(context.Background(), "mongodb"),
)

// Setup a test utility waiter that waits for all 11 events to occur before
// evaluating results.
var wg sync.WaitGroup
wg.Add(11)
eventBus.AddHandler(ctx, eh.MatchAll{}, eh.EventHandlerFunc(
func(ctx context.Context, e eh.Event) error {
wg.Done()
return nil
},
))

// Setup the guestlist.
eventID := uuid.New()
guestlist.Setup(
Expand Down Expand Up @@ -118,7 +130,6 @@ func ExampleIntegration() {
if err := commandBus.HandleCommand(ctx, &guestlist.CreateInvite{ID: poseidonID, Name: "Poseidon"}); err != nil {
log.Println("error:", err)
}
time.Sleep(100 * time.Millisecond)

// The invited guests accept and decline the event.
// Note that Athena tries to decline the event after first accepting, but
Expand All @@ -138,15 +149,14 @@ func ExampleIntegration() {
log.Println("error:", err)
}

// Poseidon is a bit late to the party...
// TODO: Remove sleeps.
time.Sleep(100 * time.Millisecond)
// Poseidon is a bit late to the party, will not be accepted...
if err := commandBus.HandleCommand(ctx, &guestlist.AcceptInvite{ID: poseidonID}); err != nil {
log.Println("error:", err)
}

// Wait for simulated eventual consistency before reading.
time.Sleep(100 * time.Millisecond)
wg.Wait()
time.Sleep(1000 * time.Millisecond)

// Read all invites.
invitationStrs := []string{}
Expand Down

0 comments on commit c473902

Please sign in to comment.