...

Source file src/go.etcd.io/etcd/server/v3/mvcc/backend/testing/betesting.go

Documentation: go.etcd.io/etcd/server/v3/mvcc/backend/testing

     1  // Copyright 2021 The etcd Authors
     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 betesting
    16  
    17  import (
    18  	"io/ioutil"
    19  	"path/filepath"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"go.etcd.io/etcd/server/v3/mvcc/backend"
    25  	"go.uber.org/zap/zaptest"
    26  )
    27  
    28  func NewTmpBackendFromCfg(t testing.TB, bcfg backend.BackendConfig) (backend.Backend, string) {
    29  	dir, err := ioutil.TempDir(t.TempDir(), "etcd_backend_test")
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	tmpPath := filepath.Join(dir, "database")
    34  	bcfg.Path = tmpPath
    35  	bcfg.Logger = zaptest.NewLogger(t)
    36  	return backend.New(bcfg), tmpPath
    37  }
    38  
    39  // NewTmpBackend creates a backend implementation for testing.
    40  func NewTmpBackend(t testing.TB, batchInterval time.Duration, batchLimit int) (backend.Backend, string) {
    41  	bcfg := backend.DefaultBackendConfig()
    42  	bcfg.BatchInterval, bcfg.BatchLimit = batchInterval, batchLimit
    43  	return NewTmpBackendFromCfg(t, bcfg)
    44  }
    45  
    46  func NewDefaultTmpBackend(t testing.TB) (backend.Backend, string) {
    47  	return NewTmpBackendFromCfg(t, backend.DefaultBackendConfig())
    48  }
    49  
    50  func Close(t testing.TB, b backend.Backend) {
    51  	assert.NoError(t, b.Close())
    52  }
    53  

View as plain text