Merge pull request #2 from aldy505/feat/rename

feat: rename method
This commit is contained in:
Reinaldy Rafli 2021-11-16 20:21:47 +07:00 committed by GitHub
commit 0a7f1acfd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 179 additions and 92 deletions

View File

@ -10,20 +10,20 @@ import (
)
func TestAppend(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("Hello"))
err := c.Write(randomValue, []byte("Hello"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Append(rand, []byte("World"))
err = c.Append(randomValue, []byte("World"))
if err != nil {
t.Error("an error was thrown:", err)
}
r, err := c.Read(rand)
r, err := c.Read(randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
@ -34,10 +34,10 @@ func TestAppend(t *testing.T) {
}
func TestAppend_NotExists(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Append(rand, []byte("Hello"))
err := c.Append(randomValue, []byte("Hello"))
if err == nil {
t.Error("expected an error, got nil")
}

View File

@ -15,6 +15,7 @@ type Cache struct {
var ErrNotExists = errors.New("key does not exist")
var ErrInvalidPath = errors.New("path supplied is invalid")
var ErrDiskFull = errors.New("there was no space left on the device")
var ErrExists = errors.New("key already exists")
func Default() *Cache {
return &Cache{

View File

@ -3,15 +3,16 @@ package cheapcash_test
import (
"math/rand"
"strconv"
"sync"
"testing"
"github.com/aldy505/cheapcash"
)
func TestExists(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
b, err := c.Exists(c.Path + "/" + rand)
b, err := c.Exists(c.Path + "/" + randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
@ -20,12 +21,12 @@ func TestExists(t *testing.T) {
t.Error("expected false, got true")
}
err = c.Write(rand, []byte("value"))
err = c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
b, err = c.Exists(c.Path + "/" + rand)
b, err = c.Exists(c.Path + "/" + randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
@ -36,80 +37,28 @@ func TestExists(t *testing.T) {
}
func TestExists_Concurrency(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
res := make(chan bool, 5)
var wg sync.WaitGroup
go func() {
b, err := c.Exists(c.Path + "/" + rand)
existsFunc := func() {
b, err := c.Exists(c.Path + "/" + randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
wg.Done()
}
go func() {
b, err := c.Exists(c.Path + "/" + rand)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
wg.Add(5)
go existsFunc()
go existsFunc()
go existsFunc()
go existsFunc()
go existsFunc()
go func() {
b, err := c.Exists(c.Path + "/" + rand)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
go func() {
b, err := c.Exists(c.Path + "/" + rand)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
go func() {
b, err := c.Exists(c.Path + "/" + rand)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
go func() {
b, err := c.Exists(c.Path + "/" + rand)
if err != nil {
t.Error("an error was thrown:", err)
}
if b == true {
t.Error("expected false, got true")
}
res <- true
}()
<-res
<-res
<-res
<-res
<-res
wg.Wait()
}

View File

@ -11,23 +11,23 @@ import (
)
func TestDelete(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("value"))
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Delete(rand)
err = c.Delete(randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
}
func TestDelete_Concurrency(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("value"))
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
@ -35,7 +35,7 @@ func TestDelete_Concurrency(t *testing.T) {
var wg sync.WaitGroup
deleteFunc := func() {
err = c.Delete(rand)
err = c.Delete(randomValue)
if err != nil && !errors.Is(err, cheapcash.ErrNotExists) {
t.Error("an error was thrown:", err)
}

View File

@ -11,14 +11,14 @@ import (
)
func TestRead(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("value"))
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
v, err := c.Read(rand)
v, err := c.Read(randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
@ -28,10 +28,10 @@ func TestRead(t *testing.T) {
}
func TestRead_Concurrency(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("value"))
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
@ -39,7 +39,7 @@ func TestRead_Concurrency(t *testing.T) {
var wg sync.WaitGroup
readFunc := func() {
r, err := c.Read(rand)
r, err := c.Read(randomValue)
if err != nil {
t.Error("an error was thrown:", err)
}
@ -60,10 +60,10 @@ func TestRead_Concurrency(t *testing.T) {
}
func TestRead_NotExists(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
_, err := c.Read(rand)
_, err := c.Read(randomValue)
if err == nil {
t.Error("expected an error, got nil")
}

35
rename.go Normal file
View File

@ -0,0 +1,35 @@
package cheapcash
import "os"
func (c *Cache) Rename(old, new string) error {
err := checkDir(sanitizePath(c.Path))
if err != nil {
return err
}
checkOld, err := c.Exists(c.Path + old)
if err != nil {
return err
}
checkNew, err := c.Exists(c.Path + new)
if err != nil {
return err
}
if !checkOld {
return ErrNotExists
}
if checkNew {
return ErrExists
}
err = os.Rename(c.Path+old, c.Path+new)
if err != nil {
return err
}
return nil
}

102
rename_test.go Normal file
View File

@ -0,0 +1,102 @@
package cheapcash_test
import (
"errors"
"math/rand"
"strconv"
"sync"
"testing"
"github.com/aldy505/cheapcash"
)
func TestRename(t *testing.T) {
randomValue := strconv.Itoa(rand.Int())
randomValue2 := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(randomValue, []byte("Another random value"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Rename(randomValue, randomValue2)
if err != nil {
t.Error("an error was thrown:", err)
}
v, err := c.Read(randomValue2)
if err != nil {
t.Error("an error was thrown:", err)
}
if string(v) != "Another random value" {
t.Errorf("expected %s, got %v", "Another random value", string(v))
}
_, err = c.Read(randomValue)
if err != nil && !errors.Is(err, cheapcash.ErrNotExists) {
t.Error("expected ErrNotExists, got:", err.Error())
}
}
func TestRename_Concurrency(t *testing.T) {
randomValue := strconv.Itoa(rand.Int())
randomValue2 := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(randomValue, []byte("Another random value"))
if err != nil {
t.Error("an error was thrown:", err)
}
var wg sync.WaitGroup
renameFunc := func() {
err = c.Rename(randomValue, randomValue2)
if err != nil && !errors.Is(err, cheapcash.ErrNotExists) {
t.Error("an error was thrown", err)
}
wg.Done()
}
wg.Add(5)
go renameFunc()
go renameFunc()
go renameFunc()
go renameFunc()
go renameFunc()
wg.Wait()
}
func TestRename_OldNotExists(t *testing.T) {
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Rename(randomValue, "test")
if !errors.Is(err, cheapcash.ErrNotExists) {
t.Error("expected ErrNotExists, got:", err)
}
}
func TestRename_NewExists(t *testing.T) {
randomValue := strconv.Itoa(rand.Int())
randomValue2 := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Write(randomValue2, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Rename(randomValue, randomValue2)
if !errors.Is(err, cheapcash.ErrExists) {
t.Error("expected ErrExists, got:", err)
}
}

View File

@ -41,15 +41,15 @@ func TestWrite_Concurrency(t *testing.T) {
}
func TestWrite_Exists(t *testing.T) {
rand := strconv.Itoa(rand.Int())
randomValue := strconv.Itoa(rand.Int())
c := cheapcash.Default()
err := c.Write(rand, []byte("value"))
err := c.Write(randomValue, []byte("value"))
if err != nil {
t.Error("an error was thrown:", err)
}
err = c.Write(rand, []byte("another value"))
err = c.Write(randomValue, []byte("another value"))
if err != nil {
t.Error("an error was thrown:", err)
}