Skip to content

Commit

Permalink
#249 view example
Browse files Browse the repository at this point in the history
  • Loading branch information
frairon committed Apr 16, 2020
1 parent 1eaa22f commit 311e9aa
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"hash"
"log"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -746,3 +747,67 @@ func TestView_NewView(t *testing.T) {
test.AssertNil(t, view)
})
}

// This example shows how views are typically created and used
// in the most basic way.
func ExampleView() {
// create a new view
view, err := NewView([]string{"localhost:9092"},
"input-topic",
new(codec.String))

if err != nil {
log.Fatalf("error creating view: %v", err)
}

// provide a cancelable
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start the view
done := make(chan struct{})
go func() {
defer close(done)
err := view.Run(ctx)
if err != nil {
log.Fatalf("Error running view: %v", err)
}
}()

// wait for the view to be recovered

// Option A: by polling
for !view.Recovered() {
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
}

// Option B: by waiting for the signal
<-view.WaitRunning()

// retrieve a value from the view
val, err := view.Get("some-key")
if err != nil {
log.Fatalf("Error getting item from view: %v", err)
}

if val != nil {
// cast it to string
// no need for type assertion, if it was not that type, the codec would've failed
log.Printf("got value %s", val.(string))
}

has, err := view.Has("some-key")
if err != nil {
log.Fatalf("Error getting item from view: %v", err)
}

_ = has

// stop the view and wait for it to shut down before returning
cancel()
<-done
}

0 comments on commit 311e9aa

Please sign in to comment.