go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/matchmaker/pkg/sim/clock.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package sim
     9  
    10  import "time"
    11  
    12  // Clock is a source of time
    13  type Clock interface {
    14  	Now() time.Time
    15  	Wait(time.Duration)
    16  }
    17  
    18  type wallClock struct{}
    19  
    20  func (wc wallClock) Now() time.Time       { return time.Now() }
    21  func (wc wallClock) Wait(d time.Duration) { time.Sleep(d) }
    22  
    23  func NewSimulatedClock(startAt time.Time) Clock {
    24  	return &simulatedClock{ts: startAt}
    25  }
    26  
    27  type simulatedClock struct {
    28  	ts time.Time
    29  }
    30  
    31  func (sc *simulatedClock) Now() time.Time        { return sc.ts }
    32  func (sc *simulatedClock) Wait(by time.Duration) { sc.ts = sc.ts.Add(by) }