github.com/bhojpur/cache@v0.0.4/pkg/file/types/sharddb_test.go (about) 1 package types 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 import ( 24 "math/rand" 25 "testing" 26 27 "github.com/bhojpur/cache/pkg/file/crypto" 28 ) 29 30 func TestGenerateShardDB(t *testing.T) { 31 // Generate shards 32 testBytes := []byte("im a neat test file") 33 shards, err := GenerateShards(testBytes, 3) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 // Make len(shards) random nodes 39 var nodes []NodeID 40 for i := 0; i < len(shards); i++ { 41 randomPort := rand.Intn(10000-9000) + 9000 42 randomNode, err := NewNodeID("0.0.0.0", randomPort) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 nodes = append(nodes, *randomNode) 48 } 49 50 // Generate shardDB 51 sharddb, err := generateShardDB(shards, nodes) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 // Test the map 57 for k, v := range sharddb.shardMap { 58 t.Logf("%x: %v\n\n", k, v) 59 } 60 61 // Test map access 62 h, err := crypto.HashFromString("bafc4c93a862aecc87368f291090fc2fe479eecc3bd15b7efdde01ff92c42592") 63 if err != nil { 64 t.Fatal(err) 65 } 66 t.Logf("\nGET: [%v]\n", sharddb.shardMap[shardID(h)]) 67 68 // Analyze shard sizes 69 var sizeCounter uint32 70 for _, v := range sharddb.shardMap { 71 t.Logf("size: %d\n", v.Size) 72 sizeCounter = sizeCounter + v.Size 73 } 74 t.Logf("correct size: %d", len(testBytes)) 75 t.Logf(" actual size: %d", sizeCounter) 76 }