github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/ledger/util/leveldbhelper/pkg_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package leveldbhelper
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  )
    25  
    26  const testDBPath = "/tmp/fabric/ledgertests/util/leveldbhelper"
    27  
    28  type testDBEnv struct {
    29  	t    *testing.T
    30  	path string
    31  	db   *DB
    32  }
    33  
    34  type testDBProviderEnv struct {
    35  	t        *testing.T
    36  	path     string
    37  	provider *Provider
    38  }
    39  
    40  func newTestDBEnv(t *testing.T, path string) *testDBEnv {
    41  	testDBEnv := &testDBEnv{t: t, path: path}
    42  	testDBEnv.cleanup()
    43  	testDBEnv.db = CreateDB(&Conf{path})
    44  	return testDBEnv
    45  }
    46  
    47  func newTestProviderEnv(t *testing.T, path string) *testDBProviderEnv {
    48  	testProviderEnv := &testDBProviderEnv{t: t, path: path}
    49  	testProviderEnv.cleanup()
    50  	testProviderEnv.provider = NewProvider(&Conf{path})
    51  	return testProviderEnv
    52  }
    53  
    54  func (dbEnv *testDBEnv) cleanup() {
    55  	if dbEnv.db != nil {
    56  		dbEnv.db.Close()
    57  	}
    58  	testutil.AssertNoError(dbEnv.t, os.RemoveAll(dbEnv.path), "")
    59  }
    60  
    61  func (providerEnv *testDBProviderEnv) cleanup() {
    62  	if providerEnv.provider != nil {
    63  		providerEnv.provider.Close()
    64  	}
    65  	testutil.AssertNoError(providerEnv.t, os.RemoveAll(providerEnv.path), "")
    66  }