mirror of
https://github.com/octoleo/syncthing.git
synced 2024-09-19 21:29:01 +00:00
c71116ee94
This PR does two things, because one lead to the other: - Move the leveldb specific stuff into a small "backend" package that defines a backend interface and the leveldb implementation. This allows, potentially, in the future, switching the db implementation so another KV store should we wish to do so. - Add proper error handling all along the way. The db and backend packages are now errcheck clean. However, I drew the line at modifying the FileSet API in order to keep this manageable and not continue refactoring all of the rest of Syncthing. As such, the FileSet methods still panic on database errors, except for the "database is closed" error which is instead handled by silently returning as quickly as possible, with the assumption that we're anyway "on the way out".
148 lines
3.5 KiB
Go
148 lines
3.5 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package db
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/db/backend"
|
|
)
|
|
|
|
func TestNamespacedInt(t *testing.T) {
|
|
ldb := NewLowlevel(backend.OpenMemory())
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
n2 := NewNamespacedKV(ldb, "bar")
|
|
|
|
// Key is missing to start with
|
|
|
|
if v, ok := n1.Int64("test"); v != 0 || ok {
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
}
|
|
|
|
if err := n1.PutInt64("test", 42); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// It should now exist in n1
|
|
|
|
if v, ok := n1.Int64("test"); v != 42 || !ok {
|
|
t.Errorf("Incorrect return v %v != 42 || ok %v != true", v, ok)
|
|
}
|
|
|
|
// ... but not in n2, which is in a different namespace
|
|
|
|
if v, ok := n2.Int64("test"); v != 0 || ok {
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
}
|
|
|
|
if err := n1.Delete("test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// It should no longer exist
|
|
|
|
if v, ok := n1.Int64("test"); v != 0 || ok {
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
}
|
|
}
|
|
|
|
func TestNamespacedTime(t *testing.T) {
|
|
ldb := NewLowlevel(backend.OpenMemory())
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
if v, ok := n1.Time("test"); !v.IsZero() || ok {
|
|
t.Errorf("Incorrect return v %v != %v || ok %v != false", v, time.Time{}, ok)
|
|
}
|
|
|
|
now := time.Now()
|
|
if err := n1.PutTime("test", now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if v, ok := n1.Time("test"); !v.Equal(now) || !ok {
|
|
t.Errorf("Incorrect return v %v != %v || ok %v != true", v, now, ok)
|
|
}
|
|
}
|
|
|
|
func TestNamespacedString(t *testing.T) {
|
|
ldb := NewLowlevel(backend.OpenMemory())
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
if v, ok := n1.String("test"); v != "" || ok {
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
}
|
|
|
|
if err := n1.PutString("test", "yo"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if v, ok := n1.String("test"); v != "yo" || !ok {
|
|
t.Errorf("Incorrect return v %q != \"yo\" || ok %v != true", v, ok)
|
|
}
|
|
}
|
|
|
|
func TestNamespacedReset(t *testing.T) {
|
|
ldb := NewLowlevel(backend.OpenMemory())
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
if err := n1.PutString("test1", "yo1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := n1.PutString("test2", "yo2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := n1.PutString("test3", "yo3"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if v, ok := n1.String("test1"); v != "yo1" || !ok {
|
|
t.Errorf("Incorrect return v %q != \"yo1\" || ok %v != true", v, ok)
|
|
}
|
|
if v, ok := n1.String("test2"); v != "yo2" || !ok {
|
|
t.Errorf("Incorrect return v %q != \"yo2\" || ok %v != true", v, ok)
|
|
}
|
|
if v, ok := n1.String("test3"); v != "yo3" || !ok {
|
|
t.Errorf("Incorrect return v %q != \"yo3\" || ok %v != true", v, ok)
|
|
}
|
|
|
|
reset(n1)
|
|
|
|
if v, ok := n1.String("test1"); v != "" || ok {
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
}
|
|
if v, ok := n1.String("test2"); v != "" || ok {
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
}
|
|
if v, ok := n1.String("test3"); v != "" || ok {
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
}
|
|
}
|
|
|
|
// reset removes all entries in this namespace.
|
|
func reset(n *NamespacedKV) {
|
|
tr, err := n.db.NewWriteTransaction()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer tr.Release()
|
|
|
|
it, err := tr.NewPrefixIterator(n.prefix)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for it.Next() {
|
|
_ = tr.Delete(it.Key())
|
|
}
|
|
it.Release()
|
|
_ = tr.Commit()
|
|
}
|