From d638d2078b342e93ab3e64709321a3178298d576 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Wed, 30 May 2018 14:45:15 +0200 Subject: [PATCH] common/mclock: add SimulatedClock --- common/mclock/mclock.go | 26 ++++++++ common/mclock/simclock.go | 130 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 common/mclock/simclock.go diff --git a/common/mclock/mclock.go b/common/mclock/mclock.go index 02608d17b0..76d1e5cdb7 100644 --- a/common/mclock/mclock.go +++ b/common/mclock/mclock.go @@ -30,3 +30,29 @@ type AbsTime time.Duration func Now() AbsTime { return AbsTime(monotime.Now()) } + +// Clock interface makes it possible to replace the monotonic system clock with +// a simulated clock +type Clock interface { + Now() AbsTime + Sleep(time.Duration) + After(time.Duration) <-chan time.Time +} + +// MonotonicClock implements Clock using the system clock +type MonotonicClock struct{} + +// Now implements Clock +func (MonotonicClock) Now() AbsTime { + return AbsTime(monotime.Now()) +} + +// Sleep implements Clock +func (MonotonicClock) Sleep(d time.Duration) { + time.Sleep(d) +} + +// After implements Clock +func (MonotonicClock) After(d time.Duration) <-chan time.Time { + return time.After(d) +} diff --git a/common/mclock/simclock.go b/common/mclock/simclock.go new file mode 100644 index 0000000000..11cff0dc4a --- /dev/null +++ b/common/mclock/simclock.go @@ -0,0 +1,130 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// package mclock is a wrapper for a monotonic clock source +package mclock + +import ( + "runtime" + "sync" + "time" +) + +type event struct { + do func() + at AbsTime +} + +// SimulatedClock implements a virtual Clock for reproducible time-sensitive tests. +// It simulates a scheduler on a virtual timescale where actual processing takes zero time. +// +// Note: since there is no way in Go to know when all goroutines have reached a waiting +// state (which should theoretically happen in each virtual moment), the algorithm runs +// GoSched a fixed number of times after each step and limits time steps in order to +// minimize precision loss (see maxStep and goSchedCount). +type SimulatedClock struct { + now AbsTime + scheduled []event + stop bool + lock sync.RWMutex +} + +// NewSimulatedClock creates a new simulated clock +func NewSimulatedClock(maxStep time.Duration, goSchedCount int) *SimulatedClock { + s := &SimulatedClock{scheduled: make([]event, 0, 100)} + + go func() { + lastScheduled := 0 + for { + for i := 0; i < goSchedCount; i++ { + runtime.Gosched() + } + s.lock.Lock() + if s.stop { + s.lock.Unlock() + return + } + scheduled := len(s.scheduled) + if scheduled > 0 && scheduled == lastScheduled { + ev := s.scheduled[0] + if ev.at <= s.now+AbsTime(maxStep) { + s.scheduled = s.scheduled[1:] + s.now = ev.at + ev.do() + } else { + s.now += AbsTime(maxStep) + } + } + lastScheduled = scheduled + s.lock.Unlock() + } + }() + + return s +} + +// Stop stops the clock (Sleeps and Afters will never return after this) +func (s *SimulatedClock) Stop() { + s.lock.Lock() + s.stop = true + s.lock.Unlock() +} + +// Now implements Clock +func (s *SimulatedClock) Now() AbsTime { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.now +} + +// Sleep implements Clock +func (s *SimulatedClock) Sleep(d time.Duration) { + done := make(chan struct{}) + s.insert(d, func() { + close(done) + }) + <-done +} + +// After implements Clock +func (s *SimulatedClock) After(d time.Duration) <-chan time.Time { + after := make(chan time.Time, 1) + s.insert(d, func() { + after <- time.Unix(0, int64(s.now)) + }) + return after +} + +func (s *SimulatedClock) insert(d time.Duration, do func()) { + s.lock.Lock() + defer s.lock.Unlock() + + at := s.now + AbsTime(d) + l, h := 0, len(s.scheduled) + ll := h + for l != h { + m := (l + h) / 2 + if at < s.scheduled[m].at { + h = m + } else { + l = m + 1 + } + } + s.scheduled = append(s.scheduled, event{}) + copy(s.scheduled[l+1:], s.scheduled[l:ll]) + s.scheduled[l] = event{do: do, at: at} +}