Skip to content

Commit

Permalink
Explain the use of WaitGroup in the example test
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphpl committed Dec 1, 2023
1 parent cb6f7b6 commit b15eafc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion redis/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type channel[E typevent.Event] struct {
//
// Redis Pub/Sub is used as the underlying event bus. The events emitted on the channel are passed
// to all channels subscribed on the same `name` on the same redis server, regardless of the DB
// they're connected to – see [https://redis.io/docs/interact/pubsub/#database--scoping].
// they're connected to – see https://redis.io/docs/interact/pubsub/#database--scoping.
func NewChannel[E typevent.Event](conf *Config, event string) typevent.Channel[E] {
return &channel[E]{
Config: conf,
Expand Down
2 changes: 1 addition & 1 deletion redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Config struct {
keyPrefix string
}

// NewConfig returns a new [config] for a redis channel.
// NewConfig returns a new [Config] for a redis channel.
func NewConfig(client *redis.Client, opts ...ConfigOption) *Config {
c := &Config{
client: client,
Expand Down
7 changes: 5 additions & 2 deletions redis/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ func ExampleNewChannel() {
conf := redis.NewConfig(client)
channel := redis.NewChannel[event](conf, "CHANNEL_NAME")

// Register a subscriber for the channel.
// The WaitGroup is necessary to make the example test pass. In a real application,
// you would likely just let the handler run until its context is canceled.
wg := sync.WaitGroup{}
wg.Add(1)

// Register a subscriber for the channel.
sub, _ := channel.Subscribe(context.Background(), func(ctx context.Context, ev event) error {
defer wg.Done()
fmt.Printf("subscriber says: %s\n", ev)
Expand All @@ -37,6 +40,6 @@ func ExampleNewChannel() {
// Emit an event on the channel.
channel.Emit("Hello World!")

wg.Wait()
wg.Wait() // again, just there to statisfy the example test
// Output: subscriber says: Hello World!
}

0 comments on commit b15eafc

Please sign in to comment.