summaryrefslogtreecommitdiff
path: root/txnsync/emulatorTimer_test.go
blob: f6d0dc57b92d75da7fd64b102fa2bc787e25ee2e (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package txnsync

import (
	"sort"
	"sync"
	"time"

	"github.com/algorand/go-algorand/util/timers"
)

// guidedClock implements the WallClock interface
type guidedClock struct {
	sync.Mutex `algofix:"allow sync.Mutex"`
	zero       time.Time
	adv        time.Duration
	timers     map[time.Duration]chan time.Time
	children   []*guidedClock
}

func makeGuidedClock() *guidedClock {
	return &guidedClock{
		zero: time.Now(),
	}
}
func (g *guidedClock) Zero() timers.Clock {
	// the real monotonic clock doesn't return the same clock object, which is fine.. but for our testing
	// we want to keep the same clock object so that we can tweak with it.
	child := &guidedClock{
		zero: g.zero.Add(g.adv),
	}
	g.Lock()
	defer g.Unlock()
	g.children = append(g.children, child)
	return child
}

func (g *guidedClock) TimeoutAt(delta time.Duration) <-chan time.Time {
	if delta <= g.adv {
		c := make(chan time.Time, 1)
		close(c)
		return c
	}
	g.Lock()
	defer g.Unlock()
	if g.timers == nil {
		g.timers = make(map[time.Duration]chan time.Time)
	}
	c, has := g.timers[delta]
	if has {
		return c
	}
	c = make(chan time.Time, 1)
	g.timers[delta] = c
	return c
}

func (g *guidedClock) Encode() []byte {
	return []byte{}
}
func (g *guidedClock) Decode([]byte) (timers.Clock, error) {
	return &guidedClock{}, nil
}

func (g *guidedClock) Since() time.Duration {
	return g.adv
}

func (g *guidedClock) DeadlineMonitorAt(at time.Duration) timers.DeadlineMonitor {
	return timers.MakeMonotonicDeadlineMonitor(g, at)
}

func (g *guidedClock) Advance(adv time.Duration) {
	g.adv += adv

	type entryStruct struct {
		duration time.Duration
		ch       chan time.Time
	}
	expiredClocks := []entryStruct{}
	g.Lock()
	// find all the expired clocks.
	for delta, ch := range g.timers {
		if delta < g.adv {
			expiredClocks = append(expiredClocks, entryStruct{delta, ch})
		}
	}
	sort.SliceStable(expiredClocks, func(i, j int) bool {
		return expiredClocks[i].duration < expiredClocks[j].duration
	})

	// remove from map
	for _, entry := range expiredClocks {
		delete(g.timers, entry.duration)
	}
	g.Unlock()
	// fire expired clocks
	for _, entry := range expiredClocks {
		entry.ch <- g.zero.Add(g.adv)
		close(entry.ch)
	}
	g.Lock()
	defer g.Unlock()
	for _, child := range g.children {
		child.Advance(adv)
	}
}