github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logtail/service/waterline_test.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package service
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    22  	"github.com/matrixorigin/matrixone/pkg/txn/clock"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestWaterliner(t *testing.T) {
    27  	current := mockTimestamp(100, 100)
    28  	clock := mockPermanentClock(current)
    29  
    30  	w := NewWaterliner(clock)
    31  
    32  	/* ---- advance on uninitialized instance ---- */
    33  	require.Panics(t, func() {
    34  		w.Advance(current)
    35  	})
    36  
    37  	/* ---- initialized on its first call ---- */
    38  	waterline := w.Waterline()
    39  	require.Equal(t, current, waterline)
    40  
    41  	/* ---- advance with a backward ts ---- */
    42  	require.Panics(t, func() {
    43  		prev := current.Prev()
    44  		w.Advance(prev)
    45  	})
    46  
    47  	/* ---- advance with a monotonous ts ---- */
    48  	next := current.Next()
    49  	w.Advance(next)
    50  	waterline = w.Waterline()
    51  	require.Equal(t, next, waterline)
    52  }
    53  
    54  type permanentClock struct {
    55  	ts timestamp.Timestamp
    56  }
    57  
    58  func mockPermanentClock(ts timestamp.Timestamp) clock.Clock {
    59  	return &permanentClock{
    60  		ts: ts,
    61  	}
    62  }
    63  
    64  func (m *permanentClock) Now() (timestamp.Timestamp, timestamp.Timestamp) {
    65  	return m.ts, timestamp.Timestamp{}
    66  }
    67  
    68  func (m *permanentClock) Update(ts timestamp.Timestamp) {
    69  	m.ts = ts
    70  }
    71  
    72  func (m *permanentClock) HasNetworkLatency() bool {
    73  	return false
    74  }
    75  
    76  func (m *permanentClock) MaxOffset() time.Duration {
    77  	return 0
    78  }
    79  
    80  func (m *permanentClock) SetNodeID(id uint16) {
    81  
    82  }