code.vegaprotocol.io/vega@v0.79.0/core/integration/stubs/time_stub.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package stubs
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	vegacontext "code.vegaprotocol.io/vega/libs/context"
    23  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    24  )
    25  
    26  type TimeStub struct {
    27  	now         time.Time
    28  	subscribers []func(context.Context, time.Time)
    29  }
    30  
    31  func NewTimeStub() *TimeStub {
    32  	startTime, _ := time.Parse("2006-01-02T15:04:05Z", "2019-11-30T00:00:00Z")
    33  	return &TimeStub{
    34  		now: startTime,
    35  	}
    36  }
    37  
    38  func (t *TimeStub) GetTimeNow() time.Time {
    39  	return t.now
    40  }
    41  
    42  func (t *TimeStub) SetTimeNow(_ context.Context, newNow time.Time) {
    43  	t.SetTime(newNow)
    44  }
    45  
    46  func (t *TimeStub) SetPrevTime(newNow time.Time) {
    47  }
    48  
    49  func (t *TimeStub) GetTimeLastBatch() time.Time {
    50  	return time.Time{}
    51  }
    52  
    53  func (t *TimeStub) SetTime(newNow time.Time) {
    54  	t.now = newNow
    55  	ctx := vegacontext.WithTraceID(context.Background(), vgcrypto.RandomHash())
    56  	t.notify(ctx, t.now)
    57  }
    58  
    59  func (t *TimeStub) NotifyOnTick(scbs ...func(context.Context, time.Time)) {
    60  	t.subscribers = append(t.subscribers, scbs...)
    61  }
    62  
    63  func (t *TimeStub) notify(context context.Context, newTime time.Time) {
    64  	for _, subscriber := range t.subscribers {
    65  		subscriber(context, newTime)
    66  	}
    67  }