mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-10 07:11:08 +00:00
b50d57b7fd
This adds a thin type that holds the state associated with the leveldb.DB, leaving the huge Instance type more or less stateless. Also moves some keying stuff into the DB package so that other packages need not know the keying specifics. (This does not, yet, fix the cmd/stindex program, in order to keep the diff size down. Hence the keying constants are still exported.)
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// Copyright (C) 2018 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"
|
|
|
|
func TestSmallIndex(t *testing.T) {
|
|
db := OpenMemory()
|
|
idx := newSmallIndex(db.DB, []byte{12, 34})
|
|
|
|
// ID zero should be unallocated
|
|
if val, ok := idx.Val(0); ok || val != nil {
|
|
t.Fatal("Unexpected return for nonexistent ID 0")
|
|
}
|
|
|
|
// A new key should get ID zero
|
|
if id := idx.ID([]byte("hello")); id != 0 {
|
|
t.Fatal("Expected 0, not", id)
|
|
}
|
|
// Looking up ID zero should work
|
|
if val, ok := idx.Val(0); !ok || string(val) != "hello" {
|
|
t.Fatalf(`Expected true, "hello", not %v, %q`, ok, val)
|
|
}
|
|
|
|
// Delete the key
|
|
idx.Delete([]byte("hello"))
|
|
|
|
// Next ID should be one
|
|
if id := idx.ID([]byte("key2")); id != 1 {
|
|
t.Fatal("Expected 1, not", id)
|
|
}
|
|
|
|
// Now lets create a new index instance based on what's actually serialized to the database.
|
|
idx = newSmallIndex(db.DB, []byte{12, 34})
|
|
|
|
// Status should be about the same as before.
|
|
if val, ok := idx.Val(0); ok || val != nil {
|
|
t.Fatal("Unexpected return for deleted ID 0")
|
|
}
|
|
if id := idx.ID([]byte("key2")); id != 1 {
|
|
t.Fatal("Expected 1, not", id)
|
|
}
|
|
|
|
// Setting "hello" again should get us ID 2, not 0 as it was originally.
|
|
if id := idx.ID([]byte("hello")); id != 2 {
|
|
t.Fatal("Expected 2, not", id)
|
|
}
|
|
}
|