github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/index/kvfile_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     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 index_test
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"sync"
    24  	"testing"
    25  
    26  	"camlistore.org/pkg/index"
    27  	"camlistore.org/pkg/index/indextest"
    28  	"camlistore.org/pkg/sorted"
    29  	"camlistore.org/pkg/sorted/kvfile"
    30  	"camlistore.org/pkg/sorted/kvtest"
    31  	"camlistore.org/pkg/test"
    32  )
    33  
    34  func newKvfileSorted(t *testing.T) (kv sorted.KeyValue, cleanup func()) {
    35  	td, err := ioutil.TempDir("", "kvfile-test")
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	kv, err = kvfile.NewStorage(filepath.Join(td, "kvfile"))
    40  	if err != nil {
    41  		os.RemoveAll(td)
    42  		t.Fatal(err)
    43  	}
    44  	return kv, func() {
    45  		kv.Close()
    46  		os.RemoveAll(td)
    47  	}
    48  }
    49  
    50  func TestSorted_Kvfile(t *testing.T) {
    51  	kv, cleanup := newKvfileSorted(t)
    52  	defer cleanup()
    53  	kvtest.TestSorted(t, kv)
    54  }
    55  
    56  func indexTest(t *testing.T,
    57  	sortedGenfn func(t *testing.T) (sorted.KeyValue, func()),
    58  	tfn func(*testing.T, func() *index.Index)) {
    59  	defer test.TLog(t)()
    60  	var mu sync.Mutex // guards cleanups
    61  	var cleanups []func()
    62  	defer func() {
    63  		mu.Lock() // never unlocked
    64  		for _, fn := range cleanups {
    65  			fn()
    66  		}
    67  	}()
    68  	makeIndex := func() *index.Index {
    69  		s, cleanup := sortedGenfn(t)
    70  		mu.Lock()
    71  		cleanups = append(cleanups, cleanup)
    72  		mu.Unlock()
    73  		return index.MustNew(t, s)
    74  	}
    75  	tfn(t, makeIndex)
    76  }
    77  
    78  func TestIndex_Kvfile(t *testing.T) {
    79  	indexTest(t, newKvfileSorted, indextest.Index)
    80  }
    81  
    82  func TestPathsOfSignerTarget_Kvfile(t *testing.T) {
    83  	indexTest(t, newKvfileSorted, indextest.PathsOfSignerTarget)
    84  }
    85  
    86  func TestFiles_Kvfile(t *testing.T) {
    87  	indexTest(t, newKvfileSorted, indextest.Files)
    88  }
    89  
    90  func TestEdgesTo_Kvfile(t *testing.T) {
    91  	indexTest(t, newKvfileSorted, indextest.EdgesTo)
    92  }
    93  
    94  func TestDelete_Kvfile(t *testing.T) {
    95  	indexTest(t, newKvfileSorted, indextest.Delete)
    96  }