From 51ee9b8cee4076bf756d99923ee74fad58f0bcf4 Mon Sep 17 00:00:00 2001 From: DoMyJob <46307927+DoMyJob@users.noreply.github.com> Date: Thu, 21 Feb 2019 15:57:26 +0800 Subject: [PATCH] Add test for pattern write/close/write/close/read. (#11) This pattern happens in command line calling like: #yottadisk add 1 1 #yottadisk add 2 2 #yottadisk get 1 --> return 1. Add this go test to simulate this behavior. --- yottadisk_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/yottadisk_test.go b/yottadisk_test.go index 900eeb2..9abfb3f 100644 --- a/yottadisk_test.go +++ b/yottadisk_test.go @@ -151,6 +151,66 @@ func stressRead(yd *YottaDisk) error { return nil } +func Test2YottaDiskPutAndAnotherYottaDiskRead(t *testing.T) { + config := opt.DefaultOptions() + // defer os.Remove(config.StorageName) + + yd1, err := OpenYottaDisk(config) + if err != nil { + t.Fatal(err) + } + + type KeyValuePair struct{ + hash types.IndexTableKey + buf []byte + } + + meta := yd1.Meta() + dataCaps := (uint64)(meta.RangeCaps) * (uint64)(meta.RangeCoverage) + fmt.Printf("Starting insert %d data blocks\n", dataCaps) + for i := (uint64)(0); i < dataCaps/2; i++ { + testHash := (types.IndexTableKey)(common.HexToHash(fmt.Sprintf("%032X", i))) + err := yd1.Put(testHash, testHash[:]) + if err != nil { + panic(err) + } + } + yd1.Close() + + yd2, err := OpenYottaDisk(config) + if err != nil { + t.Fatal(err) + } + for i := (uint64)(0); i < dataCaps/2; i++ { + testHash := (types.IndexTableKey)(common.HexToHash(fmt.Sprintf("%032X", i + dataCaps/2))) + err := yd2.Put(testHash, testHash[:]) + if err != nil { + panic(err) + } + } + yd2.Close() + + yd3, err := OpenYottaDisk(config) + if err != nil { + t.Fatal(err) + } + defer yd3.Close() + fmt.Printf("Starting validata %d data blocks\n", dataCaps) + for i := (uint64)(0); i < dataCaps; i++ { + testHash := (types.IndexTableKey)(common.HexToHash(fmt.Sprintf("%032X", i))) + buf, err := yd3.Get(testHash) + if err != nil { + panic(fmt.Sprintf("Error: %v in %d check", err, i)) + } + + if bytes.Compare(buf[:len(testHash)], testHash[:]) != 0 { + panic(fmt.Sprintf("Fatal: %d test fail, want:\n%x\n, get:\n%x\n", i, testHash, buf[:len(testHash)])) + } + } + + fmt.Println(yd3) +} + func stressWrite(yd *YottaDisk) error { type KeyValuePair struct { hash common.Hash -- GitLab