2021-11-16 04:56:33 +00:00
|
|
|
package cheapcash_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aldy505/cheapcash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRead(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := c.Read(rand)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
if string(v) != "value" {
|
|
|
|
t.Errorf("expected %s, got %v", "value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
func TestRead_Concurrency(t *testing.T) {
|
2021-11-16 04:56:33 +00:00
|
|
|
rand := strconv.Itoa(rand.Int())
|
|
|
|
c := cheapcash.Default()
|
|
|
|
|
|
|
|
err := c.Write(rand, []byte("value"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
readFunc := func() {
|
2021-11-16 04:56:33 +00:00
|
|
|
r, err := c.Read(rand)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
if string(r) != "value" {
|
|
|
|
t.Error("expected value, got:", string(r))
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(5)
|
|
|
|
go readFunc()
|
|
|
|
go readFunc()
|
|
|
|
go readFunc()
|
|
|
|
go readFunc()
|
|
|
|
go readFunc()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|