backend_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package backend
  7. import "testing"
  8. // testBackendBehavior is the generic test suite that must be fulfilled by
  9. // every backend implementation. It should be called by each implementation
  10. // as (part of) their test suite.
  11. func testBackendBehavior(t *testing.T, open func() Backend) {
  12. t.Run("WriteIsolation", func(t *testing.T) { testWriteIsolation(t, open) })
  13. t.Run("DeleteNonexisten", func(t *testing.T) { testDeleteNonexistent(t, open) })
  14. }
  15. func testWriteIsolation(t *testing.T, open func() Backend) {
  16. // Values written during a transaction should not be read back, our
  17. // updateGlobal depends on this.
  18. db := open()
  19. defer db.Close()
  20. // Sanity check
  21. _ = db.Put([]byte("a"), []byte("a"))
  22. v, _ := db.Get([]byte("a"))
  23. if string(v) != "a" {
  24. t.Fatal("read back should work")
  25. }
  26. // Now in a transaction we should still see the old value
  27. tx, _ := db.NewWriteTransaction()
  28. defer tx.Release()
  29. _ = tx.Put([]byte("a"), []byte("b"))
  30. v, _ = tx.Get([]byte("a"))
  31. if string(v) != "a" {
  32. t.Fatal("read in transaction should read the old value")
  33. }
  34. }
  35. func testDeleteNonexistent(t *testing.T, open func() Backend) {
  36. // Deleting a non-existent key is not an error
  37. db := open()
  38. defer db.Close()
  39. err := db.Delete([]byte("a"))
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. }