-
Notifications
You must be signed in to change notification settings - Fork 661
/
test_state.go
65 lines (54 loc) · 1.43 KB
/
test_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package uptime
import (
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)
var _ State = &TestState{}
type uptime struct {
upDuration time.Duration
lastUpdated time.Time
startTime time.Time
}
type TestState struct {
dbReadError error
dbWriteError error
nodes map[ids.NodeID]*uptime
}
func NewTestState() *TestState {
return &TestState{
nodes: make(map[ids.NodeID]*uptime),
}
}
func (s *TestState) AddNode(nodeID ids.NodeID, startTime time.Time) {
st := time.Unix(startTime.Unix(), 0)
s.nodes[nodeID] = &uptime{
lastUpdated: st,
startTime: st,
}
}
func (s *TestState) GetUptime(nodeID ids.NodeID) (time.Duration, time.Time, error) {
up, exists := s.nodes[nodeID]
if !exists {
return 0, time.Time{}, database.ErrNotFound
}
return up.upDuration, up.lastUpdated, s.dbReadError
}
func (s *TestState) SetUptime(nodeID ids.NodeID, upDuration time.Duration, lastUpdated time.Time) error {
up, exists := s.nodes[nodeID]
if !exists {
return database.ErrNotFound
}
up.upDuration = upDuration
up.lastUpdated = time.Unix(lastUpdated.Unix(), 0)
return s.dbWriteError
}
func (s *TestState) GetStartTime(nodeID ids.NodeID) (time.Time, error) {
up, exists := s.nodes[nodeID]
if !exists {
return time.Time{}, database.ErrNotFound
}
return up.startTime, s.dbReadError
}