Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ExampleDB_Subscribe Test #1214

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
Expand Down Expand Up @@ -1967,70 +1966,6 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func ExampleDB_Subscribe() {
prefix := []byte{'a'}

// This key should be printed, since it matches the prefix.
aKey := []byte("a-key")
aValue := []byte("a-value")

// This key should not be printed.
bKey := []byte("b-key")
bValue := []byte("b-value")

// Open the DB.
dir, err := ioutil.TempDir("", "badger-test")
if err != nil {
panic(err)
}
defer removeDir(dir)
db, err := Open(DefaultOptions(dir))
if err != nil {
panic(err)
}
defer db.Close()

// Create the context here so we can cancel it after sending the writes.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Use the WaitGroup to make sure we wait for the subscription to stop before continuing.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
cb := func(kvs *KVList) error {
for _, kv := range kvs.Kv {
fmt.Printf("%s is now set to %s\n", kv.Key, kv.Value)
}
return nil
}
if err := db.Subscribe(ctx, cb, prefix); err != nil && err != context.Canceled {
panic(err)
}
log.Printf("subscription closed")
}()

// Wait for the above go routine to be scheduled.
time.Sleep(time.Second)
// Write both keys, but only one should be printed in the Output.
err = db.Update(func(txn *Txn) error { return txn.Set(aKey, aValue) })
if err != nil {
panic(err)
}
err = db.Update(func(txn *Txn) error { return txn.Set(bKey, bValue) })
if err != nil {
panic(err)
}

log.Printf("stopping subscription")
cancel()
log.Printf("waiting for subscription to close")
wg.Wait()
// Output:
// a-key is now set to a-value
}

func removeDir(dir string) {
if err := os.RemoveAll(dir); err != nil {
panic(err)
Expand Down