Skip to content

Commit

Permalink
Remove ExampleDB_Subscribe Test (#1214)
Browse files Browse the repository at this point in the history
The test ExampleDB_Subscribe doesn't run reliably on appveyor.
This commit removes it.
  • Loading branch information
Ibrahim Jarif authored Feb 24, 2020
1 parent 8734e3a commit e029e93
Showing 1 changed file with 0 additions and 65 deletions.
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 @@ -1962,70 +1961,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

0 comments on commit e029e93

Please sign in to comment.