2021-11-16 04:56:33 +00:00
|
|
|
package cheapcash_test
|
|
|
|
|
|
|
|
import (
|
2021-11-16 05:13:49 +00:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
2021-11-16 04:56:33 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aldy505/cheapcash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
c := cheapcash.Default()
|
|
|
|
err := c.Write("key", []byte("value"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
func TestWrite_Concurrency(t *testing.T) {
|
2021-11-16 04:56:33 +00:00
|
|
|
c := cheapcash.Default()
|
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-11-16 04:56:33 +00:00
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
writeFunc := func() {
|
2021-11-16 04:56:33 +00:00
|
|
|
err := c.Write("key1", []byte("value1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
2021-11-16 05:01:41 +00:00
|
|
|
wg.Done()
|
2021-11-16 04:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(5)
|
2021-11-16 05:01:41 +00:00
|
|
|
go writeFunc()
|
|
|
|
go writeFunc()
|
|
|
|
go writeFunc()
|
|
|
|
go writeFunc()
|
|
|
|
go writeFunc()
|
2021-11-16 04:56:33 +00:00
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
wg.Wait()
|
2021-11-16 04:56:33 +00:00
|
|
|
}
|
2021-11-16 05:13:49 +00:00
|
|
|
|
|
|
|
func TestWrite_Exists(t *testing.T) {
|
|
|
|
rand := strconv.Itoa(rand.Int())
|
|
|
|
c := cheapcash.Default()
|
|
|
|
|
|
|
|
err := c.Write(rand, []byte("value"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Write(rand, []byte("another value"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|