From b74015952976e55cba1b8c3abdaac91f3febbf6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Mert=20Y=C4=B1ld=C4=B1ran?= Date: Wed, 2 Mar 2022 21:23:54 +0300 Subject: [PATCH] Add unit tests for `dumpCore` and `restoreCore` methods (#19) * Add unit tests for `dumpCore` and `restoreCore` methods * Increase the timeout of `TestQuery` and `TestFetch` to a minute * Revert "Increase the timeout of `TestQuery` and `TestFetch` to a minute" This reverts commit 8808f1c596744f3d34d56bf8039f88c4bf7997bc. --- server/server.go | 21 +++++++++++++++------ server/server_test.go | 8 ++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/server/server.go b/server/server.go index 5e8243f..325d402 100644 --- a/server/server.go +++ b/server/server.go @@ -297,10 +297,13 @@ func handleExit(sig syscall.Signal) { } // Dumps the core into a file named "basenine.gob" -func dumpCore(silent bool, dontLock bool) { +func dumpCore(silent bool, dontLock bool) (err error) { cdl.Lock() - f, err := os.Create(coreDumpFilenameTemp) - check(err) + var f *os.File + f, err = os.Create(coreDumpFilenameTemp) + if err != nil { + return + } defer f.Close() encoder := gob.NewEncoder(f) @@ -341,6 +344,7 @@ func dumpCore(silent bool, dontLock bool) { log.Printf("Dumped the core to: %s\n", coreDumpFilename) } cdl.Unlock() + return } // Restores the core from a file named "basenine.gob" @@ -370,12 +374,17 @@ func restoreCore() (err error) { cs.partitions = append(cs.partitions, nil) continue } - paritition, err := os.OpenFile(partitionPath, os.O_CREATE|os.O_WRONLY, 0644) - check(err) + var paritition *os.File + paritition, err = os.OpenFile(partitionPath, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return + } cs.partitions = append(cs.partitions, paritition) err = watcher.Add(paritition.Name()) - check(err) + if err != nil { + return + } } cs.partitionIndex = csExport.PartitionIndex cs.partitionSizeLimit = csExport.PartitionSizeLimit diff --git a/server/server_test.go b/server/server_test.go index 8331c15..83a5a3a 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -42,6 +42,14 @@ func TestServerNewPartition(t *testing.T) { f := newPartition() assert.NotNil(t, f) assert.FileExists(t, fmt.Sprintf("%s_%09d.%s", DB_FILE, cs.partitionIndex, DB_FILE_EXT)) +} + +func TestServerDumpRestoreCore(t *testing.T) { + err := dumpCore(false, false) + assert.Nil(t, err) + + err = restoreCore() + assert.Nil(t, err) removeDatabaseFiles() }