cheapcash/writer_test.go

57 lines
941 B
Go
Raw Normal View History

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) {
2021-11-16 13:18:23 +00:00
randomValue := strconv.Itoa(rand.Int())
2021-11-16 05:13:49 +00:00
c := cheapcash.Default()
2021-11-16 13:18:23 +00:00
err := c.Write(randomValue, []byte("value"))
2021-11-16 05:13:49 +00:00
if err != nil {
t.Error("an error was thrown:", err)
}
2021-11-16 13:18:23 +00:00
err = c.Write(randomValue, []byte("another value"))
2021-11-16 05:13:49 +00:00
if err != nil {
t.Error("an error was thrown:", err)
}
}