Skip to content

Instantly share code, notes, and snippets.

@Komosa
Last active July 9, 2016 11:05
Show Gist options
  • Save Komosa/0dd8fd1d837f9c39016a511c93baabad to your computer and use it in GitHub Desktop.
Save Komosa/0dd8fd1d837f9c39016a511c93baabad to your computer and use it in GitHub Desktop.
func TestUpdate(t *testing.T) {
os.RemoveAll(TEST_DATA_DIR)
defer os.RemoveAll(TEST_DATA_DIR)
if err := os.MkdirAll(TEST_DATA_DIR, 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(TEST_DATA_DIR+"/number_of_partitions", []byte("2"), 0600); err != nil {
t.Fatal(err)
}
db, err := OpenDB(TEST_DATA_DIR)
if err != nil {
t.Fatal(err)
}
if err = db.Create("col"); err != nil {
t.Fatal(err)
}
col := db.Use("col")
id, err := col.Insert(map[string]interface{}{"a": "x"})
if err != nil {
t.Fatal(err)
}
const N = 100
var wg sync.WaitGroup
wg.Add(N)
for i := 0; i < N; i++ {
go func() {
defer wg.Done()
err := col.Update(id, map[string]interface{}{"a": "x"})
if err != nil {
t.Fatal(err)
}
}()
}
wg.Wait()
doc, err := col.Read(id)
if err != nil {
t.Fatal(err)
}
if doc["a"] != "x" {
t.Fatal("unexpected result")
}
if err = db.Close(); err != nil {
t.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment