stats_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2020 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. // The existence of this file means we get 0% test coverage rather than no
  7. // test coverage at all. Remove when implementing an actual test.
  8. package stats
  9. import (
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/db/backend"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. func TestDeviceStat(t *testing.T) {
  16. db := backend.OpenLevelDBMemory()
  17. defer db.Close()
  18. sr := NewDeviceStatisticsReference(db, protocol.LocalDeviceID)
  19. if err := sr.WasSeen(); err != nil {
  20. t.Fatal(err)
  21. }
  22. if err := sr.LastConnectionDuration(42 * time.Second); err != nil {
  23. t.Fatal(err)
  24. }
  25. stat, err := sr.GetStatistics()
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if d := time.Since(stat.LastSeen); d > 5*time.Second {
  30. t.Error("Last seen far in the past:", d)
  31. }
  32. if d := stat.LastConnectionDurationS; d != 42 {
  33. t.Error("Bad last duration:", d)
  34. }
  35. }