cheapcash/delete_test.go

54 lines
944 B
Go
Raw Permalink Normal View History

2021-11-16 04:56:33 +00:00
package cheapcash_test
import (
"errors"
"math/rand"
"strconv"
"sync"
"testing"
"github.com/aldy505/cheapcash"
)
func TestDelete(t *testing.T) {
2021-11-16 13:18:23 +00:00
randomValue := strconv.Itoa(rand.Int())
2021-11-16 04:56:33 +00:00
c := cheapcash.Default()
2021-11-16 13:18:23 +00:00
err := c.Write(randomValue, []byte("value"))
2021-11-16 04:56:33 +00:00
if err != nil {
t.Error("an error was thrown:", err)
}
2021-11-16 13:18:23 +00:00
err = c.Delete(randomValue)
2021-11-16 04:56:33 +00:00
if err != nil {
t.Error("an error was thrown:", err)
}
}
2021-11-16 05:01:41 +00:00
func TestDelete_Concurrency(t *testing.T) {
2021-11-16 13:18:23 +00:00
randomValue := strconv.Itoa(rand.Int())
2021-11-16 04:56:33 +00:00
c := cheapcash.Default()
2021-11-16 13:18:23 +00:00
err := c.Write(randomValue, []byte("value"))
2021-11-16 04:56:33 +00:00
if err != nil {
t.Error("an error was thrown:", err)
}
var wg sync.WaitGroup
deleteFunc := func() {
2021-11-16 13:18:23 +00:00
err = c.Delete(randomValue)
2021-11-16 04:56:33 +00:00
if err != nil && !errors.Is(err, cheapcash.ErrNotExists) {
t.Error("an error was thrown:", err)
}
wg.Done()
}
wg.Add(5)
go deleteFunc()
go deleteFunc()
go deleteFunc()
go deleteFunc()
go deleteFunc()
wg.Wait()
}