github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/swarm/storage/dbstore_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  func initDbStore(t *testing.T) *DbStore {
    28  	dir, err := ioutil.TempDir("", "bzz-storage-test")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	m, err := NewDbStore(dir, MakeHashFunc(defaultHash), defaultDbCapacity, defaultRadius)
    33  	if err != nil {
    34  		t.Fatal("can't create store:", err)
    35  	}
    36  	return m
    37  }
    38  
    39  func testDbStore(l int64, branches int64, t *testing.T) {
    40  	m := initDbStore(t)
    41  	defer m.Close()
    42  	testStore(m, l, branches, t)
    43  }
    44  
    45  func TestDbStore128_0x1000000(t *testing.T) {
    46  	testDbStore(0x1000000, 128, t)
    47  }
    48  
    49  func TestDbStore128_10000_(t *testing.T) {
    50  	testDbStore(10000, 128, t)
    51  }
    52  
    53  func TestDbStore128_1000_(t *testing.T) {
    54  	testDbStore(1000, 128, t)
    55  }
    56  
    57  func TestDbStore128_100_(t *testing.T) {
    58  	testDbStore(100, 128, t)
    59  }
    60  
    61  func TestDbStore2_100_(t *testing.T) {
    62  	testDbStore(100, 2, t)
    63  }
    64  
    65  func TestDbStoreNotFound(t *testing.T) {
    66  	m := initDbStore(t)
    67  	defer m.Close()
    68  	_, err := m.Get(ZeroKey)
    69  	if err != notFound {
    70  		t.Errorf("Expected notFound, got %v", err)
    71  	}
    72  }
    73  
    74  func TestDbStoreSyncIterator(t *testing.T) {
    75  	m := initDbStore(t)
    76  	defer m.Close()
    77  	keys := []Key{
    78  		Key(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")),
    79  		Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
    80  		Key(common.Hex2Bytes("5000000000000000000000000000000000000000000000000000000000000000")),
    81  		Key(common.Hex2Bytes("3000000000000000000000000000000000000000000000000000000000000000")),
    82  		Key(common.Hex2Bytes("2000000000000000000000000000000000000000000000000000000000000000")),
    83  		Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
    84  	}
    85  	for _, key := range keys {
    86  		m.Put(NewChunk(key, nil))
    87  	}
    88  	it, err := m.NewSyncIterator(DbSyncState{
    89  		Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
    90  		Stop:  Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
    91  		First: 2,
    92  		Last:  4,
    93  	})
    94  	if err != nil {
    95  		t.Fatalf("unexpected error creating NewSyncIterator")
    96  	}
    97  
    98  	var chunk Key
    99  	var res []Key
   100  	for {
   101  		chunk = it.Next()
   102  		if chunk == nil {
   103  			break
   104  		}
   105  		res = append(res, chunk)
   106  	}
   107  	if len(res) != 1 {
   108  		t.Fatalf("Expected 1 chunk, got %v: %v", len(res), res)
   109  	}
   110  	if !bytes.Equal(res[0][:], keys[3]) {
   111  		t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
   112  	}
   113  
   114  	if err != nil {
   115  		t.Fatalf("unexpected error creating NewSyncIterator")
   116  	}
   117  
   118  	it, err = m.NewSyncIterator(DbSyncState{
   119  		Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
   120  		Stop:  Key(common.Hex2Bytes("5000000000000000000000000000000000000000000000000000000000000000")),
   121  		First: 2,
   122  		Last:  4,
   123  	})
   124  
   125  	res = nil
   126  	for {
   127  		chunk = it.Next()
   128  		if chunk == nil {
   129  			break
   130  		}
   131  		res = append(res, chunk)
   132  	}
   133  	if len(res) != 2 {
   134  		t.Fatalf("Expected 2 chunk, got %v: %v", len(res), res)
   135  	}
   136  	if !bytes.Equal(res[0][:], keys[3]) {
   137  		t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
   138  	}
   139  	if !bytes.Equal(res[1][:], keys[2]) {
   140  		t.Fatalf("Expected %v chunk, got %v", keys[2], res[1])
   141  	}
   142  
   143  	if err != nil {
   144  		t.Fatalf("unexpected error creating NewSyncIterator")
   145  	}
   146  
   147  	it, _ = m.NewSyncIterator(DbSyncState{
   148  		Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
   149  		Stop:  Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
   150  		First: 2,
   151  		Last:  5,
   152  	})
   153  	res = nil
   154  	for {
   155  		chunk = it.Next()
   156  		if chunk == nil {
   157  			break
   158  		}
   159  		res = append(res, chunk)
   160  	}
   161  	if len(res) != 2 {
   162  		t.Fatalf("Expected 2 chunk, got %v", len(res))
   163  	}
   164  	if !bytes.Equal(res[0][:], keys[4]) {
   165  		t.Fatalf("Expected %v chunk, got %v", keys[4], res[0])
   166  	}
   167  	if !bytes.Equal(res[1][:], keys[3]) {
   168  		t.Fatalf("Expected %v chunk, got %v", keys[3], res[1])
   169  	}
   170  
   171  	it, _ = m.NewSyncIterator(DbSyncState{
   172  		Start: Key(common.Hex2Bytes("2000000000000000000000000000000000000000000000000000000000000000")),
   173  		Stop:  Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
   174  		First: 2,
   175  		Last:  5,
   176  	})
   177  	res = nil
   178  	for {
   179  		chunk = it.Next()
   180  		if chunk == nil {
   181  			break
   182  		}
   183  		res = append(res, chunk)
   184  	}
   185  	if len(res) != 1 {
   186  		t.Fatalf("Expected 1 chunk, got %v", len(res))
   187  	}
   188  	if !bytes.Equal(res[0][:], keys[3]) {
   189  		t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
   190  	}
   191  }