github.com/matrixorigin/matrixone@v1.2.0/pkg/tests/txn/cluster.go (about)

     1  // Copyright 2021 - 2022 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 txn
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/stopper"
    24  	"github.com/matrixorigin/matrixone/pkg/logutil"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/tests/service"
    27  	"github.com/matrixorigin/matrixone/pkg/txn/clock"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"go.uber.org/zap"
    31  )
    32  
    33  var (
    34  	defaultTestTimeout = time.Minute
    35  )
    36  
    37  type cluster struct {
    38  	t       *testing.T
    39  	logger  *zap.Logger
    40  	clock   clock.Clock
    41  	env     service.Cluster
    42  	stopper *stopper.Stopper
    43  }
    44  
    45  // NewCluster new txn testing cluster based on the service.Cluster
    46  func NewCluster(ctx context.Context, t *testing.T, options service.Options) (Cluster, error) {
    47  	logger := logutil.GetPanicLoggerWithLevel(zap.DebugLevel)
    48  	env, err := service.NewCluster(ctx, t, options.WithLogger(logger))
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	stopper := stopper.NewStopper("test-env-stopper")
    53  	return &cluster{
    54  		t:       t,
    55  		env:     env,
    56  		logger:  logger,
    57  		clock:   clock.NewUnixNanoHLCClockWithStopper(stopper, 0),
    58  		stopper: stopper,
    59  	}, nil
    60  }
    61  
    62  func (c *cluster) GetLogger() *zap.Logger {
    63  	return c.logger
    64  }
    65  
    66  func (c *cluster) Start() {
    67  	if err := c.env.Start(); err != nil {
    68  		assert.FailNow(c.t, fmt.Sprintf("start testing cluster failed, %v", err))
    69  	}
    70  
    71  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    72  	defer cancel()
    73  	c.env.WaitHAKeeperState(ctx, logservice.HAKeeperRunning)
    74  	c.env.WaitHAKeeperLeader(ctx)
    75  	c.env.WaitTNShardsReported(ctx)
    76  }
    77  
    78  func (c *cluster) Stop() {
    79  	c.logger.Info("cluster start stop")
    80  	if err := c.env.Close(); err != nil {
    81  		assert.FailNow(c.t, "stop testing cluster failed")
    82  	}
    83  	c.logger.Info("cluster stop completed")
    84  }
    85  
    86  func (c *cluster) Env() service.Cluster {
    87  	return c.env
    88  }
    89  
    90  func (c *cluster) NewClient() Client {
    91  	cli, err := newSQLClient(c.env)
    92  	require.NoError(c.t, err)
    93  	return cli
    94  }